You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PineScript入门:三根K线判断后plotshape绘制位置错误的修正方法

PineScript 三根K线 Swing High 标记位置修正

你的问题出在PineScript的bar索引逻辑和plotshape的默认绘制位置上:

  • high[1]指代前一根K线,你的条件high[1]>high[0] and high[1]>high[2]是在**当前K线(索引0)**上触发的,所以plotshape默认会把形态画在当前K线,而非你要的中间那根(索引1)。

修正方案(两种可选)

方案1:偏移绘制位置(推荐,标准Swing High标记逻辑)

通过plotshape的offset参数,将形态往前偏移1根K线,直接画在中间那根K线上方:

//@version=5
indicator("修正后的Swing High标记", overlay = true)
// 原条件不变:判断前一根K线是三根中的高点
swing_highs = high[1] > high[0] and high[1] > high[2]
// 使用offset=-1偏移到前一根K线,指定在K线上方绘制三角形标记
plotshape(
    swing_highs, 
    title="Swing High", 
    location=location.abovebar, 
    offset=-1, 
    style=shape.triangleup, 
    color=color.red, 
    size=size.small
)

关键参数说明:

  • offset=-1:将绘制位置向前偏移1根K线,对应到你要标记的中间K线
  • location.abovebar:指定形态画在K线高点上方

方案2:调整条件触发时机(仅作逻辑参考)

若想让条件逻辑更贴合“中间K线为高点”的表述(本质效果和方案1一致,需等第三根K线走完才能确认),可调整索引逻辑:

//@version=5
indicator("Swing High 标记", overlay = true)
// 当处于第三根K线时,判断中间那根(前一根)是高点
swing_highs = high[0] < high[1] and high[2] < high[1]
// 同样需要偏移1根K线到中间位置
plotshape(swing_highs, title="Swing High", location=location.abovebar, offset=-1)

内容的提问来源于stack exchange,提问作者Pooyan Mahmoodi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 13:52:22