ATR Table (SMA)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © KhashayarJan

//@version=5
indicator("SMA ATR Table", overlay = true)

color i_TriggerBGColor = input(defval = color.green, title = "Trigger Time Background Color")
color i_TriggerTxtColor = input(defval = color.black,title = "Trigger Time Text Color")

color i_TradingBGColor = input(defval = color.blue,title = "Trading Time Background Color")
color i_TradingTxtColor = input(defval = color.white,title = "Trading Time Text Color")

color i_OtherBGColor = input(defval = color.gray,title = "Other Time Background Color")
color i_OtherTxtColor = input(defval = color.white,title = "Other Time Text Color")

i_decimal = input(defval = 2, title = "Decimal points")

var table atrDisplay = table.new(position.top_right, 3, 4, bgcolor = color.gray, frame_width = 2, frame_color = color.black)
// We call `ta.atr()` outside the `if` block so it executes on each bar.

atr5 = request.security(syminfo.tickerid, '5', ta.sma(ta.tr(true),12))
atr15 = request.security(syminfo.tickerid, '15', ta.sma(ta.tr(true),16))
atr1h = request.security(syminfo.tickerid, '60', ta.sma(ta.tr(true),24))
atr4h = request.security(syminfo.tickerid, '240', ta.sma(ta.tr(true),42))
atrD = request.security(syminfo.tickerid, '1D',ta.sma(ta.tr(true),30))
atrW = request.security(syminfo.tickerid, '1W', ta.sma(ta.tr(true),52))

rsi5 = request.security(syminfo.tickerid, '5', ta.rsi(close,12))
rsi15 = request.security(syminfo.tickerid, '15', ta.rsi(close,16))
rsi1h = request.security(syminfo.tickerid, '60', ta.rsi(close,24))
rsi4h = request.security(syminfo.tickerid, '240', ta.rsi(close,42))
rsi1D = request.security(syminfo.tickerid, '1D',ta.rsi(close,30))
rsi1W = request.security(syminfo.tickerid, '1W', ta.rsi(close,52))




if barstate.islast

    ATRformatStructure = '#.'

    for i = 1 to i_decimal
        ATRformatStructure += '0'
    

    color BGColor5 = i_OtherBGColor
    color BGColor15 = i_OtherBGColor
    color BGColor1h = i_OtherBGColor
    color BGColor4h = i_OtherBGColor
    color BGColor1D = i_OtherBGColor
    color BGColor1W = i_OtherBGColor
    
    color TxtColor5 = i_OtherTxtColor
    color TxtColor15 = i_OtherTxtColor
    color TxtColor1h = i_OtherTxtColor
    color TxtColor4h = i_OtherTxtColor
    color TxtColor1D = i_OtherTxtColor
    color TxtColor1W = i_OtherTxtColor


    switch timeframe.period
        "5" => 
            BGColor5 := i_TriggerBGColor
            TxtColor5 := i_TriggerTxtColor
                
            BGColor1h := i_TradingBGColor
            TxtColor1h := i_TradingTxtColor
                   
                
        "15" => 
            BGColor15 := i_TriggerBGColor
            TxtColor15 := i_TriggerTxtColor
                
            BGColor4h := i_TradingBGColor
            TxtColor4h := i_TradingTxtColor
           
                
        "60" => 
            BGColor1h := i_TriggerBGColor
            TxtColor1h := i_TriggerTxtColor
            
            BGColor1D := i_TradingBGColor
            TxtColor1D := i_TradingTxtColor        
            
            
        "240" =>    
            BGColor4h := i_TriggerBGColor
            TxtColor4h := i_TriggerTxtColor
            
            BGColor1W := i_TradingBGColor
            TxtColor1W := i_TradingTxtColor
            
        "D" =>  
            BGColor1D := i_TriggerBGColor
            TxtColor1D := i_TriggerTxtColor
            
            BGColor1W := i_TradingBGColor
            TxtColor1W := i_TradingTxtColor
    
        "W" =>  
            BGColor1W := i_TriggerBGColor
            TxtColor1W := i_TriggerTxtColor
        


    // We only populate the table on the last bar.
    table.cell(atrDisplay, 0, 0, "ATR 5m: " + str.tostring(atr5, ATRformatStructure), bgcolor = BGColor5, text_color = TxtColor5)
    table.cell(atrDisplay, 1, 0, "ATR 15m: " + str.tostring(atr15, ATRformatStructure), bgcolor = BGColor15, text_color = TxtColor15)
    table.cell(atrDisplay, 2, 0, "ATR 1H: " + str.tostring(atr1h, ATRformatStructure), bgcolor = BGColor1h, text_color = TxtColor1h)
    
    table.cell(atrDisplay, 0, 1, "RSI 5m: " + str.tostring(rsi5, ATRformatStructure), bgcolor = BGColor5, text_color = TxtColor5)
    table.cell(atrDisplay, 1, 1, "RSI 15m: " + str.tostring(rsi15, ATRformatStructure), bgcolor = BGColor15, text_color = TxtColor15)
    table.cell(atrDisplay, 2, 1, "RSI 1H: " + str.tostring(rsi1h, ATRformatStructure), bgcolor = BGColor1h, text_color = TxtColor1h)
    
    
    table.cell(atrDisplay, 0, 2, "ATR 4H: " + str.tostring(atr4h, ATRformatStructure), bgcolor = BGColor4h, text_color = TxtColor4h)
    table.cell(atrDisplay, 1, 2, "ATR 1D: " + str.tostring(atrD, ATRformatStructure), bgcolor = BGColor1D, text_color = TxtColor1D)
    table.cell(atrDisplay, 2, 2, "ATR 1W: " + str.tostring(atrW, ATRformatStructure), bgcolor = BGColor1W, text_color = TxtColor1W)
    
    table.cell(atrDisplay, 0, 3, "RSI 4H: " + str.tostring(rsi4h, ATRformatStructure), bgcolor = BGColor4h, text_color = TxtColor4h)
    table.cell(atrDisplay, 1, 3, "RSI 1D: " + str.tostring(rsi1D, ATRformatStructure), bgcolor = BGColor1D, text_color = TxtColor1D)
    table.cell(atrDisplay, 2, 3, "RSI 1W: " + str.tostring(rsi1W, ATRformatStructure), bgcolor = BGColor1W, text_color = TxtColor1W)