[HM] Fibonacci Fractals Absolute Auto v20221114
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © HelionMelion

//@version=5
indicator('[HM] Fibonacci Fractals Absolute Auto v20230608', shorttitle='FFAA', overlay=true)

calctype = input.string(title='CALCULUS TYPE', defval='⅓', options=['⅓','Φ', 'fibonaccing'], tooltip='Φ: default 0.382 and 0.618 levels \n ⅓: not fibo but 1/3 and 2/3 levels (based on chessmaster Hindenburg Melao article hint - give a try)  \n fibonaccing: 0.236 and 0.764 levels (based on trader Marco Antonio Rossi method hint - give a try)') //, 'Φ3', 'Φ2', ])
float third = float(2) / 3
float phix = math.rphi
float phix2 = math.pow(phix, 2)
float phix3 = math.pow(phix, 3) //0.236
float fibonaccing = 1-phix3 //0.764
float phi = calctype == '⅓' ? third : calctype == 'Φ' ? phix : calctype == 'fibonaccing'? fibonaccing: na
n = input.int(defval=3, title='CALCULATION FOR MIN RANGE', minval=1)

highest_high() =>
    var float h = 0.0
    h := math.max(h, high)
    h

lowest_low() =>
    var float l = 10e10
    l := math.min(l, low)
    l

[low_12M, high_12M, yatr] = request.security(syminfo.tickerid, '12M', [lowest_low(), highest_high(), ta.atr(n)], lookahead=barmerge.lookahead_off)
[low_1M, high_1M, matr] = request.security(syminfo.tickerid, '1M', [lowest_low(), highest_high(), ta.atr(n)], lookahead=barmerge.lookahead_off)
[low_1W, high_1W, watr] = request.security(syminfo.tickerid, '1W', [lowest_low(), highest_high(), ta.atr(n)], lookahead=barmerge.lookahead_off)
[low_1D, high_1D, datr] = request.security(syminfo.tickerid, '1D', [lowest_low(), highest_high(), ta.atr(n)], lookahead=barmerge.lookahead_off)
var ATH = nz(high_12M, nz(high_1M, nz(high_1W, nz(high_1D, high))))
//ATH := high_12M //math.max(high, nz(ATH[1], ATH))
ATH := math.max(high, nz(ATH[1], ATH))
var ATL = nz(low_12M, nz(low_1M, nz(low_1W, nz(low_1D, low))))
//ATL := low_12M //math.min(low, nz(ATL[1], ATL))
ATL := math.min(low, nz(ATL[1], ATL))

//free = input(title="USE FREE ATR OR LOCKED", type=input.bool, defval=true)
//atrcount = input(defval=200, title="bars to consider", type=input.integer, minval=1)
//datrpercent = free? atr(atrcount) : (datr/ATH)*100 
datrpercent = datr / ATH * 100

r1 = input.string(title='MIN RANGE INTRADAY CALC', defval='DAY ATR', options=['DAY ATR', 'WEEK ATR', 'MONTH ATR', 'YEAR ATR'])
r2 = input.string(title='MIN RANGE DAY CALC', defval='WEEK ATR', options=['DAY ATR', 'WEEK ATR', 'MONTH ATR', 'YEAR ATR'])
r3 = input.string(title='MIN RANGE MONTH CALC', defval='MONTH ATR', options=['DAY ATR', 'WEEK ATR', 'MONTH ATR', 'YEAR ATR'])
r4 = input.string(title='MIN RANGE YEAR CALC', defval='YEAR ATR', options=['DAY ATR', 'WEEK ATR', 'MONTH ATR', 'YEAR ATR'])
r = timeframe.isintraday ? r1 : timeframe.isdaily ? r2 : timeframe.isweekly ? r3 : timeframe.ismonthly ? r4 : na
rmultiplier = input.float(defval=2, title='DEPTH FOR MIN RANGE', minval=0.01)
range_1 = r == 'DAY ATR' ? datr * rmultiplier : r == 'WEEK ATR' ? watr * rmultiplier : r == 'MONTH ATR' ? matr * rmultiplier : r == 'YEAR ATR' ? yatr * rmultiplier : na

minrange(x, y) =>
    m = x - y >= range_1 ? 1 : 0
    m

//color2 = color.new(#1976d2,0)
//color3 = color.new(color.orange,0)
color1 = color.new(color.orange, 50)
color2 = color.new(color.orange, 50)
color3 = color.new(color.orange, 0)

//input(defval="red",type=input.string,options=[black,gray,white,red,fuchsia,lime,orange,aqua,yellow])
showhist = input(defval=true, title='SHOW HISTORY')
iSHOWlast = input(title='SHOW LAST N BARS', defval=500)
showlvlchange = input(defval=false, title='SHOW HISTORY LEVELS CHANGE')  //and timeframe.isdwm
showline = input(defval=false, title='SHOW LINE')
showlabeldef = input(defval=true, title='SHOW LABEL def')
showlabelprice = input(defval=false, title='SHOW LABEL price')
//showlabelfibo = input(defval=true, title="SHOW LABEL fibonacci lvl", type=input.bool)
lwidth = input.int(defval=1, title='LINE WIDTH', minval=1)
ahead = input(defval=10, title='BAR AHEAD')  //, minval=1)
multi = 0.0  //ahead*timeframe.multiplier * (timeframe.isintraday ? 1000*60 : timeframe.isdaily ? 1000*60*60*24 : timeframe.isweekly ? 1000*60*60*24*7 : timeframe.ismonthly ? 1000*60*60*24*30 : na)
space0 = ''
space = '           '
//space = '                                '
space1 = space + space0
space2 = space1 + space
space3 = space2 + space
space4 = space3 + space
space5 = space4 + space

autofibo(UP, DOWN, EUP, CAP1, CAP2, CAP3, COLOR1, COLOR2, COLOR3) =>
    for _j = 0 to 55 by 1
        line.new(bar_index, open, bar_index, open)
        //label.new(bar_index, open, "", color=#ffffffff)
    a = minrange(UP, DOWN) ? (UP - DOWN) * phi + DOWN : na
    b = minrange(UP, DOWN) ? (UP - DOWN) * (1 - phi) + DOWN : na
    extt = input.string(title='EXTENSION TYPE', defval='less', options=['less', 'more'])
    c = minrange(UP, DOWN) ? extt == 'less' ? (UP - DOWN) * (1 - phi) + UP : (UP - DOWN) * phi + UP : na
    c := na
    //space=" "
    string texta1 = showlabelprice ? '' + str.tostring(a, '#.##') + '' : na
    string textb1 = showlabelprice ? '' + str.tostring(b, '#.##') + '' : na
    string textc1 = showlabelprice ? '' + str.tostring(c, '#.##') + '' : na
    string texta2 = showlabeldef ? CAP1 + ' ' : na
    string textb2 = showlabeldef ? CAP2 + ' ' : na
    string textc2 = showlabeldef ? CAP3 + ' ' : na
    string texta = space2 + texta2 + texta1
    string textb = space2 + textb2 + textb1
    string textc = space2 + textc2 + textc1
    alabel = a and barstate.islast ? label.new(time, a, text=texta, style=label.style_none, xloc=xloc.bar_time, textcolor=COLOR1, size=size.normal, textalign=text.align_right) : na
    label.delete(alabel[1])
    blabel = b and barstate.islast ? label.new(time, b, text=textb, style=label.style_none, xloc=xloc.bar_time, textcolor=COLOR2, size=size.normal, textalign=text.align_right) : na
    label.delete(blabel[1])
    clabel = c and barstate.islast ? label.new(time, c, text=textc, style=label.style_none, xloc=xloc.bar_time, textcolor=COLOR3, size=size.normal, textalign=text.align_right) : na
    label.delete(clabel[1])
    aline = a and showline and barstate.islast ? showhist ? line.new(time, a, time + 1, a, xloc=xloc.bar_time, color=COLOR1, style=line.style_dotted, extend=extend.right) : line.new(time, a, time + 1, a, xloc=xloc.bar_time, color=COLOR1, style=line.style_dotted, extend=extend.both) : na
    line.delete(aline[1])
    bline = b and showline and barstate.islast ? showhist ? line.new(time, b, time + 1, b, xloc=xloc.bar_time, color=COLOR2, style=line.style_dotted, extend=extend.right) : line.new(time, b, time + 1, b, xloc=xloc.bar_time, color=COLOR2, style=line.style_dotted, extend=extend.both) : na
    line.delete(bline[1])
    cline = c and showline and barstate.islast ? showhist ? line.new(time, c, time + 1, c, xloc=xloc.bar_time, color=COLOR3, style=line.style_dotted, extend=extend.right) : line.new(time, c, time + 1, c, xloc=xloc.bar_time, color=COLOR3, style=line.style_dotted, extend=extend.both) : na
    line.delete(cline[1])
    [a, b, c, UP, DOWN, CAP1, CAP2, CAP3, COLOR1, COLOR2, COLOR3]

autotier(PHI1, PHI2, PHI3, UP, DOWN, CAP1, CAP2, CAP3, COLOR1, COLOR2, COLOR3) =>
    if close > PHI1
        up = UP
        down = PHI1
        eup = PHI3
        //cap1 = "A"+CAP1//+"⬆"上中下
        //cap2 = "A"+CAP2//+"⬆"上中下
        cap1 = CAP1 + 'a'  //"∾"//"→"//+CAP2//+"⬆"
        cap2 = CAP2 + 'a'  //"∾"//"→"//+CAP2//+"⬆"
        cap3 = CAP3 + 'a'  //"∾"//"→"//+CAP2//+"⬆"
        colour1 = COLOR1
        colour2 = COLOR2
        colour3 = COLOR3
        [phi1, phi2, phi3, xx1, xx2, xx3, xx4, xx5, xx6, xx7, xx8] = autofibo(up, down, eup, cap1, cap2, cap3, colour1, colour2, colour3)
        [phi1, phi2, phi3, up, down, cap1, cap2, cap3, colour1, colour2, colour3]
    else if close < PHI2
        up = PHI2
        down = DOWN
        eup = PHI3
        //cap1 = "E"+CAP1//+"⬇"上中下
        //cap2 = "E"+CAP2//+"⬇"上中下
        cap1 = CAP1 + 'c'  //"≋"//↯"//"⇶"//+CAP2//+"⬆
        cap2 = CAP2 + 'c'  //"≋"//↯"//"⇶"//+CAP2//+"⬆
        cap3 = CAP3 + 'c'  //"≋"//↯"//"⇶"//+CAP2//+"⬆
        colour1 = COLOR1
        colour2 = COLOR2
        colour3 = COLOR3
        [phi1, phi2, phi3, xx1, xx2, xx3, xx4, xx5, xx6, xx7, xx8] = autofibo(up, down, eup, cap1, cap2, cap3, colour1, colour2, colour3)
        [phi1, phi2, phi3, up, down, cap1, cap2, cap3, colour1, colour2, colour3]
    else if close >= PHI2 and close <= PHI1
        up = PHI1
        down = PHI2
        eup = PHI3
        //cap1 = "Æ"+CAP1//+"♱"上中下±
        //cap2 = "Æ"+CAP2//+"♱"ÆÆAE±
        cap1 = CAP1 + 'b'  //"≍"//"⇉"//+CAP2//+"⬆"
        cap2 = CAP2 + 'b'  //"≍"//"⇉"//+CAP2//+"⬆" 
        cap3 = CAP3 + 'b'  //"≋"//↯"//"⇶"//+CAP2//+"⬆
        colour1 = COLOR1
        colour2 = COLOR2
        colour3 = COLOR3
        [phi1, phi2, phi3, xx1, xx2, xx3, xx4, xx5, xx6, xx7, xx8] = autofibo(up, down, eup, cap1, cap2, cap3, colour1, colour2, colour3)
        [phi1, phi2, phi3, up, down, cap1, cap2, cap3, colour1, colour2, colour3]

fx(X) =>
    showhist ? ta.change(X) and not showlvlchange ? na : X : na

show_labels = input(title='Show labels ATH ATL?', defval=true)
var ATL_label = show_labels ? label.new(bar_index, low, 'ATL', color=color.black, textcolor=color.orange, style=label.style_label_upper_right, size=size.small, tooltip='ALL TIME HIGH') : na
var ATH_label = show_labels ? label.new(bar_index, high, 'ATH', color=color.black, textcolor=color.orange, style=label.style_label_lower_right, size=size.small, tooltip='ALL TIME LOW') : na
if ATL < ATL[1] and show_labels
    label.set_xy(ATL_label, bar_index, ATL * 1.00)
if ATH > ATH[1] and show_labels
    label.set_xy(ATH_label, bar_index, ATH * 1.00)

char1 = input(defval='↥', title="character for higher segment separator")  //"↥↥"//⬆"//⬆
char2 = input(defval='↧', title="character for lower segment separator")  //"↧"//⬇"//"
char3 = '+'  //"↧"//⬇"//"
up = ATH
down = 0.0
down := input(title='use ZERO for down point instead ALL TIME LOW?', defval=true , tooltip='This is my insight, never seen before, give a try') ? 0 : ATL
eup = 0.0
//eup := (up-down)*(phi)+up
[a, b, c, d, e, f, g, h, i, j, k] = autofibo(up, down, eup, char1, char2, char3, color1, color2, color3)
[a1, b1, c1, d1, e1, f1, g1, h1, i1, j1, k1] = autotier(a, b, c, d, e, f, g, h, i, j, k)
[a2, b2, c2, d2, e2, f2, g2, h2, i2, j2, k2] = autotier(a1, b1, c1, d1, e1, f1, g1, h1, i1, j1, k1)
[a3, b3, c3, d3, e3, f3, g3, h3, i3, j3, k3] = autotier(a2, b2, c2, d2, e2, f2, g2, h2, i2, j2, k2)
[a4, b4, c4, d4, e4, f4, g4, h4, i4, j4, k4] = autotier(a3, b3, c3, d3, e3, f3, g3, h3, i3, j3, k3)
[a5, b5, c5, d5, e5, f5, g5, h5, i5, j5, k5] = autotier(a4, b4, c4, d4, e4, f4, g4, h4, i4, j4, k4)
[a6, b6, c6, d6, e6, f6, g6, h6, i6, j6, k6] = autotier(a5, b5, c5, d5, e5, f5, g5, h5, i5, j5, k5)
[a7, b7, c7, d7, e7, f7, g7, h7, i7, j7, k7] = autotier(a6, b6, c6, d6, e6, f6, g6, h6, i6, j6, k6)
[a8, b8, c8, d8, e8, f8, g8, h8, i8, j8, k8] = autotier(a7, b7, c7, d7, e7, f7, g7, h7, i7, j7, k7)
[a9, b9, c9, d9, e9, f9, g9, h9, i9, j9, k9] = autotier(a8, b8, c8, d8, e8, f8, g8, h8, i8, j8, k8)
[a10, b10, c10, d10, e10, f10, g10, h10, i10, j10, k10] = autotier(a9, b9, c9, d9, e9, f9, g9, h9, i9, j9, k9)
showup = input(title='Show up point?', defval=true)
showdown = input(title='Show down point?', defval=true)

plot(showup ? fx(up) : na, color=color1, linewidth=lwidth * 2, style=plot.style_linebr, show_last=iSHOWlast)
plot(showdown ? fx(down) : na, color=color2, linewidth=lwidth * 2, style=plot.style_linebr, show_last=iSHOWlast)

plot(fx(a), color=color1, linewidth=lwidth * 1, style=plot.style_linebr, show_last=iSHOWlast)
plot(fx(b), color=color2, linewidth=lwidth * 1, style=plot.style_linebr, show_last=iSHOWlast)
//plot(fx(c),color=color3,linewidth=lwidth*1,style=plot.style_linebr, show_last=iSHOWlast)
plot(fx(a1), color=color1, linewidth=lwidth, style=plot.style_linebr, show_last=iSHOWlast)
plot(fx(b1), color=color2, linewidth=lwidth, style=plot.style_linebr, show_last=iSHOWlast)
//plot(fx(c1),color=color3,linewidth=lwidth,style=plot.style_linebr, show_last=iSHOWlast)
plot(fx(a2), color=color1, linewidth=lwidth, style=plot.style_linebr, show_last=iSHOWlast)
plot(fx(b2), color=color2, linewidth=lwidth, style=plot.style_linebr, show_last=iSHOWlast)
//plot(fx(c2),color=color3,linewidth=lwidth,style=plot.style_linebr, show_last=iSHOWlast)
plot(fx(a3), color=color1, linewidth=lwidth, style=plot.style_linebr, show_last=iSHOWlast)
plot(fx(b3), color=color2, linewidth=lwidth, style=plot.style_linebr, show_last=iSHOWlast)
//plot(fx(c3),color=color3,linewidth=lwidth,style=plot.style_linebr, show_last=iSHOWlast)
plot(fx(a4), color=color1, linewidth=lwidth, style=plot.style_linebr, show_last=iSHOWlast)
plot(fx(b4), color=color2, linewidth=lwidth, style=plot.style_linebr, show_last=iSHOWlast)
//plot(fx(c4),color=color3,linewidth=lwidth,style=plot.style_linebr, show_last=iSHOWlast)
plot(fx(a5), color=color1, linewidth=lwidth, style=plot.style_linebr, show_last=iSHOWlast)
plot(fx(b5), color=color2, linewidth=lwidth, style=plot.style_linebr, show_last=iSHOWlast)
//plot(fx(c5),color=color3,linewidth=lwidth,style=plot.style_linebr, show_last=iSHOWlast)
plot(fx(a6), color=color1, linewidth=lwidth, style=plot.style_linebr, show_last=iSHOWlast)
plot(fx(b6), color=color2, linewidth=lwidth, style=plot.style_linebr, show_last=iSHOWlast)
//plot(fx(c6),color=color3,linewidth=lwidth,style=plot.style_linebr, show_last=iSHOWlast)
plot(fx(a7), color=color1, linewidth=lwidth, style=plot.style_linebr, show_last=iSHOWlast)
plot(fx(b7), color=color2, linewidth=lwidth, style=plot.style_linebr, show_last=iSHOWlast)
//plot(fx(c7),color=color3,linewidth=lwidth,style=plot.style_linebr, show_last=iSHOWlast)
plot(fx(a8), color=color1, linewidth=lwidth, style=plot.style_linebr, show_last=iSHOWlast)
plot(fx(b8), color=color2, linewidth=lwidth, style=plot.style_linebr, show_last=iSHOWlast)
//plot(fx(c8),color=color3,linewidth=lwidth,style=plot.style_linebr, show_last=iSHOWlast)
plot(fx(a9), color=color1, linewidth=lwidth, style=plot.style_linebr, show_last=iSHOWlast)
plot(fx(b9), color=color2, linewidth=lwidth, style=plot.style_linebr, show_last=iSHOWlast)
//plot(fx(c9),color=color3,linewidth=lwidth,style=plot.style_linebr, show_last=iSHOWlast)
plot(fx(a10), color=color1, linewidth=lwidth, style=plot.style_linebr, show_last=iSHOWlast)
plot(fx(b10), color=color2, linewidth=lwidth, style=plot.style_linebr, show_last=iSHOWlast)
//plot(fx(c10),color=color3,linewidth=lwidth,style=plot.style_linebr, show_last=iSHOWlast)

