# 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 Barbaros to add MTF
# v3.4
input Agg = AggregationPeriod.DAY;
input AtrMult = 1.00;
input nATR = 6;
input AvgType = AverageType.HULL;
input PaintBars = yes;
input ShowBubbles = yes;
input ShowLabels = yes;
input UseEmaCross = yes;
input EMA1 = 10;
input EMA2 = 20;
def closePrice = close(period = Agg);
def highPrice = high(period = Agg);
def lowPrice = low(period = Agg);
def HL2Price = HL2(period = Agg);
def ATR = MovingAverage(AvgType, TrueRange(highPrice, closePrice, lowPrice), nATR);
def UP_Band_Basic = HL2Price + (AtrMult * ATR);
def LW_Band_Basic = HL2Price + (-AtrMult * ATR);
def UP_Band = if ((UP_Band_Basic < UP_Band[1]) or (closePrice[1] > UP_Band[1])) then UP_Band_Basic else UP_Band[1];
def LW_Band = if ((LW_Band_Basic > LW_Band[1]) or (closePrice[1] < LW_Band[1])) then LW_Band_Basic else LW_Band[1];
def ST = if ((ST[1] == UP_Band[1]) and (closePrice < UP_Band)) then UP_Band
else if ((ST[1] == UP_Band[1]) and (closePrice > Up_Band)) then LW_Band
else if ((ST[1] == LW_Band[1]) and (closePrice > LW_Band)) then LW_Band
else if ((ST[1] == LW_Band) and (closePrice < LW_Band)) then UP_Band
else LW_Band;
def EMA1Val = MovAvgExponential(closePrice, EMA1);
def EMA2Val = MovAvgExponential(closePrice, EMA2);
def EMADirection = if EMA1Val > EMA2Val then 1 else if EMA1Val < EMA2Val then -1 else 0;
plot Long = if closePrice > ST then ST else Double.NaN;
Long.AssignValueColor(Color.GREEN);
Long.SetLineWeight(2);
plot Short = if closePrice < ST then ST else Double.NaN;
Short.AssignValueColor(Color.RED);
Short.SetLineWeight(3);
def LongTrigger = isNaN(Long[1]) and !isNaN(Long);
def ShortTrigger = isNaN(Short[1]) and !isNaN(Short);
plot LongDot = if LongTrigger then ST else Double.NaN;
LongDot.SetPaintingStrategy(PaintingStrategy.POINTS);
LongDot.AssignValueColor(Color.GREEN);
LongDot.SetLineWeight(4);
plot ShortDot = if ShortTrigger then ST else Double.NaN;
ShortDot.SetPaintingStrategy(PaintingStrategy.POINTS);
ShortDot.AssignValueColor(Color.RED);
ShortDot.SetLineWeight(4);
def LongConfirm = (UseEmaCross and closePrice > ST and EMADirection == 1) or (!UseEmaCross and LongTrigger);
def ShortConfirm = (UseEmaCross and closePrice < ST and EMADirection == -1) or (!UseEmaCross and ShortTrigger);
def LongAlert = LongConfirm and LongConfirm[1] != LongConfirm;
def ShortAlert = ShortConfirm and ShortConfirm[1] != ShortConfirm;
AddLabel(ShowLabels, if Agg < AggregationPeriod.HOUR then Round(Agg / AggregationPeriod.MIN, 0) + "m"
else if Agg < AggregationPeriod.DAY then Round(Agg / AggregationPeriod.HOUR, 0) + "H"
else if Agg < AggregationPeriod.WEEK then Round(Agg / AggregationPeriod.DAY, 0) + "D"
else if Agg < AggregationPeriod.MONTH then Round(Agg / AggregationPeriod.WEEK, 0) + "W"
else if Agg < AggregationPeriod.YEAR then Round(Agg / AggregationPeriod.MONTH, 0) + "M"
else "1Y"
, Color.GRAY);
AddLabel(ShowLabels, "ST: " + (if closePrice > ST then "Bullish" else if closePrice < ST then "Bearish" else "Neutral"),
if closePrice > ST then Color.GREEN else if closePrice < ST then Color.RED else Color.GRAY);
AddLabel(ShowLabels and UseEmaCross, "EMA: " + (if EMADirection == 1 then "Bullish" else if EMADirection == -1 then "Bearish" else "Neutral"),
if EMADirection == 1 then Color.GREEN else if EMADirection == -1 then Color.RED else Color.GRAY);
AddChartBubble(ShowBubbles and LongAlert, ST, "BUY", Color.GREEN, no);
AddChartBubble(ShowBubbles and ShortAlert, ST, "SELL", Color.RED, yes);
AssignPriceColor(if PaintBars and closePrice < ST and (!UseEmaCross or EMADirection == -1) then Color.RED
else if PaintBars and closePrice > ST and (!UseEmaCross or EMADirection == 1) then Color.GREEN
else if PaintBars then Color.GRAY
else Color.CURRENT);
Alert(LongAlert, "Long", Alert.BAR, Sound.Ding);
Alert(ShortAlert, "Short", Alert.BAR, Sound.Ding);
# End Code SuperTrend Yahoo Finance Replica