# 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
# Modified by SAM4COK to add GetAgg, ST Cloud, Arrow and Alert/Cloud/ST Line.
# v3.45
input AtrMult = 2.00;
input nATR = 10;
input AvgType = AverageType.HULL;
input PaintBars = yes;
input Alert = no;
input ShowBubbles = yes;
input ShowSignal = yes;
input ShowLabels = yes;
input CloudST = yes;
input LinesST = yes;
input UseEmaCross = yes;
input EMA1 = 10;
input EMA2 = 20;
input Signal = {default Arrow, Bubbles, Non};
def Agg = GetAggregationPeriod();
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 LinesST and closePrice > ST then ST else Double.NaN;
Long.AssignValueColor(Color.GREEN);
Long.SetLineWeight(2);
plot Short = if LinesST and 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, "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);
def LongSignal;
def ShortSignal;
def shape;
switch (Signal) {
case Arrow:
Shape = 1;
LongSignal = if LongAlert then Low else 0;
ShortSignal = if ShortAlert then High else 0;
case Bubbles:
Shape = 2;
LongSignal = if LongAlert then Low else 0;
ShortSignal = if ShortAlert then High else 0;
case Non:
Shape = 0;
LongSignal = 0;
ShortSignal = 0;
}
Plot LongArrow = if LongSignal and shape == 1 then low else double.nan;;
LongArrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
LongArrow.AssignValueColor(Color.GREEN);
LongArrow.SetLineWeight(4);
Plot ShortArrow = if ShortSignal and shape == 1 then high else double.nan;
ShortArrow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
ShortArrow.AssignValueColor(Color.RED);
ShortArrow.SetLineWeight(4);
Def LongBubble = if LongSignal and shape == 2 then LongSignal else double.nan;
Def ShortBubble = if ShortSignal and shape == 2then ShortSignal else double.nan;
AddChartBubble(LongSignal and shape==2, low, "BUY", Color.GREEN, no);
AddChartBubble (ShortSignal and shape==2, high, "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);
def Cloud = if CloudST and ST then ST else Double.NaN;
DefineGlobalColor("Bullish", Color.DARK_GREEN);
DefineGlobalColor("Bearish", Color.DARK_RED);
AddCloud(Cloud, hl2, GlobalColor("Bearish"), GlobalColor("Bullish"));
def LongAlertON = Alert and LongAlert;
def ShortAlertON = Alert and ShortAlert;
Alert(LongAlertON, "Long", Alert.BAR, Sound.Ding);
Alert(ShortAlertON, "Short", Alert.BAR, Sound.Ding);
# End Code SuperTrend Yahoo Finance Replica