Debug tool - table
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © fikira

//@version=5
indicator("Debug tool - table", max_bars_back=1000, overlay=true)

i_choice = input.string('middle', title='options', options=['middle', 'time'])
i_time   = input.time(timestamp("20 Jul 2022 00:00 +0200"), "Date")
i_pos    = input.string(position.top_right, title='position table', 
 options=[position.top_left   , position.top_center   , position.top_right   ,
          position.middle_left, position.middle_center, position.middle_right,
          position.bottom_left, position.bottom_center, position.bottom_right])
var int bixL = na, var int bixM = na, var int dist = na, var float hi = na, var float lo = na, var table tab = na, var int[] num = array.from(-1)

// box
var box bxM  = box.new(na, na, na, na, xloc= xloc.bar_index, bgcolor= color.new(color.orange, 90), border_color= color.orange)
var box bxT  = box.new(na, na, na, na, xloc= xloc.bar_time , bgcolor= color.new(color.orange, 90), border_color= color.orange)

//––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
// example                                                              |
v_rsi1   = ta.rsi(close,  5) // value from script                       |
var rsi1 = v_rsi1            // value which will be placed in table     |
//                                                                      |
//––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

// further values

// macd - signal - histogram
[v_macd1, v_sign1, v_hist1]  = ta.macd(close,  9, 14,  5)
[v_macd2, v_sign2, v_hist2]  = ta.macd(close, 12, 26,  9)
[v_macd3, v_sign3, v_hist3]  = ta.macd(close, 15, 30, 11)
[v_macd4, v_sign4, v_hist4]  = ta.macd(close, 18, 34, 13)
[v_macd5, v_sign5, v_hist5]  = ta.macd(close, 21, 38, 15)
[v_macd6, v_sign6, v_hist6]  = ta.macd(close, 24, 42, 17)

// dir macd - signal - histogram
v_dir_macd1 = math.sign(v_macd1), v_dir_sign1 = math.sign(v_sign2), v_dir_hist1 = math.sign(v_hist3)
v_dir_macd2 = math.sign(v_macd2), v_dir_sign2 = math.sign(v_sign2), v_dir_hist2 = math.sign(v_hist2)
v_dir_macd3 = math.sign(v_macd3), v_dir_sign3 = math.sign(v_sign3), v_dir_hist3 = math.sign(v_hist3)
v_dir_macd4 = math.sign(v_macd4), v_dir_sign4 = math.sign(v_sign4), v_dir_hist4 = math.sign(v_hist4)
v_dir_macd5 = math.sign(v_macd5), v_dir_sign5 = math.sign(v_sign5), v_dir_hist5 = math.sign(v_hist5)
v_dir_macd6 = math.sign(v_macd6), v_dir_sign6 = math.sign(v_sign6), v_dir_hist6 = math.sign(v_hist6)

// rsi
v_rsi2 = ta.rsi(close,  7)
v_rsi3 = ta.rsi(close, 10)
v_rsi4 = ta.rsi(close, 14)
v_rsi5 = ta.rsi(close, 21)
v_rsi6 = ta.rsi(close, 30)

// dir rsi
v_dir_rsi1  = v_rsi1 > 50 ? 1 : v_rsi1 < 50 ? -1 : 0
v_dir_rsi2  = v_rsi2 > 50 ? 1 : v_rsi2 < 50 ? -1 : 0
v_dir_rsi3  = v_rsi3 > 50 ? 1 : v_rsi3 < 50 ? -1 : 0
v_dir_rsi4  = v_rsi4 > 50 ? 1 : v_rsi4 < 50 ? -1 : 0
v_dir_rsi5  = v_rsi5 > 50 ? 1 : v_rsi5 < 50 ? -1 : 0
v_dir_rsi6  = v_rsi6 > 50 ? 1 : v_rsi6 < 50 ? -1 : 0

// supertrend
[v_st1, v_dir_st1] = ta.supertrend(2,  7), v_dir_st1 *= -1
[v_st2, v_dir_st2] = ta.supertrend(3, 14), v_dir_st2 *= -1
[v_st3, v_dir_st3] = ta.supertrend(4, 20), v_dir_st3 *= -1
[v_st4, v_dir_st4] = ta.supertrend(5, 30), v_dir_st4 *= -1
[v_st5, v_dir_st5] = ta.supertrend(6, 50), v_dir_st5 *= -1
[v_st6, v_dir_st6] = ta.supertrend(7, 70), v_dir_st6 *= -1

// values for table
var macd1    = v_macd1, var dir_macd1 = v_dir_macd1, var sign1 = v_sign1, var dir_sign1 = v_dir_sign2, var hist1 = v_hist1, var dir_hist1 = v_dir_hist3
var macd2    = v_macd2, var dir_macd2 = v_dir_macd2, var sign2 = v_sign2, var dir_sign2 = v_dir_sign2, var hist2 = v_hist2, var dir_hist2 = v_dir_hist2
var macd3    = v_macd3, var dir_macd3 = v_dir_macd3, var sign3 = v_sign3, var dir_sign3 = v_dir_sign3, var hist3 = v_hist3, var dir_hist3 = v_dir_hist3
var macd4    = v_macd4, var dir_macd4 = v_dir_macd4, var sign4 = v_sign4, var dir_sign4 = v_dir_sign4, var hist4 = v_hist4, var dir_hist4 = v_dir_hist4
var macd5    = v_macd5, var dir_macd5 = v_dir_macd5, var sign5 = v_sign5, var dir_sign5 = v_dir_sign5, var hist5 = v_hist5, var dir_hist5 = v_dir_hist5
var macd6    = v_macd6, var dir_macd6 = v_dir_macd6, var sign6 = v_sign6, var dir_sign6 = v_dir_sign6, var hist6 = v_hist6, var dir_hist6 = v_dir_hist6

var dir_rsi1 = v_dir_rsi1
var rsi2     = v_rsi2 , var dir_rsi2  = v_dir_rsi2
var rsi3     = v_rsi3 , var dir_rsi3  = v_dir_rsi3
var rsi4     = v_rsi3 , var dir_rsi4  = v_dir_rsi4
var rsi5     = v_rsi3 , var dir_rsi5  = v_dir_rsi5
var rsi6     = v_rsi3 , var dir_rsi6  = v_dir_rsi6

var dir_st1  = v_dir_st1
var dir_st2  = v_dir_st2
var dir_st3  = v_dir_st3
var dir_st4  = v_dir_st4
var dir_st5  = v_dir_st5
var dir_st6  = v_dir_st6

tf_s = timeframe.in_seconds(timeframe.period) * 1000

switch
    time == chart.left_visible_bar_time  =>
        bixL := bar_index
        hi   := high
        lo   := low
    time  > chart.left_visible_bar_time and
     time < chart.right_visible_bar_time =>
        if high  > hi
            hi  := high
        if low   < lo
            lo  := low
    time == chart.right_visible_bar_time =>
        //
        bixM    := math.round(math.avg(bar_index, bixL))  
        dist    := bar_index - bixM
        //
        if i_choice == 'middle'
            box.set_lefttop    (bxM, bixM - 1, hi)
            box.set_rightbottom(bxM, bixM + 1, lo)    
            //
            // option chart.left_visible_bar_time - chart.right_visible_bar_time 
            bixM    := math.round(math.avg(bar_index, bixL))  
            dist    := bar_index - bixM
            //
            macd1   := v_macd1  [dist], dir_macd1 := v_dir_macd1[dist], sign1 := v_sign1[dist], dir_sign1 := v_dir_sign2[dist], hist1 := v_hist1[dist], dir_hist1 := v_dir_hist3[dist]
            macd2   := v_macd2  [dist], dir_macd2 := v_dir_macd2[dist], sign2 := v_sign2[dist], dir_sign2 := v_dir_sign2[dist], hist2 := v_hist2[dist], dir_hist2 := v_dir_hist2[dist]
            macd3   := v_macd3  [dist], dir_macd3 := v_dir_macd3[dist], sign3 := v_sign3[dist], dir_sign3 := v_dir_sign3[dist], hist3 := v_hist3[dist], dir_hist3 := v_dir_hist3[dist]        
            macd4   := v_macd4  [dist], dir_macd4 := v_dir_macd4[dist], sign4 := v_sign4[dist], dir_sign4 := v_dir_sign4[dist], hist4 := v_hist4[dist], dir_hist4 := v_dir_hist4[dist]        
            macd5   := v_macd5  [dist], dir_macd5 := v_dir_macd5[dist], sign5 := v_sign5[dist], dir_sign5 := v_dir_sign5[dist], hist5 := v_hist5[dist], dir_hist5 := v_dir_hist5[dist]        
            macd6   := v_macd6  [dist], dir_macd6 := v_dir_macd6[dist], sign6 := v_sign6[dist], dir_sign6 := v_dir_sign6[dist], hist6 := v_hist6[dist], dir_hist6 := v_dir_hist6[dist]        
            //
            //––––––––––––––––––––––––––––––––––––––––––––––––
            rsi1    := v_rsi1   [dist]  // value from example |
            //––––––––––––––––––––––––––––––––––––––––––––––––
            //
            dir_rsi1:= v_dir_rsi1 [dist]
            rsi2    := v_rsi2   [dist], dir_rsi2  := v_dir_rsi2 [dist]
            rsi3    := v_rsi3   [dist], dir_rsi3  := v_dir_rsi3 [dist]
            rsi4    := v_rsi4   [dist], dir_rsi4  := v_dir_rsi4 [dist]
            rsi5    := v_rsi5   [dist], dir_rsi5  := v_dir_rsi5 [dist]
            rsi6    := v_rsi6   [dist], dir_rsi6  := v_dir_rsi6 [dist]
            //
            dir_st1 := v_dir_st1[dist] 
            dir_st2 := v_dir_st2[dist] 
            dir_st3 := v_dir_st3[dist] 
            dir_st4 := v_dir_st4[dist] 
            dir_st5 := v_dir_st5[dist] 
            dir_st6 := v_dir_st6[dist] 
        else
            //if i_choice == 'time'
            box.set_lefttop    (bxT, i_time - tf_s, hi)
            box.set_rightbottom(bxT, i_time + tf_s, lo)         
    //
// option input.time
if i_choice == 'time' and time == i_time 
    //
    macd1   := v_macd1, dir_macd1 := v_dir_macd1, sign1 := v_sign1, dir_sign1 := v_dir_sign2, hist1 := v_hist1, dir_hist1 := v_dir_hist3
    macd2   := v_macd2, dir_macd2 := v_dir_macd2, sign2 := v_sign2, dir_sign2 := v_dir_sign2, hist2 := v_hist2, dir_hist2 := v_dir_hist2
    macd3   := v_macd3, dir_macd3 := v_dir_macd3, sign3 := v_sign3, dir_sign3 := v_dir_sign3, hist3 := v_hist3, dir_hist3 := v_dir_hist3        
    macd4   := v_macd4, dir_macd4 := v_dir_macd4, sign4 := v_sign4, dir_sign4 := v_dir_sign4, hist4 := v_hist4, dir_hist4 := v_dir_hist4        
    macd5   := v_macd5, dir_macd5 := v_dir_macd5, sign5 := v_sign5, dir_sign5 := v_dir_sign5, hist5 := v_hist5, dir_hist5 := v_dir_hist5        
    macd6   := v_macd6, dir_macd6 := v_dir_macd6, sign6 := v_sign6, dir_sign6 := v_dir_sign6, hist6 := v_hist6, dir_hist6 := v_dir_hist6        
    //
    //––––––––––––––––––––––––––––––––––––––––––––––––
    rsi1    := v_rsi1           // value from example |
    //––––––––––––––––––––––––––––––––––––––––––––––––
    //    
    dir_rsi1:= v_dir_rsi1
    rsi2    := v_rsi2 , dir_rsi2  := v_dir_rsi2
    rsi3    := v_rsi3 , dir_rsi3  := v_dir_rsi3
    rsi4    := v_rsi4 , dir_rsi4  := v_dir_rsi4
    rsi5    := v_rsi5 , dir_rsi5  := v_dir_rsi5
    rsi6    := v_rsi6 , dir_rsi6  := v_dir_rsi6
    //
    dir_st1 := v_dir_st1 
    dir_st2 := v_dir_st2 
    dir_st3 := v_dir_st3 
    dir_st4 := v_dir_st4 
    dir_st5 := v_dir_st5 
    dir_st6 := v_dir_st6 
    //

plusMin(value) => value > 0 ? '+' : ' '

value_dir(value, dir) => plusMin(value) + str.format("{0, number, 0.00}", value) + ' ' + plusMin(dir) + str.format("{0, number, #}", dir)

addToTable(leftValue, rightValue, col) =>	
    set = array.get(num, 0) + 1
    table.cell(table_id= tab, column= 0, row= set, text= leftValue , text_size= size.small, text_color= col)
    table.cell(table_id= tab, column= 1, row= set, text= rightValue, text_size= size.small, text_color= col)
    array.set (num, 0, set)

// table
if barstate.islast
	tab := table.new(position= i_pos, columns= 2, rows= 300, bgcolor= color.black, border_width= 1)
	//
	addToTable('macd 1:'       , value_dir(macd1, dir_macd1), color.aqua  ), addToTable('signal 1:', value_dir(sign1, dir_sign1), color.aqua), addToTable('hist 1:', value_dir(hist1, dir_hist1), color.aqua)
	addToTable('macd 2:'       , value_dir(macd2, dir_macd2), color.aqua  ), addToTable('signal 2:', value_dir(sign2, dir_sign2), color.aqua), addToTable('hist 2:', value_dir(hist2, dir_hist2), color.aqua)
	addToTable('macd 3:'       , value_dir(macd3, dir_macd3), color.aqua  ), addToTable('signal 3:', value_dir(sign3, dir_sign3), color.aqua), addToTable('hist 3:', value_dir(hist3, dir_hist3), color.aqua)	
	addToTable('macd 4:'       , value_dir(macd4, dir_macd4), color.aqua  ), addToTable('signal 4:', value_dir(sign4, dir_sign4), color.aqua), addToTable('hist 4:', value_dir(hist4, dir_hist4), color.aqua)	
	addToTable('macd 5:'       , value_dir(macd5, dir_macd5), color.aqua  ), addToTable('signal 5:', value_dir(sign5, dir_sign5), color.aqua), addToTable('hist 5:', value_dir(hist5, dir_hist5), color.aqua)	
	addToTable('macd 6:'       , value_dir(macd6, dir_macd6), color.aqua  ), addToTable('signal 6:', value_dir(sign6, dir_sign6), color.aqua), addToTable('hist 6:', value_dir(hist6, dir_hist6), color.aqua)	
	//
	//––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
	addToTable('rsi 1 :'       , value_dir(rsi1 , dir_rsi1) , color.yellow)  // value from example -> table |
	//––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
	//
	addToTable('rsi 2 :'       , value_dir(rsi2 , dir_rsi2) , color.yellow)
	addToTable('rsi 3 :'       , value_dir(rsi3 , dir_rsi3) , color.yellow)
	addToTable('rsi 4 :'       , value_dir(rsi4 , dir_rsi4) , color.yellow)
	addToTable('rsi 5 :'       , value_dir(rsi5 , dir_rsi5) , color.yellow)
	addToTable('rsi 6 :'       , value_dir(rsi6 , dir_rsi6) , color.yellow)
	//
	addToTable('supertrend 1:' , plusMin(dir_st1) + str.format("{0, number, ##}", dir_st1), dir_st1 > 0 ? color.lime : color.red)
	addToTable('supertrend 2:' , plusMin(dir_st2) + str.format("{0, number, ##}", dir_st2), dir_st2 > 0 ? color.lime : color.red)
	addToTable('supertrend 3:' , plusMin(dir_st3) + str.format("{0, number, ##}", dir_st3), dir_st3 > 0 ? color.lime : color.red)
	addToTable('supertrend 4:' , plusMin(dir_st4) + str.format("{0, number, ##}", dir_st4), dir_st4 > 0 ? color.lime : color.red)
	addToTable('supertrend 5:' , plusMin(dir_st5) + str.format("{0, number, ##}", dir_st5), dir_st5 > 0 ? color.lime : color.red)
	addToTable('supertrend 6:' , plusMin(dir_st6) + str.format("{0, number, ##}", dir_st6), dir_st6 > 0 ? color.lime : color.red)

plot(v_macd1  , color= color.aqua  ), plot(v_dir_macd1, color= color.aqua), plot(v_sign1, color= color.aqua), plot(v_dir_sign1, color= color.aqua), plot(v_hist1, color= color.aqua), plot(v_dir_hist1, color= color.aqua)
plot(v_macd2  , color= color.aqua  ), plot(v_dir_macd2, color= color.aqua), plot(v_sign2, color= color.aqua), plot(v_dir_sign2, color= color.aqua), plot(v_hist2, color= color.aqua), plot(v_dir_hist2, color= color.aqua)
plot(v_macd3  , color= color.aqua  ), plot(v_dir_macd3, color= color.aqua), plot(v_sign3, color= color.aqua), plot(v_dir_sign3, color= color.aqua), plot(v_hist3, color= color.aqua), plot(v_dir_hist3, color= color.aqua)
plot(v_macd4  , color= color.aqua  ), plot(v_dir_macd4, color= color.aqua), plot(v_sign4, color= color.aqua), plot(v_dir_sign4, color= color.aqua), plot(v_hist4, color= color.aqua), plot(v_dir_hist4, color= color.aqua)
plot(v_macd5  , color= color.aqua  ), plot(v_dir_macd5, color= color.aqua), plot(v_sign5, color= color.aqua), plot(v_dir_sign5, color= color.aqua), plot(v_hist5, color= color.aqua), plot(v_dir_hist5, color= color.aqua)
plot(v_macd6  , color= color.aqua  ), plot(v_dir_macd6, color= color.aqua), plot(v_sign6, color= color.aqua), plot(v_dir_sign6, color= color.aqua), plot(v_hist6, color= color.aqua), plot(v_dir_hist6, color= color.aqua)
//
//–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
plot(v_rsi1   , color= color.yellow), plot(v_dir_rsi1 , color= color.yellow) // value from example -> series (chart) |
//–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
//
plot(v_rsi2   , color= color.yellow), plot(v_dir_rsi2 , color= color.yellow)
plot(v_rsi3   , color= color.yellow), plot(v_dir_rsi3 , color= color.yellow)
plot(v_rsi4   , color= color.yellow), plot(v_dir_rsi4 , color= color.yellow)
plot(v_rsi5   , color= color.yellow), plot(v_dir_rsi5 , color= color.yellow)
plot(v_rsi6   , color= color.yellow), plot(v_dir_rsi6 , color= color.yellow)

plot(v_dir_st1, color= v_dir_st1 > 0 ? color.lime : color.red)
plot(v_dir_st2, color= v_dir_st2 > 0 ? color.lime : color.red)
plot(v_dir_st3, color= v_dir_st3 > 0 ? color.lime : color.red)
plot(v_dir_st4, color= v_dir_st4 > 0 ? color.lime : color.red)
plot(v_dir_st5, color= v_dir_st5 > 0 ? color.lime : color.red)
plot(v_dir_st6, color= v_dir_st6 > 0 ? color.lime : color.red)
