Average RSI of multiple preset stock as an indicator line in a separate pane

35 Views Asked by At

I wanna fetch the end of day RSI of a series of stocks then average them to a final number and plot the results as line in a separate pane.

I tried the following code but get stuck at the for loop:

//@version=5
indicator("Average End-of-Day RSI", overlay=true)

// List of ticker symbols
symbols = input.symbol ("AAPL", "GOOGL", "MSFT", "AMZN")

// Input for RSI period
rsiPeriod = input(14, title="RSI Period")

// Function to calculate average RSI
calculateAverageRSI(symbol) =>
    // Fetch RSI data for the specified symbol
    rsiData = request.security(symbol, "D", ta.rsi(close, rsiPeriod))

    // Calculate the last RSI value
    lastRSI = ta.valuewhen(na(rsiData[1]) ? na : true, rsiData, 0)

    // Plot the last RSI value
    plot(lastRSI, title=symbol + " RSI", color=color.new(color.blue, 0), linewidth=2)

    // Return the last RSI value
    lastRSI

// Array to store RSI values for each symbol
rsiValues = array.new_float(size=0)

// Loop through each symbol, calculate last RSI, and store in array
for symbol in symbols
    array.push(rsiValues, calculateAverageRSI(symbol))

// Calculate and plot the average RSI
avgRSI = array.avg(rsiValues)
plot(avgRSI, title="Average RSI", color=color.red, linewidth=2)
0

There are 0 best solutions below