Firestorm Trend Chaser
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © firestormtd

//@version=5
indicator('Firestorm Trend Chaser', overlay=true, format=format.price, precision=2, timeframe='')

Periods = input(title='ATR Period', defval=10)
src = input(hl2, title='Source')
Multiplier = input.float(title='ATR Multiplier', step=0.1, defval=3.0)
changeATR = input(title='Change ATR Calculation Method ?', defval=true)
showsignals = input(title='Show Buy/Sell Signals ?', defval=true)
highlighting = input(title='Highlighter On/Off ?', defval=true)
atr2 = ta.sma(ta.tr, Periods)
atr = changeATR ? ta.atr(Periods) : atr2
up = src - Multiplier * atr
up1 = nz(up[1], up)
up := close[1] > up1 ? math.max(up, up1) : up
dn = src + Multiplier * atr
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? math.min(dn, dn1) : dn
trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend
upPlot = plot(trend == 1 ? up : na, title='Up Trend', style=plot.style_linebr, linewidth=2, color=color.new(color.green, 0))
buySignal = trend == 1 and trend[1] == -1
plotshape(buySignal ? up : na, title='UpTrend Begins', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.green, 0))
plotshape(buySignal and showsignals ? up : na, title='Buy', text='Buy', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.green, 0), textcolor=color.new(color.white, 0))
dnPlot = plot(trend == 1 ? na : dn, title='Down Trend', style=plot.style_linebr, linewidth=2, color=color.new(color.red, 0))
sellSignal = trend == -1 and trend[1] == 1
plotshape(sellSignal ? dn : na, title='DownTrend Begins', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.red, 0))
plotshape(sellSignal and showsignals ? dn : na, title='Sell', text='Sell', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.red, 0), textcolor=color.new(color.white, 0))
mPlot = plot(ohlc4, title='', style=plot.style_circles, linewidth=0)
longFillColor = highlighting ? trend == 1 ? color.green : color.white : color.white
shortFillColor = highlighting ? trend == -1 ? color.red : color.white : color.white
fill(mPlot, upPlot, title='UpTrend Highligter', color=longFillColor, transp=90)
fill(mPlot, dnPlot, title='DownTrend Highligter', color=shortFillColor, transp=90)
alertcondition(buySignal, title='SuperTrend Buy', message='SuperTrend Buy!')
alertcondition(sellSignal, title='SuperTrend Sell', message='SuperTrend Sell!')
changeCond = trend != trend[1]
alertcondition(changeCond, title='SuperTrend Direction Change', message='SuperTrend has changed direction!')


//Inputs
avg_type = input.string("EMA", "Average Type", options=["EMA","SMA"])
annual_avg = input(252, "Annual Moving Avg Length")
month_step_down = input(21, "Monthly Step Down")

annual_ribbon = input.color(color.rgb(255, 255, 255, 88), "Annual Ribbon")
bull_ribbon = input.color(color.rgb(83, 224, 88, 68), "Bull Ribbon")
bear_ribbon = input.color(color.rgb(255, 82, 82, 73), "Bear Ribbon")
bull_fill = input.color(color.new(#62df66, 80), "Bull Fill")
bear_fill = input.color(color.new(#f76c6c, 88), "Bear Fill")

//Ribbion Colors
ribbon_color1 = annual_ribbon
ribbon_color2 = bull_ribbon
ribbon_color3 = bull_ribbon
ribbon_color4 = bull_ribbon
ribbon_color5 = bull_ribbon
ribbon_color6 = bull_ribbon
ribbon_color7 = bull_ribbon
ribbon_color8 = bull_ribbon
ribbon_color9 = bull_ribbon
ribbon_color10 = bull_ribbon
ribbon_color11 = bull_ribbon
ribbon_color12 = bull_ribbon

//Fill Color
fill_color1 = bull_fill
fill_color2 = bull_fill
fill_color3 = bull_fill
fill_color4 = bull_fill
fill_color5 = bull_fill
fill_color6 = bull_fill
fill_color7 = bull_fill
fill_color8 = bull_fill
fill_color9 = bull_fill
fill_color10 = bull_fill
fill_color11 = bull_fill

//Step Values
month_step_down1 = month_step_down
month_step_down2 = month_step_down*2
month_step_down3 = month_step_down*3
month_step_down4 = month_step_down*4
month_step_down5 = month_step_down*5
month_step_down6 = month_step_down*6
month_step_down7 = month_step_down*7
month_step_down8 = month_step_down*8
month_step_down9 = month_step_down*9
month_step_down10 = month_step_down*10
month_step_down11 = month_step_down*11

//Inital Avg plot values
avg1 = 0.0
avg2 = 0.0
avg3 = 0.0
avg4 = 0.0
avg5 = 0.0
avg6 = 0.0
avg7 = 0.0
avg8 = 0.0
avg9 = 0.0
avg10 = 0.0
avg11 = 0.0
avg12 = 0.0

// MA Values
if avg_type == "EMA"

    avg1 := ta.ema(close, annual_avg)
    
    if ta.ema(close,annual_avg-month_step_down1) < ta.ema(close,annual_avg)
        ribbon_color2 := bear_ribbon
    avg2 := ta.ema(close,annual_avg-month_step_down1)
    
    if ta.ema(close,annual_avg-month_step_down2) < ta.ema(close,annual_avg)
        ribbon_color3 := bear_ribbon
    avg3 := ta.ema(close,annual_avg-month_step_down2)
    
    if ta.ema(close,annual_avg-month_step_down3) < ta.ema(close,annual_avg)
        ribbon_color4 := bear_ribbon
    avg4 := ta.ema(close,annual_avg-month_step_down3)
    
    if ta.ema(close,annual_avg-month_step_down4) < ta.ema(close,annual_avg)
        ribbon_color5 := bear_ribbon
    avg5 := ta.ema(close,annual_avg-month_step_down4)
    
    if ta.ema(close,annual_avg-month_step_down5) < ta.ema(close,annual_avg)
        ribbon_color6 := bear_ribbon
    avg6 := ta.ema(close,annual_avg-month_step_down5)
    
    if ta.ema(close,annual_avg-month_step_down6) < ta.ema(close,annual_avg)
        ribbon_color7 := bear_ribbon
    avg7 := ta.ema(close,annual_avg-month_step_down6)
    
    if ta.ema(close,annual_avg-month_step_down7) < ta.ema(close,annual_avg)
        ribbon_color8 := bear_ribbon
    avg8 := ta.ema(close,annual_avg-month_step_down7)
    
    if ta.ema(close,annual_avg-month_step_down8) < ta.ema(close,annual_avg)
        ribbon_color9 := bear_ribbon
    avg9 := ta.ema(close,annual_avg-month_step_down8)
    
    if ta.ema(close,annual_avg-month_step_down9) < ta.ema(close,annual_avg)
        ribbon_color10 := bear_ribbon
    avg10 := ta.ema(close,annual_avg-month_step_down9)

    if ta.ema(close,annual_avg-month_step_down10) < ta.ema(close,annual_avg)
        ribbon_color11 := bear_ribbon
    avg11 := ta.ema(close,annual_avg-month_step_down10)
    
    if ta.ema(close,annual_avg-month_step_down11) < ta.ema(close,annual_avg)
        ribbon_color12 := bear_ribbon
    avg12 := ta.ema(close,annual_avg-month_step_down11)

else
   
    avg1 := ta.sma(close, annual_avg)
    
    if ta.sma(close,annual_avg-month_step_down1) < ta.sma(close,annual_avg)
        ribbon_color2 := bear_ribbon
    avg2 := ta.sma(close,annual_avg-month_step_down1)
    
    if ta.sma(close,annual_avg-month_step_down2) < ta.sma(close,annual_avg)
        ribbon_color3 := bear_ribbon
    avg3 := ta.sma(close,annual_avg-month_step_down2)
    
    if ta.sma(close,annual_avg-month_step_down3) < ta.sma(close,annual_avg)
        ribbon_color4 := bear_ribbon
    avg4 := ta.sma(close,annual_avg-month_step_down3)
    
    if ta.sma(close,annual_avg-month_step_down4) < ta.sma(close,annual_avg)
        ribbon_color5 := bear_ribbon
    avg5 := ta.sma(close,annual_avg-month_step_down4)
    
    if ta.sma(close,annual_avg-month_step_down5) < ta.sma(close,annual_avg)
        ribbon_color6 := bear_ribbon
    avg6 := ta.sma(close,annual_avg-month_step_down5)
    
    if ta.sma(close,annual_avg-month_step_down6) < ta.sma(close,annual_avg)
        ribbon_color7 := bear_ribbon
    avg7 := ta.sma(close,annual_avg-month_step_down6)
    
    if ta.sma(close,annual_avg-month_step_down7) < ta.sma(close,annual_avg)
        ribbon_color8 := bear_ribbon
    avg8 := ta.sma(close,annual_avg-month_step_down7)
    
    if ta.sma(close,annual_avg-month_step_down8) < ta.sma(close,annual_avg)
        ribbon_color9 := bear_ribbon
    avg9 := ta.sma(close,annual_avg-month_step_down8)
    
    if ta.sma(close,annual_avg-month_step_down9) < ta.sma(close,annual_avg)
        ribbon_color10 := bear_ribbon
    avg10 := ta.sma(close,annual_avg-month_step_down9)
    
    if ta.sma(close,annual_avg-month_step_down10) < ta.sma(close,annual_avg)
        ribbon_color11 := bear_ribbon
    avg11 := ta.sma(close,annual_avg-month_step_down10)
    
    if ta.sma(close,annual_avg-month_step_down11) < ta.sma(close,annual_avg)
        ribbon_color12 := bear_ribbon
    avg12 := ta.sma(close,annual_avg-month_step_down11)
    
//Fill Colors
if avg2 < avg1
    fill_color1 := bear_fill
if avg3 < avg2
    fill_color2 := bear_fill
if avg4 < avg3
    fill_color3 := bear_fill
if avg5 < avg4
    fill_color4 := bear_fill
if avg6 < avg5
    fill_color5 := bear_fill
if avg7 < avg6
    fill_color6 := bear_fill
if avg8 < avg7
    fill_color7 := bear_fill
if avg9 < avg8
    fill_color8 := bear_fill
if avg10 < avg9
    fill_color9 := bear_fill
if avg11 < avg10
    fill_color10 := bear_fill
if avg12 < avg11
    fill_color11 := bear_fill

//Plots
p1 = plot(avg1, linewidth = 3, title="Annual Avg", color = ribbon_color1)
p2 = plot(avg2, title="Monthly Step Down 1", color = ribbon_color2)
p3 = plot(avg3, title="Monthly Step Down 2", color = ribbon_color3)
p4 = plot(avg4, title="Monthly Step Down 3", color = ribbon_color4)
p5 = plot(avg5, title="Monthly Step Down 4", color = ribbon_color5)
p6 = plot(avg6, title="Monthly Step Down 5", color = ribbon_color6)
p7 = plot(avg7, title="Monthly Step Down 6", color = ribbon_color7)
p8 = plot(avg8, title="Monthly Step Down 7", color = ribbon_color8)
p9 = plot(avg9, title="Monthly Step Down 8", color = ribbon_color9)
p10 = plot(avg10, title="Monthly Step Down 9", color = ribbon_color10)
p11 = plot(avg11, title="Monthly Step Down 10", color = ribbon_color11)
p12 = plot(avg12, linewidth = 3, title="Monthly Step Down 11", color = ribbon_color12)

fill(p1, p2, color=fill_color1, title = "Gap Slow Avg Step Down 1")
fill(p2, p3, color=fill_color2, title = "Step Down 1 Step Down 2")
fill(p3, p4, color=fill_color3, title = "Step Down 2 Step Down 3")
fill(p4, p5, color=fill_color4, title = "Step Down 3 Step Down 4")
fill(p5, p6, color=fill_color5, title = "Step Down 4 Step Down 5")
fill(p6, p7, color=fill_color6, title = "Step Down 5 Step Down 6")
fill(p7, p8, color=fill_color7, title = "Step Down 6 Step Down 7")
fill(p8, p9, color=fill_color8, title = "Step Down 7 Step Down 8")
fill(p9, p10, color=fill_color9, title = "Step Down 8 Step Down 9")
fill(p10, p11, color=fill_color10, title = "Step Down 9 Step Down 10")
fill(p11, p12, color=fill_color11, title = "Step Down 10 Step Down 11")
