Performance Table
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © tv94067

//@version=5
indicator('Performance Table', overlay=true)

TablePos = input.string(title='Tabel Position', defval='Bottom Center', options=['Top Right',  'Middle Right',  'Bottom Right', 
     'Top Center', 'Middle Center', 'Bottom Center', 'Top Left', 'Middle Left', 'Bottom Left'], inline='tabp', group = '')

tableColumnsInput = input.int(9, 'Columns', minval=1, maxval=9, step=1, inline='tabp', group = '')

_tablePos = TablePos == 'Top Right' ? position.top_right : TablePos == 'Middle Right' ? position.middle_right : 
     TablePos == 'Bottom Right' ? position.bottom_right : TablePos == 'Top Center' ? position.top_center : 
         TablePos == 'Middle Center'? position.middle_center : TablePos == 'Bottom Center'? position.bottom_center : 
             TablePos == 'Top Left' ? position.top_left : TablePos == 'Middle Left' ? position.middle_left : position.bottom_left 

//tooltipText = 'Custom timeframes can be added via the chart's Timeframe dropdown. Until added, custom timeframes show up as 'Chart' instead.'
showTF1 = input.bool(true, '1-D', inline = 'TF1', group = '')
showTF2 = input.bool(true, '5-D', inline = 'TF1', group = '')
showTF3 = input.bool(true, '1-W', inline = 'TF1', group = '')
showTF4 = input.bool(true, '4-W', inline = 'TF1', group = '')
showTF5 = input.bool(true, '1-M', inline = 'TF2', group = '')
showTF6 = input.bool(true, '3-M', inline = 'TF2', group = '')
showTF7 = input.bool(true, '6-M', inline = 'TF2', group = '')
showTF8 = input.bool(true, '12-M', inline = 'TF2', group = '')
showTF9 = input.bool(true, '52-W', inline = 'TF2', group = '')

showTFArray = array.from(showTF1, showTF2, showTF3, showTF4, showTF5, showTF6, showTF7, showTF8, showTF9)
timeframeArray = array.from('1D', '5D', '1W', '4W', '1M', '3M', '6M', '12M', '52W')

var table perfTable = table.new(position = _tablePos, columns = tableColumnsInput, rows = array.size(timeframeArray), 
     bgcolor = na, frame_color = na , frame_width = 0, border_color = na, border_width = 1)

lightTransp = 90
avgTransp = 80
heavyTransp = 70

// === USER INPUTS ===
i_posColor = input(color.rgb(38, 166, 154), title='Positive Color', inline='col')
i_negColor = input(color.rgb(240, 83, 80), title='Negative Color', inline='col')
i_volColor = input(color.new(#999999, 0), title='Neutral Color', inline='col')

f_rateOfreturn(current_close, previous_close) =>
    (current_close - previous_close) * 100 / math.abs(previous_close)

f_performance(_security, _timeframe, _barsBack) =>
    request.security(_security, _timeframe, f_rateOfreturn(close, close[_barsBack]), gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)

f_fillCell(_table, _column, _row, _value, _timeframe) =>
    _c_color = _value >= 0 ? i_posColor : i_negColor
    _transp = math.abs(_value) > 10 ? heavyTransp : math.abs(_value) > 5 ? avgTransp : lightTransp
    _cellText = str.tostring(_value, '0.00') + '%\n' + _timeframe
    table.cell(_table, _column, _row, _cellText, bgcolor=color.new(_c_color, _transp), text_color=_c_color, width=6)

performanceArray = array.from(f_performance(syminfo.tickerid, '1D', 1), f_performance(syminfo.tickerid, '1D', 5),
     f_performance(syminfo.tickerid, '1W', 1), f_performance(syminfo.tickerid, '1W', 4),
         f_performance(syminfo.tickerid, '1M', 1), f_performance(syminfo.tickerid, '1M', 3),
             f_performance(syminfo.tickerid, '1M', 6), f_performance(syminfo.tickerid, '1M', 12),
                 f_performance(syminfo.tickerid, '1W', 52))

if barstate.islast
    filled = 0
    for i = 0 to array.size(showTFArray) - 1
        if array.get(showTFArray, i)
            f_fillCell(perfTable, filled % tableColumnsInput, filled / tableColumnsInput, array.get(performanceArray, i), 
             array.get(timeframeArray, i))
            filled += 1


   
    


