switches [experimental / tools]
// 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("Switches v2")

c = '     color line'

// –––––––––––––––––––––––––––––––––––––––––––––[ BOX 1 ]––––––––––––––––––––––––––––––––––––––––––––––––––– //
// ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– //
//
c2   = input.color    (color.orange             , title=c         , group='🔶   set 1'         , inline='s')
//
// –––––––[ input.time -> location switch ]–––––––
tB2  = input.time     (timestamp("1 Jul 2022")  , title='     '   , group='     🔹  line '     , inline='1')
//
// –––––––[    different ticker options   ]–––––––
tck1 = input.symbol   ('BINANCE:BTCUSDT'        , title='      1' , group='     🔹  Ticker'    , inline='1')
tck2 = input.symbol   ('BINANCE:ETHUSDT'        , title='      2' , group='     🔹  Ticker'    , inline='2')
tck3 = input.symbol   ('BINANCE:BNBUSDT'        , title='      3' , group='     🔹  Ticker'    , inline='3')


// ––––––––––––––––––––––––––––––––––––––––––––––[ BOX 2 ]––––––––––––––––––––––––––––––––––––––––––––––––––– //
// –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– //
//
c3   = input.color    (color.fuchsia            , title=c         , group='🔶   set 2'         , inline='s')
//
// –––––––[ input.time -> location switch ]–––––––
tB3  = input.time     (timestamp("4 Jul 2022")  , title='     '   , group='     🔹  line  '    , inline='2')
//
// –––––––[  different timeframe options  ]–––––––
res1 = input.timeframe( 'D'                     , title='      1' , group='     🔹  Timeframe' , inline='1')
res2 = input.timeframe('3D'                     , title='      2' , group='     🔹  Timeframe' , inline='2')
res3 = input.timeframe( 'W'                     , title='      3' , group='     🔹  Timeframe' , inline='3')


// ––––––––––––––––––––––––––––––––––––––––––––––[ BOX 3 ]––––––––––––––––––––––––––––––––––––––––––––––––––– //
// –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– //
//
c1   = input.color    (color.blue               , title=c         , group='🔶   set 3'         , inline='s')
//
// –––––––[ input.time -> location switch ]–––––––
tB1  = input.time     (timestamp("7 Jul 2022")  , title='     '   , group='     🔹  line'      , inline='3')
//
// –––––––[   different lengths for sma   ]–––––––
l1   = input.int      (   7                     , title='      1' , group='     🔹  Length RSI', inline='1')
l2   = input.int      (  10                     , title='      2' , group='     🔹  Length RSI', inline='2')
l3   = input.int      (  14                     , title='      3' , group='     🔹  Length RSI', inline='3')
l4   = input.int      (  21                     , title='      4' , group='     🔹  Length RSI', inline='4')


// –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– //
// –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– //


// –––––––[           variables          ]–––––––
res = '' 

var label lab  = label.new(bar_index    , 0, xloc=xloc.bar_index, style=label.style_none , text='', textcolor=color.white)

var line lnA_1 = line.new (time, 0, time, 0, xloc=xloc.bar_time , style=line.style_solid , color=color.new(c1, 25), extend=extend.none, width=5)
var line lnB_1 = line.new (time, 0, time, 0, xloc=xloc.bar_time , style=line.style_dotted, color=color.new(c1, 50), extend=extend.both, width=1)
var box  box_1 =  box.new (time, 0, time, 0, xloc=xloc.bar_time , bgcolor=na, border_color=na)

var line lnA_2 = line.new (time, 0, time, 0, xloc=xloc.bar_time , style=line.style_solid , color=color.new(c2, 25), extend=extend.none, width=5)
var line lnB_2 = line.new (time, 0, time, 0, xloc=xloc.bar_time , style=line.style_dotted, color=color.new(c2, 50), extend=extend.both, width=1)
var box  box_2 =  box.new (time, 0, time, 0, xloc=xloc.bar_time , bgcolor=na, border_color=na)

var line lnA_3 = line.new (time, 0, time, 0, xloc=xloc.bar_time , style=line.style_solid , color=color.new(c3, 25), extend=extend.none, width=5)
var line lnB_3 = line.new (time, 0, time, 0, xloc=xloc.bar_time , style=line.style_dotted, color=color.new(c3, 50), extend=extend.both, width=1)
var box  box_3 =  box.new (time, 0, time, 0, xloc=xloc.bar_time , bgcolor=na, border_color=na)

var float  rsi = na

var float[] a_rsi  = array.new_float(36)
var color[] a_c_1  = array.from(  #FF0000   ,   color.blue  ,  color.yellow,  color.lime)
var color[] a_c_2  = array.from(color.red   ,   color.blue  ,  color.lime  )
var color[] a_c_3  = array.from(color.red   ,   color.blue  ,  color.lime  )

var int[]  aTime1  = array.new_int (4)
var int[]  aTime2  = array.new_int (3)
var int[]  aTime3  = array.new_int (3)

var label[]  lb1   = array.new_label()
var label[]  lb2   = array.new_label()
var label[]  lb3   = array.new_label()

var string[] a_str = array.from(    tck1    ,       tck2    ,      tck3    )
var int   [] a_len = array.from(    l1      ,        l2     ,       l3     ,       l4   )
var string[] a_st3 = array.from(    res1    ,       res2    ,      res3    )

var int ix1 = 0, var int ix2 = 0, var int ix3 = 0
var int tA1 = 0, var int tA2 = 0, var int tA3 = 0

var int b1 = -1
var int width = na

sec(ticker, res, len) => request.security(ticker, res, ta.rsi(close, len))
set(value      ,   n) => array.set       (a_rsi ,   n,  value) 

if time >= chart.left_visible_bar_time and b1 < 0 
    b1    := bar_index
    width := last_bar_index - b1

part = (chart.right_visible_bar_time - chart.left_visible_bar_time) / width    

pos1_1 = chart.right_visible_bar_time - (part *  5)
pos2_1 = chart.right_visible_bar_time - (part *  4)
pos3_1 = chart.right_visible_bar_time - (part *  3)
pos4_1 = chart.right_visible_bar_time - (part *  2)

pos1_2 = chart.right_visible_bar_time - (part *  9)
pos2_2 = chart.right_visible_bar_time - (part *  8)
pos3_2 = chart.right_visible_bar_time - (part *  7)

pos1_3 = chart.right_visible_bar_time - (part * 13)
pos2_3 = chart.right_visible_bar_time - (part * 12)
pos3_3 = chart.right_visible_bar_time - (part * 11)


ix1   := tB1 <= pos1_1 ?   0    : tB1 > pos1_1 and tB1 <= pos2_1 ?    1   : tB1 > pos2_1 and tB1 <= pos3_1 ?    2   :    3 
tA1   := tB1 <= pos1_1 ? pos1_1 : tB1 > pos1_1 and tB1 <= pos2_1 ? pos2_1 : tB1 > pos2_1 and tB1 <= pos3_1 ? pos3_1 : pos4_1
ix2   := tB2 <= pos1_2 ?   0    : tB2 > pos1_2 and tB2 <= pos2_2 ?    1   :    2   
tA2   := tB2 <= pos1_2 ? pos1_2 : tB2 > pos1_2 and tB2 <= pos2_2 ? pos2_2 : pos3_2
ix3   := tB3 <= pos1_3 ?   0    : tB3 > pos1_3 and tB3 <= pos2_3 ?    1   :    2 
tA3   := tB3 <= pos1_3 ? pos1_3 : tB3 > pos1_3 and tB3 <= pos2_3 ? pos2_3 : pos3_3

if bar_index == b1
    aTime1 := array.from(pos1_1, pos2_1, pos3_1, pos4_1)
    aTime2 := array.from(pos1_2, pos2_2, pos3_2        )
    aTime3 := array.from(pos1_3, pos2_3, pos3_3        )

n = 0    
tf1_ticker1_len1 = sec(tck1, res1, l1), set(tf1_ticker1_len1, n), n += 1
tf1_ticker1_len2 = sec(tck1, res1, l2), set(tf1_ticker1_len2, n), n += 1
tf1_ticker1_len3 = sec(tck1, res1, l3), set(tf1_ticker1_len3, n), n += 1
tf1_ticker1_len4 = sec(tck1, res1, l4), set(tf1_ticker1_len4, n), n += 1

tf1_ticker2_len1 = sec(tck2, res1, l1), set(tf1_ticker2_len1, n), n += 1
tf1_ticker2_len2 = sec(tck2, res1, l2), set(tf1_ticker2_len2, n), n += 1
tf1_ticker2_len3 = sec(tck2, res1, l3), set(tf1_ticker2_len3, n), n += 1
tf1_ticker2_len4 = sec(tck2, res1, l4), set(tf1_ticker2_len4, n), n += 1

tf1_ticker3_len1 = sec(tck3, res1, l1), set(tf1_ticker3_len1, n), n += 1
tf1_ticker3_len2 = sec(tck3, res1, l2), set(tf1_ticker3_len2, n), n += 1
tf1_ticker3_len3 = sec(tck3, res1, l3), set(tf1_ticker3_len3, n), n += 1
tf1_ticker3_len4 = sec(tck3, res1, l4), set(tf1_ticker3_len4, n), n += 1


tf2_ticker1_len1 = sec(tck1, res2, l1), set(tf2_ticker1_len1, n), n += 1
tf2_ticker1_len2 = sec(tck1, res2, l2), set(tf2_ticker1_len2, n), n += 1
tf2_ticker1_len3 = sec(tck1, res2, l3), set(tf2_ticker1_len3, n), n += 1
tf2_ticker1_len4 = sec(tck1, res2, l4), set(tf2_ticker1_len4, n), n += 1

tf2_ticker2_len1 = sec(tck2, res2, l1), set(tf2_ticker2_len1, n), n += 1
tf2_ticker2_len2 = sec(tck2, res2, l2), set(tf2_ticker2_len2, n), n += 1
tf2_ticker2_len3 = sec(tck2, res2, l3), set(tf2_ticker2_len3, n), n += 1
tf2_ticker2_len4 = sec(tck2, res2, l4), set(tf2_ticker2_len4, n), n += 1

tf2_ticker3_len1 = sec(tck3, res2, l1), set(tf2_ticker3_len1, n), n += 1
tf2_ticker3_len2 = sec(tck3, res2, l2), set(tf2_ticker3_len2, n), n += 1
tf2_ticker3_len3 = sec(tck3, res2, l3), set(tf2_ticker3_len3, n), n += 1
tf2_ticker3_len4 = sec(tck3, res2, l4), set(tf2_ticker3_len4, n), n += 1


tf3_ticker1_len1 = sec(tck1, res3, l1), set(tf3_ticker1_len1, n), n += 1
tf3_ticker1_len2 = sec(tck1, res3, l2), set(tf3_ticker1_len2, n), n += 1
tf3_ticker1_len3 = sec(tck1, res3, l3), set(tf3_ticker1_len3, n), n += 1
tf3_ticker1_len4 = sec(tck1, res3, l4), set(tf3_ticker1_len4, n), n += 1

tf3_ticker2_len1 = sec(tck2, res3, l1), set(tf3_ticker2_len1, n), n += 1
tf3_ticker2_len2 = sec(tck2, res3, l2), set(tf3_ticker2_len2, n), n += 1
tf3_ticker2_len3 = sec(tck2, res3, l3), set(tf3_ticker2_len3, n), n += 1
tf3_ticker2_len4 = sec(tck2, res3, l4), set(tf3_ticker2_len4, n), n += 1

tf3_ticker3_len1 = sec(tck3, res3, l1), set(tf3_ticker3_len1, n), n += 1
tf3_ticker3_len2 = sec(tck3, res3, l2), set(tf3_ticker3_len2, n), n += 1
tf3_ticker3_len3 = sec(tck3, res3, l3), set(tf3_ticker3_len3, n), n += 1
tf3_ticker3_len4 = sec(tck3, res3, l4), set(tf3_ticker3_len4, n), n += 1

if barstate.isfirst
    for i = 0 to 4
        array.unshift(lb1, label.new(bar_index, 105, xloc=xloc.bar_time, style=label.style_none, size=size.tiny))
    for i = 0 to 3
        array.unshift(lb2, label.new(bar_index, 105, xloc=xloc.bar_time, style=label.style_none, size=size.tiny))
    for i = 0 to 3
        array.unshift(lb3, label.new(bar_index, 105, xloc=xloc.bar_time, style=label.style_none, size=size.tiny)) 

if barstate.islast
    box.set_bgcolor        (box_1, color.new(array.get(a_c_1, ix1), 50))
    box.set_border_color   (box_1, color.new(array.get(a_c_1, ix1), 50))
    box.set_lefttop        (box_1, pos1_1, 102), box.set_rightbottom(box_1, pos4_1, 112)
    line.set_xy1           (lnA_1, tA1   , 102), line.set_xy2       (lnA_1, tA1   , 112)
    line.set_xy1           (lnB_1, tB1   , 102), line.set_xy2       (lnB_1, tB1   , 112)    
    //
    box.set_bgcolor        (box_2, color.new(array.get(a_c_2, ix2), 50))
    box.set_border_color   (box_2, color.new(array.get(a_c_2, ix2), 50)) 
    box.set_lefttop        (box_2, pos1_2, 102), box.set_rightbottom(box_2, pos3_2, 112)
    line.set_xy1           (lnA_2, tA2   , 102), line.set_xy2       (lnA_2, tA2   , 112)
    line.set_xy1           (lnB_2, tB2   , 102), line.set_xy2       (lnB_2, tB2   , 112)      
    //
    //
    box.set_bgcolor        (box_3, color.new(array.get(a_c_3, ix3), 50))
    box.set_border_color   (box_3, color.new(array.get(a_c_3, ix3), 50))  
    box.set_lefttop        (box_3, pos1_3, 102), box.set_rightbottom(box_3, pos3_3, 112)
    line.set_xy1           (lnA_3, tA3   , 102), line.set_xy2       (lnA_3, tA3   , 112)
    line.set_xy1           (lnB_3, tB3   , 102), line.set_xy2       (lnB_3, tB3   , 112) 
    //
    for i = 0 to 3
        label.set_x        (array.get(lb1, i  ), array.get(aTime1, i))
        label.set_text     (array.get(lb1, i  ),    str.tostring(i+1))
        label.set_textcolor(array.get(lb1, i  ),      color.white    )
    label.set_y            (array.get(lb1, ix1),          115        )
    label.set_size         (array.get(lb1, ix1),      size.small     )
    label.set_textcolor    (array.get(lb1, ix1),      color.yellow   )
    //
    label.set_x            (array.get(lb1, 4  ), array.get(aTime1, 2))
    label.set_y            (array.get(lb1, 4  ),           95        )
    label.set_size         (array.get(lb1, 4  ),      size.small     )
    label.set_text         (array.get(lb1, 4  ),     'Length sma'    )
    label.set_textcolor    (array.get(lb1, 4  ),      color.yellow   )    
    //
    for i = 0 to 2
        label.set_x        (array.get(lb2, i  ), array.get(aTime2, i))
        label.set_text     (array.get(lb2, i  ),    str.tostring(i+1))
        label.set_textcolor(array.get(lb2, i  ),      color.white    )
        //
        label.set_x        (array.get(lb3, i  ), array.get(aTime3, i))
        label.set_text     (array.get(lb3, i  ),    str.tostring(i+1))
        label.set_textcolor(array.get(lb3, i  ),      color.white    )   
        //
    label.set_y            (array.get(lb2, ix2),          115        )  
    label.set_size         (array.get(lb2, ix2),      size.small     )
    label.set_textcolor    (array.get(lb2, ix2),      color.yellow   )
    //
    label.set_x            (array.get(lb2, 3  ), array.get(aTime2, 1))
    label.set_y            (array.get(lb2, 3  ),           95        )
    label.set_size         (array.get(lb2, 3  ),      size.small     )
    label.set_text         (array.get(lb2, 3  ),        'ticker'     )
    label.set_textcolor    (array.get(lb2, 3  ),      color.yellow   )    
    //
    label.set_y            (array.get(lb3, ix3),          115        )  
    label.set_size         (array.get(lb3, ix3),      size.small     )
    label.set_textcolor    (array.get(lb3, ix3),      color.yellow   )
    //
    label.set_x            (array.get(lb3, 3  ), array.get(aTime3, 1))
    label.set_y            (array.get(lb3, 3  ),           95        )
    label.set_size         (array.get(lb3, 3  ),      size.small     )
    label.set_text         (array.get(lb3, 3  ),          'tf'       )
    label.set_textcolor    (array.get(lb3, 3  ),      color.yellow   )  
    //
    label.set_xy  (lab, bar_index + 3, 50)
    label.set_text(lab, 
      array.get(a_str, ix2) + 
      '\nRSI length :  ' + str.tostring(array.get(a_len, ix1)) +
      '\ntimeframe  :  ' +              array.get(a_st3, ix3))

beginPos_timeframe =  ix3 * (4 * 3)
beginPos_ticker    =  ix2 *  4
position_len       =  ix1 
positionInArray    =  beginPos_timeframe + beginPos_ticker + position_len

rsi               := array.size(a_rsi) > 0 ? array.get(a_rsi, positionInArray) : na

plot(rsi, color=array.get(a_c_1, ix1))

hline(100, color=color.new(color.white, 75), linestyle= hline.style_solid, linewidth= 1)
hline( 80, color=color.new(color.red  , 50), linestyle= hline.style_solid, linewidth= 3)
hline( 50, color=color.new(color.white, 75), linestyle= hline.style_solid, linewidth= 1)
hline( 20, color=color.new(color.lime , 50), linestyle= hline.style_solid, linewidth= 3)
hline(  0, color=color.new(color.white, 75), linestyle= hline.style_solid, linewidth= 1)

