Is there an alternative to putting technical analysis calculations inside for loops in Pinescript?

50 Views Asked by At

I am running into the following issue with my code: ** Warning at 259:21 The function 'ta.rsi' should be called on each calculation for consistency. It is recommended to extract the call from this scope**

I want to see if RSI dipped below 40 at any point while a pullback was happening. I don't see how I could check this without performing an RSI calculation with a for loop. I already have the start and stop bars of the pullback and whether it was a positive or negative pullback. Any help would be much appreciated, thank you!

getRSIStatus(startBar, endBar, pullbackType) =>
    status = "neutral"

    var float rsiValue = na
    for i = startBar to endBar
        rsiValue := ta.rsi(close[i], 14)

        isPositivePullback = pullbackType == "up"
        if (isPositivePullback and rsiValue > 60)
            status := "overbought"
            break
        else if (not isPositivePullback and rsiValue < 40)
            status := "oversold"
            break
    status```
1

There are 1 best solutions below

3
SRPV On
//@version=5
indicator("stk",overlay=true)
getRSI(cl,len)=>
    ta.rsi(cl,len)
getRSIStatus(startBar, endBar, pullbackType) =>
    status = "neutral"

    var float rsiValue = na
    for i = startBar to endBar
        rsiValue := getRSI(close[i],14)

        isPositivePullback = pullbackType == "up"
        if (isPositivePullback and rsiValue > 60)
            status := "overbought"
            break
        else if (not isPositivePullback and rsiValue < 40)
            status := "oversold"
            break
    status
plot(ta.vwap(close))

Moving RSI fetch part to another method will resolve this issue.