ADX Trend Long\Short Strategy for ThinkorSwim

Zachc

Zachc

Member
VIP
This is my first indicator that looks fairly effective. My goal is to reduce choppy trades and elongate hold times. The longer we can stay in the trend the better. This one was fun because it forced me to write and refine a universal trend indicator I can use to enhance already useful trend trading strategies. I will release the trend indicator separate as well so you can copy and paste into your own studies.

Most of my backtesting was done in futures /ES, /RTY, /NQ, /CL, /YM, /GC
Some things I have found while testing: Lower time frames benefit if you change the trend detection to aggressive. The higher time frames work much better with basic or turned off.

I am looking for criticism so I can get better at expressing my ideas into these systems.

Cheers!
-ZachC

The ADX Trend Strategy works pretty well without the enhancement. It should help to identify longer type trends.



Now we can change the settings to Basic



After the basic enhancement, the trend time frame is elongated to capture just a little more alpha.



I would also encourage anyone who can not short in their account to turn off the downtrend and short cover options. If you are in a short trade you could miss out on a long indicator.

Here is the code
Rich (BB code):
#hint:<b>Use ADX as your Long and Short entry points</b>\nEnhanced with trend confirmation strat on entry and exits.

input length = 14;
input lag = 12;
input averageLength = 25;
input trendLevel = 20;
input maxLevel = 42.0;
input critLevel = 22.0;
input mult = 1.8;
input averageType = AverageType.SIMPLE;
input EnhancedLong = {default Off, Basic, Simple, Aggressive};
input EnhancedShort = {default Off, Basic, Simple, Aggressive};

def ma = MovingAverage(averageType, close, averageLength);
def adx = reference ADX(length = length);

plot movingAVG = ma;

# Trend Quality
# Author: ZachC on useThinkscript.com
# You can use the uptrend and downtrend indicators in addition to a positive signal
# OR
# Use the stay long indicator to ride out your bullish trend a little longer
# Use the stay short indicator to ride out your bullish trend a little longer

###
# Basic Trend Level, Crude but effective
##
# Simple Trend
# Bar 1 is greater then bar two or TQ is above 0
# Vice Versa for down trend
##
# Aggressive Trend
# The last 3 bars are in an up trend
# The last 3 bars are in a down trend
###

def trLong;
switch (EnhancedLong) {
case Basic:
    trLong = TrendPeriods() == 1;
case Simple:
    trLong = TrendQuality().TQ[0] > TrendQuality().TQ[1] or TrendQuality().TQ >= 0;
case Aggressive:
    trLong = TrendQuality().TQ[0] > TrendQuality().TQ[1] and TrendQuality().TQ[1] > TrendQuality().TQ[2];
case Off:
    trLong = TrendQuality().TQ;
}

def trShort;
switch (EnhancedShort) {
case Basic:
    trShort = TrendPeriods() == 1;
case Simple:
    trShort = TrendQuality().TQ[0] > TrendQuality().TQ[1] or TrendQuality().TQ >= 0;
case Aggressive:
    trShort = TrendQuality().TQ[0] > TrendQuality().TQ[1] and TrendQuality().TQ[1] > TrendQuality().TQ[2];
case Off:
    trShort = TrendQuality().TQ;
}

# 1st Long Setup ( Strong Uptrend )
# ADX First needs to cross above the Trend Level
# But it must be below the Max Level
# The last closing bar must be above the SMA

AddOrder(OrderType.BUY_TO_OPEN, adx crosses above trendLevel and
    adx < maxLevel and
    close > ma and trLong, tickcolor = GetColor(0), arrowcolor = GetColor(0), name = "StrongUptrend");

# 2nd Long Setup ( Typical Uptrend )
# ADX must be less then the Multiplyer times the lowest ADX value in the last 12 bars
# ADX must also cross above the critical level set 22
# The last closing bar must be above the SMA

AddOrder(OrderType.BUY_TO_OPEN, adx > mult * Lowest(adx, lag) and
   adx crosses above critLevel and
   close > ma, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "Uptrend");

# 1st Short Setup (Strong Down Trend)
# ADX first must cross above the trendlevel
# ADX must be less than the max level
# The last closing bar must be below the SMA
AddOrder(OrderType.SELL_TO_OPEN, adx crosses above trendLevel and
    adx < maxLevel and
    close < ma and trShort, tickcolor = GetColor(2), arrowcolor = GetColor(2), name = "StrongDowntrend");

# 2nd Short Setup ( Typical Downtrend )
# ADX is greater then the multiplyer of 1.8 times the lowest ADX in the last 12 bars
# ADX must cross above the critical level
# The last closing bar must be below the SMA
AddOrder(OrderType.SELL_TO_OPEN, adx > mult * Lowest(adx, lag) and
    adx crosses above critLevel and
    close < ma, tickcolor = GetColor(3), arrowcolor = GetColor(3), name = "Downtrend");

#Covering needs improvement
# Price closes below the SMA and TrendShort period selected above
AddOrder(OrderType.SELL_TO_CLOSE, Average(close) < ma and trLong, tickcolor = GetColor(4), arrowcolor = GetColor(4), name = "LongCover");

# Price closes above the SMA and TrendLong period selected above
AddOrder(OrderType.BUY_TO_CLOSE, Average(close) > ma and trShort, tickcolor = GetColor(5), arrowcolor = GetColor(5), name = "ShortCover");
 
Last edited by a moderator:
Shinthus

Shinthus

Member
VIP
NICE! Great share and thank you! Will be testing this with enthusiasm.
 
Zachc

Zachc

Member
VIP
ADX is not a very noisy indicator so in a new stock like BYND or even the micro futures it really struggles. Still a 2k profit is a win in my book 🤑
Thank you for sharing. This is an interesting one.

$BYND for the giggles :D

 
H

horserider

Active member
Using the SMA of 25 in this study might give better results. I overlaid a SMA 25 (white line) and turned on the signals (white arrows).
 
Zachc

Zachc

Member
VIP
Using the SMA of 25 in this study might give better results. I overlaid a SMA 25 (white line) and turned on the signals (white arrows).
Hi, horserider thanks for checking out the study. You nailed it the default it is currently set to the 25 SMA. The difference is the addition of the trend enhancement. Turning off the trend enhancement would result in the signals you have displayed but for the exits only. The ADX indicator is used as the entry for both the short and long trades.

In a perfect world where the first long position was stopped out for a small loss would have been a perfect example of a time to hold through the sideways action to allow the trend to continue.

It would be interesting to see whether just using the 25 SMA has more potential that the ADX itself.

I think for the next iteration I am going to add a label at the top left that just displays the current ADX level and the significance of it.
I am also going to plot entry and exit signals to better show how the strategy is working.
 
H

horserider

Active member
Look more closely. The white arrows are 25 SMA signals. Or just the price crossing 25 SMA looks better to me.
 
Zachc

Zachc

Member
VIP
Look more closely. The white arrows are 25 SMA signals. Or just the price crossing 25 SMA looks better to me.
I see what you mean. I did notice that is what prompted me to add the option to turn off the Trend Enhancement. Some securities and ETF's have a better flow and respond better to the moving averages and don't require having the extra helping hand. It's all about preference, IMO.
 
T

tabs999

New member
Can someone explain how this indicator works in a detail video for complete noobs?
 
markos

markos

Well-known member
VIP
Thats not a bad Idea ill put one together that will help explain.
@Zachc this being your first script, I definitely say Good Job!
I have no PC for this week, only android. I would encourage you to put up a video of how the indicator works or just write it out.
Just be sure that no matter what you do, you are not in any way giving investment advice. Markos
 
Zachc

Zachc

Member
VIP
Yes,
@Zachc this being your first script, I definitely say Good Job!
I have no PC for this week, only android. I would encourage you to put up a video of how the indicator works or just write it out.
Just be sure that no matter what you do, you are not in any way giving investment advice. Markos
Thanks!
I had intended on putting a video together over the weekend but I never got the time. I am working on a 1.2 version of the code adding signals, and the indicator for the lower section of the chart. It will be easier to make one video describing both IMO. I look forward to writing more as I am just working my way down the list of indicators in ToS to learn think script.

Horserider made a good point regarding the 25 SMA and I am working that into the code as well.
 
H

horserider

Active member
@Zachc Thanks. My opinion is you are working hard on a study you will have a very hard time making it what you desire. The problem is the ADX. I have done a quick chart to give you ideas that may be easier to work into your strategy. That ADX oscillator is in ToS somewhere. Sorry I forgot the name. So take a look and see if it can improve your approach to this strategy.



The White line on top indicator matches the red/green line of the lower. On the upper the cross of 2 and -2 would be the signals and the parts inbetween can be ignored. The white line of the lower should signal the points or reversal of price when it crosses zero. Some way to avoid excess signals in chop may be needed. Ooops may have missed marking a few places but you get the idea. Good coding to you.
 
Zachc

Zachc

Member
VIP
Thanks, @horserider , Your analysis will go a long way in helping develop this strategy. Coding in a signal that is simply on or off is much simpler than messing with multiple levels. As soon as I have the new strat coded ill share for your feedback.
 
markos

markos

Well-known member
VIP
Hi, where can I find the ADX indicator ? Thank you
Hi @Gildes ToS offers a free tour. Pleae call support to schedule one. Have a list of 10 things that are important to you to ask. The ADX is located alphabetically with the studies.
 
T

Trading51

New member
VIP
can someone post a link or the code for the ADX with any updates i loaded the code and nothing populated thanks group
 
Zachc

Zachc

Member
VIP
@Trading51 I am working on a new version of this script I just don't get a lot of time to code them due to work hours, it may be more worth it to wait for the rewrite since the original is not that good.
 
T

Trading51

New member
VIP
@Trading51 I am working on a new version of this script I just don't get a lot of time to code them due to work hours, it may be more worth it to wait for the rewrite since the original is not that good.
Cool thanks for the reply have a great weekend
 

Top