HighLowBox 1+3TF
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © nazo

//@version=5
indicator(title="HighLowBox HTF+RSI",shorttitle="HLBox+RSI",overlay=true, max_boxes_count=500,max_labels_count=500, max_lines_count=500)

//
idChart = syminfo.tickerid
tfChart = timeframe.period

// t00,t01,t02,t03,t04,t05,t06,t07,t08,t09,t10,t11,t12,t13,t14,t15
t00="Chart", t01="1", t02="3", t03="5", t04="15", t05="30",t06="60",t07="120", t08="180",t09="240",
t10="D", t11='W',t12="M",t13="3M", t14="6M",t15="Y"
th1="higher1", th2="higher2", th3="higher3", th4="higher4"
tl1="lower1", tl2="lower2", tl3="lower3", tl4="lower4"

f_gt(x,y) => x>y?x:y
f_lt(x,y) => x<y?x:y

f_tfMultiplier(tf) =>
    x=
     str.endswith(tf,"M")? str.length(tf)==1?"1":str.replace(tf,"M",""):
     str.endswith(tf,"W")? str.length(tf)==1?"1":str.replace(tf,"W",""):
     str.endswith(tf,"D")? str.length(tf)==1?"1":str.replace(tf,"D",""):
     str.endswith(tf,"S")? str.length(tf)==1?"1":str.replace(tf,"S",""):
     tf
    str.tonumber(x)

f_time_ddHHmm (_t)=>
    _s=1000
    _m=60*_s
    _h=60*_m
    _d=24*_h
    _sec = _t
    _day = int(_sec/_d)
    _mod = _sec%_d
    _hour= int(_mod/_h)
    _mod:= _mod%_h
    _min = int(_mod/_m)
    (_day>0?str.tostring(_day,"00")+"d":"")+str.tostring(_hour,"00")+"h"+str.tostring(_min,"00")+"m"

f_tfUp (tf) =>
    m = f_tfMultiplier(tf)
    x= str.endswith(tf,"M")? m<3?"3M": m<6?"6M": m<12?"12M":na:
     str.endswith(tf,"W")? "M":
     str.endswith(tf,"D")? "W":
     str.endswith(tf,"S")? "1":
     m<15?"15": m<60?"60":  m<240?"240": m>=240?"D":na

f_tfDn (tf) =>
    m = f_tfMultiplier(tf)
    x= str.endswith(tf,"M")? m>6?"6M": m>3?"3M":"W":
     str.endswith(tf,"W")? "D":
     str.endswith(tf,"D")? "240":
     str.endswith(tf,"S")? na:
     m>240?"240": m>60?"60": m>15?"15": m>5?"5": m>3?"3":"1"

string tfHigher1 = f_tfUp(tfChart)
string tfHigher2 = f_tfUp(tfHigher1)
string tfHigher3 = f_tfUp(tfHigher2)
string tfHigher4 = f_tfUp(tfHigher3)
string tfLower1 = f_tfDn(tfChart)
string tfLower2 = f_tfDn(tfLower1)
string tfLower3 = f_tfDn(tfLower2)
string tfLower4 = f_tfDn(tfLower3)

f_opt2tf(opt) => opt==t00?tfChart:
 opt==th1?tfHigher1: opt==th2?tfHigher2: opt==th3?tfHigher3: opt==th4?tfHigher4:
 opt==tl1?tfLower1: opt==tl2?tfLower2: opt==tl3?tfLower3: opt==tl4?tfLower4:
 opt

b_tfgt(tfa,tfb) => timeframe.in_seconds(tfa)>timeframe.in_seconds(tfb)
b_tflt(tfa,tfb) => timeframe.in_seconds(tfa)<timeframe.in_seconds(tfb)

b_tfgtCur(tf) => timeframe.in_seconds(tf)>timeframe.in_seconds(tfChart)
b_tfltCur(tf) => timeframe.in_seconds(tf)<timeframe.in_seconds(tfChart)

f_getMtfLowerAvg(tf,x) => na(tf)?na:array.avg(request.security_lower_tf(idChart,tf,x))
f_getMtfLowerMax(tf,x) => na(tf)?na:array.avg(request.security_lower_tf(idChart,tf,x))
f_getMtf(tf,x) => na(tf)?na: tf==tfChart?x:
 b_tfgtCur(tf)?request.security(idChart,tf,x, gaps=barmerge.gaps_off,lookahead=barmerge.lookahead_off):na

f_bb (center,std,sigma) => center + std*sigma
f_bbsetup(center,std) => [
     f_bb(center,std,4),
     f_bb(center,std,3),
     f_bb(center,std,2),
     f_bb(center,std,1),
     f_bb(center,std,-1),
     f_bb(center,std,-2),
     f_bb(center,std,-3),
     f_bb(center,std,-4)
     ] 

f_bb3sigma1side (val, center, std) => center<val?f_bb(center,std,3):f_bb(center,std,-3)

f_madev(val, ma) => (val - ma) / ma * 100
s_madev(val, ma) => str.tostring(val-ma,"0.000")+"("+str.tostring(math.round(f_madev(val,ma),2))+"%)"

f_fibo(a,z) =>
    diff = z-a
    [z-diff*3.618,z-diff*2.618,z-diff*1.618,
     a+diff*0.236,a+diff*0.382,a+diff*0.5,a+diff*0.618,a+diff*0.786,
     a+diff*1.618,a+diff*2.618,a+diff*3.618]

f_fibo_range(a,z,x) =>
    diff = z-a
    _rti=-1
    _rbi=-1
    listfibo = array.from(z-diff*3.618,z-diff*2.618,z-diff*1.618,a,a+diff*0.236,a+diff*0.382,a+diff*0.5,a+diff*0.618,a+diff*0.786,z,a+diff*1.618,a+diff*2.618,a+diff*3.618)
    s_listfibo = array.from("3.618fromT","2.618fromT","1.618fromT","____Bottom","0.236fromB","0.382fromB","____Center","0.382fromT","0.236fromT","_______Top","1.618fromB","2.618fromB","3.618fromB")
    for i = 0 to array.size(listfibo)-1
        if x<array.get(listfibo,i)
            _rti:=i
            break
    for i = array.size(listfibo)-1 to 0
        if x>array.get(listfibo,i)
            _rbi:=i
            break
    [_rti==-1?na:array.get(listfibo,_rti),_rti==-1?na:array.get(s_listfibo,_rti),
     _rbi==-1?na:array.get(listfibo,_rbi),_rbi==-1?na:array.get(s_listfibo,_rbi)]

f_fibo_level(a,z,x) =>
    diff = z-a
    level = x<=a+diff*0.5? x>a+diff*0.382?"ML": x>a+diff*0.236?"L": x>a?"L-": "BB":
     x<a+diff*0.618?"MH": x<a+diff*0.786?"H": x<z?"H+": "BT"

f_fl_va(_fl) =>
    switch _fl
        "ML" => text.align_center
        "L" => text.align_bottom
        "L-" => text.align_bottom
        "BB" => text.align_bottom
        "MH" => text.align_center
        "H" => text.align_top
        "H+" => text.align_top
        "BT" => text.align_top

f_fl_c(_fl) =>
    switch _fl
        "ML" => color.new(color.orange,20)
        "L" => color.new(color.red,60)
        "L-" => color.new(color.red,40)
        "BB" =>  color.new(color.red,20)
        "MH" => color.new(color.yellow,20)
        "H" => color.new(color.blue,60)
        "H+" => color.new(color.blue,40)
        "BT" => color.new(color.blue,20)

l00="solid",l01="dashed",l02="dotted"
f_linestyle (x) => x==l00?line.style_solid: x==l01?line.style_dashed: x==l02?line.style_dotted:na
s00="auto",s01="huge",s02="large",s03="normal",s04="small",s05="tiny"
f_textsize (x) => x==s00?size.auto: x==s01?size.huge: x==s02?size.large: x==s03?size.normal: x==s04?size.small: x==s05?size.tiny:na

f_renew_high(_last_renew,_high,_high_time,_m_high,_m_return_high,_m_low,_m_return_low) =>
    _i_renew =0
    
    _a_high = matrix.row(_m_high,0)
    _a_high_time = matrix.row(_m_high,1)
    _a_return_high = matrix.row(_m_return_high,0)
    _a_return_high_time = matrix.row(_m_return_high,1)
    _a_low = matrix.row(_m_low,0)
    _a_low_time = matrix.row(_m_low,1)
    _a_return_low = matrix.row(_m_return_low,0)
    _a_return_low_time = matrix.row(_m_return_low,1)
    
    if array.size(_a_return_high)>0
        if array.last(_a_return_high)<_high
            while(array.size(_a_return_high)>0)
                if array.last(_a_return_high)<_high
                    array.pop(_a_return_high)
                    array.pop(_a_return_high_time)
                else
                    break
    array.push(_a_return_high,_high)
    array.push(_a_return_high_time,_high_time)
    
    if array.last(_a_high)<array.last(_a_return_high)
        _i_renew:=1
        if _i_renew==_last_renew
            array.pop(_a_high)
            array.pop(_a_high_time)
        array.push(_a_high,array.last(_a_return_high))
        array.push(_a_high_time,array.last(_a_return_high_time))
        array.push(_a_low,array.last(_a_return_low))
        array.push(_a_low_time,array.last(_a_return_low_time))

    o_m_high = matrix.new<float>(2,array.size(_a_high),na)//0: price, 1:time
    matrix.add_row(o_m_high,0,_a_high)
    matrix.add_row(o_m_high,1,_a_high_time)
    o_m_return_high = matrix.new<float>(2,array.size(_a_return_high),na)//0: price, 1:time
    matrix.add_row(o_m_return_high,0,_a_return_high)
    matrix.add_row(o_m_return_high,1,_a_return_high_time)
    o_m_low = matrix.new<float>(2,array.size(_a_low),na)//0: price, 1:time
    matrix.add_row(o_m_low,0,_a_low)
    matrix.add_row(o_m_low,1,_a_low_time)
    o_m_return_low = matrix.new<float>(2,array.size(_a_return_low),na)//0: price, 1:time
    matrix.add_row(o_m_return_low,0,_a_return_low)
    matrix.add_row(o_m_return_low,1,_a_return_low_time)

    [_i_renew,o_m_high,o_m_return_high,o_m_low,o_m_return_low]

f_renew_low(_last_renew,_low,_low_time,_m_low,_m_return_low,_m_high,_m_return_high) =>
    _i_renew =0
    
    _a_high = matrix.row(_m_high,0)
    _a_high_time = matrix.row(_m_high,1)
    _a_return_high = matrix.row(_m_return_high,0)
    _a_return_high_time = matrix.row(_m_return_high,1)
    _a_low = matrix.row(_m_low,0)
    _a_low_time = matrix.row(_m_low,1)
    _a_return_low = matrix.row(_m_return_low,0)
    _a_return_low_time = matrix.row(_m_return_low,1)
    
    if array.size(_a_return_low)>0
        if array.last(_a_return_low)>_low
            while(array.size(_a_return_low)>0)
                if array.last(_a_return_low)>_low
                    array.pop(_a_return_low)
                    array.pop(_a_return_low_time)
                else
                    break
    array.push(_a_return_low,_low)
    array.push(_a_return_low_time,_low_time)
    
    if array.last(_a_low)>array.last(_a_return_low)
        _i_renew:=-1
        if _i_renew==_last_renew
            array.pop(_a_low)
            array.pop(_a_low_time)
        array.push(_a_low,array.last(_a_return_low))
        array.push(_a_low_time,array.last(_a_return_low_time))
        array.push(_a_high,array.last(_a_return_high))
        array.push(_a_high_time,array.last(_a_return_high_time))

    o_m_high = matrix.new<float>(2,array.size(_a_high),na)//0: price, 1:time
    matrix.add_row(o_m_high,0,_a_high)
    matrix.add_row(o_m_high,1,_a_high_time)
    o_m_return_high = matrix.new<float>(2,array.size(_a_return_high),na)//0: price, 1:time
    matrix.add_row(o_m_return_high,0,_a_return_high)
    matrix.add_row(o_m_return_high,1,_a_return_high_time)
    o_m_low = matrix.new<float>(2,array.size(_a_low),na)//0: price, 1:time
    matrix.add_row(o_m_low,0,_a_low)
    matrix.add_row(o_m_low,1,_a_low_time)
    o_m_return_low = matrix.new<float>(2,array.size(_a_return_low),na)//0: price, 1:time
    matrix.add_row(o_m_return_low,0,_a_return_low)
    matrix.add_row(o_m_return_low,1,_a_return_low_time)

    [_i_renew,o_m_high,o_m_return_high,o_m_low,o_m_return_low]

//RSI span string
//rsi01,rsi02,rsi03,rsi04,rsi05,rsi06,rsi07,rsi08,rsi09,rsi10,rsi11,rsi12,rsi13,rsi14,rsi15,rsi16,rsi17,rsi18,rsi19,rsi20,rsi21,rsi22,rsi23,rsi24,rsi25,rsi26,rsi27,rsi28,rsi29,rsi30
rsi01="①",rsi02="②",rsi03="③",rsi04="④",rsi05="⑤",rsi06="⑥",rsi07="⑦",rsi08="⑧",rsi09="⑨",rsi10="⑩",
rsi11="⑪",rsi12="⑫",rsi13="⑬",rsi14="⑭",rsi15="⑮",rsi16="⑯",rsi17="⑰",rsi18="⑱",rsi19="⑲",rsi20="⑳",
rsi21="㉑",rsi22="㉒",rsi23="㉓",rsi24="⑭",rsi25="㉕",rsi26="㉖",rsi27="㉗",rsi28="㉘",rsi29="㉙",rsi30="㉚"
f_rsi_span2label(_i) => _i==1?rsi01: _i==2?rsi02: _i==3?rsi03: _i==4?rsi04: _i==5?rsi05: _i==6?rsi06: _i==7?rsi07: _i==8?rsi08: _i==9?rsi09: _i==10?rsi10:
 _i==11?rsi11: _i==12?rsi12: _i==13?rsi13: _i==14?rsi14: _i==15?rsi15: _i==16?rsi16: _i==17?rsi17: _i==18?rsi18: _i==19?rsi19: _i==20?rsi20:
 _i==21?rsi21: _i==22?rsi22: _i==23?rsi23: _i==24?rsi14: _i==25?rsi25: _i==26?rsi26: _i==27?rsi27: _i==28?rsi28: _i==29?rsi29: _i==30?rsi30:na

rsi00="▼", rsi99="▲"
f_rsi_direc(_isup)=>_isup?rsi99:rsi00

//
// colors
//
colorLabelText=color.new(color.white,60)

colorBoxT=color.new(color.white,5)
colorBoxU=color.new(#aaffaa,5)
colorBoxD=color.new(#ffaaaa,5)

colorRSIi=color.new(color.white,5)
colorRSIt1=color.new(color.yellow,5)
colorRSIt2=color.new(color.orange,5)
colorRSIt3=color.new(color.red,5)
colorRSIb1=color.new(color.aqua,5)
colorRSIb2=color.new(color.blue,5)
colorRSIb3=color.new(color.green,5)

//settings
show0 = input.bool(defval=true,title="show current TF")
transBox = input.int(defval=40,minval=0,maxval=99,title="border transpar",inline="tfC")
cBoxU = input.color(defval=colorBoxU,title="newHigh",inline="tfC")
cBoxD = input.color(defval=colorBoxD,title="newLow",inline="tfC")
cBoxU:=color.new(cBoxU,transBox)
cBoxD:=color.new(cBoxD,transBox)
widthBox = input.int(defval=1,minval=1,maxval=4,title="width",inline="tfC")
_styleBox = input.string(defval=l01,options=[l00,l01,l02],title="box style",inline="tfC")
styleBox = f_linestyle(_styleBox)
b_fillBox = input.bool(defval=true,title="fill box",inline="tfCf")
transFill = input.int(defval=90,minval=0,maxval=99,title="fill transpar",inline="tfCf")
cBoxUf = input.color(defval=colorBoxU,title="newHigh",inline="tfCf")
cBoxDf = input.color(defval=colorBoxD,title="newLow",inline="tfCf")
cBoxUf:=b_fillBox?color.new(cBoxUf,transFill):na
cBoxDf:=b_fillBox?color.new(cBoxDf,transFill):na
b_fibo = input.bool(defval=true,title="show fibonacci scale",inline="tfCF")
b_info = input.bool(defval=true,title="range info",inline="tfCi")
_infoSize = input.string(defval=s04,options=[s01,s02,s03,s04,s05],title="info size",inline="tfCi")
infoSize = f_textsize(_infoSize)
_cBoxT = input.color(defval=colorBoxT,title="info color",inline="tfCi")
transInfo = input.int(defval=30,minval=0,maxval=99,title="text transpar",inline="tfCi")
cBoxT = color.new(_cBoxT,transInfo)

show1 = input.bool(defval=true,title="show TF1")
_tf1 = input.timeframe(defval=th1, title="", inline="tfH1",
 options=[th1,th2,th3,th4,t01,t02,t03,t04,t05,t06,t07,t08,t09,t10,t11,t12,t13,t14,t15])
tf1 = f_opt2tf(_tf1)
transBox1 = input.int(defval=50,minval=0,maxval=99,title="border transpar",inline="tfH1")
cBoxU1 = input.color(defval=colorBoxU,title="newHigh",inline="tfH1")
cBoxD1 = input.color(defval=colorBoxD,title="newLow",inline="tfH1")
cBoxU1:=color.new(cBoxU1,transBox1)
cBoxD1:=color.new(cBoxD1,transBox1)
widthBox1 = input.int(defval=2,minval=1,maxval=4,title="width",inline="tfH1")
_styleBox1 = input.string(defval=l00,options=[l00,l01,l02],title="box style",inline="tfH1")
styleBox1 = f_linestyle(_styleBox1)
b_fibo1 = input.bool(defval=true,title="show fibonacci level",inline="tfH1")
b_info1 = input.bool(defval=true,title="range info",inline="tfH1i")
_infoSize1 = input.string(defval=s04,options=[s01,s02,s03,s04,s05],title="info size",inline="tfH1i")
infoSize1 = f_textsize(_infoSize1)
_cBoxT1 = input.color(defval=colorBoxT,title="info color",inline="tfH1i")
transInfo1 = input.int(defval=30,minval=0,maxval=99,title="text transpar",inline="tfH1i")
cBoxT1 = color.new(_cBoxT1,transInfo1)

show2 = input.bool(defval=true,title="show TF2")
_tf2 = input.timeframe(defval=th2, title="", inline="tfH2",
 options=[th1,th2,th3,th4,t01,t02,t03,t04,t05,t06,t07,t08,t09,t10,t11,t12,t13,t14,t15])
tf2 = f_opt2tf(_tf2)
transBox2 = input.int(defval=60,minval=0,maxval=99,title="border transpar",inline="tfH2")
cBoxU2 = input.color(defval=colorBoxU,title="newHigh",inline="tfH2")
cBoxD2 = input.color(defval=colorBoxD,title="newLow",inline="tfH2")
cBoxU2:=color.new(cBoxU2,transBox2)
cBoxD2:=color.new(cBoxD2,transBox2)
widthBox2 = input.int(defval=3,minval=1,maxval=4,title="width",inline="tfH2")
_styleBox2 = input.string(defval=l01,options=[l00,l01,l02],title="box style",inline="tfH2")
styleBox2 = f_linestyle(_styleBox2)
b_fibo2 = input.bool(defval=true,title="show fibonacci level",inline="tfH2")
b_info2 = input.bool(defval=true,title="range info",inline="tfH2i")
_infoSize2 = input.string(defval=s04,options=[s01,s02,s03,s04,s05],title="info size",inline="tfH2i")
infoSize2 = f_textsize(_infoSize2)
_cBoxT2 = input.color(defval=colorBoxT,title="info color",inline="tfH2i")
transInfo2 = input.int(defval=30,minval=0,maxval=99,title="text transpar",inline="tfH2i")
cBoxT2 = color.new(_cBoxT2,transInfo2)

show3 = input.bool(defval=true,title="show TF3")
_tf3 = input.timeframe(defval=th3, title="", inline="tfH3",
 options=[th1,th2,th3,th4,t01,t02,t03,t04,t05,t06,t07,t08,t09,t10,t11,t12,t13,t14,t15])
tf3 = f_opt2tf(_tf3)
transBox3 = input.int(defval=70,minval=0,maxval=99,title="border transpar",inline="tfH3")
cBoxU3 = input.color(defval=colorBoxU,title="newHigh",inline="tfH3")
cBoxD3 = input.color(defval=colorBoxD,title="newLow",inline="tfH3")
cBoxU3:=color.new(cBoxU3,transBox3)
cBoxD3:=color.new(cBoxD3,transBox3)
widthBox3 = input.int(defval=4,minval=1,maxval=4,title="width",inline="tfH3")
_styleBox3 = input.string(defval=l00,options=[l00,l01,l02],title="box style",inline="tfH3")
styleBox3 = f_linestyle(_styleBox3)
b_fibo3 = input.bool(defval=true,title="show fibonacci level",inline="tfH3")
b_info3 = input.bool(defval=true,title="range info",inline="tfH3i")
_infoSize3 = input.string(defval=s04,options=[s01,s02,s03,s04,s05],title="info size",inline="tfH3i")
infoSize3 = f_textsize(_infoSize3)
_cBoxT3 = input.color(defval=colorBoxT,title="info color",inline="tfH3i")
transInfo3 = input.int(defval=30,minval=0,maxval=99,title="text transpar",inline="tfH3i")
cBoxT3 = color.new(_cBoxT3,transInfo3)

show4 = input.bool(defval=false,title="show TF4")
_tf4 = input.timeframe(defval=th4, title="", inline="tfH4",
 options=[th1,th2,th3,th4,t01,t02,t03,t04,t05,t06,t07,t08,t09,t10,t11,t12,t13,t14,t15])
tf4 = f_opt2tf(_tf4)
transBox4 = input.int(defval=80,minval=0,maxval=99,title="border transpar",inline="tfH4")
cBoxU4 = input.color(defval=colorBoxU,title="newHigh",inline="tfH4")
cBoxD4 = input.color(defval=colorBoxD,title="newLow",inline="tfH4")
cBoxU4:=color.new(cBoxU4,transBox4)
cBoxD4:=color.new(cBoxD4,transBox4)
widthBox4 = input.int(defval=4,minval=1,maxval=4,title="width",inline="tfH4")
_styleBox4 = input.string(defval=l02,options=[l00,l01,l02],title="box style",inline="tfH4")
styleBox4 = f_linestyle(_styleBox4)
b_fibo4 = input.bool(defval=true,title="show fibonacci level",inline="tfH4")
b_info4 = input.bool(defval=true,title="range info",inline="tfH4i")
_infoSize4 = input.string(defval=s04,options=[s01,s02,s03,s04,s05],title="info size",inline="tfH4i")
infoSize4 = f_textsize(_infoSize4)
_cBoxT4 = input.color(defval=colorBoxT,title="info color",inline="tfH4i")
transInfo4 = input.int(defval=30,minval=0,maxval=99,title="text transpar",inline="tfH4i")
cBoxT4 = color.new(_cBoxT4,transInfo4)

showMinimap = input.bool(defval=false,title="show minimap(experimental)",inline="tfM")
minimapHist = input.int(defval=32,minval=10,maxval=99,title="minimap hist",inline="tfM")

srcRSI = input.source(defval=hlc3,title="RSI src",group="RSI")
spanRSI_1 = input.int(defval=9,minval=1,maxval=30,title="span1", inline="RSIspan", group="RSI")
spanRSI_2 = input.int(defval=14,minval=1,maxval=30,title="span2", inline="RSIspan", group="RSI")
_cRSIi = input.color(defval=colorRSIi,title="only span1>=70 or only span1<=30",inline="top",group="RSI >= 70")
_cRSIt1 = input.color(defval=colorRSIt1,title="both>=70 and span1>span2",inline="top",group="RSI >= 70")
_cRSIt2 = input.color(defval=colorRSIt2,title="both>=70 and span1<span2",inline="top",group="RSI >= 70")
_cRSIt3 = input.color(defval=colorRSIt3,title="span1<70",inline="top",group="RSI >= 70")
_cRSIb1 = input.color(defval=colorRSIb1,title="both<=30 and span1<span2",inline="top",group="RSI <= 30")
_cRSIb2 = input.color(defval=colorRSIb2,title="both<=30 and span1>span2",inline="top",group="RSI <= 30")
_cRSIb3 = input.color(defval=colorRSIb3,title="span1>30",inline="top",group="RSI <= 30")

showRSI = input.bool(defval=true,title="use RSI in current TF",inline="tf",group="RSI")
transRSI = input.int(defval=80,minval=0,maxval=99,title="border transpar",inline="tf",group="RSI")
cRSIi=color.new(_cRSIi,transRSI)
cRSIt1=color.new(_cRSIt1,transRSI)
cRSIt2=color.new(_cRSIt2,transRSI)
cRSIt3=color.new(_cRSIt3,transRSI)
cRSIb1=color.new(_cRSIb1,transRSI)
cRSIb2=color.new(_cRSIb2,transRSI)
cRSIb3=color.new(_cRSIb3,transRSI)

showRSI1 = input.bool(defval=true,title="use RSI in Higher TF1",inline="tf1",group="RSI")
transRSI1 = input.int(defval=70,minval=0,maxval=99,title="border transpar",inline="tf1",group="RSI")
cRSI1i=color.new(_cRSIi,transRSI1)
cRSI1t1=color.new(_cRSIt1,transRSI1)
cRSI1t2=color.new(_cRSIt2,transRSI1)
cRSI1t3=color.new(_cRSIt3,transRSI1)
cRSI1b1=color.new(_cRSIb1,transRSI1)
cRSI1b2=color.new(_cRSIb2,transRSI1)
cRSI1b3=color.new(_cRSIb3,transRSI1)

showRSI2 = input.bool(defval=true,title="use RSI in Higher TF2",inline="tf2",group="RSI")
transRSI2 = input.int(defval=60,minval=0,maxval=99,title="border transpar",inline="tf2",group="RSI")
cRSI2i=color.new(_cRSIi,transRSI2)
cRSI2t1=color.new(_cRSIt1,transRSI2)
cRSI2t2=color.new(_cRSIt2,transRSI2)
cRSI2t3=color.new(_cRSIt3,transRSI2)
cRSI2b1=color.new(_cRSIb1,transRSI2)
cRSI2b2=color.new(_cRSIb2,transRSI2)
cRSI2b3=color.new(_cRSIb3,transRSI2)

showRSI3 = input.bool(defval=true,title="use RSI in Higher TF3",inline="tf3",group="RSI")
transRSI3 = input.int(defval=50,minval=0,maxval=99,title="border transpar",inline="tf3",group="RSI")
cRSI3i=color.new(_cRSIi,transRSI3)
cRSI3t1=color.new(_cRSIt1,transRSI3)
cRSI3t2=color.new(_cRSIt2,transRSI3)
cRSI3t3=color.new(_cRSIt3,transRSI3)
cRSI3b1=color.new(_cRSIb1,transRSI3)
cRSI3b2=color.new(_cRSIb2,transRSI3)
cRSI3b3=color.new(_cRSIb3,transRSI3)

showRSI4 = input.bool(defval=true,title="use RSI in Higher TF4",inline="tf4",group="RSI")
transRSI4 = input.int(defval=40,minval=0,maxval=99,title="border transpar",inline="tf4",group="RSI")
cRSI4i=color.new(_cRSIi,transRSI4)
cRSI4t1=color.new(_cRSIt1,transRSI4)
cRSI4t2=color.new(_cRSIt2,transRSI4)
cRSI4t3=color.new(_cRSIt3,transRSI4)
cRSI4b1=color.new(_cRSIb1,transRSI4)
cRSI4b2=color.new(_cRSIb2,transRSI4)
cRSI4b3=color.new(_cRSIb3,transRSI4)

//
//high low box
//
var a_hist_top = array.new_float(0,na)
var a_hist_bottom  = array.new_float(0,na)
var a_hist_time = array.new_int(0,na)
var a_hist_tf = array.new_string(0,na)
var a_hist_close = array.new_float(0,na)
var a_hist_cbox = array.new_color(0,na)

var m_high = matrix.new<float>(2,1,hl2)//0: price, 1:time
var m_return_high = matrix.new<float>(2,1,time)//0: price, 1:time
var m_low = matrix.new<float>(2,1,hl2)//0: price, 1:time
var m_return_low = matrix.new<float>(2,1,time)//0: price, 1:time

b_isLastUp=close[1]>open[1]?true:false
b_isUp=close>open?true:false
b_peak=b_isLastUp and not b_isUp
b_bottom=not b_isLastUp and b_isUp

i_renew=0
var i_last_renew=0

if b_peak or b_bottom
    if b_peak
        _high=close[1]>=open?close[1]:open
        _high_time=close[1]>=open?time_close[1]:time_close
        [_i_renew,o_m_high,o_m_return_high,o_m_low,o_m_return_low]=
         f_renew_high(i_last_renew,_high,_high_time,m_high,m_return_high,m_low,m_return_low)
        i_renew:=_i_renew
        m_high:= o_m_high
        m_return_high:=o_m_return_high
        m_low:= o_m_low
        m_return_low:= o_m_return_low
    else if b_bottom
        _low=close[1]<=open?close[1]:open
        _low_time=close[1]<=open?time_close[1]:time_close
        [_i_renew,o_m_high,o_m_return_high,o_m_low,o_m_return_low]=
         f_renew_low(i_last_renew,_low,_low_time,m_low,m_return_low,m_high,m_return_high)
        i_renew:=_i_renew
        m_high:= o_m_high
        m_return_high:= o_m_return_high
        m_low:= o_m_low
        m_return_low:= o_m_return_low
    else
        na

else
    na    
//
i_last_renew:=i_renew

var mybox=box.new(na,na,na,na,xloc=xloc.bar_time,bgcolor=na,border_color=na)
var myfibo23=line.new(na,na,na,na,xloc=xloc.bar_time)
var myfibo38=line.new(na,na,na,na,xloc=xloc.bar_time)
var myfibo50=line.new(na,na,na,na,xloc=xloc.bar_time)
var myfibo61=line.new(na,na,na,na,xloc=xloc.bar_time)
var myfibo78=line.new(na,na,na,na,xloc=xloc.bar_time)
var myfibo161=label.new(na,na,na,xloc=xloc.bar_time,style=label.style_none)
var myfibo261=label.new(na,na,na,xloc=xloc.bar_time,style=label.style_none)
var myfibo361=label.new(na,na,na,xloc=xloc.bar_time,style=label.style_none)
var myfibom161=label.new(na,na,na,xloc=xloc.bar_time,style=label.style_none)
var myfibom261=label.new(na,na,na,xloc=xloc.bar_time,style=label.style_none)
var myfibom361=label.new(na,na,na,xloc=xloc.bar_time,style=label.style_none)

var i_breakT=0
var i_breakf161=0
var i_breakf261=0
var i_breakB=0
var i_breakfm161=0
var i_breakfm261=0


top= matrix.get(m_high,0,matrix.columns(m_high)-1)
top_time=  matrix.get(m_high,1,matrix.columns(m_high)-1)
bottom= matrix.get(m_low,0,matrix.columns(m_low)-1)
bottom_time= matrix.get(m_low,1,matrix.columns(m_low)-1)

c_box_bg=bottom_time<top_time?cBoxUf:cBoxDf
c_box=bottom_time<top_time?cBoxU:cBoxD

_h = array.from(int(top_time),int(bottom_time))
_v = array.from(top,bottom)
_t = array.max(_v)
_b = array.min(_v)
_l = array.min(_h)

[fm361,fm261,fm161,f23,f38,f50,f61,f78,f161,f261,f361] = f_fibo(_b,_t)

if show0 and i_renew!=0
    mybox:=box.new(_l,na,na,na,xloc=xloc.bar_time,border_width=widthBox,text_halign=text.align_center,text_size=infoSize,
     border_color=c_box,text_color=cBoxT,bgcolor=c_box_bg,border_style=styleBox)
    array.push(a_hist_time,_l)
    array.push(a_hist_tf,tfChart)
    array.push(a_hist_top,_t)
    array.push(a_hist_bottom,_b)
    array.push(a_hist_close,close)
    array.push(a_hist_cbox,c_box)

    if b_fibo
        myfibo23:=line.new(_l,f23,na,f23,xloc.bar_time,extend.none,c_box,line.style_dashed,1)
        myfibo38:=line.new(_l,f38,na,f38,xloc.bar_time,extend.none,c_box,line.style_dashed,1)
        myfibo50:=line.new(_l,f50,na,f50,xloc.bar_time,extend.none,c_box,line.style_solid,1)
        myfibo61:=line.new(_l,f61,na,f61,xloc.bar_time,extend.none,c_box,line.style_dashed,1)
        myfibo78:=line.new(_l,f78,na,f78,xloc.bar_time,extend.none,c_box,line.style_dashed,1)
        myfibo161:=label.new(na,f161,"___1.618:"+str.tostring(f161,"#.0000")+"___",xloc.bar_time,yloc.price,c_box_bg,label.style_none,c_box,infoSize)
        myfibo261:=label.new(na,f261,"___2.618:"+str.tostring(f261,"#.0000")+"___",xloc.bar_time,yloc.price,c_box_bg,label.style_none,c_box,infoSize)
        myfibo361:=label.new(na,f361,"___3.618:"+str.tostring(f361,"#.0000")+"___",xloc.bar_time,yloc.price,c_box_bg,label.style_none,c_box,infoSize)
        myfibom161:=label.new(na,fm161,"___1.618:"+str.tostring(fm161,"#.0000")+"___",xloc.bar_time,yloc.price,c_box_bg,label.style_none,c_box,infoSize)
        myfibom261:=label.new(na,fm261,"___2.618:"+str.tostring(fm261,"#.0000")+"___",xloc.bar_time,yloc.price,c_box_bg,label.style_none,c_box,infoSize)
        myfibom361:=label.new(na,fm361,"___3.618:"+str.tostring(fm361,"#.0000")+"___",xloc.bar_time,yloc.price,c_box_bg,label.style_none,c_box,infoSize)
    else
        label.delete(myfibo161),label.delete(myfibo261),label.delete(myfibo361)
        label.delete(myfibom161),label.delete(myfibom261),label.delete(myfibo361)
    i_breakT:=0
    i_breakB:=0
    i_breakf161:=0
    i_breakf261:=0
    i_breakfm161:=0
    i_breakfm261:=0

i_breakT+=high>_t?1:0
i_breakB+=low<_t?1:0
i_breakf161:=high>f161?1:0
i_breakf261:=high>f261?1:0
i_breakfm161:=low<fm161?1:0
i_breakfm261:=low<fm261?1:0

if show0
    box.set_right(mybox,time_close)
    box.set_bottom(mybox,_b)
    box.set_top(mybox,_t)
    box.set_text(mybox,
     b_info?
      "TF="+tfChart+":"+f_time_ddHHmm(box.get_right(mybox)-box.get_left(mybox))+":"+
      str.tostring(math.abs(box.get_top(mybox)-box.get_bottom(mybox))):na)

    if array.lastindexof(a_hist_tf,tfChart)!=-1
        array.set(a_hist_top,array.lastindexof(a_hist_tf,tfChart),_t)
        array.set(a_hist_bottom,array.lastindexof(a_hist_tf,tfChart),_b)
        array.set(a_hist_close,array.lastindexof(a_hist_tf,tfChart),close)

    line.set_x2(myfibo23,time_close)
    line.set_x2(myfibo38,time_close)
    line.set_x2(myfibo50,time_close)
    line.set_x2(myfibo61,time_close)
    line.set_x2(myfibo78,time_close)

    if i_breakT>0
        label.set_xy(myfibo161,(_l+time_close)/2,f161)
    if i_breakf161>0
        label.set_xy(myfibo261,(_l+time_close)/2,f261)
    if i_breakf261>0
        label.set_xy(myfibo361,(_l+time_close)/2,f361)
    if i_breakB>0
        label.set_xy(myfibom161,(_l+time_close)/2,fm161)
    if i_breakfm161>0
        label.set_xy(myfibom261,(_l+time_close)/2,fm261)
    if i_breakfm261>0
        label.set_xy(myfibom361,(_l+time_close)/2,fm361)

//box H1
close1=f_getMtf(tf1,close)
open1=f_getMtf(tf1,open)
time1=f_getMtf(tf1,time)
time_close1=f_getMtf(tf1,time_close)

var m_high1 = matrix.new<float>(2,1,hl2)//0: price, 1:time
var m_return_high1 = matrix.new<float>(2,1,time)//0: price, 1:time
var m_low1 = matrix.new<float>(2,1,hl2)//0: price, 1:time
var m_return_low1 = matrix.new<float>(2,1,time)//0: price, 1:time

b_isLastUp1=close1[1]>open1[1]?true:false
b_isUp1=close1>open1?true:false
b_peak1=b_isLastUp1 and not b_isUp1
b_bottom1=not b_isLastUp1 and b_isUp1

i_renew1=0
var i_last_renew1=0//laststate

if b_peak1 or b_bottom1
    if b_peak1
        _high=close1[1]>=open1?close1[1]:open1
        _high_time=close1[1]>=open1?time_close1[1]:time_close1

        [_i_renew,o_m_high,o_m_return_high,o_m_low,o_m_return_low]=
         f_renew_high(i_last_renew1,_high,_high_time,m_high1,m_return_high1,m_low1,m_return_low1)
        i_renew1:=_i_renew
        m_high1:= o_m_high
        m_return_high1:=o_m_return_high
        m_low1:= o_m_low
        m_return_low1:= o_m_return_low

    else if b_bottom1
        _low=close1[1]<=open1?close1[1]:open1
        _low_time=close1[1]<=open1?time_close1[1]:time_close1

        [_i_renew,o_m_high,o_m_return_high,o_m_low,o_m_return_low]=
         f_renew_low(i_last_renew1,_low,_low_time,m_low1,m_return_low1,m_high1,m_return_high1)
        i_renew1:=_i_renew
        m_high1:= o_m_high
        m_return_high1:= o_m_return_high
        m_low1:= o_m_low
        m_return_low1:= o_m_return_low
    else
        na

else
    na    
//
i_last_renew1:=i_renew1

var mybox1=box.new(na,na,na,na,xloc=xloc.bar_time,bgcolor=na,border_color=na)

top1= matrix.get(m_high1,0,matrix.columns(m_high1)-1)
top_time1=  matrix.get(m_high1,1,matrix.columns(m_high1)-1)
bottom1= matrix.get(m_low1,0,matrix.columns(m_low1)-1)
bottom_time1= matrix.get(m_low1,1,matrix.columns(m_low1)-1)

c_box1=bottom_time1<top_time1?cBoxU1:cBoxD1
va1=bottom_time1<top_time1?text.align_top:text.align_bottom

_h1 = array.from(int(top_time1),int(bottom_time1))
_v1 = array.from(top1,bottom1)
_t1 = array.max(_v1)
_b1 = array.min(_v1)
_l1 = array.min(_h1)

if show1 and i_renew1!=0
    mybox1:=box.new(_l1,na,na,na,xloc=xloc.bar_time,bgcolor=na,border_width=widthBox1,border_style=styleBox1,
     text_halign=text.align_right,text_valign=va1,text_size=infoSize1,border_color=c_box1,text_color=cBoxT1)
    array.push(a_hist_time,_l1)
    array.push(a_hist_tf,tf1)
    array.push(a_hist_top,_t1)
    array.push(a_hist_bottom,_b1)
    array.push(a_hist_close,close)
    array.push(a_hist_cbox,c_box)

if show1
    box.set_right(mybox1,time_close1)
    box.set_bottom(mybox1,_b1)
    box.set_top(mybox1,_t1)
    box.set_text(mybox1,b_info1?"TF➊="+tf1+"\n"+
     f_time_ddHHmm(box.get_right(mybox1)-box.get_left(mybox1))+"\n"+
     str.tostring(math.abs(box.get_top(mybox1)-box.get_bottom(mybox1))):na)

    if array.lastindexof(a_hist_tf,tf1)!=-1
        array.set(a_hist_top,array.lastindexof(a_hist_tf,tf1),_t1)
        array.set(a_hist_bottom,array.lastindexof(a_hist_tf,tf1),_b1)
        array.set(a_hist_close,array.lastindexof(a_hist_tf,tf1),close1)

//box H2
close2=f_getMtf(tf2,close)
open2=f_getMtf(tf2,open)
time2=f_getMtf(tf2,time)
time_close2=f_getMtf(tf2,time_close)

var m_high2 = matrix.new<float>(2,1,hl2)//0: price, 1:time
var m_return_high2 = matrix.new<float>(2,1,time)//0: price, 1:time
var m_low2 = matrix.new<float>(2,1,hl2)//0: price, 1:time
var m_return_low2 = matrix.new<float>(2,1,time)//0: price, 1:time

b_isLastUp2=close2[1]>open2[1]?true:false
b_isUp2=close2>open2?true:false
b_peak2=b_isLastUp2 and not b_isUp2
b_bottom2=not b_isLastUp2 and b_isUp2

i_renew2=0
var i_last_renew2=0//laststate

if b_peak2 or b_bottom2
    if b_peak2
        _high=close2[1]>=open2?close2[1]:open2
        _high_time=close2[1]>=open2?time_close2[1]:time_close2

        [_i_renew,o_m_high,o_m_return_high,o_m_low,o_m_return_low]=
         f_renew_high(i_last_renew2,_high,_high_time,m_high2,m_return_high2,m_low2,m_return_low2)
        i_renew2:=_i_renew
        m_high2:= o_m_high
        m_return_high2:=o_m_return_high
        m_low2:= o_m_low
        m_return_low2:= o_m_return_low

    else if b_bottom2
        _low=close2[1]<=open2?close2[1]:open2
        _low_time=close2[1]<=open2?time_close2[1]:time_close2

        [_i_renew,o_m_high,o_m_return_high,o_m_low,o_m_return_low]=
         f_renew_low(i_last_renew2,_low,_low_time,m_low2,m_return_low2,m_high2,m_return_high2)
        i_renew2:=_i_renew
        m_high2:= o_m_high
        m_return_high2:= o_m_return_high
        m_low2:= o_m_low
        m_return_low2:= o_m_return_low

    else
        na

else
    na    
//
i_last_renew2:=i_renew2

var mybox2=box.new(na,na,na,na,xloc=xloc.bar_time,bgcolor=na,border_color=na)

top2= matrix.get(m_high2,0,matrix.columns(m_high2)-1)
top_time2=  matrix.get(m_high2,1,matrix.columns(m_high2)-1)
bottom2= matrix.get(m_low2,0,matrix.columns(m_low2)-1)
bottom_time2= matrix.get(m_low2,1,matrix.columns(m_low2)-1)

c_box2=bottom_time2<top_time2?cBoxU2:cBoxD2
va2=bottom_time2<top_time2?text.align_top:text.align_bottom

_h2 = array.from(int(top_time2),int(bottom_time2))
_v2 = array.from(top2,bottom2)
_t2 = array.max(_v2)
_b2 = array.min(_v2)
_l2 = array.min(_h2)

if show2 and i_renew2!=0
    mybox2:=box.new(_l2,na,na,na,xloc=xloc.bar_time,bgcolor=na,border_width=widthBox2,border_style=styleBox2,
     text_halign=text.align_right,text_valign=va2,text_size=infoSize2,border_color=c_box2,text_color=cBoxT2)
    array.push(a_hist_time,_l2)
    array.push(a_hist_tf,tf2)
    array.push(a_hist_top,_t2)
    array.push(a_hist_bottom,_b2)
    array.push(a_hist_close,close)
    array.push(a_hist_cbox,c_box)

if show2
    box.set_right(mybox2,time_close2)
    box.set_bottom(mybox2,_b2)
    box.set_top(mybox2,_t2)

    box.set_text(mybox2,b_info2?"TF➋="+tf2+"\n"+
     f_time_ddHHmm(box.get_right(mybox2)-box.get_left(mybox2))+"\n"+
     str.tostring(math.abs(box.get_top(mybox2)-box.get_bottom(mybox2))):na)

    if array.lastindexof(a_hist_tf,tf2)!=-1
        array.set(a_hist_top,array.lastindexof(a_hist_tf,tf2),_t2)
        array.set(a_hist_bottom,array.lastindexof(a_hist_tf,tf2),_b2)
        array.set(a_hist_close,array.lastindexof(a_hist_tf,tf2),close2)

//box H3
close3=f_getMtf(tf3,close)
open3=f_getMtf(tf3,open)
time3=f_getMtf(tf3,time)
time_close3=f_getMtf(tf3,time_close)

var m_high3 = matrix.new<float>(2,1,hl2)//0: price, 1:time
var m_return_high3 = matrix.new<float>(2,1,time)//0: price, 1:time
var m_low3 = matrix.new<float>(2,1,hl2)//0: price, 1:time
var m_return_low3 = matrix.new<float>(2,1,time)//0: price, 1:time

b_isLastUp3=close3[1]>open3[1]?true:false
b_isUp3=close3>open3?true:false
b_peak3=b_isLastUp3 and not b_isUp3
b_bottom3=not b_isLastUp3 and b_isUp3

i_renew3=0
var i_last_renew3=0

if b_peak3 or b_bottom3
    if b_peak3
        _high=close3[1]>=open3?close3[1]:open3
        _high_time=close3[1]>=open3?time_close3[1]:time_close3

        [_i_renew,o_m_high,o_m_return_high,o_m_low,o_m_return_low]=
         f_renew_high(i_last_renew3,_high,_high_time,m_high3,m_return_high3,m_low3,m_return_low3)
        i_renew3:=_i_renew
        m_high3:= o_m_high
        m_return_high3:=o_m_return_high
        m_low3:= o_m_low
        m_return_low3:= o_m_return_low

    else if b_bottom3
        _low=close3[1]<=open3?close3[1]:open3
        _low_time=close3[1]<=open3?time_close3[1]:time_close3

        [_i_renew,o_m_high,o_m_return_high,o_m_low,o_m_return_low]=
         f_renew_low(i_last_renew3,_low,_low_time,m_low3,m_return_low3,m_high3,m_return_high3)
        i_renew3:=_i_renew
        m_high3:= o_m_high
        m_return_high3:= o_m_return_high
        m_low3:= o_m_low
        m_return_low3:= o_m_return_low

    else
        na

else
    na    
//
i_last_renew3:=i_renew3

var mybox3=box.new(na,na,na,na,xloc=xloc.bar_time,bgcolor=na,border_color=na)

top3= matrix.get(m_high3,0,matrix.columns(m_high3)-1)
top_time3=  matrix.get(m_high3,1,matrix.columns(m_high3)-1)
bottom3= matrix.get(m_low3,0,matrix.columns(m_low3)-1)
bottom_time3= matrix.get(m_low3,1,matrix.columns(m_low3)-1)

c_box3=bottom_time3<top_time3?cBoxU3:cBoxD3
va3=bottom_time3<top_time3?text.align_top:text.align_bottom

_h3 = array.from(int(top_time3),int(bottom_time3))
_v3 = array.from(top3,bottom3)
_t3 = array.max(_v3)
_b3 = array.min(_v3)
_l3 = array.min(_h3)

if show3 and i_renew3!=0
    mybox3:=box.new(_l3,na,na,na,xloc=xloc.bar_time,bgcolor=na,border_width=widthBox3,border_style=styleBox3,
     text_halign=text.align_right,text_valign=va3,text_size=infoSize3,border_color=c_box3,text_color=cBoxT3)
    array.push(a_hist_time,_l3)
    array.push(a_hist_tf,tf3)
    array.push(a_hist_top,_t3)
    array.push(a_hist_bottom,_b3)
    array.push(a_hist_close,close)
    array.push(a_hist_cbox,c_box)

if show3
    box.set_right(mybox3,time_close3)
    box.set_bottom(mybox3,_b3)
    box.set_top(mybox3,_t3)
    box.set_text(mybox3,b_info3?"TF➌="+tf3+"\n"+
     f_time_ddHHmm(box.get_right(mybox3)-box.get_left(mybox3))+"\n"+
     str.tostring(math.abs(box.get_top(mybox3)-box.get_bottom(mybox3))):na)

    if array.lastindexof(a_hist_tf,tf3)!=-1
        array.set(a_hist_top,array.lastindexof(a_hist_tf,tf3),_t3)
        array.set(a_hist_bottom,array.lastindexof(a_hist_tf,tf3),_b3)
        array.set(a_hist_close,array.lastindexof(a_hist_tf,tf3),close3)

//box H4
close4=f_getMtf(tf4,close)
open4=f_getMtf(tf4,open)
time4=f_getMtf(tf4,time)
time_close4=f_getMtf(tf4,time_close)

var m_high4 = matrix.new<float>(2,1,hl2)//0: price, 1:time
var m_return_high4 = matrix.new<float>(2,1,time)//0: price, 1:time
var m_low4 = matrix.new<float>(2,1,hl2)//0: price, 1:time
var m_return_low4 = matrix.new<float>(2,1,time)//0: price, 1:time

b_isLastUp4=close4[1]>open4[1]?true:false
b_isUp4=close4>open4?true:false
b_peak4=b_isLastUp4 and not b_isUp4
b_bottom4=not b_isLastUp4 and b_isUp4

i_renew4=0
var i_last_renew4=0

if b_peak4 or b_bottom4
    if b_peak4
        _high=close4[1]>=open4?close4[1]:open4
        _high_time=close4[1]>=open4?time_close4[1]:time_close4

        [_i_renew,o_m_high,o_m_return_high,o_m_low,o_m_return_low]=
         f_renew_high(i_last_renew4,_high,_high_time,m_high4,m_return_high4,m_low4,m_return_low4)
        i_renew4:=_i_renew
        m_high4:= o_m_high
        m_return_high4:=o_m_return_high
        m_low4:= o_m_low
        m_return_low4:= o_m_return_low

    else if b_bottom4
        _low=close4[1]<=open4?close4[1]:open4
        _low_time=close4[1]<=open4?time_close4[1]:time_close4

        [_i_renew,o_m_high,o_m_return_high,o_m_low,o_m_return_low]=
         f_renew_low(i_last_renew4,_low,_low_time,m_low4,m_return_low4,m_high4,m_return_high4)
        i_renew4:=_i_renew
        m_high4:= o_m_high
        m_return_high4:= o_m_return_high
        m_low4:= o_m_low
        m_return_low4:= o_m_return_low

    else
        na

else
    na    
//
i_last_renew4:=i_renew4

var mybox4=box.new(na,na,na,na,xloc=xloc.bar_time,bgcolor=na,border_color=na)

top4= matrix.get(m_high4,0,matrix.columns(m_high4)-1)
top_time4=  matrix.get(m_high4,1,matrix.columns(m_high4)-1)
bottom4= matrix.get(m_low4,0,matrix.columns(m_low4)-1)
bottom_time4= matrix.get(m_low4,1,matrix.columns(m_low4)-1)

c_box4=bottom_time4<top_time4?cBoxU4:cBoxD4
va4=bottom_time4<top_time4?text.align_top:text.align_bottom

_h4 = array.from(int(top_time4),int(bottom_time4))
_v4 = array.from(top4,bottom4)
_t4 = array.max(_v4)
_b4 = array.min(_v4)
_l4 = array.min(_h4)

if show4 and i_renew4!=0
    mybox4:=box.new(_l4,na,na,na,xloc=xloc.bar_time,bgcolor=na,border_width=widthBox4,border_style=styleBox4,
     text_halign=text.align_right,text_valign=va4,text_size=infoSize4,border_color=c_box4,text_color=cBoxT4)
    array.push(a_hist_time,_l4)
    array.push(a_hist_tf,tf4)
    array.push(a_hist_top,_t4)
    array.push(a_hist_bottom,_b4)
    array.push(a_hist_close,close)
    array.push(a_hist_cbox,c_box)

if show4
    box.set_right(mybox4,time_close4)
    box.set_bottom(mybox4,_b4)
    box.set_top(mybox4,_t4)
    box.set_text(mybox4,b_info4?"TF➍="+tf4+"\n"+
     f_time_ddHHmm(box.get_right(mybox4)-box.get_left(mybox4))+"\n"+
     str.tostring(math.abs(box.get_top(mybox4)-box.get_bottom(mybox4))):na)

    if array.lastindexof(a_hist_tf,tf4)!=-1
        array.set(a_hist_top,array.lastindexof(a_hist_tf,tf4),_t4)
        array.set(a_hist_bottom,array.lastindexof(a_hist_tf,tf4),_b4)
        array.set(a_hist_close,array.lastindexof(a_hist_tf,tf4),close4)

//
//fibonacci levels of higher boxes
//
var l_mybox1_rt=label.new(na,na,na,xloc.bar_time,yloc.price,na,label.style_none,na,infoSize1,text.align_left)
var l_mybox1_rb=label.new(na,na,na,xloc.bar_time,yloc.price,na,label.style_none,na,infoSize1,text.align_left)
var l_mybox2_rt=label.new(na,na,na,xloc.bar_time,yloc.price,na,label.style_none,na,infoSize2,text.align_left)
var l_mybox2_rb=label.new(na,na,na,xloc.bar_time,yloc.price,na,label.style_none,na,infoSize2,text.align_left)
var l_mybox3_rt=label.new(na,na,na,xloc.bar_time,yloc.price,na,label.style_none,na,infoSize3,text.align_left)
var l_mybox3_rb=label.new(na,na,na,xloc.bar_time,yloc.price,na,label.style_none,na,infoSize3,text.align_left)
var l_mybox4_rt=label.new(na,na,na,xloc.bar_time,yloc.price,na,label.style_none,na,infoSize3,text.align_left)
var l_mybox4_rb=label.new(na,na,na,xloc.bar_time,yloc.price,na,label.style_none,na,infoSize3,text.align_left)

if show1 and b_fibo1
    [_top,_tops,_bottom,_bottoms] = f_fibo_range(box.get_bottom(mybox1),box.get_top(mybox1),close)
    label.set_xy(l_mybox1_rt,box.get_right(mybox1),_top)
    label.set_textcolor(l_mybox1_rt,c_box1)
    label.set_text(l_mybox1_rt,"_____"+_tops+":"+str.tostring(_top,"#.0000")+"@➊"+tf1)
    label.set_xy(l_mybox1_rb,box.get_right(mybox1),_bottom)
    label.set_textcolor(l_mybox1_rb,c_box1)
    label.set_text(l_mybox1_rb,"_____"+_bottoms+":"+str.tostring(_bottom,"#.0000")+"@➊"+tf1)
if show2 and b_fibo2
    [_top,_tops,_bottom,_bottoms] = f_fibo_range(box.get_bottom(mybox2),box.get_top(mybox2),close)
    label.set_xy(l_mybox2_rt,box.get_right(mybox2),_top)
    label.set_textcolor(l_mybox2_rt,c_box2)
    label.set_text(l_mybox2_rt,"_____"+_tops+":"+str.tostring(_top,"#.0000")+"@➋"+tf2)
    label.set_xy(l_mybox2_rb,box.get_right(mybox2),_bottom)
    label.set_textcolor(l_mybox2_rb,c_box2)
    label.set_text(l_mybox2_rb,"_____"+_bottoms+":"+str.tostring(_bottom,"#.0000")+"@➋"+tf2)
if show3 and b_fibo3
    [_top,_tops,_bottom,_bottoms] = f_fibo_range(box.get_bottom(mybox3),box.get_top(mybox3),close)
    label.set_xy(l_mybox3_rt,box.get_right(mybox3),_top)
    label.set_textcolor(l_mybox3_rt,c_box3)
    label.set_text(l_mybox3_rt,"_____"+_tops+":"+str.tostring(_top,"#.0000")+"@➌"+tf3)
    label.set_xy(l_mybox3_rb,box.get_right(mybox3),_bottom)
    label.set_textcolor(l_mybox3_rb,c_box3)
    label.set_text(l_mybox3_rb,"_____"+_bottoms+":"+str.tostring(_bottom,"#.0000")+"@➌"+tf3)
if show4 and b_fibo4
    [_top,_tops,_bottom,_bottoms] = f_fibo_range(box.get_bottom(mybox4),box.get_top(mybox4),close)
    label.set_xy(l_mybox4_rt,box.get_right(mybox4),_top)
    label.set_textcolor(l_mybox4_rt,c_box4)
    label.set_text(l_mybox4_rt,"_____"+_tops+":"+str.tostring(_top,"#.0000")+"@➍"+tf4)
    label.set_xy(l_mybox4_rb,box.get_right(mybox4),_bottom)
    label.set_textcolor(l_mybox4_rb,c_box4)
    label.set_text(l_mybox4_rb,"_____"+_bottoms+":"+str.tostring(_bottom,"#.0000")+"@➍"+tf4)

var max=minimapHist

a_hist_top_mm=array.new_float(0,na)
a_hist_bottom_mm=array.new_float(0,na)
a_hist_time_mm=array.new_int(0,na)
a_hist_tf_mm=array.new_string(0,na)
a_hist_close_mm=array.new_float(0,na)
a_hist_cbox_mm=array.new_color(0,na)

a_hist_top_mm :=array.size(a_hist_top)>max?array.slice(a_hist_top,array.size(a_hist_top)-max,array.size(a_hist_top)-1):a_hist_top
a_hist_bottom_mm :=array.size(a_hist_bottom)>max?array.slice(a_hist_bottom,array.size(a_hist_bottom)-max,array.size(a_hist_bottom)-1):a_hist_bottom
a_hist_time_mm :=array.size(a_hist_time)>max?array.slice(a_hist_time,array.size(a_hist_time)-max,array.size(a_hist_time)-1):a_hist_time
a_hist_tf_mm :=array.size(a_hist_tf)>max?array.slice(a_hist_tf,array.size(a_hist_tf)-max,array.size(a_hist_tf)-1):a_hist_tf
a_hist_close_mm :=array.size(a_hist_close)>max?array.slice(a_hist_close,array.size(a_hist_close)-max,array.size(a_hist_close)-1):a_hist_close
a_hist_cbox_mm :=array.size(a_hist_cbox)>max?array.slice(a_hist_cbox,array.size(a_hist_cbox)-max,array.size(a_hist_cbox)-1):a_hist_cbox

hist_top = array.max(a_hist_top_mm)
hist_bottom = array.min(a_hist_bottom_mm)

f_cellpos(_top,_bottom,_rt,_rb) =>
    _p0 = 10
    _p100 = 50
    _diff = _top-_bottom
    _rtdiff = _top-_rt
    _rbdiff = _top-_rb
    _topp = int(10 - (_p0-_p100)*_rtdiff/_diff)
    _bottomp =int(10 - (_p0-_p100)*_rbdiff/_diff)
    [_topp,_bottomp]

if showMinimap
    minimap = table.new(position.bottom_left,100,100,color.new(color.white,90),frame_color=color.white,frame_width=1,border_color=na,border_width=1)
    if array.size(a_hist_time_mm)>0
        table.cell(minimap,0,0,text="minimap from "+f_time_ddHHmm(array.last(a_hist_time_mm)-array.get(a_hist_time_mm,0))+" ago",text_color=color.white,text_size=size.small,text_halign=text.align_left)
        table.merge_cells(minimap,0,0,10,0)
    table.cell(minimap,0,1,text=str.tostring(hist_top),text_color=color.white,text_size=size.small,text_halign=text.align_left)
    table.merge_cells(minimap,0,1,10,1)
    table.cell(minimap,0,99,text=str.tostring(hist_bottom),text_color=color.white,text_size=size.small,text_halign=text.align_left)
    table.merge_cells(minimap,0,99,10,99)
    if array.size(a_hist_top_mm)>0
        for i = 0 to array.size(a_hist_top_mm)-1
            [_top,_bottom]=f_cellpos(hist_top,hist_bottom,array.get(a_hist_top_mm,i),array.get(a_hist_bottom_mm,i))
            if array.get(a_hist_tf_mm,i) == tfChart
                _fl = f_fibo_level(array.get(a_hist_bottom_mm,i),array.get(a_hist_top_mm,i),array.get(a_hist_close_mm,i))
                table.cell(minimap,i,_top,_fl,text_color=f_fl_c(_fl),bgcolor=array.get(a_hist_cbox_mm,i),text_size=size.tiny,text_valign=f_fl_va(_fl))
                table.merge_cells(minimap,i,_top,i,_bottom)
            else
                table.cell(minimap,i,_top-1,"▼",text_color=array.get(a_hist_cbox_mm,i),text_size=size.tiny,bgcolor=na)
                table.cell(minimap,i,_top,array.get(a_hist_tf_mm,i),text_color=color.white,bgcolor=array.get(a_hist_cbox_mm,i),text_size=size.tiny)
                table.merge_cells(minimap,i,_top,i,_bottom)
                table.cell(minimap,i,_bottom+1,"▲",text_color=array.get(a_hist_cbox_mm,i),text_size=size.tiny,bgcolor=na)
            na

//*****
//end of high low box
//*****

//
//RSI
//
var int RSIt =70
var int RSIb =30

s_RSI_1= show0 and showRSI?ta.rsi(srcRSI,spanRSI_1):na
s_RSI_2= show0 and showRSI?ta.rsi(srcRSI,spanRSI_2):na

b_RSI_70 = s_RSI_1>=RSIt or s_RSI_2>=RSIt
b_RSI_30 = s_RSI_1<=RSIb or s_RSI_2<=RSIb

cRSI=
 s_RSI_1==s_RSI_1[1] and s_RSI_2==s_RSI_2[1]?na:
 b_RSI_70? s_RSI_1>s_RSI_2? s_RSI_2<RSIt?cRSIi: cRSIt1: s_RSI_1<RSIt?cRSIt3: cRSIt2:
 b_RSI_30? s_RSI_1<s_RSI_2? s_RSI_2>RSIb?cRSIi: cRSIb1: s_RSI_1>RSIb?cRSIb3: cRSIb2:na
RSIy=b_RSI_70? box.get_top(mybox): b_RSI_30?box.get_bottom(mybox):na

plotshape(RSIy,"SIG_RSI",shape.circle,location=location.absolute,color=cRSI,text=na,textcolor=na,size=size.normal)

//➊➋➌➍➎➏➐➑➒➓❶❷❸❹❺❻❼❽❾❿
//H1
s_RSI1_1= show1 and showRSI1?f_getMtf(tf1,ta.rsi(srcRSI,spanRSI_1)):na
s_RSI1_2= show1 and showRSI1?f_getMtf(tf1,ta.rsi(srcRSI,spanRSI_2)):na

b_RSI1_70 = s_RSI1_1>=RSIt or s_RSI1_2>=RSIt
b_RSI1_30 = s_RSI1_1<=RSIb or s_RSI1_2<=RSIb

cRSI1=
 s_RSI1_1==s_RSI1_1[1] and s_RSI1_2==s_RSI1_2[1]?na:
 b_RSI1_70? s_RSI1_1>s_RSI1_2? s_RSI1_2<RSIt?cRSI1i: cRSI1t1: s_RSI1_1<RSIt?cRSI1t3: cRSI1t2:
 b_RSI1_30? s_RSI1_1<s_RSI1_2? s_RSI1_2>RSIb?cRSI1i: cRSI1b1: s_RSI1_1>RSIb?cRSI1b3: cRSI1b2:na
RSI1y=b_RSI1_70? box.get_top(mybox1): b_RSI1_30?box.get_bottom(mybox1):na

plotchar(RSI1y,"SIG_RSI1",char="➊",location=location.absolute,color=cRSI1,textcolor=na,size=size.small)

//H2
s_RSI2_1= show2 and showRSI2?f_getMtf(tf2,ta.rsi(srcRSI,spanRSI_1)):na
s_RSI2_2= show2 and showRSI2?f_getMtf(tf2,ta.rsi(srcRSI,spanRSI_2)):na

b_RSI2_70 = s_RSI2_1>=RSIt or s_RSI2_2>=RSIt
b_RSI2_30 = s_RSI2_1<=RSIb or s_RSI2_2<=RSIb

cRSI2=
 s_RSI2_1==s_RSI2_1[1] and s_RSI2_2==s_RSI2_2[1]?na:
 b_RSI2_70? s_RSI2_1>s_RSI2_2? s_RSI2_2<RSIt?cRSI2i: cRSI2t1: s_RSI2_1<RSIt?cRSI2t3: cRSI2t2:
 b_RSI2_30? s_RSI2_1<s_RSI2_2? s_RSI2_2>RSIb?cRSI2i: cRSI2b1: s_RSI2_1>RSIb?cRSI2b3: cRSI2b2:na
RSI2y=b_RSI2_70? box.get_top(mybox2): b_RSI2_30?box.get_bottom(mybox2):na

plotchar(RSI2y,"SIG_RSI2",char="➋",location=location.absolute,color=cRSI2,textcolor=na,size=size.small)

//H3
s_RSI3_1= show3 and showRSI3?f_getMtf(tf3,ta.rsi(srcRSI,spanRSI_1)):na
s_RSI3_2= show3 and showRSI3?f_getMtf(tf3,ta.rsi(srcRSI,spanRSI_2)):na

b_RSI3_70 = s_RSI3_1>=RSIt or s_RSI3_2>=RSIt
b_RSI3_30 = s_RSI3_1<=RSIb or s_RSI3_2<=RSIb

cRSI3=
 s_RSI3_1==s_RSI3_1[1] and s_RSI3_2==s_RSI3_2[1]?na:
 b_RSI3_70? s_RSI3_1>s_RSI3_2? s_RSI3_2<RSIt?cRSI3i: cRSI3t1: s_RSI3_1<RSIt?cRSI3t3: cRSI3t2:
 b_RSI3_30? s_RSI3_1<s_RSI3_2? s_RSI3_2>RSIb?cRSI3i: cRSI3b1: s_RSI3_1>RSIb?cRSI3b3: cRSI3b2:na
RSI3y=b_RSI3_70? box.get_top(mybox3): b_RSI3_30?box.get_bottom(mybox3):na

plotchar(RSI3y,"SIG_RSI3",char="➌",location=location.absolute,color=cRSI3,textcolor=na,size=size.small)

//H4
s_RSI4_1= show4 and showRSI4?f_getMtf(tf4,ta.rsi(srcRSI,spanRSI_1)):na
s_RSI4_2= show4 and showRSI4?f_getMtf(tf4,ta.rsi(srcRSI,spanRSI_2)):na

b_RSI4_70 = s_RSI4_1>=RSIt or s_RSI4_2>=RSIt
b_RSI4_30 = s_RSI4_1<=RSIb or s_RSI4_2<=RSIb

cRSI4=
 s_RSI4_1==s_RSI4_1[1] and s_RSI4_2==s_RSI4_2[1]?na:
 b_RSI4_70? s_RSI4_1>s_RSI4_2? s_RSI4_2<RSIt?cRSI4i: cRSI4t1: s_RSI4_1<RSIt?cRSI4t3: cRSI4t2:
 b_RSI4_30? s_RSI4_1<s_RSI4_2? s_RSI4_2>RSIb?cRSI4i: cRSI4b1: s_RSI4_1>RSIb?cRSI4b3: cRSI4b2:na
RSI4y=b_RSI4_70? box.get_top(mybox4): b_RSI4_30?box.get_bottom(mybox4):na

plotchar(RSI4y,"SIG_RSI4",char="➍",location=location.absolute,color=cRSI4,textcolor=na,size=size.small)
