Pine Script v2版Heiken Ashi MTF指标转换v5版本技术求助
Heiken Ashi MTF 指标Pine Script v2转v5适配版本
转换后的代码完全保留原指标逻辑,适配v5版本语法规范,无重绘问题:
- 替换v2版本已废弃的
study声明为v5标准indicator声明 - 调整
heikinashi、跨周期数据调用函数为v5正式语法,原security函数替换为request.security,默认关闭未来数据引用避免信号漂移 - 时间周期输入改用v5原生
input.timeframe控件,支持直接在参数面板下拉选择周期,无需手动输入字符串 - 替换v2嵌套
iff判断为v5支持的三元运算,执行效率更高 - 适配v5版本内置颜色、绘图样式的命名规范
转换后的完整代码如下:
//@version=5 indicator("Heiken Ashi MTF", shorttitle="HA MTF", overlay=false) // 构造Heikin Ashi品种数据源 ha_t = ticker.heikinashi(syminfo.tickerid) // 第一个时间周期HA状态计算 res = input.timeframe('60', title="TM 1") ha_open = request.security(ha_t, res, open) ha_close = request.security(ha_t, res, close) ha_dif = ha_open - ha_close ha_diff = ha_dif > 0 ? 1 : ha_dif < 0 ? 2 : 3 // 第二个时间周期HA状态计算 res2 = input.timeframe('240', title="TM 2") ha_open2 = request.security(ha_t, res2, open) ha_close2 = request.security(ha_t, res2, close) ha_dif2 = ha_open2 - ha_close2 ha_diff2 = ha_dif2 > 0 ? 1 : ha_dif2 < 0 ? 2 : 3 // 第三个时间周期HA状态计算 res3 = input.timeframe('D', title="TM 3") ha_open3 = request.security(ha_t, res3, open) ha_close3 = request.security(ha_t, res3, close) ha_dif3 = ha_open3 - ha_close3 ha_diff3 = ha_dif3 > 0 ? 1 : ha_dif3 < 0 ? 2 : 3 // 多周期状态点绘图,和原版本显示规则完全一致 plot(15, title="TF1", color=ha_diff==1 ? color.red : ha_diff==2 ? color.green : color.white, style=plot.style_circles, linewidth=5, join=true) plot(14, title="TF2", color=ha_diff2==1 ? color.red : ha_diff2==2 ? color.green : color.white, style=plot.style_circles, linewidth=5, join=true) plot(13, title="TF3", color=ha_diff3==1 ? color.red : ha_diff3==2 ? color.green : color.white, style=plot.style_circles, linewidth=5, join=true)
显示规则和原v2版本完全匹配:红色圆点代表对应周期Heikin Ashi K线收阴,绿色代表收阳,白色代表周期开收盘价完全持平。
内容的提问来源于stack exchange,提问作者Fabian D.
相关产品推荐
相关产品推荐

