Triple SuperTrend
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © DebbyLe1487

//@version=5
indicator("Triple supertrend", overlay = true, format=format.price, precision=2)


Periods_1 = input.int(title="ATR Period 1", defval=10)
Multiplier_1 = input.float(title="ATR Multiplier 1", step=0.1, defval=1.0)
Periods_2 = input.int(title="ATR Period 2", defval=11)
Multiplier_2 = input.float(title="ATR Multiplier 2", step=0.1, defval=2.0)
Periods_3 = input.int(title="ATR Period 3", defval=12)
Multiplier_3 = input.float(title="ATR Multiplier 3", step=0.1, defval=3.0)
src = input(hl2, title="Source")
changeATR= input.bool(title="Change ATR Calculation Method ?", defval=true)
showsignals = input.bool(title="Show Buy/Sell Signals ?", defval=true)
highlighting = input.bool(title="Highlighter Trend lines On/Off ?", defval=true)
highlightingBG = input.bool(title="Highlighter BackGround Buy/Sell On/Off ?", defval=true)


//common
mPlot = plot(ohlc4, title="", style=plot.style_circles, linewidth=0)

//Line 1
atr2_1 = ta.sma(ta.tr, Periods_1)
atr_1= changeATR ? ta.atr(Periods_1) : atr2_1
up_1=src-(Multiplier_1*atr_1)
up1_1 = nz(up_1[1],up_1)
up_1 := close[1] > up1_1 ? math.max(up_1,up1_1) : up_1
dn_1=src+(Multiplier_1*atr_1)
dn1_1 = nz(dn_1[1], dn_1)
dn_1 := close[1] < dn1_1 ? math.min(dn_1, dn1_1) : dn_1
trend_1 = 1
trend_1 := nz(trend_1[1], trend_1)
trend_1 := trend_1 == -1 and close > dn1_1 ? 1 : trend_1 == 1 and close < up1_1 ? -1 : trend_1
upPlot_1 = plot(trend_1 == 1 ? up_1 : na, title="Up Trend 1", style=plot.style_linebr, linewidth=2, color=color.new(color.green,0))
buySignal_1 = trend_1 == 1 and trend_1[1] == -1
plotshape(buySignal_1 ? up_1 : na, title="UpTrend Begins 1", location=location.absolute, style=shape.circle, size=size.tiny, color=color.green)
//plotshape(buySignal_1 and showsignals ? up_1 : na, title="Buy_1", text="Buy_1", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.green,75), textcolor=color.new(color.white,75))
dnPlot_1 = plot(trend_1 == 1 ? na : dn_1, title="Down Trend", style=plot.style_linebr, linewidth=2, color=color.red)
sellSignal_1 = trend_1 == -1 and trend_1[1] == 1
plotshape(sellSignal_1 ? dn_1 : na, title="DownTrend Begins 1", location=location.absolute, style=shape.circle, size=size.tiny, color=color.red)
//plotshape(sellSignal_1 and showsignals ? dn_1 : na, title="Sell_1", text="Sell_1", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.red,75), textcolor=color.new(color.white,75))
longFillColor_1 = highlighting ? (trend_1 == 1 ? color.new(color.green,95) : color.new(color.white,100)) : color.new(color.white,100)
shortFillColor_1 = highlighting ? (trend_1 == -1 ? color.new(color.red,95) : color.new(color.white,100)) : color.new(color.white,100)
fill(mPlot, upPlot_1, title="UpTrend Highligter", color=longFillColor_1)
fill(mPlot, dnPlot_1, title="DownTrend Highligter", color=shortFillColor_1)
//alertcondition(buySignal_1, title="SuperTrend Buy", message="SuperTrend Buy!")
//alertcondition(sellSignal_1, title="SuperTrend Sell", message="SuperTrend Sell!")
changeCond_1 = trend_1 != trend_1[1]
//alertcondition(changeCond_1, title="SuperTrend Direction Change", message="SuperTrend has changed direction!")

//Line 2
atr2_2 = ta.sma(ta.tr, Periods_2)
atr_2= changeATR ? ta.atr(Periods_2) : atr2_2
up_2=src-(Multiplier_2*atr_2)
up1_2 = nz(up_2[1],up_2)
up_2 := close[1] > up1_2 ? math.max(up_2,up1_2) : up_2
dn_2=src+(Multiplier_2*atr_2)
dn1_2 = nz(dn_2[1], dn_2)
dn_2 := close[1] < dn1_2 ? math.min(dn_2, dn1_2) : dn_2
trend_2 = 1
trend_2 := nz(trend_2[1], trend_2)
trend_2 := trend_2 == -1 and close > dn1_2 ? 1 : trend_2 == 1 and close < up1_2 ? -1 : trend_2
upPlot_2 = plot(trend_2 == 1 ? up_2 : na, title="Up Trend 1", style=plot.style_linebr, linewidth=2, color=color.new(color.green,0))
buySignal_2 = trend_2 == 1 and trend_2[1] == -1
plotshape(buySignal_2 ? up_2 : na, title="UpTrend Begins 1", location=location.absolute, style=shape.circle, size=size.tiny, color=color.green)
//plotshape(buySignal_2 and showsignals ? up_2 : na, title="Buy_2", text="Buy_2", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.green,75), textcolor=color.new(color.white,75))
dnPlot_2 = plot(trend_2 == 1 ? na : dn_2, title="Down Trend", style=plot.style_linebr, linewidth=2, color=color.red)
sellSignal_2 = trend_2 == -1 and trend_2[1] == 1
plotshape(sellSignal_2 ? dn_2 : na, title="DownTrend Begins 1", location=location.absolute, style=shape.circle, size=size.tiny, color=color.red)
//plotshape(sellSignal_2 and showsignals ? dn_2 : na, title="Sell_2", text="Sell_2", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.red,75), textcolor=color.new(color.white,75))
longFillColor_2 = highlighting ? (trend_2 == 1 ? color.new(color.green,95) : color.new(color.white,100)) : color.new(color.white,100)
shortFillColor_2 = highlighting ? (trend_2 == -1 ? color.new(color.red,95) : color.new(color.white,100)) : color.new(color.white,100)
fill(mPlot, upPlot_2, title="UpTrend Highligter", color=longFillColor_2)
fill(mPlot, dnPlot_2, title="DownTrend Highligter", color=shortFillColor_2)
//alertcondition(buySignal_2, title="SuperTrend Buy", message="SuperTrend Buy!")
//alertcondition(sellSignal_2, title="SuperTrend Sell", message="SuperTrend Sell!")
changeCond_2 = trend_2 != trend_2[1]
//alertcondition(changeCond_2, title="SuperTrend Direction Change", message="SuperTrend has changed direction!")


//Line 3
atr2_3 = ta.sma(ta.tr, Periods_3)
atr_3= changeATR ? ta.atr(Periods_3) : atr2_3
up_3=src-(Multiplier_3*atr_3)
up1_3 = nz(up_3[1],up_3)
up_3 := close[1] > up1_3 ? math.max(up_3,up1_3) : up_3
dn_3=src+(Multiplier_3*atr_3)
dn1_3 = nz(dn_3[1], dn_3)
dn_3 := close[1] < dn1_3 ? math.min(dn_3, dn1_3) : dn_3
trend_3 = 1
trend_3 := nz(trend_3[1], trend_3)
trend_3 := trend_3 == -1 and close > dn1_3 ? 1 : trend_3 == 1 and close < up1_3 ? -1 : trend_3
upPlot_3 = plot(trend_3 == 1 ? up_3 : na, title="Up Trend 1", style=plot.style_linebr, linewidth=2, color=color.new(color.green,0))
buySignal_3 = trend_3 == 1 and trend_3[1] == -1
plotshape(buySignal_3 ? up_3 : na, title="UpTrend Begins 1", location=location.absolute, style=shape.circle, size=size.tiny, color=color.green)
//plotshape(buySignal_3 and showsignals ? up_3 : na, title="Buy_3", text="Buy_3", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.green,75), textcolor=color.new(color.white,75))
dnPlot_3 = plot(trend_3 == 1 ? na : dn_3, title="Down Trend", style=plot.style_linebr, linewidth=2, color=color.red)
sellSignal_3 = trend_3 == -1 and trend_3[1] == 1
plotshape(sellSignal_3 ? dn_3 : na, title="DownTrend Begins 1", location=location.absolute, style=shape.circle, size=size.tiny, color=color.red)
//plotshape(sellSignal_3 and showsignals ? dn_3 : na, title="Sell_3", text="Sell_3", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.red,75), textcolor=color.new(color.white,75))
longFillColor_3 = highlighting ? (trend_3 == 1 ? color.new(color.green,95) : color.new(color.white,100)) : color.new(color.white,100)
shortFillColor_3 = highlighting ? (trend_3 == -1 ? color.new(color.red,95) : color.new(color.white,100)) : color.new(color.white,100)
fill(mPlot, upPlot_3, title="UpTrend Highligter", color=longFillColor_3)
fill(mPlot, dnPlot_3, title="DownTrend Highligter", color=shortFillColor_3)
//alertcondition(buySignal_3, title="SuperTrend Buy", message="SuperTrend Buy!")
//alertcondition(sellSignal_3, title="SuperTrend Sell", message="SuperTrend Sell!")
changeCond_3 = trend_3 != trend_3[1]
//alertcondition(changeCond_3, title="SuperTrend Direction Change", message="SuperTrend has changed direction!")



//General Buy
buy = (buySignal_1 or buySignal_2 or buySignal_3) and trend_1 == 1 and trend_2 == 1 and trend_3 == 1
plotshape(buy and showsignals ? up_3 : na, title="Buy", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.green,60), textcolor=color.new(color.white,40))

//General Sell
sell = (sellSignal_1 or sellSignal_2 or sellSignal_3) and trend_1 == -1 and trend_2 == -1 and trend_3 == -1
plotshape(sell and showsignals ? dn_3 : na, title="Sell", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.red,60), textcolor=color.new(color.white,40))


longCondition = trend_1 == 1 and trend_2 == 1 and trend_3 == 1
shortCondition = trend_1 == -1 and trend_2 == -1 and trend_3 == -1
bgcolor(longCondition and highlightingBG ? color.new(color.blue,90) : shortCondition and highlightingBG ? color.new(color.purple,90) : color.new(color.blue,100))
