Fixing needed for a "SUPERTREND" Pine Script Code

Viewed 17

I am looking forward to combine few overlays and one of them is "Supertrend". I am trying to use the following code that I have found in community script -

//@version=5
indicator(title="Supertrend", shorttitle="SuperTrend", overlay = true, timeframe="", timeframe_gaps=true)

Factor=input.int(defval=3, minval=1,maxval = 100, title="Factor")
Pd=input.int(10, minval=1, maxval = 100, title="ATR Length")

Up=hl2-(Factor*ta.atr(Pd))
Dn=hl2+(Factor*ta.atr(Pd))

TrendUp=close[1]>TrendUp[1]? math.max(Up,TrendUp[1]) : Up
TrendDown=close[1]<TrendDown[1]? math.min(Dn,TrendDown[1]) : Dn

Trend = close > TrendDown[1] ? 1: close< TrendUp[1]? -1: nz(Trend[1],1)
Tsl = Trend==1? TrendUp: TrendDown

linecolor = Trend == 1 ? color.green : color.red

plot(Tsl, color=linecolor, style=plot.style_line, linewidth=2, title="SuperTrend")

The code is giving me several errors as follows -

line 10: Undeclared identifier 'TrendUp' 
line 11: Undeclared identifier 'TrendDown'

Please help to fix these issues. Regards.

1 Answers

Self referencing is not allowed in v3 and above.

So, you need to do something like this:

TrendUp = 0.0
TrendDown = 0.0
TrendUp:=close[1]>TrendUp[1]? math.max(Up,TrendUp[1]) : Up
TrendDown:=close[1]<TrendDown[1]? math.min(Dn,TrendDown[1]) : Dn
Related