ADX Trend Long\Short Strategy for ThinkorSwim

Zachc

Member
2019 Donor
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.

jZ9ZzY1.png


Now we can change the settings to Basic

qBLDCht.png


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

IbE48xy.png


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:

Zachc

Member
2019 Donor
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 🤑
 

Zachc

Member
2019 Donor
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.
 

horserider

Well-known member
VIP
Look more closely. The white arrows are 25 SMA signals. Or just the price crossing 25 SMA looks better to me.
 

Zachc

Member
2019 Donor
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.
 

horserider

Well-known member
VIP
@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.

kTLS8XG.png


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

Member
2019 Donor
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.
 

hashy

Member
Can someone please add arrows and alerts for the standard LBR_SmartADX indicator? I'd love to get an arrow and audible alert whenever ADX turns green. Thank you!
 

horserider

Well-known member
VIP
@UncleTerry Yes I made it long ago. I forget why and why I used that name. As it turns out it is the Detrended Price Oscillator. So I wasted my time. Oh well. Can also find it as FW DPO in ToS
 

Similar threads

Top