You need to enable JavaScript to run this app. Pine Script v2转v3后自动枢轴点S/R线实盘漂移问题求助
优惠活动
大模型
产品
解决方案
定价
更多

Pine Script v2转v3后自动枢轴点S/R线实盘漂移问题求助

问题根源

代码漂移的核心原因是Pine Script v3对security函数的偏移逻辑与合并模式默认行为发生了变动,转换写法触发了隐性未来数据问题:

  • 将历史值偏移(比如pp[1])写在security函数的内部表达式中,v3下这种写法会导致未闭合K线反复重算,引入未来数据
  • 没有显式指定security的K线合并参数,v3默认的合并模式允许向前引用未闭合的周期数据,实盘运行时值会随当前K线波动反复变化
  • 冗余的重复security调用进一步放大了重绘风险

修正方案

  1. 统一调整security函数的写法,显式指定barmerge.gaps_on和barmerge.lookahead_off禁止未来数据引用
  2. 移除冗余的重复security调用,基于已经拉取的周期数据直接计算后偏移取值
  3. 保持原有业务逻辑完全不变,仅调整语法适配v3规则

修正后完整v3代码

//@version=3
study("Auto Pivots with S/R v3",overlay = true)

// Inputs
pivotType = input(defval=false, title="Check for Fib Pivots, standard by default", type=input.bool) // Option for Fib or Standard
pivotExtend = input(defval = false, title = "Extend pivots through chart?", type=input.bool)
pivotAuto = input(defval = true, title = "Only show most recent pivot set?", type=input.bool)

// Calculate start point
currentTimePrimary =timeframe.ismonthly ? year(timenow) : timeframe.isweekly ? month(timenow) : timeframe.isdaily ? weekofyear(timenow) : timeframe.isintraday ? dayofweek(timenow) == dayofweek.saturday or dayofweek(timenow) == dayofweek.sunday ? dayofweek.friday : dayofweek(timenow) : na
currentTimeSecondary = timeframe.ismonthly ? year(timenow) : timeframe.isweekly ? year(timenow) : timeframe.isdaily ? year(timenow) : timeframe.isintraday ? weekofyear(timenow): na
currentBarTimePrimary = timeframe.ismonthly ? year(time) : timeframe.isweekly ? month(time) : timeframe.isdaily ? weekofyear(time) : timeframe.isintraday ? dayofweek(time) : na
currentBarTimeSecondary = timeframe.ismonthly ? year(time) : timeframe.isweekly ? year(time) : timeframe.isdaily ? year(time) : timeframe.isintraday ? weekofyear(time) : na
pivotTimeFrame = '1W'

// Pull traditional HLC candle values - 修正security写法,显式禁止未来数据
pHigh = security(syminfo.tickerid, pivotTimeFrame, high, barmerge.gaps_on, barmerge.lookahead_off)
pLow = security(syminfo.tickerid, pivotTimeFrame, low, barmerge.gaps_on, barmerge.lookahead_off)
pClose = security(syminfo.tickerid, pivotTimeFrame, close, barmerge.gaps_on, barmerge.lookahead_off)

// Calculate pivot points
pp  = (pHigh + pLow + pClose ) / 3.0

// First level
r1 = pivotType ? pp + ((pHigh-pLow) * 0.382) : (2 * pp) - pLow
s1 = pivotType ? pp - ((pHigh-pLow) * 0.382) : (2 * pp) - pHigh

// Second level
r2 = pivotType ? pp + ((pHigh-pLow) * 0.618) : pp + (pHigh - pLow)
s2 = pivotType ? pp - ((pHigh-pLow) * 0.618) : pp - (pHigh - pLow)

// Third level
r3 = pivotType ? pp + ((pHigh-pLow) * 1.000) : pHigh + (2 * (pp - pLow))
s3 = pivotType ? pp - ((pHigh-pLow) * 1.000) : pLow - (2 * (pHigh - pp))

// Calculate pivot plots - 移除冗余security调用,直接偏移已有计算结果
pivot = pivotAuto ? (currentTimePrimary == currentBarTimePrimary and currentTimeSecondary == currentBarTimeSecondary ? pp[1] : na) : pp[1]
pivot_r1 = pivotAuto ? (currentTimePrimary == currentBarTimePrimary and currentTimeSecondary == currentBarTimeSecondary ? r1[1] : na) : r1[1]
pivot_r2 = pivotAuto ? (currentTimePrimary == currentBarTimePrimary and currentTimeSecondary == currentBarTimeSecondary ? r2[1] : na) : r2[1]
pivot_r3 = pivotAuto ? (currentTimePrimary == currentBarTimePrimary and currentTimeSecondary == currentBarTimeSecondary ? r3[1] : na) : r3[1]
pivot_s1 = pivotAuto ? (currentTimePrimary == currentBarTimePrimary and currentTimeSecondary == currentBarTimeSecondary ? s1[1] : na) : s1[1]
pivot_s2 = pivotAuto ? (currentTimePrimary == currentBarTimePrimary and currentTimeSecondary == currentBarTimeSecondary ? s2[1] : na) : s2[1]
pivot_s3 = pivotAuto ? (currentTimePrimary == currentBarTimePrimary and currentTimeSecondary == currentBarTimeSecondary ? s3[1] : na) : s3[1]

// Plot pivots
plot(pivot and pivot > 0 ? pivot : na, title="Pivot", style = plot.style_linebr, color=color.fuchsia, linewidth=4, transp=0, trackprice = pivotExtend)
plot(pivot_r1 and pivot_r1 > 0 ? pivot_r1 : na, title="R1", style=plot.style_linebr, color=color.white, linewidth=3, transp=15, trackprice = pivotExtend)
plot(pivot_r2 and pivot_r2 > 0 ? pivot_r2 : na, title="R2", style=plot.style_linebr, color=color.white, linewidth=2, transp=30, trackprice = pivotExtend) 
plot(pivot_r3 and pivot_r3 > 0 ? pivot_r3 : na, title="R3", style=plot.style_linebr, color=color.white, linewidth=1, transp=45, trackprice = pivotExtend)
plot(pivot_s1 and pivot_s1 > 0 ? pivot_s1 : na, title="S1", style=plot.style_linebr, color=color.red, linewidth=3, transp=15, trackprice = pivotExtend)
plot(pivot_s2 and pivot_s2 > 0 ? pivot_s2 : na, title="S2", style=plot.style_linebr, color=color.red, linewidth=2, transp=30, trackprice = pivotExtend) 
plot(pivot_s3 and pivot_s3 > 0 ? pivot_s3 : na, title="S3", style=plot.style_linebr, color=color.red, linewidth=1, transp=45, trackprice = pivotExtend) 

内容的提问来源于stack exchange,提问作者oliver meshram

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.20 21:27:02