merge this two indicator for tradingview. //@version=5
//The Average Daily Range is a simple concept, calculated as the difference between daily highs and lows averaged over some period.
//This indicator uses that range in conjunction with Fibonacci ratios to create zones centered on the day's open that tends to act as areas of support and resistance.
//
//A simpler version of this concept can be seen in my other script, Average Daily Range Zones,
//which does not include the Fibonacci ratio zones.
//
//Thanks to @Hank Hill for the original idea and ToS script this is based on, and
//TV for the feedback and support on the use of the security() function.
//Also thanks to @GoldenCross for the Fibonacci obsession.

//For future reference; an sma() on a security return is not the same as a security() with an sma argument.
// i.e., security(tickerid, 'D', sma(dayRange, length)) != sma(security(tickerid, 'D', dayRange), length)

//Includes TradingView's own f_security function for better historical data.

indicator(' Weekly Fib Levels ', 'Weekly Fib Levels', overlay=true)
firstLength = input(50, title='ADR Calculation Period (Days)')
calcType = input.string("Basic", title = "Fibs Calculation", options=["Basic", "Standard Deviation", "Variance"])

showFib1 = input.bool(true, title='', group='Fibonacci Levels', inline='1')
fibLevel1 = input.float(0.25, title='', group='Fibonacci Levels', inline='1')
fib1Color = input.color(#F44336, title='', group='Fibonacci Levels', inline='1')
showFib1_line = input.bool(true, title='Line', group='Fibonacci Levels', inline='1')
fib1Alert = input.bool(false, title='Alert', group='Fibonacci Levels', inline='1')

showFib2 = input.bool(false, title='', group='Fibonacci Levels', inline='2')
fibLevel2 = input.float(0.382, title='', group='Fibonacci Levels', inline='2')
fib2Color = input.color(#81C784, title='', group='Fibonacci Levels', inline='2')
showFib2_line = input.bool(false, title='Line', group='Fibonacci Levels', inline='2')
fib2Alert = input.bool(false, title='Alert', group='Fibonacci Levels', inline='2')

showFib3 = input.bool(true, title='', group='Fibonacci Levels', inline='3')
fibLevel3 = input.float(0.77, title='', group='Fibonacci Levels', inline='3')
fib3Color = input.color(#4CAF50, title='', group='Fibonacci Levels', inline='3')
showFib3_line = input.bool(false, title='Line', group='Fibonacci Levels', inline='3')
fib3Alert = input.bool(false, title='Alert', group='Fibonacci Levels', inline='3')

showFib4 = input.bool(false, title='', group='Fibonacci Levels', inline='4')
fibLevel4 = input.float(0.618, title='', group='Fibonacci Levels', inline='4')
fib4Color = input.color(#009688, title='', group='Fibonacci Levels', inline='4')
showFib4_line = input.bool(false, title='Line', group='Fibonacci Levels', inline='4')
fib4Alert = input.bool(false, title='Alert', group='Fibonacci Levels', inline='4')

showFib5 = input.bool(true, title='', group='Fibonacci Levels', inline='5')
fibLevel5 = input.float(0.786, title='', group='Fibonacci Levels', inline='5')
fib5Color = input.color(color.rgb(72, 22, 252), title='', group='Fibonacci Levels', inline='5')
showFib5_line = input.bool(true, title='Line', group='Fibonacci Levels', inline='5')
fib5Alert = input.bool(false, title='Alert', group='Fibonacci Levels', inline='5')

showFib6 = input.bool(true, title='', group='Fibonacci Levels', inline='6')
fibLevel6 = input.int(1, title='', group='Fibonacci Levels', inline='6')
fib6Color = input.color(color.rgb(83, 49, 255), title='', group='Fibonacci Levels', inline='6')
showFib6_line = input.bool(true, title='Line', group='Fibonacci Levels', inline='6')
fib6Alert = input.bool(true, title='Alert', group='Fibonacci Levels', inline='6')

showFib7 = input.bool(false, title='', inline='7', group='Overridden by Dynamic Mode')
fibLevel7 = input.float(1.618, title='', inline='7', group='Overridden by Dynamic Mode')
fib7Color = input.color(#2196F3, title='', inline='7', group='Overridden by Dynamic Mode')
showFib7_line = input.bool(false, title='Line', group='Overridden by Dynamic Mode', inline='7')
fib7Alert = input.bool(false, title='Alert', group='Overridden by Dynamic Mode', inline='7')

showFib8 = input.bool(false, title='', inline='8', group='Overridden by Dynamic Mode')
fibLevel8 = input.float(2.618, title='', inline='8', group='Overridden by Dynamic Mode')
fib8Color = input.color(#F44336, title='', inline='8', group='Overridden by Dynamic Mode')
showFib8_line = input.bool(false, title='Line', group='Overridden by Dynamic Mode', inline='8')
fib8Alert = input.bool(false, title='Alert', group='Overridden by Dynamic Mode', inline='8')

showFib9 = input.bool(false, title='', inline='9', group='Overridden by Dynamic Mode')
fibLevel9 = input.float(3.618, title='', inline='9', group='Overridden by Dynamic Mode')
fib9Color = input.color(#9C27B0, title='', inline='9', group='Overridden by Dynamic Mode')
showFib9_line = input.bool(false, title='Line', group='Overridden by Dynamic Mode', inline='9')
fib9Alert = input.bool(false, title='Alert', group='Overridden by Dynamic Mode', inline='9')

showFib10 = input.bool(false, title='', inline='10', group='Overridden by Dynamic Mode')
fibLevel10 = input.float(4.236, title='', inline='10', group='Overridden by Dynamic Mode')
fib10Color = input.color(#E91E63, title='', inline='10', group='Overridden by Dynamic Mode')
showFib10_line = input.bool(false, title='Line', group='Overridden by Dynamic Mode', inline='10')
fib10Alert = input.bool(false, title='Alert', group='Overridden by Dynamic Mode', inline='10')

dynamic = input(false, title='Dynamically Show Zones Outside ADR')


openColor = input.color(#ffffff, title='Open Plot Color')
fibScale = input.float(1, step = 0.1, title='Fibonacci Scale', tooltip = "Allows custom scale of the fib levels; 1.0 is default and puts the 1.0 fib level at ADR.")
lineVisibility = 100 - input.int(70, minval=0, maxval=100, step=5, title='Line Visibility % (Transparency)')
fillVisibility = 100 - input.int(20, minval=0, maxval=100, step=5, title='Fill Visibility % (Transparency)')
scaledVisibility = fillVisibility + 0 * ((100 - fillVisibility) / 100)
scaledLineVisibility = lineVisibility + 0 * ((100 - lineVisibility) / 100)
showPrices = input(false, title='Show Price Lines')
showZonesFill = input(true, title='Fill Zones')
showZonesOnly = input(false, title='Show Zones Only')
showTodayOnly = input(false, title='Plot For Today Only')
disableNonIntraday = input(true, title='Disable On Daily or Above')

isToday = false
if year(timenow) == year(time) and month(timenow) == month(time) and dayofmonth(timenow) == dayofmonth(time)
    isToday := true
if not showTodayOnly
    isToday := true
    
i_timeframe = input.timeframe(title='Custom Timeframe', defval='D')
bool i_reminder = false
var string ticker = syminfo.tickerid

bool enabled = true
if (disableNonIntraday and not timeframe.isintraday)
    enabled := false
    
//▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼
f_security(_sym, _res, _src) =>
    request.security(_sym, _res, _src[barstate.isrealtime ? 1 : 0], barmerge.gaps_off, lookahead=barmerge.lookahead_off)[barstate.isrealtime ? 0 : 1]
f_security_lookahead(_sym, _res, _src) =>
    request.security(_sym, _res, _src[barstate.isrealtime ? 1 : 0], barmerge.gaps_off, lookahead=barmerge.lookahead_on)[barstate.isrealtime ? 0 : 1]
//▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲

dayOpen = request.security(ticker, i_timeframe, open, barmerge.gaps_off, barmerge.lookahead_on)
dayHigh = f_security(ticker, i_timeframe, high)
dayLow = f_security(ticker, i_timeframe, low)


//adr_high_1 = plot(enabled and isToday ? dayOpen + avgRange_1 / 2 : na, color=dayOpen != dayOpen[1] ? na : color.new(color.white, 30), linewidth=1, title="ADR High", style=plot.style_linebr)
//adr_low_1 = plot(enabled and isToday ? dayOpen - avgRange_1 / 2 : na, color=dayOpen != dayOpen[1] ? na : color.new(color.white, 30), linewidth=1, title="ADR Low", style=plot.style_linebr)

dayRange = dayHigh - dayLow
avgRange_1 = f_security(ticker, i_timeframe, ta.sma(dayRange, firstLength))
dailyRange_stDev    = f_security(ticker, i_timeframe, ta.stdev(dayRange, firstLength))
dailyRange_variance = f_security(ticker, i_timeframe, ta.variance(dayRange, firstLength))
dailyRange_calc     = calcType == "Variance" ? dailyRange_variance : dailyRange_stDev

//Fib Levels

fibRange_1 = avgRange_1 * fibLevel1 * fibScale / 2
fibRange_2 = avgRange_1 * fibLevel2 * fibScale / 2
fibRange_3 = avgRange_1 * fibLevel3 * fibScale / 2
fibRange_4 = avgRange_1 * fibLevel4 * fibScale / 2
fibRange_5 = avgRange_1 * fibLevel5 * fibScale / 2
fibRange_6 = avgRange_1 * fibLevel6 * fibScale / 2
fibRange_7 = avgRange_1 * fibLevel7 * fibScale / 2
fibRange_8 = avgRange_1 * fibLevel8 * fibScale / 2
fibRange_9 = avgRange_1 * fibLevel9 * fibScale / 2
fibRange_10 = avgRange_1 * fibLevel10 * fibScale / 2

if (calcType != "Basic")
    fibRange_1  := dailyRange_calc * fibLevel1 * fibScale / 2
    fibRange_2  := dailyRange_calc * fibLevel2 * fibScale / 2
    fibRange_3  := dailyRange_calc * fibLevel3 * fibScale / 2
    fibRange_4  := dailyRange_calc * fibLevel4 * fibScale / 2
    fibRange_5  := dailyRange_calc * fibLevel5 * fibScale / 2
    fibRange_6  := dailyRange_calc * fibLevel6 * fibScale / 2
    fibRange_7  := dailyRange_calc * fibLevel7 * fibScale / 2
    fibRange_8  := dailyRange_calc * fibLevel8 * fibScale / 2
    fibRange_9  := dailyRange_calc * fibLevel9 * fibScale / 2
    fibRange_10 := dailyRange_calc * fibLevel10 * fibScale / 2

hod = request.security(ticker, i_timeframe, high, barmerge.gaps_off, barmerge.lookahead_on)
lod = request.security(ticker, i_timeframe, low, barmerge.gaps_off, barmerge.lookahead_on)

showFib2Low = showFib2
showFib3Low = showFib3
showFib4Low = showFib4
showFib5Low = showFib5
showFib6Low = showFib6
showFib7Low = showFib7
showFib8Low = showFib8
showFib9Low = showFib9
showFib10Low = showFib10

if dynamic
    showFib7 := hod > dayOpen + fibRange_6
    showFib7Low := lod < dayOpen - fibRange_6
    showFib8 := hod > dayOpen + fibRange_7
    showFib8Low := lod < dayOpen - fibRange_7
    showFib9 := hod > dayOpen + fibRange_8
    showFib9Low := lod < dayOpen - fibRange_8
    showFib10 := hod > dayOpen + fibRange_9
    showFib10Low := lod < dayOpen - fibRange_9
    

dayOpenPlot = plot(enabled and isToday ? dayOpen : na, color=showZonesOnly or dayOpen != dayOpen[1] ? na : color.new(openColor, scaledLineVisibility), trackprice=showPrices, title="Day Open", style=plot.style_linebr)


adr_fib_high_1  = plot(enabled and isToday and showFib1 ? dayOpen + fibRange_1 : na, color=showZonesOnly or not showFib1_line or dayOpen != dayOpen[1] ? na : color.new(fib1Color, scaledLineVisibility), trackprice=showPrices, title="ADR Fib 1 High", style=plot.style_linebr)
adr_fib_low_1   = plot(enabled and isToday and showFib1 ? dayOpen - fibRange_1 : na, color=showZonesOnly or not showFib1_line or dayOpen != dayOpen[1] ? na : color.new(fib1Color, scaledLineVisibility), trackprice=showPrices, title="ADR Fib 1 Low", style=plot.style_linebr)
adr_fib_high_2  = plot(enabled and isToday and showFib2 ? dayOpen + fibRange_2 : na, color=showZonesOnly or not showFib2_line or dayOpen != dayOpen[1] ? na : color.new(fib2Color, scaledLineVisibility), trackprice=showPrices, title="ADR Fib 2 High", style=plot.style_linebr)
adr_fib_low_2   = plot(enabled and isToday and showFib2 ? dayOpen - fibRange_2 : na, color=showZonesOnly or not showFib2_line or dayOpen != dayOpen[1] ? na : color.new(fib2Color, scaledLineVisibility), trackprice=showPrices, title="ADR Fib 2 Low", style=plot.style_linebr)
adr_fib_high_3  = plot(enabled and isToday and showFib3 ? dayOpen + fibRange_3 : na, color=showZonesOnly or not showFib3_line or dayOpen != dayOpen[1] ? na : color.new(fib3Color, scaledLineVisibility), trackprice=showPrices, title="ADR Fib 3 High", style=plot.style_linebr)
adr_fib_low_3   = plot(enabled and isToday and showFib3 ? dayOpen - fibRange_3 : na, color=showZonesOnly or not showFib3_line or dayOpen != dayOpen[1] ? na : color.new(fib3Color, scaledLineVisibility), trackprice=showPrices, title="ADR Fib 3 Low", style=plot.style_linebr)
adr_fib_high_4  = plot(enabled and isToday and showFib4 ? dayOpen + fibRange_4 : na, color=showZonesOnly or not showFib4_line or dayOpen != dayOpen[1] ? na : color.new(fib4Color, scaledLineVisibility), trackprice=showPrices, title="ADR Fib 4 High", style=plot.style_linebr)
adr_fib_low_4   = plot(enabled and isToday and showFib4 ? dayOpen - fibRange_4 : na, color=showZonesOnly or not showFib4_line or dayOpen != dayOpen[1] ? na : color.new(fib4Color, scaledLineVisibility), trackprice=showPrices, title="ADR Fib 4 Low", style=plot.style_linebr)
adr_fib_high_5  = plot(enabled and isToday and showFib5 ? dayOpen + fibRange_5 : na, color=showZonesOnly or not showFib5_line or dayOpen != dayOpen[1] ? na : color.new(fib5Color, scaledLineVisibility), trackprice=showPrices, title="ADR Fib 5 High", style=plot.style_linebr)
adr_fib_low_5   = plot(enabled and isToday and showFib5 ? dayOpen - fibRange_5 : na, color=showZonesOnly or not showFib5_line or dayOpen != dayOpen[1] ? na : color.new(fib5Color, scaledLineVisibility), trackprice=showPrices, title="ADR Fib 5 Low", style=plot.style_linebr)
adr_fib_high_6  = plot(enabled and isToday and showFib6 ? dayOpen + fibRange_6 : na, color=showZonesOnly or not showFib6_line or dayOpen != dayOpen[1] ? na : color.new(fib6Color, scaledLineVisibility), trackprice=showPrices, title="ADR Fib 6 High", style=plot.style_linebr)
adr_fib_low_6   = plot(enabled and isToday and showFib6 ? dayOpen - fibRange_6 : na, color=showZonesOnly or not showFib6_line or dayOpen != dayOpen[1] ? na : color.new(fib6Color, scaledLineVisibility), trackprice=showPrices, title="ADR Fib 6 Low", style=plot.style_linebr)

adr_fib_high_7  = plot(enabled and isToday and showFib7     ? dayOpen + fibRange_7  : na, color=showZonesOnly or not showFib7_line  or dayOpen != dayOpen[1] ? na : color.new(fib7Color,  scaledLineVisibility), trackprice=showPrices, title="ADR Fib 7 High", style=plot.style_linebr)
adr_fib_low_7   = plot(enabled and isToday and showFib7Low  ? dayOpen - fibRange_7  : na, color=showZonesOnly or not showFib7_line  or dayOpen != dayOpen[1] ? na : color.new(fib7Color,  scaledLineVisibility), trackprice=showPrices, title="ADR Fib 7 Low", style=plot.style_linebr)
adr_fib_high_8  = plot(enabled and isToday and showFib8     ? dayOpen + fibRange_8  : na, color=showZonesOnly or not showFib8_line  or dayOpen != dayOpen[1] ? na : color.new(fib8Color,  scaledLineVisibility), trackprice=showPrices, title="ADR Fib 8 High", style=plot.style_linebr)
adr_fib_low_8   = plot(enabled and isToday and showFib8Low  ? dayOpen - fibRange_8  : na, color=showZonesOnly or not showFib8_line  or dayOpen != dayOpen[1] ? na : color.new(fib8Color,  scaledLineVisibility), trackprice=showPrices, title="ADR Fib 8 Low", style=plot.style_linebr)
adr_fib_high_9  = plot(enabled and isToday and showFib9     ? dayOpen + fibRange_9  : na, color=showZonesOnly or not showFib9_line  or dayOpen != dayOpen[1] ? na : color.new(fib9Color,  scaledLineVisibility), trackprice=showPrices, title="ADR Fib 9 High", style=plot.style_linebr)
adr_fib_low_9   = plot(enabled and isToday and showFib9Low  ? dayOpen - fibRange_9  : na, color=showZonesOnly or not showFib9_line  or dayOpen != dayOpen[1] ? na : color.new(fib9Color,  scaledLineVisibility), trackprice=showPrices, title="ADR Fib 9 Low", style=plot.style_linebr)
adr_fib_high_10 = plot(enabled and isToday and showFib10    ? dayOpen + fibRange_10 : na, color=showZonesOnly or not showFib10_line or dayOpen != dayOpen[1] ? na : color.new(fib10Color, scaledLineVisibility), trackprice=showPrices, title="ADR Fib 10 High", style=plot.style_linebr)
adr_fib_low_10  = plot(enabled and isToday and showFib10Low ? dayOpen - fibRange_10 : na, color=showZonesOnly or not showFib10_line or dayOpen != dayOpen[1] ? na : color.new(fib10Color, scaledLineVisibility), trackprice=showPrices, title="ADR Fib 10 Low", style=plot.style_linebr)


fill(dayOpenPlot,    adr_fib_high_1,  color=not showZonesFill or dayOpen != dayOpen[1] ? na : color.new(fib1Color, scaledVisibility))
fill(dayOpenPlot,    adr_fib_low_1,   color=not showZonesFill or dayOpen != dayOpen[1] ? na : color.new(fib1Color, scaledVisibility))
fill(adr_fib_high_1, adr_fib_high_2,  color=not showZonesFill or dayOpen != dayOpen[1] ? na : color.new(fib2Color, scaledVisibility))
fill(adr_fib_low_1,  adr_fib_low_2,   color=not showZonesFill or dayOpen != dayOpen[1] ? na : color.new(fib2Color, scaledVisibility))
fill(adr_fib_high_2, adr_fib_high_3,  color=not showZonesFill or dayOpen != dayOpen[1] ? na : color.new(fib3Color, scaledVisibility))
fill(adr_fib_low_2,  adr_fib_low_3,   color=not showZonesFill or dayOpen != dayOpen[1] ? na : color.new(fib3Color, scaledVisibility))
fill(adr_fib_high_3, adr_fib_high_4,  color=not showZonesFill or dayOpen != dayOpen[1] ? na : color.new(fib4Color, scaledVisibility))
fill(adr_fib_low_3,  adr_fib_low_4,   color=not showZonesFill or dayOpen != dayOpen[1] ? na : color.new(fib4Color, scaledVisibility))
fill(adr_fib_high_4, adr_fib_high_5,  color=not showZonesFill or dayOpen != dayOpen[1] ? na : color.new(fib5Color, scaledVisibility))
fill(adr_fib_low_4,  adr_fib_low_5,   color=not showZonesFill or dayOpen != dayOpen[1] ? na : color.new(fib5Color, scaledVisibility))
fill(adr_fib_high_5, adr_fib_high_6,  color=not showZonesFill or dayOpen != dayOpen[1] ? na : color.new(fib6Color, scaledVisibility))
fill(adr_fib_low_5,  adr_fib_low_6,   color=not showZonesFill or dayOpen != dayOpen[1] ? na : color.new(fib6Color, scaledVisibility))
fill(adr_fib_high_6, adr_fib_high_7,  color=not showZonesFill or dayOpen != dayOpen[1] ? na : color.new(fib7Color, scaledVisibility))
fill(adr_fib_low_6,  adr_fib_low_7,   color=not showZonesFill or dayOpen != dayOpen[1] ? na : color.new(fib7Color, scaledVisibility))
fill(adr_fib_high_7, adr_fib_high_8,  color=not showZonesFill or dayOpen != dayOpen[1] ? na : color.new(fib8Color, scaledVisibility))
fill(adr_fib_low_7,  adr_fib_low_8,   color=not showZonesFill or dayOpen != dayOpen[1] ? na : color.new(fib8Color, scaledVisibility))
fill(adr_fib_high_8, adr_fib_high_9,  color=not showZonesFill or dayOpen != dayOpen[1] ? na : color.new(fib9Color, scaledVisibility))
fill(adr_fib_low_8,  adr_fib_low_9,   color=not showZonesFill or dayOpen != dayOpen[1] ? na : color.new(fib9Color, scaledVisibility))
fill(adr_fib_high_9, adr_fib_high_10, color=not showZonesFill or dayOpen != dayOpen[1] ? na : color.new(fib10Color, scaledVisibility))
fill(adr_fib_low_9,  adr_fib_low_10,  color=not showZonesFill or dayOpen != dayOpen[1] ? na : color.new(fib10Color, scaledVisibility))

f_fibCrossAlert(fib, fibName) =>
    if ta.cross(close, dayOpen + fib) or ta.cross(close, dayOpen - fib)
        alert('Price crossed: ' + fibName, alert.freq_once_per_bar)

if fib1Alert
    f_fibCrossAlert(fibRange_1, 'Fib Level ' + str.tostring(fibLevel1))
if fib2Alert
    f_fibCrossAlert(fibRange_2, 'Fib Level ' + str.tostring(fibLevel2))
if fib3Alert
    f_fibCrossAlert(fibRange_3, 'Fib Level ' + str.tostring(fibLevel3))
if fib4Alert
    f_fibCrossAlert(fibRange_4, 'Fib Level ' + str.tostring(fibLevel4))
if fib5Alert
    f_fibCrossAlert(fibRange_5, 'Fib Level ' + str.tostring(fibLevel5))
if fib6Alert
    f_fibCrossAlert(fibRange_6, 'Fib Level ' + str.tostring(fibLevel6))
if fib7Alert
    f_fibCrossAlert(fibRange_7, 'Fib Level ' + str.tostring(fibLevel7))
if fib8Alert
    f_fibCrossAlert(fibRange_8, 'Fib Level ' + str.tostring(fibLevel8))
if fib9Alert
    f_fibCrossAlert(fibRange_9, 'Fib Level ' + str.tostring(fibLevel9))
if fib10Alert
    f_fibCrossAlert(fibRange_10, 'Fib Level ' + str.tostring(fibLevel10))

// ————— Show warning or HTF reminder, if needed.) =>
f_tfReminderAndErrorCheck(_userSelectionOfTf, _tfReminder) =>
    // Get chart's TF.
    var float _chartTfInMinutes = timeframe.multiplier * (timeframe.isseconds ? 1. / 60 : timeframe.isminutes ? 1. : timeframe.isdaily ? 60. * 24 : timeframe.isweekly ? 60. * 24 * 7 : timeframe.ismonthly ? 60. * 24 * 30.4375 : na)

    // Get HTF.
    float _htfInMinutes = request.security(syminfo.tickerid, _userSelectionOfTf, _chartTfInMinutes)

    // Label.
    string _txt = ''
    var color _color = na
    if _chartTfInMinutes > _htfInMinutes
        // Chart TF is higher than user-selected TF.
        _txt := 'The chart\'s timeframe\nshould not be greater than ' + _userSelectionOfTf
        _color := color.red
        _color
        // Display reminder of HTF.
    else if _tfReminder
        _txt := _userSelectionOfTf
        _color := color.silver
        _color
    float _y = ta.lowest(50)[1]
    var label _lbl = label.new(bar_index, _y, _txt, xloc.bar_index, yloc.price, #00000000, label.style_label_left, _color, size.large, text.align_left)
    if barstate.islast and _txt != ''
        // Update label.
        label.set_xy(_lbl, bar_index, _y)
        label.set_text(_lbl, _txt)
        label.set_textcolor(_lbl, _color)
    [_chartTfInMinutes, _htfInMinutes]

f_tfReminderAndErrorCheck(i_timeframe, i_reminder)

   //@version=3

study(title="Magic MA", shorttitle="Magic MA", overlay=true, max_bars_back=2000)

// 
// Author:  JustUncleL
// Date:    10-July-2016
// Version: v3
// 
// Description:
// A trend following indicator that uses fast QQE crosses with Moving Averages
// for trend direction filtering. QQE or Qualitative Quantitative Estimation is based 
// on the relative strength index, but uses a smoothing technique as an additional 
// transformation.
//

// - INPUTS START
// Slow MA - type, source, length
type3   = input(defval="HullMA", title="Slow MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "ZEMA", "TMA", "SSMA"], type=string)
len3    = input(defval=57, title="Slow Length", minval=1)  // Changed slow length to 57
//
// QQE rsi Length, Smoothing, fast ATR factor, source
RSILen  = input(6,title='RSI Length')
SF      = input(3,title='RSI Smoothing Factor')
QQE     = input(2.618,title='Fast QQE Factor')
threshhold = input(10, title="RSI Threshhold")
//
filter  = input(false,title="Use Moving Average Filter")
dfilter = input(false, title="Use Trend Directional Filter" )
ushunt  = input(false, title="Use Shunt Alerts to prevent Repaint")
RSIsrc  = input(close,title="Source")
srcclose= RSIsrc
shunt  = ushunt ? (RSIsrc==open ? 0 : 1) : 0
// - INPUTS END

// - FUNCTIONS

// Returns MA input selection variant, default to SMA if blank or typo.
variant(type, src, len) =>
    v1 = sma(src, len)                                                  // Simple
    v2 = ema(src, len)                                                  // Exponential
    v3 = wma(src, len)                                                  // Weighted
    v4 = vwma(src, len)                                                 // Volume Weighted
    v5 = 0.0
    v5 := na(v5[1]) ? sma(src, len) : (v5[1] * (len - 1) + src) / len    // Smoothed
    v6 = 2 * v2 - ema(v2, len)                                          // Double Exponential
    v7 = 3 * (v2 - ema(v2, len)) + ema(ema(v2, len), len)               // Triple Exponential
    v8 = wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len)))   // Hull
    v11 = sma(sma(src,len),len)                                         // Triangular
    // SuperSmoother filter
    // © 2013  John F. Ehlers
    a1 = exp(-1.414*3.14159 / len)
    b1 = 2*a1*cos(1.414*3.14159 / len)
    c2 = b1
    c3 = (-a1)*a1
    c1 = 1 - c2 - c3
    v9 = 0.0
    v9 := c1*(src + nz(src[1])) / 2 + c2*nz(v9[1]) + c3*nz(v9[2])
    // Zero Lag Exponential
    e = ema(v1, len)
    v10 = v1+(v1-e)
    // return variant, defaults to SMA if input invalid.
    type=="EMA"?v2 : type=="WMA"?v3 : type=="VWMA"?v4 : type=="SMMA"?v5 : type=="DEMA"?v6 : type=="TEMA"?v7 : type=="HullMA"?v8 : type=="SSMA"?v9 : type=="ZEMA"?v10 : type=="TMA"? v11: v1

// - FUNCTIONS END

// - Fast ATR QQE
//
Wilders_Period = RSILen * 2 - 1
//
Rsi = rsi(RSIsrc,RSILen)
RSIndex = ema(Rsi, SF)
AtrRsi = abs(RSIndex[1] - RSIndex)
MaAtrRsi = ema(AtrRsi, Wilders_Period)
DeltaFastAtrRsi = ema(MaAtrRsi,Wilders_Period) * QQE
//
newshortband=  RSIndex + DeltaFastAtrRsi
newlongband= RSIndex - DeltaFastAtrRsi
longband = 0.0
shortband=0.0
trend = 0
longband:=RSIndex[1] > longband[1] and RSIndex > longband[1] ? max(longband[1],newlongband) : newlongband
shortband:=RSIndex[1] < shortband[1] and  RSIndex < shortband[1] ? min(shortband[1],newshortband) : newshortband
trend:=cross(RSIndex, shortband[1])? 1 : cross(longband[1], RSIndex) ? -1 : nz(trend[1],1)
FastAtrRsiTL = trend==1 ? longband : shortband

// - SERIES VARIABLES
// MA's
ma_slow = variant(type3, srcclose, len3)
//
// Thresh Hold channel Crosses give the BUY/SELL alerts.
QQEclong = 0
QQEcshort=0
QQEclong := RSIndex>(50+threshhold) ? na(QQEclong[1]) ? 1 : QQEclong[1]+1 : 0
QQEcshort := RSIndex<(50-threshhold) ? na(QQEcshort[1]) ? 1 : QQEcshort[1]+1 : 0

//
// Check Filtering.
QQEflong = (not filter or srcclose > ma_slow) 
QQEfshort = (not filter or srcclose < ma_slow)
//
// Get final BUY / SELL alert determination
buy = 0
sell = 0
buy := QQEclong>0 and QQEflong ? na(buy[1]) ? 1 : buy[1]+1 : 0
sell := QQEcshort>0 and QQEfshort ? na(sell[1]) ? 1 : sell[1]+1 : 0

// - SERIES VARIABLES END

// - PLOTTING
// Ma's
plot(ma_slow, title="MA Slow", color=blue, linewidth=2, transp=0)

// - PLOTTING END

// - ALERTING

// Removed QQE BUY/SELL alert plotting and alertcondition

// - ALERTING END

//EOF