TradingCube : Moving Average : Data table
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © tradingcube

//@version=5
indicator(title='TradingCube : Moving Average : Data table', shorttitle='Moving Average : Data table', overlay=true, precision=1)

htf_tf = input.string('Auto', 'Table Timeframe', options=['Auto', '5 Min','10 Min', '15 Min', '1 Hour', '4 Hour', 'Daily', 'Weekly', 'Monthly', 'Quarterly', 'Yearly'])
htf    = htf_tf == '5 Min' ? '5' : htf_tf == '10 Min' ? '10' : htf_tf == '15 Min' ? '15' : htf_tf == '1 Hour' ? '60' : htf_tf == '4 Hour' ? '240' : htf_tf == 'Daily' ? 'D' : htf_tf == 'Weekly' ? 'W' : htf_tf == 'Monthly' ? 'M' : htf_tf == 'Quarterly' ? '3M' : htf_tf == 'Yearly' ? '12M' : 
          timeframe.isintraday and (timeframe.period == '1'  or timeframe.period == '3'  or timeframe.period == '5'  or timeframe.period == '15') ? 'D' : 
          timeframe.isintraday and (timeframe.period == '30' or timeframe.period == '45' or timeframe.period == '60' or timeframe.period == '120' or timeframe.period == '180' or timeframe.period == '240') ? 'W' : 
          timeframe.isdaily ? 'M' : timeframe.isweekly or timeframe.ismonthly ? '12M' : '3M'
          
          
timeFrameD = htf

source = request.security(syminfo.tickerid, timeFrameD, close, lookahead=barmerge.lookahead_on)

ema_enable = input.bool(title='Enable EMA [* Default is SMA]', defval=false)

moving_avg = array.new_int(0, 0)
moving_avg_data = array.new_float(0, 0.0)
moving_avg_show = array.new_bool(0, false)

ma_1_show = input.bool(title='', defval=true, inline='cell_1')
ma_1 = input.int(title="Data Cell 1", defval=5, inline='cell_1')
ma_1_data = ema_enable ? ta.ema(source, ma_1) : ta.sma(source, ma_1)

ma_2_show = input.bool(title = '', defval =true, inline = 'cell_2')
ma_2 = input.int(title = "Data Cell 2", defval = 8, inline = 'cell_2')
ma_2_data = ema_enable ? ta.ema(source, ma_2) : ta.sma(source, ma_2)

ma_3_show = input.bool(title = '', defval =true, inline = 'cell_3')
ma_3 = input.int(title = "Data Cell 3", defval = 13, inline = 'cell_3')
ma_3_data = ema_enable ? ta.ema(source, ma_3) : ta.sma(source, ma_3)

ma_4_show = input.bool(title = '', defval =true, inline = 'cell_4')
ma_4 = input.int(title = "Data Cell 4", defval = 21, inline = 'cell_4')
ma_4_data = ema_enable ? ta.ema(source, ma_4) : ta.sma(source, ma_4)

ma_5_show = input.bool(title = '', defval =true, inline = 'cell_5')
ma_5 = input.int(title = "Data Cell 5", defval = 34, inline = 'cell_5')
ma_5_data = ema_enable ? ta.ema(source, ma_5) : ta.sma(source, ma_5)

ma_6_show = input.bool(title = '', defval =true, inline = 'cell_6')
ma_6 = input.int(title = "Data Cell 6", defval = 55, inline = 'cell_6')
ma_6_data = ema_enable ? ta.ema(source, ma_6) : ta.sma(source, ma_6)

ma_7_show = input.bool(title = '', defval = true, inline = 'cell_7')
ma_7 = input.int(title = "Data Cell 7", defval = 89, inline = 'cell_7')
ma_7_data = ema_enable ? ta.ema(source, ma_7) : ta.sma(source, ma_7)

ma_8_show = input.bool(title = '', defval = true, inline = 'cell_8')
ma_8 = input.int(title = "Data Cell 8", defval = 100, inline = 'cell_8')
ma_8_data = ema_enable ? ta.ema(source, ma_8) : ta.sma(source, ma_8)

ma_9_show = input.bool(title = '', defval = true, inline = 'cell_9')
ma_9 = input.int(title = "Data Cell 9", defval = 144, inline = 'cell_9')
ma_9_data = ema_enable ? ta.ema(source, ma_9) : ta.sma(source, ma_9)

ma_10_show = input.bool(title = '', defval = true, inline = 'cell_10')
ma_10 = input.int(title = "Data Cell 10", defval = 200, inline = 'cell_10')
ma_10_data = ema_enable ? ta.ema(source, ma_10) : ta.sma(source, ma_10)

ma_11_show = input.bool(title = '', defval = true, inline = 'cell_11')
ma_11 = input.int(title = "Data Cell 11", defval = 233, inline = 'cell_11')
ma_11_data = ema_enable ? ta.ema(source, ma_11) : ta.sma(source, ma_11)

ma_12_show = input.bool(title = '', defval = true, inline = 'cell_12')
ma_12 = input.int(title = "Data Cell 12", defval = 377, inline = 'cell_12')
ma_12_data = ema_enable ? ta.ema(source, ma_12) : ta.sma(source, ma_12)

ma_13_show = input.bool(title = '', defval = false, inline = 'cell_13')
ma_13 = input.int(title = "Data Cell 13", defval = 400, inline = 'cell_13')
ma_13_data = ema_enable ? ta.ema(source, ma_13) : ta.sma(source, ma_13)

ma_14_show = input.bool(title = '', defval = false, inline = 'cell_14')
ma_14 = input.int(title = "Data Cell 14", defval = 10, inline = 'cell_14')
ma_14_data = ema_enable ? ta.ema(source, ma_14) : ta.sma(source, ma_14)

ma_15_show = input.bool(title = '', defval = false, inline = 'cell_15')
ma_15 = input.int(title = "Data Cell 15", defval = 20, inline = 'cell_15')
ma_15_data = ema_enable ? ta.ema(source, ma_15) : ta.sma(source, ma_15)

ma_16_show = input.bool(title = '', defval = false, inline = 'cell_16')
ma_16 = input.int(title = "Data Cell 16", defval = 30, inline = 'cell_16')
ma_16_data = ema_enable ? ta.ema(source, ma_16) : ta.sma(source, ma_16)


array.push(moving_avg, ma_1)
array.push(moving_avg_data, ma_1_data)
array.push(moving_avg_show, ma_1_show)

array.push(moving_avg, ma_2)
array.push(moving_avg_data, ma_2_data)
array.push(moving_avg_show, ma_2_show)

array.push(moving_avg, ma_3)
array.push(moving_avg_data, ma_3_data)
array.push(moving_avg_show, ma_3_show)

array.push(moving_avg, ma_4)
array.push(moving_avg_data, ma_4_data)
array.push(moving_avg_show, ma_4_show)

array.push(moving_avg, ma_5)
array.push(moving_avg_data, ma_5_data)
array.push(moving_avg_show, ma_5_show)

array.push(moving_avg, ma_6)
array.push(moving_avg_data, ma_6_data)
array.push(moving_avg_show, ma_6_show)

array.push(moving_avg, ma_7)
array.push(moving_avg_data, ma_7_data)
array.push(moving_avg_show, ma_7_show)

array.push(moving_avg, ma_8)
array.push(moving_avg_data, ma_8_data)
array.push(moving_avg_show, ma_8_show)

array.push(moving_avg, ma_9)
array.push(moving_avg_data, ma_9_data)
array.push(moving_avg_show, ma_9_show)

array.push(moving_avg, ma_10)
array.push(moving_avg_data, ma_10_data)
array.push(moving_avg_show, ma_10_show)

array.push(moving_avg, ma_11)
array.push(moving_avg_data, ma_11_data)
array.push(moving_avg_show, ma_11_show)

array.push(moving_avg, ma_12)
array.push(moving_avg_data, ma_12_data)
array.push(moving_avg_show, ma_12_show)

array.push(moving_avg, ma_13)
array.push(moving_avg_data, ma_13_data)
array.push(moving_avg_show, ma_13_show)

array.push(moving_avg, ma_14)
array.push(moving_avg_data, ma_14_data)
array.push(moving_avg_show, ma_14_show)

array.push(moving_avg, ma_15)
array.push(moving_avg_data, ma_15_data)
array.push(moving_avg_show, ma_15_show)

array.push(moving_avg, ma_16)
array.push(moving_avg_data, ma_16_data)
array.push(moving_avg_show, ma_16_show)


//table colors
top_row_col = color.new(color.gray, 0)
// plot(ltp)
var string display_box = 'Display Table Position'
string table_position_y = input.string('bottom', 'Panel position', inline='11', options=['top', 'middle', 'bottom'], group=display_box)
string table_position_x = input.string('left', '', inline='11', options=['left', 'center', 'right'], group=display_box)

//create table
var table_panel = table.new(position = table_position_y + '_' + table_position_x, columns = 20, rows = 20, frame_color = color.new(color.black, 0), frame_width = 1, border_color =color.new(color.black, 0), border_width = 1)

if barstate.islast
    table.cell(table_panel, 0, 0, syminfo.ticker, bgcolor = top_row_col, text_color = color.white, text_size = size.tiny)
    table.cell(table_panel, 0, 0, syminfo.ticker, bgcolor = top_row_col, text_color = color.white, text_size = size.tiny)
    table.cell(table_panel, 0, 1, str.tostring(close), bgcolor = top_row_col, text_color = color.white, text_size = size.tiny)
for i = 0 to 15 by 1
    if array.get(moving_avg_show, i)
        table.cell(table_panel, i+1, 0, str.tostring(array.get(moving_avg, i)), bgcolor = top_row_col, text_color = color.white, text_size = size.tiny)
        table.cell(table_panel, i+1, 1, str.tostring(array.get(moving_avg_data, i), format.mintick), bgcolor = array.get(moving_avg_data, i) < close ? color.new(color.green, 0) : color.new(color.red, 0), text_color = color.white, text_size = size.tiny)    
    