# SuperTrend Yahoo Finance Replica - Modified from Modius SuperTrend
# Modified Modius ver. by RConner7
# Modified by Barbaros to replicate look from TradingView version
# Modified by Barbaros to add EMA cross for bubbles and alerts
# Modified by Barbaros to update bar color painting
# Modified by Fluideng to make the triple SuplerTrend into one indicator. Also removed EMA2 and Bar color.
# Modified by Barbaros to show as a lower indicator, removed EMA and others
# Modified by Barbaros to add trend consensus and bar painting
# modified by Barbaros to add gear numbers
# v4.1
declare lower;
input Limit = 3;
input BarColor = yes;
### SuperTrend #1
input AtrMult = 3.00;
input nATR = 12;
input AvgType = AverageType.HULL;
def ATR = ATR("length" = nATR, "average type" = AvgType);
def UP_Band_Basic = HL2 + (AtrMult * ATR);
def LW_Band_Basic = HL2 + (-AtrMult * ATR);
def UP_Band = if ((UP_Band_Basic < UP_Band[1]) or (close[1] > UP_Band[1])) then UP_Band_Basic else UP_Band[1];
def LW_Band = if ((LW_Band_Basic > LW_Band[1]) or (close[1] < LW_Band[1])) then LW_Band_Basic else LW_Band[1];
def ST = if ((ST[1] == UP_Band[1]) and (close < UP_Band)) then UP_Band
else if ((ST[1] == UP_Band[1]) and (close > Up_Band)) then LW_Band
else if ((ST[1] == LW_Band[1]) and (close > LW_Band)) then LW_Band
else if ((ST[1] == LW_Band) and (close < LW_Band)) then UP_Band
else LW_Band;
### SuperTrend #2
input AtrMult2 = 2.00;
input nATR2 = 11;
def ATR2 = ATR("length" = nATR2, "average type" = AvgType);
def UP_Band_Basic2 = HL2 + (AtrMult2 * ATR);
def LW_Band_Basic2 = HL2 + (-AtrMult2 * ATR);
def UP_Band2 = if ((UP_Band_Basic2 < UP_Band2[1]) or (close[1] > UP_Band2[1])) then UP_Band_Basic2 else UP_Band2[1];
def LW_Band2 = if ((LW_Band_Basic2 > LW_Band2[1]) or (close[1] < LW_Band2[1])) then LW_Band_Basic2 else LW_Band2[1];
def ST2 = if ((ST2[1] == UP_Band2[1]) and (close < UP_Band2)) then UP_Band2
else if ((ST2[1] == UP_Band2[1]) and (close > Up_Band2)) then LW_Band2
else if ((ST2[1] == LW_Band2[1]) and (close > LW_Band2)) then LW_Band2
else if ((ST2[1] == LW_Band2) and (close < LW_Band2)) then UP_Band2
else LW_Band2;
### SuperTrend #3
input AtrMult3 = 1.00;
input nATR3 = 10;
def ATR3 = ATR("length" = nATR3, "average type" = AvgType);
def UP_Band_Basic3 = HL2 + (AtrMult3 * ATR);
def LW_Band_Basic3 = HL2 + (-AtrMult3 * ATR);
def UP_Band3 = if ((UP_Band_Basic3 < UP_Band3[1]) or (close[1] > UP_Band3[1])) then UP_Band_Basic3 else UP_Band3[1];
def LW_Band3 = if ((LW_Band_Basic3 > LW_Band3[1]) or (close[1] < LW_Band3[1])) then LW_Band_Basic3 else LW_Band3[1];
def ST3 = if ((ST3[1] == UP_Band3[1]) and (close < UP_Band3)) then UP_Band3
else if ((ST3[1] == UP_Band3[1]) and (close > Up_Band3)) then LW_Band3
else if ((ST3[1] == LW_Band3[1]) and (close > LW_Band3)) then LW_Band3
else if ((ST3[1] == LW_Band3) and (close < LW_Band3)) then UP_Band3
else LW_Band3;
### ST consensus
def ST1Val = (if close > ST then 1 else if close < ST then -1 else 0);
def ST2Val = (if close > ST2 then 1 else if close < ST2 then -1 else 0);
def ST3Val = (if close > ST3 then 1 else if close < ST3 then -1 else 0);
def totalShorts = (if ST1Val < 0 then -1 else 0) +
(if ST2Val < 0 then -1 else 0) +
(if ST3Val < 0 then -1 else 0);
def totalLongs = (if ST1Val > 0 then 1 else 0) +
(if ST2Val > 0 then 1 else 0) +
(if ST3Val > 0 then 1 else 0);
def TrendDirection = if totalLongs > 0 and totalLongs >= Limit then 1 else if totalShorts < 0 and totalShorts <= -Limit then -1 else 0;
### Gears
def DirectionFilter = if totalLongs == 3 then 1 else if totalShorts == -3 then -1 else DirectionFilter[1];
def LongGear1_ = totalShorts crosses above -3;
def LongGear2_ = totalShorts crosses above -2;
def LongGear3_ = totalShorts crosses above -1;
def LongGear1 = DirectionFilter == -1 and LongGear1_ and !LongGear2_ and !LongGear3_;
def LongGear2 = DirectionFilter == -1 and LongGear2_ and !LongGear3_;
def LongGear3 = DirectionFilter == 1 and LongGear3_;
def ShortGear1_ = totalLongs crosses below 3;
def ShortGear2_ = totalLongs crosses below 2;
def ShortGear3_ = totalLongs crosses below 1;
def ShortGear1 = DirectionFilter == 1 and ShortGear1_ and !ShortGear2_ and !ShortGear3_;
def ShortGear2 = DirectionFilter == 1 and ShortGear2_ and !ShortGear3_;
def ShortGear3 = DirectionFilter == -1 and ShortGear3_;
### Plots
plot SeparatorTop = if isNaN(close) then Double.NaN else 7;
SeparatorTop.SetDefaultColor(Color.GRAY);
plot Trend = if isNaN(close) then Double.NaN else 5;
Trend.SetPaintingStrategy(PaintingStrategy.POINTS);
Trend.SetLineWeight(3);
Trend.AssignValueColor(if TrendDirection == 1 then Color.GREEN else if TrendDirection == -1 then Color.RED else Color.GRAY);
plot SeparatorBottom = if isNaN(close) then Double.NaN else 4;
SeparatorBottom.SetDefaultColor(Color.GRAY);
plot SuperTrend1 = if isNaN(close) then Double.NaN else 1;
SuperTrend1.SetPaintingStrategy(PaintingStrategy.POINTS);
SuperTrend1.SetLineWeight(3);
SuperTrend1.AssignValueColor(if close > ST then Color.GREEN else if close < ST then Color.RED else Color.GRAY);
plot SuperTrend2 = if isNaN(close) then Double.NaN else 2;
SuperTrend2.SetPaintingStrategy(PaintingStrategy.POINTS);
SuperTrend2.SetLineWeight(3);
SuperTrend2.AssignValueColor(if close > ST2 then Color.GREEN else if close < ST2 then Color.RED else Color.GRAY);
plot SuperTrend3 = if isNaN(close) then Double.NaN else 3;
SuperTrend3.SetPaintingStrategy(PaintingStrategy.POINTS);
SuperTrend3.SetLineWeight(3);
SuperTrend3.AssignValueColor(if close > ST3 then Color.GREEN else if close < ST3 then Color.RED else Color.GRAY);
AddChartBubble(LongGear1, 5, "1", Color.YELLOW);
AddChartBubble(LongGear2, 5, "2", Color.GREEN);
AddChartBubble(LongGear3, 5, "3", Color.LIGHT_GREEN);
AddChartBubble(ShortGear1, 5, "1", Color.DARK_RED);
AddChartBubble(ShortGear2, 5, "2", Color.RED);
AddChartBubble(ShortGear3, 5, "3", Color.LIGHT_RED);
### Bar Color
AssignPriceColor(if BarColor then if TrendDirection == 1 then Color.GREEN else if TrendDirection == -1 then Color.RED else Color.GRAY else Color.CURRENT);
# End Code SuperTrend Yahoo Finance Replica