Alternative MTF Table
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RozaniGhani-RG

//@version=5
indicator('Alternative MTF Table', shorttitle = 'AMT', overlay = true)

// 1. Inputs
// 2. Variables
// 3. Arrays
// 4. Matrix
// 5. Construct

// ————————————————————————————————————————————————————————————————————————————— 1. Inputs {
G2       = 'TABLE'
T0       = 'For Timeframe Table only'
T1       = 'For both table'
i_b_TF   = input.bool(            true,   'Timeframe Table', group = G2, inline = '6')
i_s_TF   = input.string(      'Detail',                  '', group = G2, inline = '6', options = ['Simple', 'Detail'])
i_b_HP   = input.bool(           false,      'Helper Table', group = G2)
i_s_Y    = input.string(      'bottom',    'Table Position', group = G2, inline = '7', options = ['top',  'middle', 'bottom'])
i_s_X    = input.string(        'left',                  '', group = G2, inline = '7', options = ['left', 'center', 'right'],                   tooltip = T0)
i_s_font = input.string(      'normal',         'Font size', group = G2,               options = ['tiny', 'small',  'normal', 'large', 'huge'], tooltip = T1)
// }
// ————————————————————————————————————————————————————————————————————————————— 2. Variables {
PO = 'POSITION', SW = 'SWING', IN = 'INTRADAY'
var HPTable = table.new(position.middle_center,  4,  4, border_width = 1)
var TFtable = table.new(   i_s_Y + '_' + i_s_X, 20, 10, border_width = 1)
// }
// ————————————————————————————————————————————————————————————————————————————— 3. Arrays {
arr_time   = array.new_bool(6)
arr_trader = array.from('POSITION',   'SWING',  'INTRADAY')
arr_res    = array.from(  'HIGHER', 'PRIMARY',     'LOWER')
arr_res2   = array.from(  'DISTAL',   'CLEAR', 'PROXIMATE')
arr_tf0    = array.from(       'W',       'D',        '60')
arr_tf1    = array.from(       'D',      '60',        '15')
arr_tf2    = array.from(      '60',      '15',         '5')

for i = 0 to 5
    arr_time.set(0, timeframe.period == 'W'  and timeframe.change('W'))
    arr_time.set(1, timeframe.period == 'D'  and timeframe.change('D'))
    arr_time.set(2, timeframe.period == '60' and timeframe.change('60'))
    arr_time.set(3, timeframe.period == '15' and timeframe.change('15'))
    arr_time.set(4, timeframe.period == '5'  and timeframe.change('5'))
    arr_time.set(5, timeframe.period == '1'  and timeframe.change('1'))

index_TF = array.indexof(arr_time, true)

[TYPE, str_TF0, str_TF1, str_TF2, TF0, TF1, TF2] = switch index_TF
    // [TYPE,   str_TF0,   str_TF1,  str_TF2,  TF0,  TF1,  TF2]
    0 => [PO,  'HIGHER', 'PRIMARY',  'LOWER',  'W',  'D', '60']
    1 => [PO,  'HIGHER', 'PRIMARY',  'LOWER',  'W',  'D', '60']
    2 => [SW,  'HIGHER', 'PRIMARY',  'LOWER',  'D', '60', '15']
    3 => [IN,  'HIGHER', 'PRIMARY',  'LOWER', '60', '15',  '5']
    4 => [IN, 'PRIMARY',   'LOWER', 'LOWEST', '15',  '5',  '1']
    5 => [IN, 'PRIMARY',   'LOWER', 'LOWEST', '15',  '5',  '1']

[len_TF0, len_TF1, len_TF2] = switch index_TF
    //   [len_TF0, len_TF1, len_TF2]
    0 => [    100,      20,       4]
    1 => [    465,     100,      12]
    2 => [    700,     100,      28]
    3 => [    356,     100,      33]
    4 => [    300,     100,      22]
    5 => [   1461,     479,     100]

res_TF  = array.from(    TF0,       TF1,      TF2) // From index_TF
str_TF  = array.from(str_TF0,   str_TF1,  str_TF2) // From index_TF
arr_len = array.from(len_TF0,   len_TF1,  len_TF2) // From index_TF
// }
// ————————————————————————————————————————————————————————————————————————————— 4. Matrix {
m = matrix.new<string>(4, 4)
matrix.set(m, 0, 0, '')
for i = 0 to 2
    matrix.set(m, i + 1,     0, arr_trader.get(i))
    matrix.set(m, i + 1,     1, arr_tf0.get(i))
    matrix.set(m, i + 1,     2, arr_tf1.get(i))
    matrix.set(m, i + 1,     3, arr_tf2.get(i))
    matrix.set(m,     0, i + 1, arr_res.get(i) + ' / ' +  arr_res2.get(i))
// }
// ————————————————————————————————————————————————————————————————————————————— 5. Construct {
if barstate.islast
    if array.includes(arr_time, true) and i_b_TF
        table.cell(TFtable,     0,     0,     TYPE + ' TRADER', text_color = color.white, text_size = i_s_font, bgcolor = color.black)
        if i_s_TF == 'Detail'
            table.cell(TFtable,     0,     1,          'TIMEFRAME', text_color = color.white, text_size = i_s_font, bgcolor = color.black)
            table.cell(TFtable,     2,     1,               'ITEM', text_color = color.white, text_size = i_s_font, bgcolor = color.black)
            for i = 0 to 2
                table.cell(TFtable, 0, i + 2,               str_TF.get(i),            text_color = color.white, text_size = i_s_font, bgcolor = color.black)
                table.cell(TFtable, 1, i + 2,               res_TF.get(i),            text_color = color.white, text_size = i_s_font, bgcolor = color.black)
                table.cell(TFtable, 2, i + 2, str.tostring(arr_len.get(i)) + ' BARS', text_color = color.white, text_size = i_s_font, bgcolor = color.black)
            table.merge_cells(TFtable, 0, 0, 2, 0)
            table.merge_cells(TFtable, 0, 1, 1, 1)    
            if TYPE == 'INTRADAY'
                table.cell(TFtable, 0,    5,  '* CHECK PRIMARY TF', text_color = color.blue,  text_size = i_s_font, bgcolor = color.new(color.blue, 100))
                table.merge_cells(TFtable, 0, 5, 2, 5)
    if i_b_HP
        for x = 0 to 3
            for y = 0 to 3
                table.cell(HPTable, y, x, matrix.get(m, y, x), text_color = color.white, text_size = i_s_font, bgcolor = color.black)
        for y = 0 to 3
            table.cell_set_bgcolor(HPTable, y, 1, color.gray)
            table.cell_set_bgcolor(HPTable, y, 2, color.blue)
            table.cell_set_bgcolor(HPTable, y, 3, color.purple)
        table.cell_set_bgcolor(HPTable, 0, 0, color.new(color.blue, 100))
// }
// }