MACD趋势指标开发:如何将箭头向左偏移一根K线绘制?
在Pine Script v5中偏移MACD高低点箭头至对应K线
你当前的代码是在确认前一根K线为MACD局部高点的当下K线绘制箭头,要把箭头移到那个高点所在的K线,有两种简洁的实现方式:
方法1:使用plotshape的offset参数(推荐)
直接在plotshape中添加offset=-1参数,即可将箭头向左偏移1根K线,无需修改原条件逻辑:
//@version=5 indicator("MACD Trend") // Setup and plot MACD [macdLine, signalLine, histLine] = ta.macd(close, 12, 26, 9) plot(macdLine, color=color.blue) // Setup and plot arrows high_cross = macdLine[1] > macdLine and macdLine[1] > macdLine[2] // 添加offset=-1,将箭头向左偏移1根K线到高点所在位置 plotshape(high_cross, style=shape.triangleup, location=location.top, offset=-1, color=color.green)
方法2:调整条件的索引逻辑
把判断逻辑改为在当前K线确认自身是局部高点,此时箭头会直接画在当前K线,但需要注意:这个判断要等下一根K线收盘后才会触发(因为需要确认下一根K线的MACD值更低),本质和原逻辑一致,只是写法不同:
//@version=5 indicator("MACD Trend") // Setup and plot MACD [macdLine, signalLine, histLine] = ta.macd(close, 12, 26, 9) plot(macdLine, color=color.blue) // 调整条件:当前K线MACD值高于前后两根K线 high_cross = macdLine > macdLine[1] and macdLine > macdLine[-1] plotshape(high_cross, style=shape.triangleup, location=location.top, color=color.green)
注意事项
- 方法1更贴合你原有的逻辑,不需要调整条件,仅通过偏移参数实现位置修正,避免逻辑混淆;
- 若后续要绘制低点箭头(局部低点),同理可使用
offset=-1或对应索引逻辑,条件改为macdLine[1] < macdLine and macdLine[1] < macdLine[2]即可。
内容的提问来源于stack exchange,提问作者Zettt
相关产品推荐
相关产品推荐

