Supertrend Indicator by Mobius for ThinkorSwim

C

corello

New member
VIP
@corello Sure, adding a strategy to this indicator should be easy. How do you want it? Buy when there is a bullish supertrend and sell when there is a bearish supertrend? Same for shorting?
@BenTen Yep, exactly that.
 
BenTen

BenTen

Administrative
Staff
VIP
Warehouse
@corello Here is the code for that. It comes in 2 parts.

Step 1: Create a new Strategy (not a Study) > Copy and Paste the original code in the first page into it.

Step 2: Add the following code to the end of the script:

Code:
# The following code is for backtesting
def SuperTrendUP = if ST crosses below close then 1 else 0;
def SuperTrendDN = if ST crosses above close then 1 else 0;
Step 3a: If you want to test out bullish strategy then add this code after the code from Step 2:

Code:
# Bullish Orders
AddOrder(OrderType.BUY_TO_OPEN, condition = SuperTrendUp, price = close,100, tickcolor = Color.GREEN, arrowcolor = Color.GREEN, name = "BUY");
AddOrder(OrderType.SELL_TO_CLOSE, condition = SuperTrendDN, price = open,100, tickcolor = Color.RED, arrowcolor = Color.RED, name = "SELL");
Step 3b: If you want to test out bearish strategy then add this code after the code from Step 2:

Code:
# Bearish Orders
AddOrder(OrderType.SELL_TO_OPEN, condition = SuperTrendDN, price = open,100, tickcolor = Color.RED, arrowcolor = Color.RED, name = "SELL");
AddOrder(OrderType.BUY_TO_CLOSE, condition = SuperTrendUp, price = close,100, tickcolor = Color.GREEN, arrowcolor = Color.GREEN, name = "BUY");
Do not place both codes from 3a and 3b into the same script.
 
Billions

Billions

Member
VIP
Warehouse
@BenTen Are you able to backtest this strategy with ThinkorSwim? Would like to see how it would work with ES future for example over a certain time period.

Unsure of how to make a backtest of this strategy, so any help would be great.

Thanks in advance.
This is what I have for a magic trend I use it on ES:
def agg = getAggregationPeriod();
def c = close(period = agg);
def h = high(period = agg);
def l = low(period = agg);
def pricedata = hl2(period = agg);
DefineGlobalColor("TrendUp", CreateColor(0, 254, 30));
DefineGlobalColor("TrendDown", CreateColor(255, 3, 2));
input lengthCCI = 50;
input lengthATR = 5;
input AtrFactor = 0.7;
def ATRcci = Average(TrueRange(h, c, l), lengthATR) * AtrFactor;
def price = c + l + h;
def linDev = LinDev(price, lengthCCI);
def CCI = if linDev == 0
then 0
else (price - Average(price, lengthCCI)) / linDev / 0.015;
def MT1 = if CCI > 0
then Max(MT1[1], pricedata - ATRcci)
else Min(MT1[1], pricedata + ATRcci);
plot data = MT1;
data.AssignValueColor(if c < MT1 then GlobalColor("TrendDown") else GlobalColor("TrendUp"));
 
Billions

Billions

Member
VIP
Warehouse
@corello Put this together when I was on the go. See if it works. Add it to the bottom of the indicator.

Code:
def bullish = close crosses below ST;
def bearish = close crosses above ST;

# Alerts
Alert(bullish, " ", Alert.Bar, Sound.Chimes);
Alert(bearish, " ", Alert.Bar, Sound.Bell);
Ben I tried to add this to my study its not working:
Here is the study I would like and alert when it crosses above or below can you help?
def agg = getAggregationPeriod();
def c = close(period = agg);
def h = high(period = agg);
def l = low(period = agg);
def pricedata = hl2(period = agg);
DefineGlobalColor("TrendUp", CreateColor(0, 254, 30));
DefineGlobalColor("TrendDown", CreateColor(255, 3, 2));
input lengthCCI = 50;
input lengthATR = 5;
input AtrFactor = 0.7;
def ATRcci = Average(TrueRange(h, c, l), lengthATR) * AtrFactor;
def price = c + l + h;
def linDev = LinDev(price, lengthCCI);
def CCI = if linDev == 0
then 0
else (price - Average(price, lengthCCI)) / linDev / 0.015;
def MT1 = if CCI > 0
then Max(MT1[1], pricedata - ATRcci)
else Min(MT1[1], pricedata + ATRcci);
plot data = MT1;
data.AssignValueColor(if c < MT1 then GlobalColor("TrendDown") else GlobalColor("TrendUp"));
 
retrobeast

retrobeast

New member
Seems there were many adjustments to this script thru out this posting.
So what is the correct full script please?
Also what is this script supposed to be show? Looks like it follows heiken ashi candles
 
Last edited:
J

Jenny

New member
Hi Ben,

Just an update and a favour. I have my chart set up on the ES over Globex as well as during cash market. I have the ORB as well as the supertrend indicator on and guided by Squeeze indicator and the MACD on a 15 min . Tried a few live trades guided by these indicators -seem to work well.

But find myself in front of the screen waiting for the colour change on the candlesticks. Is it possible to have an audio alert when the color changes or when the price numbers appear? What do you think? If you could help to have write a code on it.
 
BenTen

BenTen

Administrative
Staff
VIP
Warehouse
@Jenny Here is the code for that. Just add it to the bottom of your Supertrend script.

Code:
def bullish = close crosses below ST;
def bearish = close crosses above ST;

# Alerts
Alert(bullish, " ", Alert.Bar, Sound.Chimes);
Alert(bearish, " ", Alert.Bar, Sound.Bell);
 
H

haha54

New member
@netarchitech @danl please see their comments above.
Hello Ben, I am trying to run a bullish scan using the Supertrend scanner you posted. I activated the uptrend plot and am using peoples suggestions on how to run a bullish scan.(Supertrend Up crosses below price at close within 1 bar.) I can not get any results on any time frames. Could you (or anybody) please tell me what I am doing wrong. Thanks
 
MBF

MBF

Active member
VIP
Warehouse
Hello Ben, I am trying to run a bullish scan using the Supertrend scanner you posted. I activated the uptrend plot and am using peoples suggestions on how to run a bullish scan.(Supertrend Up crosses below price at close within 1 bar.) I can not get any results on any time frames. Could you (or anybody) please tell me what I am doing wrong. Thanks
I’m just going to give you my two cents. At the end of the market usually it is either up or down or sideways, meaning that whatever direction it was in at end of day, the trend was already confirmed with a bubble way back before 1 candle at the close. End of day trend already established. Meaning it’s hard to find bubbles at the close of market at the last candles. Try 15 candles just to see if your scan works. If you see a stock pop up then that’s probably why. Premarket on and off count too. I’m not sure this is it but I’m suspicious since I was having the same problems. That was my theory.
 
Y

YungTraderFromMontana

Member
what does natr represent?
This is a Supertrend indicator for ThinkorSwim created by Mobius.





thinkScript Code for Supertrend

Code:
input AtrMult = 1.0;
input nATR = 4;
input AvgType = AverageType.HULL;
input PaintBars = yes;
def ATR = MovingAverage(AvgType, TrueRange(high, close, low), nATR);
def UP = HL2 + (AtrMult * ATR);
def DN = HL2 + (-AtrMult * ATR);
def ST = if close < ST[1] then UP else DN;
plot SuperTrend = ST;
SuperTrend.AssignValueColor(if close < ST then Color.RED else Color.GREEN);
AssignPriceColor(if PaintBars and close < ST

                 then Color.RED

                 else if PaintBars and close > ST

                      then Color.GREEN

                      else Color.CURRENT);

AddChartBubble(close crosses below ST, low[1], low[1], color.Dark_Gray);
AddChartBubble(close crosses above ST, high[1], high[1], color.Dark_Gray, no);
# End Code SuperTrend

SuperTrend Scanner for ThinkorSwim

Code:
# SuperTrend Scan
# Mobius
# V01.10.2015
# Comment out (#) the direction NOT to use for a scan

input AtrMult = .70;
input nATR = 4;
input AvgType = AverageType.HULL;
def h = high;
def l = low;
def c = close;
def v = volume;
def ATR = MovingAverage(AvgType, TrueRange(h, c, l), nATR);
def UP = HL2 + (AtrMult * ATR);
def DN = HL2 + (-AtrMult * ATR);
def ST = if c < ST[1]
         then Round(UP / tickSize(), 0) * tickSize()
         else Round(DN / tickSize(), 0) * tickSize();
#plot SuperTrendUP = if ST crosses below close then 1 else 0;
plot SuperTrendDN = if ST crosses above close then 1 else 0;
# End Code SuperTrend

Shareable Link


Video Tutorial

What does nATR represent? Some kind of bar value?
 
MBF

MBF

Active member
VIP
Warehouse
what does natr represent?

What does nATR represent? Some kind of bar value?
Hi Yung,
ATR is important. Go to investopedia.com and look up things you don't really know anything about. They explain things clearly and it's easy to understand. You will probably need further study after that by looking at charts at the same time you're learning so you can see what they are talking about. Easiest way.
 
Y

YungTraderFromMontana

Member
Hi Yung,
ATR is important. Go to investopedia.com and look up things you don't really know anything about. They explain things clearly and it's easy to understand. You will probably need further study after that by looking at charts at the same time you're learning so you can see what they are talking about. Easiest way.
I understand how atr is calculated but is the supertrend's atr a variation of the normal atr? Does nATR just stand for the length of the atr or is it somehow integrating normalized true range also known as NATR.
 
Y

YungTraderFromMontana

Member
Just did some manual testing and found it's just a substitute for atr length, supertrend is so simple yet so powerful.
 
T

Talochka

New member
This is a Supertrend indicator for ThinkorSwim created by Mobius.





thinkScript Code for Supertrend

Code:
input AtrMult = 1.0;
input nATR = 4;
input AvgType = AverageType.HULL;
input PaintBars = yes;
def ATR = MovingAverage(AvgType, TrueRange(high, close, low), nATR);
def UP = HL2 + (AtrMult * ATR);
def DN = HL2 + (-AtrMult * ATR);
def ST = if close < ST[1] then UP else DN;
plot SuperTrend = ST;
SuperTrend.AssignValueColor(if close < ST then Color.RED else Color.GREEN);
AssignPriceColor(if PaintBars and close < ST

                 then Color.RED

                 else if PaintBars and close > ST

                      then Color.GREEN

                      else Color.CURRENT);

AddChartBubble(close crosses below ST, low[1], low[1], color.Dark_Gray);
AddChartBubble(close crosses above ST, high[1], high[1], color.Dark_Gray, no);
# End Code SuperTrend

SuperTrend Scanner for ThinkorSwim

Code:
# SuperTrend Scan
# Mobius
# V01.10.2015
# Comment out (#) the direction NOT to use for a scan

input AtrMult = .70;
input nATR = 4;
input AvgType = AverageType.HULL;
def h = high;
def l = low;
def c = close;
def v = volume;
def ATR = MovingAverage(AvgType, TrueRange(h, c, l), nATR);
def UP = HL2 + (AtrMult * ATR);
def DN = HL2 + (-AtrMult * ATR);
def ST = if c < ST[1]
         then Round(UP / tickSize(), 0) * tickSize()
         else Round(DN / tickSize(), 0) * tickSize();
#plot SuperTrendUP = if ST crosses below close then 1 else 0;
plot SuperTrendDN = if ST crosses above close then 1 else 0;
# End Code SuperTrend

Shareable Link


Video Tutorial


Hello, BenTen and thanks,
can you please let me know what is the best time frame for supertrend indicator?
 
C

Carl

New member
I found this really good indicator on TradingView.com. Its free to use and I am wondering if anyone could convert the code to TOS

Code:
//@version=3
// Copyright (c) 2019-present, Alex Orekhov (everget)
// SuperTrend script may be freely distributed under the terms of the GPL-3.0 license.
study("SuperTrend", overlay=true)

length = input(title="ATR Period", type=integer, defval=22)
mult = input(title="ATR Multiplier", type=float, step=0.1, defval=3.0)
showLabels = input(title="Show Buy/Sell Labels ?", type=bool, defval=true)

atr = mult * atr(length)

longStop = hl2 - atr
longStopPrev = nz(longStop[1], longStop)
longStop := close[1] > longStopPrev ? max(longStop, longStopPrev) : longStop

shortStop = hl2 + atr
shortStopPrev = nz(shortStop[1], shortStop)
shortStop := close[1] < shortStopPrev ? min(shortStop, shortStopPrev) : shortStop

dir = 1
dir := nz(dir[1], dir)
dir := dir == -1 and close > shortStopPrev ? 1 : dir == 1 and close < longStopPrev ? -1 : dir

longColor = green
shortColor = red

plot(dir == 1 ? longStop : na, title="Long Stop", style=linebr, linewidth=2, color=longColor)
buySignal = dir == 1 and dir[1] == -1
plotshape(buySignal ? longStop : na, title="Long Stop Start", location=location.absolute, style=shape.circle, size=size.tiny, color=longColor, transp=0)
plotshape(buySignal and showLabels ? longStop : na, title="Buy Label", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=longColor, textcolor=white, transp=0)

plot(dir == 1 ? na : shortStop, title="Short Stop", style=linebr, linewidth=2, color=shortColor)
sellSignal = dir == -1 and dir[1] == 1
plotshape(sellSignal ? shortStop : na, title="Short Stop Start", location=location.absolute, style=shape.circle, size=size.tiny, color=shortColor, transp=0)
plotshape(sellSignal and showLabels ? shortStop : na, title="Sell Label", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=shortColor, textcolor=white, transp=0)

alertcondition(buySignal, title="Alert on SuperTrend Buy", message="SuperTrend Buy!")
alertcondition(sellSignal, title="Alert on SuperTrend Sell", message="SuperTrend Sell!")

changeCond = dir != dir[1]
alertcondition(changeCond, title="Alert on SuperTrend Direction Change", message="SuperTrend has changed direction!")
 
H

HighBredCloud

Active member
Has anyone tried to play around with the ATR MULT? I have changed the settings from 1.0 to .5 on a 5 min chart and .4 on 15 min+...these settings closely resemble Heikin Ashi candles sticks for such time frames...
 
J

JoshF

New member
Guys - I think this script has potential, but I am looking to create a Study Alert via the MarketWatch tab for a particular equity. However I get the error "rec usage not allowed in this context". This is due to the line:

def ST = if close < ST[1] then UP else DN; since it is referencing itself as a float or "rec = recursive" variable.

Does anyone have a solution on how to use this script to avoid the "rec usage" error to create a study alert or conditional trade order? I understand the "rec usage" is allowed for a custom scan. However for creating a study alert or conditional order it is not. Any help?

Is there an alternate way that ST could be defined which wouldn't require referencing itself as a variable even if it took more lines of code to look back at the previous ST value?
 

Top