简体   繁体   中英

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

I try to convert pine v2 to v5 but keep getting error with line 32: Undeclared identifier 'pos' (full code below), kinda get lost to deal with self referencing variable like this.

Thanks in advance

// 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))

Here is v2 full code:

//@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)

The problem is pos is defined later in the code. But there is another problem. Starting from v3, default value of the lookahead argument of the security() function has been changed to barmerge.lookahead_off . So, in v2, it is barmerge.lookahead_on and you should use that in v5 too.

//@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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM