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.
 
B

Billions

Member
VIP
@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"));
 
B

Billions

Member
VIP
@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"));
 

Top