Why Does My Indicator Keep Changing It's Timeframe Function When It's Suppose To Be Constant?

Viewed 19

First, some context before the code. It's just a multi-time frame table, that shows SMAs.

There are 3 SMAs determined by the user. Based on these 3, using the varibles I've written as bullish, or bearish, and the timeframe determined in each line, it then is suppose to show if the SMAs on each timeframe are in a bullish, or bearish formation. If it's bullish, it will show green on a cell table. If it's bearish, it will show red on the same cell table.

My issue is, when changing the charts timeframe, it changes the indicators on the cell table.

Here's the source code...

//@version=5
indicator("SMA Tble", overlay = true)
//
//
tableYposInput  = input.string("top", "Panel position", options = ["top", "middle", "bottom"], group = "Turn Table Settings")
tableXposInput  = input.string("right", "", options = ["left", "center", "right"])
//
var table TTM = table.new(tableYposInput + "_" + tableXposInput, 10, 4, border_width = 1)
//
TC = input.color(color.new(color.white, 0), "Table Text Color")
TS = input.string(size.small, "Table Text Size", options = [size.tiny, size.small, size.normal, size.large])
//
//
//
//
length1 = input(8, "Short MA", group = "MA Lengths", tooltip = "This Changes BOTH EMA & SMA Lengths")
length2 = input(15, "Mid MA Length")
length3 = input(21, "Long MA Length")
//
sma1 = ta.sma(close, length1)
sma2 = ta.sma(close, length2)
sma3 = ta.sma(close, length3)
//
//
smabull = sma1 > sma2 and sma2 > sma3
smabear = sma1 < sma2 and sma2 < sma3
//
//
smacolor = smabull ? color.green : smabear ? color.red : color.black
[sma_1m] = request.security(syminfo.tickerid, "1", [smacolor])
[sma_5m] = request.security(syminfo.tickerid, "5", [smacolor])
[sma_15m] = request.security(syminfo.tickerid, "15", [smacolor])
[sma_30m] = request.security(syminfo.tickerid, "30", [smacolor])
[sma_1H] = request.security(syminfo.tickerid, "60", [smacolor])
[sma_4H] = request.security(syminfo.tickerid, "240", [smacolor])
[sma_D] = request.security(syminfo.tickerid, "D", [smacolor])
[sma_W] = request.security(syminfo.tickerid, "W", [smacolor])
[sma_M] = request.security(syminfo.tickerid, "M", [smacolor])
//
//
if barstate.isconfirmed
    table.cell(TTM, 0, 0, "SMA", text_color = color.new(color.white, 0), bgcolor = color.new(color.gray, 0), text_size = TS)
    table.cell(TTM, 1, 0, "1m", text_color = TC, bgcolor = sma_1m, text_size = TS)
    table.cell(TTM, 2, 0, "5m", text_color = TC, bgcolor = sma_5m, text_size = TS)
    table.cell(TTM, 3, 0, "15m", text_color = TC, bgcolor = sma_15m, text_size = TS)
    table.cell(TTM, 4, 0, "30m", text_color = TC, bgcolor = sma_30m, text_size = TS)
    table.cell(TTM, 5, 0, "1H", text_color = TC, bgcolor = sma_1H, text_size = TS)
    table.cell(TTM, 6, 0, "4H", text_color = TC, bgcolor = sma_4H, text_size = TS)
    table.cell(TTM, 7, 0, "D", text_color = TC, bgcolor = sma_D, text_size = TS)
    table.cell(TTM, 8, 0, "W", text_color = TC, bgcolor = sma_W, text_size = TS)
    table.cell(TTM, 9, 0, "M", text_color = TC, bgcolor = sma_M, text_size = TS)
//
//

How is this possible, if it's going based on the timeframe already determined ? And how do I fix to where the timeframes are constant ?

0 Answers
Related