RSI & Stoch MultiTime
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © JoshuaDanford

//@version=5
indicator("MultiTime Indicator Panel", overlay=false)

rsiPeriod = input(14, "Rsi Period")
stochPeriod = input(14, "Stoch Period")
atrPeriod = input(3, "ATR Period")
supertrendFactor = input(4, "Supertrend Factor")
smaPeriod = input(20, "SMA Period")

tableAlignment = input.string(position.bottom_center, "Table Alignment", options = [position.bottom_center, position.bottom_left, position.bottom_right, position.middle_center, position.middle_left, position.middle_right, position.top_center, position.top_left, position.top_right])

getValueForTime(timeStr, expr) =>
    request.security(syminfo.ticker, timeStr, expr)

getColor(value) =>
    color.from_gradient(value, 0, 100, color.rgb(0, 255, 0), color.rgb(255, 0, 0))

var indicatorTable = table.new(position = tableAlignment, columns = 8, rows = 5, bgcolor = color.rgb(0,0,0, 50), border_width = 1)

table.cell(table_id = indicatorTable, column = 0, row = 1,  text = "RSI", text_color = color.white)
table.cell(table_id = indicatorTable, column = 0, row = 2,  text = "Stoch", text_color = color.white)
table.cell(table_id = indicatorTable, column = 0, row = 3,  text = "Supertrend", text_color = color.white)
table.cell(table_id = indicatorTable, column = 0, row = 4,  text = "SMA (" + str.tostring(smaPeriod) + ")", text_color = color.white)

table.cell(table_id = indicatorTable, column = 1, row = 0,  text = "1m", text_color = color.white)
table.cell(table_id = indicatorTable, column = 2, row = 0,  text = "5m", text_color = color.white)
table.cell(table_id = indicatorTable, column = 3, row = 0,  text = "15m", text_color = color.white)
table.cell(table_id = indicatorTable, column = 4, row = 0,  text = "30m", text_color = color.white)
table.cell(table_id = indicatorTable, column = 5, row = 0,  text = "1H", text_color = color.white)
table.cell(table_id = indicatorTable, column = 6, row = 0,  text = "4H", text_color = color.white)
table.cell(table_id = indicatorTable, column = 7, row = 0,  text = "1D", text_color = color.white)

makeTableCell(column, row, bgColor, cellText) =>
    table.cell(table_id = indicatorTable, column = column, row = row,  text = cellText, bgcolor = bgColor, text_color = color.white)

makeRow(row, bgcolors, cellTexts) =>
    for int i = 0 to 6
        makeTableCell(1 + i, row, array.get(bgcolors, i), array.get(cellTexts, i))

fillArrayWithTime(timeStr, expr, mode, cellTexts, bgColors) =>
    value = getValueForTime(timeStr, expr)
    
    if (mode == "float")
        array.push(bgColors, getColor(value))
        array.push(cellTexts, str.tostring(math.round(value, 2)))
    else if (mode == "signal")
        array.push(bgColors, getColor(value == 1 ? 100 : value == -1 ? 0 : 50))
        array.push(cellTexts, value == -1 ? "Buy" : value == 1 ? "Sell" : "Neutral")
    [cellTexts, bgColors]

makeMultitimeIndicatorArray(expr, mode) =>
    cellTexts = array.new_string(0, "")
    bgColors = array.new_color(0, color.black)
    
    fillArrayWithTime("1", expr, mode, cellTexts, bgColors)
    fillArrayWithTime("5", expr, mode, cellTexts, bgColors)
    fillArrayWithTime("15", expr, mode, cellTexts, bgColors)
    fillArrayWithTime("30", expr, mode, cellTexts, bgColors)
    fillArrayWithTime("60", expr, mode, cellTexts, bgColors)
    fillArrayWithTime("240", expr, mode, cellTexts, bgColors)
    fillArrayWithTime("1D", expr, mode, cellTexts, bgColors)
    
    [cellTexts, bgColors]
    
supertrendDirection() =>
    [supertrend, direction] = ta.supertrend(supertrendFactor, atrPeriod)
    direction

[rsiCellTexts, rsiBgColors] = makeMultitimeIndicatorArray(ta.rsi(close, rsiPeriod), "float")
[stochCellTexts, stochBgColors] = makeMultitimeIndicatorArray(ta.stoch(close, high, low, stochPeriod), "float")
[supertrendTexts, supertrendBgColors] = makeMultitimeIndicatorArray(supertrendDirection(), "signal")
[maTrendTexts, maTrendBgColors] = makeMultitimeIndicatorArray(ta.sma(close, 20) > close ? 1 : -1, "signal")

makeRow(1, rsiBgColors, rsiCellTexts)
makeRow(2, stochBgColors, stochCellTexts)
makeRow(3, supertrendBgColors, supertrendTexts)
makeRow(4, maTrendBgColors, maTrendTexts)