Faytterro Market Structere
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © faytterro

//@version=5
indicator("Faytterro Market Structere", overlay = true, max_bars_back =2000)
max_bars_back(time, 2000)
n = input.int(10, "length", minval = 1)
g = input.int(0,"go to past",minval = 0)
h = true
h0 = true
if last_bar_index-bar_index<500+g*n*2
    for i=0 to 2*n
        h := h and high[n]>=high[i]  
        h0 := h0 and high>=high[math.round(i/2)] 
    lows = array.new_float()
    lows2 = array.new_float()
    a = ta.valuewhen(h, bar_index,0+g)
    b = ta.valuewhen(h, bar_index,1+g)
    c = ta.valuewhen(h0, bar_index,0+g)
    lb = last_bar_index
    for i = 0 to a-b-1
        array.push(lows,low[i+lb-a+n])
    thelow=array.min(lows)
    lowindex=array.indexof(lows,thelow)
    for i = 0 to c>a? c+n-a-1 : c+n-b-1
        array.push(lows2,low[i+lb-c])
    thelow2=array.min(lows2)
    lowindex2=array.indexof(lows2,thelow2)
    hp = ta.valuewhen(h, high[n],0+g)
    hp1 = ta.valuewhen(h, high[n],1+g)

    flows = array.new_float()
    for i = 0 to lb-a-1+n
        array.push(flows,low[i])
    theflow=array.min(flows)
    theflowindex=array.indexof(flows,theflow)
    linebot = line.new(-lowindex2-n+a*0+c+n, thelow2,last_bar_index+10, thelow2, style = line.style_dashed, color= color.rgb(233, 111, 111))
    line.delete(linebot[1])
    linetop = line.new( a-n, hp, last_bar_index+10, hp, color= color.rgb(123, 235, 130), style = line.style_dashed)
    line.delete(linetop[1])
    line = line.new(-lowindex-n+a, thelow, a-n, hp, color= hp>hp1? color.rgb(123,235,130) : color.rgb(233, 111, 111))
    line.delete(line[1])
    line2 = line.new(b-n,hp1,-lowindex-n+a, thelow, color= hp>hp1? color.rgb(123,235,130) : color.rgb(233, 111, 111))
    line.delete(line2[1])
    lineo= line.new(-lowindex2-n+a*0+c+n, thelow2, a-n, hp, color= thelow2<thelow? color.rgb(233, 111, 111) : hp>hp1? color.rgb(123,235,130) : color.rgb(233, 111, 111))
    line.delete(lineo[1])
    //////////////////////////////////////////
    dt = math.round(100*hp*100/close-10000)/100
    dd = math.round(100*thelow2*100/close-10000)/100
    var tbl = table.new(position.top_center, 2, 2, color.rgb(120, 123, 134, 100), color.rgb(0,0,0,100), 1, color.rgb(255, 255, 255,100), 5)
    if barstate.islast
        table.cell(tbl, 0, 1, str.tostring(dt)+"%", width = 10,
         text_color = color.rgb(0, 0, 0), bgcolor = color.rgb(100, 255, 100,50), text_size = size.large) //table_id = testTable, column = 0, row = 0, text = "Open is " + str.tostring(open))
        table.cell(tbl, 1, 1, str.tostring(dd)+"%", width = 10, 
         text_color = color.rgb(255, 255, 255), bgcolor =  color.rgb(255, 0, 0,50), text_size = size.large)
        table.cell(tbl, 0, 0, "distance\n to the high", width = 2,
         text_color = color.rgb(0, 0, 0), bgcolor = color.rgb(100, 255, 100,50), text_size = size.normal) //table_id = testTable, column = 0, row = 0, text = "Open is " + str.tostring(open))
        table.cell(tbl, 1, 0,"distance\n to the low", width = 2, 
         text_color = color.rgb(255, 255, 255), bgcolor =  color.rgb(255, 0, 0,50), text_size = size.normal)
        msbb = label.new(ta.valuewhen(ta.crossunder(dt,0), bar_index,0),ta.valuewhen(ta.crossunder(dt,0), low,0), "MSB", color=color.rgb(0, 153, 5), textcolor = color.white, style= label.style_label_up)
        label.delete(msbb[1])
        msbs = label.new(ta.valuewhen(ta.crossover(dd,0), bar_index,0),ta.valuewhen(ta.crossover(dd,0), high,0), "MSB", color=color.rgb(153, 0, 0), textcolor = color.white)
        label.delete(msbs[1])
    /////////////////////
    lows2p = array.new_float()
    a2 = ta.valuewhen(h, bar_index,1+g)
    b2 = ta.valuewhen(h, bar_index,2+g)
    for i = 0 to a2-b2-1
        array.push(lows2p,low[i+lb-a2+n])
    thelow2p=array.min(lows2p)
    lowindex2p=array.indexof(lows2p,thelow2p)
    hp2 = ta.valuewhen(h, high[n],1+g)
    hp12 = ta.valuewhen(h, high[n],2+g)
    line21 = line.new(-lowindex2p-n+a2, thelow2p, a2-n, hp2, color=  hp2>hp12? color.rgb(123,235,130) : color.rgb(233, 111, 111))
    line.delete(line21[1])
    line22 = line.new(b2-n,hp12,-lowindex2p-n+a2, thelow2p, color=  hp2>hp12? color.rgb(123,235,130) : color.rgb(233, 111, 111))
    line.delete(line22[1])
    /////////////////
    lows3 = array.new_float()
    a3 = ta.valuewhen(h, bar_index,2+g)
    b3= ta.valuewhen(h, bar_index,3+g)
    for i = 0 to a3-b3-1
        array.push(lows3,low[i+lb-a3+n])
    thelow3=array.min(lows3)
    lowindex3=array.indexof(lows3,thelow3)
    hp3= ta.valuewhen(h, high[n],2+g)
    hp13 = ta.valuewhen(h, high[n],3+g)
    line31 = line.new(-lowindex3-n+a3, thelow3, a3-n, hp3, color=  hp3>hp13? color.rgb(123,235,130) : color.rgb(233, 111, 111))
    line.delete(line31[1])
    line32 = line.new(b3-n,hp13,-lowindex3-n+a3, thelow3, color=  hp3>hp13? color.rgb(123,235,130) : color.rgb(233, 111, 111))
    line.delete(line32[1])
    //////////////////////////////
    lows4 = array.new_float()
    a4 = ta.valuewhen(h, bar_index,3+g)
    b4= ta.valuewhen(h, bar_index,3+1+g)
    for i = 0 to a4-b4-1
        array.push(lows4,low[i+lb-a4+n])
    thelow4=array.min(lows4)
    lowindex4=array.indexof(lows4,thelow4)
    hp4= ta.valuewhen(h, high[n],3+g)
    hp14 = ta.valuewhen(h, high[n],3+1+g)
    line41 = line.new(-lowindex4-n+a4, thelow4, a4-n, hp4, color=  hp4>hp14? color.rgb(123,235,130) : color.rgb(233, 111, 111))
    line.delete(line41[1])
    line42 = line.new(b4-n,hp14,-lowindex4-n+a4, thelow4, color=  hp4>hp14? color.rgb(123,235,130) : color.rgb(233, 111, 111))
    line.delete(line42[1])
    ///////////////////////////
    lows5= array.new_float()
    a5 = ta.valuewhen(h, bar_index,4+g)
    b5 = ta.valuewhen(h, bar_index,4+1+g)
    for i = 0 to a5-b5-1
        array.push(lows5,low[i+lb-a5+n])
    thelow5=array.min(lows5)
    lowindex5=array.indexof(lows5,thelow5)
    hp5 = ta.valuewhen(h, high[n],4+g)
    hp15 = ta.valuewhen(h, high[n],4+1+g)
    line51 = line.new(-lowindex5-n+a5, thelow5, a5-n, hp5, color=  hp5>hp15? color.rgb(123,235,130) : color.rgb(233, 111, 111))
    line.delete(line51[1])
    line52 = line.new(b5-n,hp15,-lowindex5-n+a5, thelow5, color=  hp5>hp15? color.rgb(123,235,130) : color.rgb(233, 111, 111))
    line.delete(line52[1])
    ////////////////////////
    lows := array.new_float()
    a := ta.valuewhen(h, bar_index,5+g)
    b := ta.valuewhen(h, bar_index,5+1+g)
    for i = 0 to a-b-1
        array.push(lows,low[i+lb-a+n])
    thelow:=array.min(lows)
    lowindex:=array.indexof(lows,thelow)
    hp := ta.valuewhen(h, high[n],5+g)
    hp1 := ta.valuewhen(h, high[n],5+1+g)
    line61 = line.new(-lowindex-n+a, thelow, a-n, hp, color=  hp>hp1? color.rgb(123,235,130) : color.rgb(233, 111, 111))
    line.delete(line61[1])
    line62 = line.new(b-n,hp1,-lowindex-n+a, thelow, color=  hp>hp1? color.rgb(123,235,130) : color.rgb(233, 111, 111))
    line.delete(line62[1])
    ////////////////////////
    lows := array.new_float()
    a := ta.valuewhen(h, bar_index,6+g)
    b := ta.valuewhen(h, bar_index,6+1+g)
    for i = 0 to a-b-1
        array.push(lows,low[i+lb-a+n])
    thelow:=array.min(lows)
    lowindex:=array.indexof(lows,thelow)
    hp := ta.valuewhen(h, high[n],6+g)
    hp1 := ta.valuewhen(h, high[n],6+1+g)
    line71 = line.new(-lowindex-n+a, thelow, a-n, hp, color=  hp>hp1? color.rgb(123,235,130) : color.rgb(233, 111, 111))
    line.delete(line71[1])
    line72 = line.new(b-n,hp1,-lowindex-n+a, thelow, color=  hp>hp1? color.rgb(123,235,130) : color.rgb(233, 111, 111))
    line.delete(line72[1])
    ///////////////////////////////////
    lows := array.new_float()
    a := ta.valuewhen(h, bar_index,7+g)
    b := ta.valuewhen(h, bar_index,7+1+g)
    for i = 0 to a-b-1
        array.push(lows,low[i+lb-a+n])
    thelow:=array.min(lows)
    lowindex:=array.indexof(lows,thelow)
    hp := ta.valuewhen(h, high[n],7+g)
    hp1 := ta.valuewhen(h, high[n],7+1+g)
    line81 = line.new(-lowindex-n+a, thelow, a-n, hp, color=  hp>hp1? color.rgb(123,235,130) : color.rgb(233, 111, 111))
    line.delete(line81[1])
    line82 = line.new(b-n,hp1,-lowindex-n+a, thelow, color=  hp>hp1? color.rgb(123,235,130) : color.rgb(233, 111, 111))
    line.delete(line82[1])
    ///////////////////////////////////////
    lows := array.new_float()
    a := ta.valuewhen(h, bar_index,8+g)
    b := ta.valuewhen(h, bar_index,8+1+g)
    for i = 0 to a-b-1
        array.push(lows,low[i+lb-a+n])
    thelow:=array.min(lows)
    lowindex:=array.indexof(lows,thelow)
    hp := ta.valuewhen(h, high[n],8+g)
    hp1 := ta.valuewhen(h, high[n],8+1+g)
    line91 = line.new(-lowindex-n+a, thelow, a-n, hp, color=  hp>hp1? color.rgb(123,235,130) : color.rgb(233, 111, 111))
    line.delete(line91[1])
    line92 = line.new(b-n,hp1,-lowindex-n+a, thelow, color=  hp>hp1? color.rgb(123,235,130) : color.rgb(233, 111, 111))
    line.delete(line92[1])
    ////////////////
    lows := array.new_float()
    a := ta.valuewhen(h, bar_index,9+g)
    b := ta.valuewhen(h, bar_index,9+1+g)
    for i = 0 to a-b-1
        array.push(lows,low[i+lb-a+n])
    thelow:=array.min(lows)
    lowindex:=array.indexof(lows,thelow)
    hp := ta.valuewhen(h, high[n],9+g)
    hp1 := ta.valuewhen(h, high[n],9+1+g)
    line101 = line.new(-lowindex-n+a, thelow, a-n, hp, color=  hp>hp1? color.rgb(123,235,130) : color.rgb(233, 111, 111))
    line.delete(line101[1])
    line102 = line.new(b-n,hp1,-lowindex-n+a, thelow, color=  hp>hp1? color.rgb(123,235,130) : color.rgb(233, 111, 111))
    line.delete(line102[1])
    //////////////////






