Supertrend 3 en 1
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/

//@version=4
study("Supertrend 3 en 1", overlay = true, format=format.price, precision=2, resolution="")

Periods1 = input(title="ATR Period 1", type=input.integer, defval=10)
src1 = input(hl2, title="Source 1")
Multiplier1 = input(title="ATR Multiplier 1", type=input.float, step=0.1, defval=1.0)
Periods2 = input(title="ATR Period 2", type=input.integer, defval=11)
src2 = input(hl2, title="Source 2")
Multiplier2 = input(title="ATR Multiplier 2", type=input.float, step=0.1, defval=2.0)
Periods3 = input(title="ATR Period 3", type=input.integer, defval=12)
src3 = input(hl2, title="Source 3")
Multiplier3 = input(title="ATR Multiplier 3", type=input.float, step=0.1, defval=3.0)

changeATR= input(title="Change ATR Calculation Method ?", type=input.bool, defval=true)
showsignals = input(title="Show Buy/Sell Signals ?", type=input.bool, defval=true)
highlighting = input(title="Highlighter On/Off ?", type=input.bool, defval=true)
atr2 = sma(tr, Periods1)
atr= changeATR ? atr(Periods1) : atr2
up=src1-(Multiplier1*atr)
up1 = nz(up[1],up)
up := close[1] > up1 ? max(up,up1) : up
dn=src1+(Multiplier1*atr)
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? 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 1", style=plot.style_linebr, linewidth=2, color=color.green)
buySignal = trend == 1 and trend[1] == -1
plotshape(buySignal ? up : na, title="UpTrend Begins 1", location=location.absolute, style=shape.circle, size=size.tiny, color=color.green, transp=0)
plotshape(buySignal and showsignals ? up : na, title="Buy 1", text="Buy 1", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.green, textcolor=color.white, transp=0)
dnPlot = plot(trend == 1 ? na : dn, title="Down Trend 1", style=plot.style_linebr, linewidth=2, color=color.red)
sellSignal = trend == -1 and trend[1] == 1
plotshape(sellSignal ? dn : na, title="DownTrend Begins 1", location=location.absolute, style=shape.circle, size=size.tiny, color=color.red, transp=0)
plotshape(sellSignal and showsignals ? dn : na, title="Sell 1", text="Sell 1", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.red, textcolor=color.white, transp=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 1", color=longFillColor)
fill(mPlot, dnPlot, title="DownTrend Highligter 1", color=shortFillColor)
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!")

atr2_2 = sma(tr, Periods2)
atr_2= changeATR ? atr(Periods2) : atr2_2
up_2=src2-(Multiplier2*atr_2)
up1_2 = nz(up_2[1],up_2)
up_2 := close[1] > up1_2 ? max(up_2,up1_2) : up_2
dn_2=src2+(Multiplier2*atr_2)
dn1_2 = nz(dn_2[1], dn_2)
dn_2 := close[1] < dn1_2 ? 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 2", style=plot.style_linebr, linewidth=2, color=color.green)
buySignal_2 = trend_2 == 1 and trend_2[1] == -1
plotshape(buySignal_2 ? up_2 : na, title="UpTrend Begins 2", location=location.absolute, style=shape.circle, size=size.tiny, color=color.green, transp=0)
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.green, textcolor=color.white, transp=0)
dnPlot_2 = plot(trend_2 == 1 ? na : dn_2, title="Down Trend 2", 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 2", location=location.absolute, style=shape.circle, size=size.tiny, color=color.red, transp=0)
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.red, textcolor=color.white, transp=0)
mPlot_2 = plot(ohlc4, title="", style=plot.style_circles, linewidth=0)
longFillColor_2 = highlighting ? (trend_2 == 1 ? color.green : color.white) : color.white
shortFillColor_2 = highlighting ? (trend_2 == -1 ? color.red : color.white) : color.white
fill(mPlot_2, upPlot_2, title="UpTrend Highligter 2", color=longFillColor_2)
fill(mPlot_2, dnPlot_2, title="DownTrend Highligter 2", 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!")

atr2_3 = sma(tr, Periods3)
atr_3= changeATR ? atr(Periods3) : atr2_3
up_3=src3-(Multiplier3*atr_3)
up1_3 = nz(up_3[1],up_3)
up_3 := close[1] > up1_3 ? max(up_3,up1_3) : up_3
dn_3=src3+(Multiplier3*atr_3)
dn1_3 = nz(dn_3[1], dn_3)
dn_3 := close[1] < dn1_3 ? 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 3", style=plot.style_linebr, linewidth=2, color=color.green)
buySignal_3 = trend_3 == 1 and trend_3[1] == -1
plotshape(buySignal_3 ? up_3 : na, title="UpTrend Begins 3", location=location.absolute, style=shape.circle, size=size.tiny, color=color.green, transp=0)
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.green, textcolor=color.white, transp=0)
dnPlot_3 = plot(trend_3 == 1 ? na : dn_3, title="Down Trend 3", 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 3", location=location.absolute, style=shape.circle, size=size.tiny, color=color.red, transp=0)
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.red, textcolor=color.white, transp=0)
mPlot_3 = plot(ohlc4, title="", style=plot.style_circles, linewidth=0)
longFillColor_3 = highlighting ? (trend_3 == 1 ? color.green : color.white) : color.white
shortFillColor_3 = highlighting ? (trend_3 == -1 ? color.red : color.white) : color.white
fill(mPlot_3, upPlot_3, title="UpTrend Highligter 3", color=longFillColor_3)
fill(mPlot_3, dnPlot_3, title="DownTrend Highligter 3", 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!")
