平滑平均K线(Smoothed Heikin Ashi)
对于平均K线(Heikin Ashi),交易者一般都比较熟悉,不熟悉的可以参见我写的文章Python实现Heikin Ashi(平均K线)。最近在tradeview上发现了Heikin Ashi的一种改进版本——平滑平均K线(Smoothed Heikin Ashi),据说效果要比常规的Heikin Ashi好几倍(本人未验证)。
计算公式
可以发现Smoothed Heikin Ashi就是对开盘价、最高价、最低价、收盘价的均价进行常规Heikin Ashi的计算,非常的简单。
Python实现
def smoothed_heikin_ashi(df, n=60, ma_type='EMA'):
sha = pd.DataFrame(index=df.index, columns=['open', 'high', 'low', 'close'])
alpha = 2 / (1 + n)
if ma_type.lower() == 'sma':
alpha = 1 / n
sha['open'] = df['open'].ewm(alpha=alpha, adjust=False).mean()
sha['high'] = df['high'].ewm(alpha=alpha, adjust=False).mean()
sha['low'] = df['low'].ewm(alpha=alpha, adjust=False).mean()
sha['close'] = df['close'].ewm(alpha=alpha, adjust=False).mean()
return heikin_ashi(sha)
这里我默认使用EMA(60)对价格进行平均,当然也可以使用SMA(n, 1)进行平均。heikin_ashi()方法请参见我的这篇文章。
绘制K线
使用东方财富300059的历史日K数据进行检验,使用mplfinance库绘制K线图。数据如下:
将原始K线、常规Heikin Ashi和Smoothed Heikin Ashi都绘制出来,方便比较。
import mplfinance as mf
sha = smoothed_heikin_ashi(data)
rha = heikin_ashi(data)
fig = mf.figure(style='charles')
ax0 = fig.add_subplot(3, 1, 1)
ax1 = fig.add_subplot(3, 1, 2)
ax2 = fig.add_subplot(3, 1, 3)
mf.plot(data, type='candle', ax=ax0, mav=(60,), axtitle='Original Kline')
mf.plot(rha, type='candle', ax=ax1, mav=(60,), axtitle='Regular Heikin Ashi')
mf.plot(sha, type='candle', ax=ax2, mav=(60,), axtitle='Smoothed Heikin Ashi')
mf.show()
结果如下:
小结
肉眼可见的Smoothed Heikin Ashi要更加的平滑,错误信号更少,能够非常好的刻画大趋势。但是对于震荡行情的情况,其表现也是不尽人意。
相信配合其他过滤指标进行回测,效果肯定比常规的Heikin Ashi要好。