股价波动指标ATR的计算及绘制

在范·K·撒普的《通向财务自由之路》这本书中分析止损指标时提到波动幅度止损,并认为该资本是可以选用的最好的止损指标之一。股价的平均波动幅度在某种程度上代表了市场中的噪声,为了防止止损被短期的市场噪声所干扰,可以把初始止损价格设置为股价波动幅度的倍数,通常建议设置为ATR的3倍。那么股价的平均波动幅度究竟要如何计算呢?下面我们来仔细分析一下。

股价的平均波动幅度指标是由JR. Welles Wilder在其著作《技术交易系统中的新概念(New Concepts in Technical Trading Systems)》中提出的技术分析指标,全称为平均真实波动幅度(ATR,Average True Range),指的是一定时间周期内的股价波动幅度的移动平均值。计算方法如下:

(1)当日最高价和最低价之差;

(2)上一收盘价和当日最高价之差;

(3)上一收盘价和当日最低价之差;

(4)取以上三个差值的绝对值中的最大值作为当日的股价波动幅度TR。

(5)ATR指标是N个交易日波动幅度TR的移动平均值,N一般取14。

ATR指标在实际使用过程中经常用来衡量市场波动的程度,也可以称为波动变化率或市场活跃度。当ATR指标较小时,表示市场较为平稳;ATR指标变化较大时表示市场成交活跃。在短期交易中,由于剧烈的价格波动会导致短期的价格频繁达到止损位,因此趋势交易需要监测该指标,以便对止损位做出合理设置。

下面以上一篇绘制K线和成交量的文章中获取的招商银行日K线数据为例,来计算一下ATR指标,并以3倍ATR作为止损曲线将其叠加到K线中。

计算ATR的代码如下:

df = ak.stock_zh_a_hist(symbol="600036", start_date='20220101',end_date='20220916', adjust="qfq").iloc[:, :6]
df.columns = ['date','open','close','high','low','volume',] #列名改为英文方便下面操作
df['ATR1']=df['high']-df['low'] #当日最高价-最低价
df['ATR2']=abs(df['close'].shift(1)-df['high']) #上一日收盘价-当日最高价
df['ATR3']=abs(df['close'].shift(1)-df['low']) #上一日收盘价-当日最低价df['ATR4']=df['ATR1']
for i in range(len(df)): #取价格波动的最大值
    if df.iloc[i,14]<df.iloc[i,12] :
        df.iloc[i,14]=df.iloc[i,12]
    if df.iloc[i,14]<df.iloc[i,13] :
        df.iloc[i,14]=df.iloc[i,13]
df['ATR']=df.ATR4.rolling(14).mean() # N=14的ATR值
df['stop']=df['close'].shift(1)-df['ATR']*3 #止损价=(上一日收盘价-3*ATR)

其中’ATR’为最终计算得到的ATR指标值,这里取N=14,即采用14天移动平均。

注意:上述代码在操作表的过程中,是采用iloc(),通过列序号来找对应的列。这种方式操作简单,但是代码可读性非常差。这里可以改用loc()通过列名来操作表,这样就不用管列序号了。

df.index=range(len(df))
df['ATR1']=df['high']-df['low'] #当日最高价-最低价
df['ATR2']=abs(df['close'].shift(1)-df['high']) #上一日收盘价-当日最高价
df['ATR3']=abs(df['close'].shift(1)-df['low']) #上一日收盘价-当日最低价
df['ATR4']=df['ATR1']
for i in range(len(df)): #取价格波动的最大值
    if df.loc[i,'ATR4']<df.loc[i,'ATR2'] :
        df.loc[i,'ATR4']=df.loc[i,'ATR2']
    if df.loc[i,'ATR4']<df.loc[i,'ATR3'] :
        df.loc[i,'ATR4']=df.loc[i,'ATR3']
df['ATR']=df.ATR4.rolling(14).mean() # N=14的ATR值
df['stop']=df['close'].shift(1)-df['ATR']*3 #止损价=(上一日收盘价-3*ATR)

需要注意的是,loc()要想方便的操作行,需要将行索引设置为数字序号。这个是通过重新设置索引实现的:

df.index=range(len(df))

对于loc()和iloc()操作经常弄混的,可以看下这篇《DataFrame常用操作实例》。

把以上ATR和计算与上一篇的K线绘制代码整合起来如下:

import akshare as ak
import pandas as pd
from pyecharts.charts import *
from pyecharts import options as opts

df = ak.stock_zh_a_hist(symbol="600036", start_date='20220101',end_date='20220916', adjust="qfq").iloc[:, :6]
df.columns = ['date','open','close','high','low','volume',] #列名改为英文方便下面操作

# 把date作为日期索引
df.index = pd.to_datetime(df.date)
df.index=df.index.strftime('%Y%m%d')
df=df.sort_index()
df['sma']=df.close.rolling(5).mean()
df['lma']=df.close.rolling(10).mean()
df['lma20']=df.close.rolling(20).mean()
df['lma30']=df.close.rolling(30).mean()
df['lma60']=df.close.rolling(60).mean()
df.index=range(len(df)) #修改索引为数字序号
df['ATR1']=df['high']-df['low'] #当日最高价-最低价
df['ATR2']=abs(df['close'].shift(1)-df['high']) #上一日收盘价-当日最高价
df['ATR3']=abs(df['close'].shift(1)-df['low']) #上一日收盘价-当日最低价
df['ATR4']=df['ATR1']
for i in range(len(df)): #取价格波动的最大值
    if df.loc[i,'ATR4']<df.loc[i,'ATR2'] :
        df.loc[i,'ATR4']=df.loc[i,'ATR2']
    if df.loc[i,'ATR4']<df.loc[i,'ATR3'] :
        df.loc[i,'ATR4']=df.loc[i,'ATR3']
#for i in range(len(df)): #取价格波动的最大值
#    if df.iloc[i,14]<df.iloc[i,12] :
#        df.iloc[i,14]=df.iloc[i,12]
#    if df.iloc[i,14]<df.iloc[i,13] :
#        df.iloc[i,14]=df.iloc[i,13]
df['ATR']=df.ATR4.rolling(14).mean() # N=14的ATR值
df['stop']=df['close'].shift(1)-df['ATR']*3 #止损价=(上一日收盘价-3*ATR)

df.index = pd.to_datetime(df.date)
df.index=df.index.strftime('%Y%m%d')
kline = (
    Kline(init_opts=opts.InitOpts(width="1200px",height="600px"))
    .add_xaxis(xaxis_data=list(df.index)) #X轴数据
    .add_yaxis(
        series_name="klines", #序列名称
        y_axis=df[["open","close","low","high"]].values.tolist(), #Y轴数据
        itemstyle_opts=opts.ItemStyleOpts(color="#ec0000", color0="#00da3c"),
        markpoint_opts=opts.MarkPointOpts(
            data=[#添加标记符
            opts.MarkPointItem(type_='max', name='最大值'),
            opts.MarkPointItem(type_='min', name='最小值'), ],
            #symbol='circle',
            #symbol_size=[100,30]
        ),
    )
    .set_global_opts(
        title_opts=opts.TitleOpts(title="K线及均线",pos_left='45%'), #标题位置
        legend_opts=opts.LegendOpts(pos_right="35%",pos_top="5%"), #图例位置
        #legend_opts=opts.LegendOpts(is_show=True, pos_bottom=10, pos_left="center"),
        datazoom_opts=[
            opts.DataZoomOpts(
                is_show=False,
                type_="inside", #内部缩放
                xaxis_index=[0,1],  #可缩放的x轴坐标编号
                range_start=0, range_end=100, #初始显示范围
            ),
            opts.DataZoomOpts(
                is_show=True, #显示滑块
                type_="slider", #滑块缩放
                xaxis_index=[0,1],  #可缩放的x轴坐标编号
                pos_top="85%",
                range_start=0, range_end=100, #初始显示范围
            ),
        ],
        yaxis_opts=opts.AxisOpts(
            is_scale=True, #缩放时是否显示0值
            splitarea_opts=opts.SplitAreaOpts( #分割显示设置
                is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1) ),
        ),
        tooltip_opts=opts.TooltipOpts( #提示框配置
            trigger="axis", #坐标轴触发提示
            axis_pointer_type="cross", #鼠标变为十字准星
            background_color="rgba(245, 245, 245, 0.8)", #背景颜色
            border_width=1, border_color="#ccc", #提示框配置
            textstyle_opts=opts.TextStyleOpts(color="#000"), #文字配置
        ),
        visualmap_opts=opts.VisualMapOpts( #视觉映射配置
            is_show=False, dimension=2,
            series_index=5, is_piecewise=True,
            pieces=[ {"value": 1, "color": "#00da3c"}, {"value": -1, "color": "#ec0000"}, ],
        ),
        axispointer_opts=opts.AxisPointerOpts( #轴指示器配置
            is_show=True,
            link=[{"xAxisIndex": "all"}],
            label=opts.LabelOpts(background_color="#777"), #显示标签设置
        ),
        brush_opts=opts.BrushOpts(
            x_axis_index="all", #所有series
            brush_link="all", #不同系列选中后联动
            out_of_brush={"colorAlpha": 0.1}, #高亮显示程度
            brush_type="lineX", #纵向选择
        ),
    )
)

#均线
line=Line()
line.add_xaxis( df.index.tolist() ) #X轴数据
line.add_yaxis( 'MA5', #序列名称
                df.sma.round(2).tolist(), #Y轴数据
                is_smooth=True, #平滑曲线
                is_symbol_show=False #不显示折线的小圆圈
)
line.add_yaxis( 'MA10',df.lma.round(2).tolist(),is_smooth=True,is_symbol_show=False )
line.add_yaxis( 'MA20',df.lma20.round(2).tolist(),is_smooth=True,is_symbol_show=False )
line.add_yaxis( 'MA30',df.lma30.round(2).tolist(),is_smooth=True,is_symbol_show=False )
line.add_yaxis( 'MA60',df.lma60.round(2).tolist(),is_smooth=True,is_symbol_show=False )
line.set_series_opts(
    label_opts=opts.LabelOpts(is_show=False), #是否显示数据标签
    linestyle_opts=opts.LineStyleOpts(width=1), #线宽
)
line.set_global_opts(
    datazoom_opts=[
        opts.DataZoomOpts(
            is_show=False,
            type_="inside", #图内缩放调整
            xaxis_index=[0,1],  #可缩放的x轴坐标编号
            range_start=0, range_end=100, #初始显示范围
        ),
        opts.DataZoomOpts(
            is_show=True, #是否显示滑块
            type_="slider", #外部滑块缩放调整
            xaxis_index=[0,1],  #可缩放的x轴坐标编号
            pos_top="85%",
            range_start=0, range_end=100, #初始显示范围
        ),
    ],
    legend_opts=opts.LegendOpts(pos_right="20%",pos_top="5%"), #图例位置
    tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross") #趋势线设置
)
stop=(
    Line()
    .add_xaxis( df.index.tolist() ) #X轴数据
    .add_yaxis( 'STOP', #序列名称
                df.stop.round(2).tolist(), #Y轴数据
                is_smooth=True, #平滑曲线
    )
    .set_series_opts(
        label_opts=opts.LabelOpts(is_show=False), #是否显示数据标签
        linestyle_opts=opts.LineStyleOpts(width=2), #线宽
    )
    .set_global_opts(
        datazoom_opts=[
            opts.DataZoomOpts(
                is_show=False,
                type_="inside", #图内缩放调整
                xaxis_index=[0,1],  #可缩放的x轴坐标编号
                range_start=0, range_end=100, #初始显示范围
            ),
            opts.DataZoomOpts(
                is_show=True, #是否显示滑块
                type_="slider", #外部滑块缩放调整
                xaxis_index=[0,1],  #可缩放的x轴坐标编号
                pos_top="85%",
                range_start=0, range_end=100, #初始显示范围
            ),
        ],
        legend_opts=opts.LegendOpts(pos_right="20%",pos_top="5%"), #图例位置
        tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross") #趋势线设置
    )
)
kline.overlap(line)
kline.overlap(stop) #止损数据叠加到K线
#成交量
bar = (
    Bar()
    .add_xaxis(xaxis_data=list(df.index)) #X轴数据
    .add_yaxis(
        series_name="volume",
        y_axis=df["volume"].tolist(), #Y轴数据
        xaxis_index=1,
        yaxis_index=1,
        label_opts=opts.LabelOpts(is_show=False),
        itemstyle_opts=opts.ItemStyleOpts(
            color='#ef232a' #'#14b143'
        ),
    )
    .set_global_opts(
        xaxis_opts=opts.AxisOpts(
            type_="category", #坐标轴类型-离散数据
            grid_index=1,
            axislabel_opts=opts.LabelOpts(is_show=False),
        ),
        legend_opts=opts.LegendOpts(is_show=False),
    )
)
#价格波动指标ATR
atr=(
    Line()
    .add_xaxis( df.index.tolist() ) #X轴数据
    .add_yaxis( 'ATR', #序列名称
                df.ATR.round(2).tolist(), #Y轴数据
                yaxis_index=1,
                is_smooth=True #平滑曲线
    )
    .set_series_opts(
        label_opts=opts.LabelOpts(is_show=False), #是否显示数据标签
        linestyle_opts=opts.LineStyleOpts(width=2), #线宽
    )
    .set_global_opts(
        datazoom_opts=[
            opts.DataZoomOpts(
                is_show=False,
                type_="inside", #图内缩放调整
                xaxis_index=[0,1],  #可缩放的x轴坐标编号
                range_start=0, range_end=100, #初始显示范围
            ),
            opts.DataZoomOpts(
                is_show=True, #是否显示滑块
                type_="slider", #外部滑块缩放调整
                xaxis_index=[0,1],  #可缩放的x轴坐标编号
                pos_top="85%",
                range_start=0, range_end=100, #初始显示范围
            ),
        ],
        legend_opts=opts.LegendOpts(pos_right="20%",pos_top="5%"), #图例位置
        tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross") #趋势线设置
    )
)
#bar.overlap(atr)

grid_chart = Grid(
    init_opts=opts.InitOpts(
        width="1200px", #显示图形宽度
        height="600px",
        animation_opts=opts.AnimationOpts(animation=False), #关闭动画
    )
)
grid_chart.add( #加入均线图
    kline,
    grid_opts=opts.GridOpts(pos_left="10%", pos_right="8%", height="40%"),
)
grid_chart.add( #加入成交量图
    bar,
    grid_opts=opts.GridOpts(pos_left="10%", pos_right="8%", pos_top="55%", height="20%"),
)
grid_chart.add( #加入ATR图
    atr,
    grid_opts=opts.GridOpts(pos_left="10%", pos_right="8%", pos_top="80%", height="15%"),
)

grid_chart.render("ATR.html")
df.to_excel('example.xlsx', sheet_name='600036', index=False)

计算完成后的数据表如下图所示:

结果如下图所示:

上图中的‘ATR’即为股价的平均真实波动幅度,‘STOP’即为根据ATR计算得到的止损曲线。写完才发现加入ATR曲线以后,底部的ATR图与缩放滑块的位置居然重叠了☹,大家自己把Grid()中几个图表的pos_top和height调整一下就好了(具体可参考上一篇文章中相关参数的说明)~~😊

-----------------------------------

原创不易,请多支持

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值