我有一些{open|high|low|close}
市场数据。我想根据每行的 收盘价
值计算简单移动平均线。
我环顾四周,找不到执行此操作的简单方法。我已经通过以下方法计算了它。我想知道有没有更好的方法:
data = get_data_period_symbol('1h', 'EURUSD')
empty_list = np.zeros(len(data))
data['SMA10'] = empty_list
ma = 10
for i in range(ma-1, len(data)):
vals = data['<CLOSE>'][i-(ma-1):i+1].tolist()
mean = np.average(vals)
index = data.index[i]
data.set_value(index, 'SMA10', mean)
最佳答案
Pandas 提供了完成此类任务所需的所有工具。 假设您有按时间索引的数据:
data['SMA10'] = data['<close>'].rolling(window=10).mean()
瞧。
编辑: 我想只需注意较新的 api 用法。引用自 Pandas docs :
Warning Prior to version 0.18.0, pd.rolling_, pd.expanding_, and pd.ewm* were module level functions and are now deprecated. These are replaced by using the Rolling, Expanding and EWM. objects and a corresponding method call.
关于python - 这是计算移动平均线的有效方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39317436/