现在,我们已经看到了可视化的每日收盘价和成交量,接下来计算股票的移动平均线。
各股票的移动平均线
移动平均算法(MigAeage)是一种典型的处理[时间序列数据]的算法模型,而股票的价格跟着时间不断变化的一种数据,是典型的时间序列数据,因此利用移动平均算法对股票价格进行预测是股票趋势预测研究中最基础也是最典型的模型。
[移动平均算法]的核心思想是利用前一阶段的真实的数据值,依次利用特定的计算一定范围内的所考虑项目的随机机值,例如在股票趋势预测中是股票的价格,因此在具有周期性或者波动性较大的应用场景中,[移动平均]的实际结果准确率会受到一定的影响。
# 设置移动天数
ma_day = [10, 20, 50]
for ma in ma_day:
for company in company_list:
column_name = f"MA for {ma} days"
company[column_name] = company['close'].rolling(ma).mean()
现在继续绘制所有额外的移动平均线。
fig, axes = plt.subplots(nrows=2, ncols=2)
fig.set_figheight(8)
fig.set_figwidth(15)
maotai[['close', 'MA for 10 days', 'MA for 20 days', 'MA for 50 days']].plot(ax=axes[0,0])
axes[0,0].set_title('贵州茅台')
wuliangye[['close', 'MA for 10 days',
'MA for 20 days', 'MA for 50 days']
].plot(ax=axes[0,1])
axes[0,1].set_title('五粮液')
yanghe[['close', 'MA for 10 days',
'MA for 20 days', 'MA for 50 days']
].plot(ax=axes[1,0])
axes[1,0].set_title('洋河股份')
fenjiu[['close', 'MA for 10 days',
'MA for 20 days', 'MA for 50 days']
].plot(ax=axes[1,1])
axes[1,1].set_title('山西汾酒')
fig.tight_layout()
股票的平均日回报率是多少?
现在我们已经完成了一些基本分析,现在进一步深入研究。现在我们来分析一下股票的风险。这里需要仔细观察股票的每日变化趋势。使用pct_change
来查找每天的百分比变化。
for company in company_list:
company['Daily Return'] = company['close'].pct_change()
# 画出日收益率
fig, axes = plt.subplots(nrows=2, ncols=2)
fig.set_figheight(8)
fig.set_figwidth(15)
maotai['Daily Return'].plot(ax=axes[0,0], legend=True,
linestyle='--', marker='o')
axes[0,0].set_title('贵州茅台')
wuliangye['Daily Return'].plot(ax=axes[0,1], legend=True,
linestyle='--', marker='o')
axes[0,1].set_title('五粮液')
yanghe['Daily Return'].plot(ax=axes[1,0], legend=