SuperTrend TradingView Look-A-Like For ThinkOrSwim

barbaros

Administrator
Staff member
I modified a version of SuperTrend that is already available for Thinkorswim to imitate the TradingView style.

kyuKOnh.png


You can adjust the look from the options. Labels are turned off by default.
4GZNCcv.png

It also has alerts for trend change.
DnqfdSv.png

Latest revision adds EMA cross to verify entries.

Code:
# 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
# v3.3

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 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;

def EMA1Val = MovAvgExponential(close, EMA1);
def EMA2Val = MovAvgExponential(close, EMA2);
def EMADirection = if EMA1Val > EMA2Val then 1 else if EMA1Val < EMA2Val then -1 else 0;

plot Long = if close > ST then ST else Double.NaN;
Long.AssignValueColor(Color.GREEN);
Long.SetLineWeight(2);

plot Short = if close < 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 close > ST and EMADirection == 1) or (!UseEmaCross and LongTrigger);
def ShortConfirm = (UseEmaCross and close < 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 close > ST then "Bullish" else if close < ST then "Bearish" else "Neutral"),
                              if close > ST then Color.GREEN else if close < 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 close < ST and (!UseEmaCross or EMADirection == -1) then Color.RED
                 else if PaintBars and close > 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

Scanner

Code:
# 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
# v3.3 - Scanner

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 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;

def EMA1Val = MovAvgExponential(close, EMA1);
def EMA2Val = MovAvgExponential(close, EMA2);
def EMADirection = if EMA1Val > EMA2Val then 1 else if EMA1Val < EMA2Val then -1 else 0;

def Long = if close > ST then ST else Double.NaN;
def Short = if close < ST then ST else Double.NaN;

def LongTrigger = isNaN(Long[1]) and !isNaN(Long);
def ShortTrigger = isNaN(Short[1]) and !isNaN(Short);

def LongDot = if LongTrigger then ST else Double.NaN;
def ShortDot = if ShortTrigger then ST else Double.NaN;

def LongConfirm = (UseEmaCross and close > ST and EMADirection == 1) or (!UseEmaCross and LongTrigger);
def ShortConfirm = (UseEmaCross and close < ST and EMADirection == -1) or (!UseEmaCross and ShortTrigger);

plot LongAlert = LongConfirm and LongConfirm[1] != LongConfirm;
plot ShortAlert = ShortConfirm and ShortConfirm[1] != ShortConfirm;

# End Code SuperTrend Yahoo Finance Replica
 
Last edited:

barbaros

Administrator
Staff member
Here is the MTF version

Code:
# 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
 

mkr21

New member
Hello @barbaros When I use scanner code.. it says a message as one plot required.. If I save it as study, then it is not giving any results in scan.. Please help
 

barbaros

Administrator
Staff member
Hello @barbaros When I use scanner code.. it says a message as one plot required.. If I save it as study, then it is not giving any results in scan.. Please help
Here is the scanner version. Save the script below to a study, and then you can select either the LongAlert or ShortAlert from the filter settings as plot option and select "is true" for the value.
Code:
# 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
# v3.3 - Scanner

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 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;

def EMA1Val = MovAvgExponential(close, EMA1);
def EMA2Val = MovAvgExponential(close, EMA2);
def EMADirection = if EMA1Val > EMA2Val then 1 else if EMA1Val < EMA2Val then -1 else 0;

def Long = if close > ST then ST else Double.NaN;
def Short = if close < ST then ST else Double.NaN;

def LongTrigger = isNaN(Long[1]) and !isNaN(Long);
def ShortTrigger = isNaN(Short[1]) and !isNaN(Short);

def LongDot = if LongTrigger then ST else Double.NaN;
def ShortDot = if ShortTrigger then ST else Double.NaN;

def LongConfirm = (UseEmaCross and close > ST and EMADirection == 1) or (!UseEmaCross and LongTrigger);
def ShortConfirm = (UseEmaCross and close < ST and EMADirection == -1) or (!UseEmaCross and ShortTrigger);

plot LongAlert = LongConfirm and LongConfirm[1] != LongConfirm;
plot ShortAlert = ShortConfirm and ShortConfirm[1] != ShortConfirm;

# End Code SuperTrend Yahoo Finance Replica
 

Headhunter20

New member
I thought I had found something... I've tried everything I can think of to get SOMETHING.. anything to give results in the scan for this indicator. Nothing, nada, zip. The above indicator returned.. again.... nothing, nada, zip, even thought I was looking at a stock that fit the criteria perfectly.
Does anyone have a suggestion that will produce results?
Thank you so much for helping noobs like me.
Thank you, thank you, thank you.
 

barbaros

Administrator
Staff member
I thought I had found something... I've tried everything I can think of to get SOMETHING.. anything to give results in the scan for this indicator. Nothing, nada, zip. The above indicator returned.. again.... nothing, nada, zip, even thought I was looking at a stock that fit the criteria perfectly.
Does anyone have a suggestion that will produce results?
Thank you so much for helping noobs like me.
Thank you, thank you, thank you.
did you save it as a study?
 

barbaros

Administrator
Staff member
I thought I had found something... I've tried everything I can think of to get SOMETHING.. anything to give results in the scan for this indicator. Nothing, nada, zip. The above indicator returned.. again.... nothing, nada, zip, even thought I was looking at a stock that fit the criteria perfectly.
Does anyone have a suggestion that will produce results?
Thank you so much for helping noobs like me.
Thank you, thank you, thank you.
There must be an issue with the latest ToS update. Don't save it as a study and try this. Comment out the plot that you are not interested in.

For long scan:
fGFncAG.png


For short scan:
wwXZyn3.png
 

Headhunter20

New member
Ah yes.... I did get a hit. Thank you so much. If this proves to be profitable I will try to put my "profit" results here.
Again .... thank you so much.
 

Paypachaysa

New member
nice work @barbaros ! What are the grey bars that show up on the indicator? I'm not sure if that is coming from the script or my think or swim settings. I see in TOS that grey bars on my chart are a Doji but these bars have value..
 

barbaros

Administrator
Staff member
nice work @barbaros ! What are the grey bars that show up on the indicator? I'm not sure if that is coming from the script or my think or swim settings. I see in TOS that grey bars on my chart are a Doji but these bars have value..
Updated code has moving averages to confirm the SuperTrend direction. When gray, not confirmed.
 
Top