Search code examples
pine-scriptpine-script-v5algorithmic-tradingtradingview-api

Converting an Indicator in TradingView from V2 to V5


I am trying to convert this indicator "Hilo Activator" from v2 to v5. I did everything except the "stop", it kept saying the that it is undeclared identifier "series".

This is the original source code V2:

study(title="HiLo Activator", overlay=true)
length = input(3, minval=1)
displace = input(0, minval=0)
highsma = sma(high, length)
lowsma = sma(low, length)
swing = iff(close > highsma[displace], 1, iff(close < lowsma[displace], -1, 0))
mah = highest(high, length)
mal = lowest(low, length)
stop = iff(swing==1, mal, iff(swing==-1, mah, stop[1]))
linecolor = iff(stop < low, green, red)
plot(stop, style=line, linewidth=2, color=linecolor)

I tried using the automated pinescript complier to convert it, but it says the same problem that "stop" is not defined. I also changed all the updated variable and the iff function.

This is what I did until now (V5):

length = input.int(3, minval=1, group = GroupHiLo)
displace = input.int(0, minval=0)
highsma = ta.sma(high, length)
lowsma = ta.sma(low, length)
swing = (close > highsma[displace] ? 1 : (close < lowsma[displace]? -1: 0))
mah = ta.highest(high, length)
mal = ta.lowest(low, length)
stop = swing==1 ? mal : (swing==-1 ? mah : stop[1])
linecolor = (stop < low ? color.green : color.red)
plot(stop, style = plot.style_line, linewidth=2, color=linecolor)

Solution

  • 
    //@version=5
    indicator("HiLo Activator", overlay=true)
    
    length = input.int(3, minval=1)
    displace = input.int(0, minval=0)
    
    highsma = ta.sma(high, length)
    lowsma = ta.sma(low, length)
    
    swing = 0
    swing := close > highsma[displace] ? 1 : close < lowsma[displace] ? -1 : 0
    
    mah = ta.highest(high, length)
    mal = ta.lowest(low, length)
    
    stop = 0.0
    stop := swing == 1 ? mal : swing == -1 ? mah : stop[1]
    
    linecolor = stop < low ? color.green : color.red
    plot(stop, style=plot.style_line, linewidth=2, color=linecolor)