pine-scriptself-referencethinkscript

Referencing history within the identifier [PineScript v5]


I want to reference to the history of the identifier in the same line. It is probably not possible this way. But it works in thinkorswim. I would want the signal either print +1 or -1 else print the last signal that is either +1 or -1 so it is in a loop & wont skip signals. Is there a work around that? Any help is appreciated!

ATRFactor = ta.tr(true)
Avg = ta.ema(close, 50)

Signal = close>(Avg+ATRFactor) ?  1 : close<(Avg-ATRFactor) ? -1 :  signal[1]

Solution

  • You where on the good way. You just need to declare the variable before using it :

    //@version=5
    indicator("My script")
    ATRFactor = ta.tr(true)
    Avg = ta.ema(close, 50)
    
    var Signal = 0.0
    Signal := close>(Avg+ATRFactor) ?  1 : close<(Avg-ATRFactor) ? -1 :  Signal[1]
    plot(Signal)