设定上、下、中通道线初始值
upboundDC = pd.Series(0.0, index=Close.index)
downboundDC = pd.Series(0.0, index=Close.index)
midboundDC = pd.Series(0.0, index=Close.index)
求唐奇安上、中、下通道
for i in range(20,len(Close)):
upboundDC[i] = max(High[(i-20):i])
downboundDC[i] = min(Low[(i-20):i])
midboundDC[i] = 0.5 * (upboundDC[i] + downboundDC[i])
upboundDC = upboundDC[20:]
downboundDC = downboundDC[20:]
midboundDC = midboundDC[20:]
绘制2020年洛阳钼业价格唐奇安通道上中下轨道线图
plt.rcParams[‘font.sans-serif’] = [‘SimHei’]
plt.plot(Close[‘2020’], label=“Close”, color=“k”)
plt.plot(upboundDC[‘2020’], label=“upboundDC”, color=“b”, linestyle=“dashed”)
plt.plot(midboundDC[‘2020’], label=“midboundDC”, color=“r”, linestyle=“-.”)
plt.plot(downboundDC[‘2020’],label=“downboundDC”, color=“b”, linestyle=“dashed”)
plt.title(“2020年洛阳钼业股价唐奇安通道”)
plt.xlabel(‘日期’)
plt.ylabel(‘values’)
plt.grid(True)
plt.legend()
plt.show()
生成图像效果如下:
-
以价格运动趋势来观察,我们不难看出,当整体价格趋势呈现出上升趋势的时候,三条均线也同时会呈现出一定的上升趋势;当股价大幅回落的时候,三条线也会有明显的向下运动趋势。
-
从曲线的平滑程度来观察,价格线的上下波动较频繁,且三条轨道线相对平滑。从上下通道的间距情况大致可以看出来股价的震荡情况。股价波动较小时,两条轨道线的间距大致稳定,股价波动较大时,带宽也时大时小,股价与中间轨道交叉的次数也越多。
未来看到更多的价格信息和股票价格运动情况,我们在K线图中绘制唐奇安的上下通道线,代码如下:
s = mpf.make_mpf_style(base_mpf_style=‘nightclouds’, rc={‘font.family’: ‘SimHei’}) # 解决mplfinance绘制输出中文乱码
add_plot=[
mpf.make_addplot(upboundDC[‘2020’]),
mpf.make_addplot(midboundDC[‘2020’]),
mpf.make_addplot(downboundDC[‘2020’])]
mpf.plot(df[‘2020’], type=‘candle’, style=s, title=‘洛阳钼业2020年K线图及唐奇安通道线’, addplot=add_plot, volume=True)
图像效果如下:
这里选择的时间跨度较大,实际研究可以选择更细节的时间跨度。
使用mplfinance库我们只需要传入参数mav,就可以添加上指定时间跨度的均线图一起研究(以5日,10日,20日,40日为例):
s = mpf.make_mpf_style(base_mpf_style=‘nightclouds’, rc={‘font.family’: ‘SimHei’})
add_plot=[
mpf.make_addplot(upboundDC[‘2020’]),
mpf.make_addplot(midboundDC[‘2020’]),
mpf.make_addplot(downboundDC[‘2020’])]
mpf.plot(df[‘2020’], type=‘candle’, style=s, title=‘洛阳钼业2020年K线图附唐奇安通道线及均线’, mav=(5,10,20,40),addplot=add_plot, volume=True)
效果展示如下