python - 为什么只有 print 函数返回列表?
问题描述
所以我正在尝试做一个资金流量指数指标,到目前为止代码是:
import pandas as pd
import numpy as np
import pandas_datareader as web
import datetime as dt
#get stock prices
start = dt.datetime(2019, 12, 1)
end = dt.datetime(2020, 1, 1)
d = web.DataReader('AMD', 'yahoo', start, end)
pd.set_option('display.max_rows', 1000)
def Money_Flow_index(period = 5):
d['typical_price'] = (d['High'] + d['Close'] + d['Low'])/3
for i in reversed(range(len(d['typical_price']))):
if i > 5:
typical_price_dates = d['typical_price'][i-period:i]
print(typical_price_dates)
Money_Flow_index()
我对 python 比较陌生,所以这可能是我不了解一些基本的东西。虽然现在代码正试图索引每个增量数据集 5 个时期的调整收盘价。因此,当我按原样运行代码时,输出是:
Date
2019-12-23 45.160000
2019-12-24 46.306667
2019-12-26 46.533334
2019-12-27 46.346667
2019-12-30 45.449999
Name: typical_price, dtype: float64
Date
2019-12-20 43.889999
2019-12-23 45.160000
2019-12-24 46.306667
2019-12-26 46.533334
2019-12-27 46.346667
Name: typical_price, dtype: float64
Date
2019-12-19 42.923333
2019-12-20 43.889999
2019-12-23 45.160000
2019-12-24 46.306667
2019-12-26 46.533334
Name: typical_price, dtype: float64
Date
2019-12-18 42.533334
2019-12-19 42.923333
2019-12-20 43.889999
2019-12-23 45.160000
2019-12-24 46.306667
Name: typical_price, dtype: float64
Date
2019-12-17 42.676668
2019-12-18 42.533334
2019-12-19 42.923333
2019-12-20 43.889999
2019-12-23 45.160000
Name: typical_price, dtype: float64
Date
2019-12-16 42.353333
2019-12-17 42.676668
2019-12-18 42.533334
2019-12-19 42.923333
2019-12-20 43.889999
Name: typical_price, dtype: float64
Date
2019-12-13 41.720001
2019-12-16 42.353333
2019-12-17 42.676668
2019-12-18 42.533334
2019-12-19 42.923333
Name: typical_price, dtype: float64
Date
2019-12-12 41.486666
2019-12-13 41.720001
2019-12-16 42.353333
2019-12-17 42.676668
2019-12-18 42.533334
Name: typical_price, dtype: float64
Date
2019-12-11 39.373334
2019-12-12 41.486666
2019-12-13 41.720001
2019-12-16 42.353333
2019-12-17 42.676668
Name: typical_price, dtype: float64
Date
2019-12-10 39.329999
2019-12-11 39.373334
2019-12-12 41.486666
2019-12-13 41.720001
2019-12-16 42.353333
Name: typical_price, dtype: float64
Date
2019-12-09 39.226667
2019-12-10 39.329999
2019-12-11 39.373334
2019-12-12 41.486666
2019-12-13 41.720001
Name: typical_price, dtype: float64
Date
2019-12-06 39.793334
2019-12-09 39.226667
2019-12-10 39.329999
2019-12-11 39.373334
2019-12-12 41.486666
Name: typical_price, dtype: float64
Date
2019-12-05 39.796666
2019-12-06 39.793334
2019-12-09 39.226667
2019-12-10 39.329999
2019-12-11 39.373334
Name: typical_price, dtype: float64
Date
2019-12-04 39.546666
2019-12-05 39.796666
2019-12-06 39.793334
2019-12-09 39.226667
2019-12-10 39.329999
Name: typical_price, dtype: float64
Date
2019-12-03 38.326668
2019-12-04 39.546666
2019-12-05 39.796666
2019-12-06 39.793334
2019-12-09 39.226667
Name: typical_price, dtype: float64
这是我正在寻找的,但是当我尝试return(typical_price_dates)
它时只打印第一组:
Date
2019-12-23 45.160000
2019-12-24 46.306667
2019-12-26 46.533334
2019-12-27 46.346667
2019-12-30 45.449999
Name: typical_price, dtype: float64
这个问题是当我尝试访问typical_price_dates
时,它只显示第一个系列而不是所有系列。例如,如果我想做类似的事情:
typical_price = typical_price_dates
typical_volume = d['Volume']
d['money_flow_index'] = typical_price * typical_volume
它只访问第一个系列。所以基本上我怎样才能使变量typical_price_dates 等于所有系列而不仅仅是一个。谢谢你。
解决方案
当您在代码中使用 return 时,循环仅运行 一次并返回结果,然后您退出函数并且此后没有循环运行,您应该做的是将典型的价格日期每次都附加到python list之类的东西上,比如这
z=[]
import pandas as pd
import numpy as np
import pandas_datareader as web
import datetime as dt
#get stock prices
start = dt.datetime(2019, 12, 1)
end = dt.datetime(2020, 1, 1)
d = web.DataReader('AMD', 'yahoo', start, end)
pd.set_option('display.max_rows', 1000)
def Money_Flow_index(period = 5):
d['typical_price'] = (d['High'] + d['Close'] + d['Low'])/3
for i in reversed(range(len(d['typical_price']))):
if i > 5:
typical_price_dates = d['typical_price'][i-period:i]
z.append(typical_price_dates)
Money_Flow_index()
for i in z:
print(i)
推荐阅读
- python-3.x - 从python中的字符串中删除特殊字符
- c# - firebase 从服务器 c# 发送通知
- java - Spring Data Redis 是否支持 Java 流?
- python - 我可以同时用两个图像数据集训练一个 CNN 模型吗
- flutter - 如果第一个为空,则将其他键的值存储在飞镖模型中 - Flutter
- php - 如何将“dmY - dmY”字符串重新格式化为“Ymd Ymd”?
- google-colaboratory - TypeError:“NoneType”对象不可下标 Face_recognition Open CV
- java - Java - 如何将组件水平居中并垂直堆叠?
- python - 如何让 Discord 机器人(使用 discord.py)测试 dms 中的响应?
- vue.js - eslint-plugin-vue vue/valid-v-model 似乎使用 Vue.js 2 中的规则发出警告