(Tradingview - Pine Script v5) v2 到 v5 自引用变量错误

(Tradingview - Pine Script v5) v2 to v5 self referencing variable error

我尝试将 pine v2 转换为 v5,但在 行 32 中不断出现错误:未声明的标识符 'pos'(下面的完整代码),有点迷失了处理像这样的自引用变量。

提前致谢

// This is code from pine v2
// sigpre1 = iff(xHigh <= xLowD, -1,
//   iff(xLow >= xHighD, 1, nz(pos[1], 0)))

sigpre1 = xHigh <= xLowD ? -1 : xLow >= xHighD ? 1 : nz(pos[1], 0)

// This is code from pine v2
// sigpre2 = iff( xHigh <= xHighD, -1,
//   iff(xLow >= xLowD, 1, nz(pos[1], 0)))

sigpre2 = xHigh <= xHighD ? -1 : xLow >= xLowD ? 1 : nz(pos[1], 0)
pos = SigVal ? sigpre1 : sigpre2

// This is code from pine v2
// possig = iff(reverse and pos == 1, -1,
        //   iff(reverse and pos == -1, 1, pos))

这里是 v2 完整代码:

//@version=2
strategy(title="[Learning] Support Resistance", overlay = true)
width = input(2, minval=1)
SigVal = input(true, title="To Line \ From Line")
reverse = input(false, title="Trade reverse")
xLow = low
xHigh = high
xHighD = security(tickerid,"W", high[1])
xLowD  = security(tickerid,"W", low[1])
sigpre1 = iff(xHigh <= xLowD, -1,
   iff(xLow >= xHighD, 1, nz(pos[1], 0))) 
sigpre2 = iff( xHigh <= xHighD, -1,
   iff(xLow >= xLowD, 1, nz(pos[1], 0))) 
pos = SigVal ? sigpre1 : sigpre2
possig = iff(reverse and pos == 1, -1,
          iff(reverse and pos == -1, 1, pos))      
if (possig == 1) 
    strategy.entry("Long", strategy.long)
if (possig == -1)
    strategy.entry("Short", strategy.short)         
barcolor(possig == -1 ? red: possig == 1 ? green : blue )
plot(xLowD, color=green, title="S", style = circles, linewidth = width)
plot(xHighD, color=red, title="R", style = circles, linewidth = width)

问题是 pos 稍后在代码中定义。但是还有一个问题。从 v3 开始,security() 函数的 lookahead 参数的默认值已更改为 barmerge.lookahead_off。所以,在 v2 中,它是 barmerge.lookahead_on,你也应该在 v5 中使用它。

//@version=5
strategy(title="[Learning] Support Resistance", overlay = true)

width = input.int(2, 'Widht',minval=1)
SigVal = input.bool(true, title="To Line \ From Line")
reverse = input.bool(false, title="Trade reverse")
xLow = low
xHigh = high
xHighD = request.security(syminfo.tickerid,"W", high[1], lookahead=barmerge.lookahead_on)
xLowD  = request.security(syminfo.tickerid,"W", low[1], lookahead=barmerge.lookahead_on)
pos=0
// This is code from pine v2
// sigpre1 = iff(xHigh <= xLowD, -1,
//   iff(xLow >= xHighD, 1, nz(pos[1], 0)))

sigpre1 = xHigh <= xLowD ? -1 : xLow >= xHighD ? 1 : nz(pos[1], 0)

// This is code from pine v2
// sigpre2 = iff( xHigh <= xHighD, -1,
//   iff(xLow >= xLowD, 1, nz(pos[1], 0)))

sigpre2 = xHigh <= xHighD ? -1 : xLow >= xLowD ? 1 : nz(pos[1], 0)
pos := SigVal ? sigpre1 : sigpre2

// This is code from pine v2
// possig = iff(reverse and pos == 1, -1,
        //   iff(reverse and pos == -1, 1, pos))

possig = reverse and pos == 1 ? -1 : reverse and pos == -1 ? 1 : pos


if (possig == 1) 
    strategy.entry("Long", strategy.long)
if (possig == -1)
    strategy.entry("Short", strategy.short)
    
barcolor(possig == -1 ? color.red : possig == 1 ? color.green : color.blue )

plot(xLowD, color=color.green, title="S", style =plot.style_circles, linewidth = width)
plot(xHighD, color=color.red, title="R", style =plot.style_circles, linewidth = width)