简体   繁体   中英

Undeclared Identifier error in Pinescript while translating study script to strategy

I am trying to convert Chandelier Stop study script to strategy but getting Undeclared Identifier error in version=4

The original script can be found here - https://in.tradingview.com/script/mjBdRGXe-Chandelier-Stop/


//input variables
Length2=input(title="Look Back Period", type=input.integer, defval=22)
ATRPeriod=input(title="ATR Period", type=input.integer, defval=22)
Mult=input(title="ATR Multiplier", type=input.integer, defval=3)

//calculate stop value
short_stop = lowest(Length2)+Mult*atr(ATRPeriod)
long_stop  = highest(Length2)-Mult*atr(ATRPeriod)

shortvs=na(shortvs[1]) ? short_stop : iff(close>shortvs[1], short_stop , min(short_stop,shortvs[1]))
longvs=na(longvs[1]) ? long_stop : iff(close<longvs[1], long_stop, max(long_stop,longvs[1]))

longswitch=iff (close>=shortvs[1] and close[1]<shortvs[1] , 1 ,  0)
shortswitch=iff (close<=longvs[1] and close[1]>longvs[1] ,  1 ,  0)

direction= iff(na(direction[1]), 0, iff (direction[1]<=0 and longswitch, 1, iff (direction[1]>=0 and shortswitch, -1, direction[1])))
            
pcup=direction>0?longvs : na
pcdn=direction<0?shortvs : na

plot(pcup, color=color.aqua, style=plot.style_circles, linewidth=2)
plot(pcup, color=color.aqua, style=plot.style_linebr, linewidth=2)

plot(pcdn, color=color.fuchsia, style=plot.style_circles, linewidth=2)
plot(pcdn, color=color.fuchsia, style=plot.style_linebr, linewidth=2)

Here is Error Log

Processing script...
line 35: Undeclared identifier 'shortvs';
line 36: Undeclared identifier 'longvs';
line 38: Undeclared identifier 'shortvs';
line 39: Undeclared identifier 'longvs';
line 41: Undeclared identifier 'direction';
line 41: Undeclared identifier 'longswitch';
line 41: Undeclared identifier 'shortswitch';
line 43: Undeclared identifier 'direction';
line 43: Undeclared identifier 'longvs';
line 44: Undeclared identifier 'direction';
line 44: Undeclared identifier 'shortvs';
line 53: Undeclared identifier 'direction';
line 57: Undeclared identifier 'direction';
line 118: Undeclared identifier 'pcup';
line 119: Undeclared identifier 'pcup';
line 121: Undeclared identifier 'pcdn';
line 122: Undeclared identifier 'pcdn'

This solves the errors, and will plot.

//@version=4
study("Chandelier Stop", overlay=true)

//input variables
Length2=input(title="Look Back Period", type=input.integer, defval=22)
ATRPeriod=input(title="ATR Period", type=input.integer, defval=22)
Mult=input(title="ATR Multiplier", type=input.integer, defval=3)

var float   shortvs     = na
var float   longvs      = na
var int     direction   = na

//calculate stop value
short_stop  = lowest(Length2)  + Mult * atr(ATRPeriod)
long_stop   = highest(Length2) - Mult * atr(ATRPeriod)

shortvs     := na(shortvs[1]) ? short_stop : iff(close > shortvs[1], short_stop , min(short_stop, shortvs[1]))
longvs      := na(longvs[1])  ? long_stop  : iff(close < longvs[1],  long_stop,   max(long_stop,  longvs[1]))

longswitch  = iff(close >= shortvs[1] and close[1] < shortvs[1], 1, 0)
shortswitch = iff(close <= longvs[1]  and close[1] > longvs[1] , 1, 0)

direction   := iff(na(direction[1]), 0, iff(direction[1] <= 0 and longswitch, 1, iff(direction[1] >= 0 and shortswitch, -1, direction[1])))
            
pcup        = direction > 0 ? longvs  : na
pcdn        = direction < 0 ? shortvs : na

plot(pcup, color=color.aqua, style=plot.style_circles, linewidth=2)
plot(pcup, color=color.aqua, style=plot.style_linebr,  linewidth=2)

plot(pcdn, color=color.fuchsia, style=plot.style_circles, linewidth=2)
plot(pcdn, color=color.fuchsia, style=plot.style_linebr,  linewidth=2)

You made a mistake in the indentation on line 19.

Tradingview takes indentation very seriously. Also, I have converted it into a strategy

strategy("Chandelier Stop", overlay=true)

//input variables
Length=input(title="Look Back Period", type=integer, defval=22)
ATRPeriod=input(title="ATR Period", type=integer, defval=22)
Mult=input(title="ATR Multiplier", type=integer, defval=3)

//calculate stop value
short_stop = lowest(Length)+Mult*atr(ATRPeriod)
long_stop  = highest(Length)-Mult*atr(ATRPeriod)

shortvs=na(shortvs[1]) ? short_stop : iff(close>shortvs[1], short_stop , min(short_stop,shortvs[1]))
longvs=na(longvs[1]) ? long_stop : iff(close<longvs[1], long_stop, max(long_stop,longvs[1]))

longswitch=iff (close>=shortvs[1] and close[1]<shortvs[1] , 1 ,  0)
shortswitch=iff (close<=longvs[1] and close[1]>longvs[1] ,  1 ,  0)

direction=
     iff(na(direction[1]), 0,
     iff (direction[1]<=0 and longswitch, 1,
     iff (direction[1]>=0 and shortswitch, -1, direction[1])))

pc=direction>0?longvs:shortvs

plot(pc, color=direction>0?aqua:fuchsia, style=circles, linewidth=2)
plot(pc, color=direction>0?aqua:fuchsia, style=line, linewidth=2)

strategy.entry("Buy",true,1,when = direction == 1)
strategy.entry("Sell",false,1, when = direction == -1)

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