Thimma
Thimma

Reputation: 1492

How do I use `ta.rsi` in a double for loop in pine script?

My goal is to store RSI values for multiple symbols and timeframes.

So far, I've got the following structure:

string[] symbols = array.new_string(3)
array.set(symbols, 0, "AAPL")
array.set(symbols, 1, "TSLA")
array.set(symbols, 2, "MSFT")

string[] timeframes = array.new_string(3)
array.set(timeframes, 0, "60")
array.set(timeframes, 1, "240")
array.set(timeframes, 2, "480")

I am now trying to loop through these arrays like this:

for symbol_idx = 0 to array.size(symbols) - 1
    string symbol = array.get(symbols, symbol_idx)

    for timeframe_idx = 0 to array.size(timeframes) - 1
        string timeframe = array.get(timeframes, timeframe_idx)

After which I try to get the close price and calculate RSI like here:

for symbol_idx = 0 to array.size(symbols) - 1
    string symbol = array.get(symbols, symbol_idx)

    for timeframe_idx = 0 to array.size(timeframes) - 1
        string timeframe = array.get(timeframes, timeframe_idx)

        [_open, _close] = request.security(symbol, timeframe, [open, close])
        
        float rsi_open = ta.rsi(_open, 14)
        float rsi_close = ta.rsi(_close, 14)

        // Do some more calculations

But I get the following error every time:

"The function 'ta.rsi' should be called on each calculation for consistency. It is recommended to extract the call from this scope"

I've tried dozens of other variants such as moving the calculation to a different loop or using matrixes. I would really appreciate your help!

Upvotes: 0

Views: 32

Answers (0)

Related Questions