ADX Cross backtesting strategy

D

dmkoch

New member
I am new to thinkscript and would appreciate any help in writing the thinkscript code for a buy/sell strategy based on ADX and DMI+; DMI- trends on a 30m chart:

Long Entry (LE)
  • Using a 30 min stock chart along with the DMI(14, Wilders) study
  • Starting with the condition that ADX <20 and below both DI- and DI+
  • When the ADX crosses above DI-, then show the LE symbol on the chart
  • When the current ADX moves lower than the previous ADX one bar ago, then show LE - Exit symbol on the chart

Short Entry (SE)
  • Using a 30 min stock chart along with the DMI(14, Wilders) study
  • Starting with the condition that ADX <20 and below both DI- and DI+
  • When the ADX crosses above DI+, then show the LE symbol on the chart
  • When the current ADX is lower than the previous ADX one bar ago, then show LE - Exit symbol on the chart
Any assistance on writing my strategy would be much appreciated.
 
V

VicD

Member
You mean like this or on the candlestick chart? Change arrows, I just changed colors so screwed up the colors a bit.

Code:
#aADX_DMI2 With slow moving average of ADX and Arrows

declare lower;
input MACDfastLen = 10;
input MACDslowLen = 30;
input MACDLen = 10;
input showADX_DMI = { "No", default "Yes"};
input showMACD = { "No", default "Yes"};
input invertNegMACD = { "Yes", default "No"};
input MACDHeight = 50;
input MACDWidth = 3;
input ADX_Avg = 3;
input DMI_Len = 9;
input No_Trend = 20;
input Low_Trend = 45;
input Strong_Trend = 60;


def fastAvg = EMA2(data = close, "smoothing factor" = 2 / (1 + MACDfastLen));
def slowAvg = EMA2(data = close, "smoothing factor" = 2 / (1 + MACDslowLen));
def Value = fastAvg - slowAvg;
def nextAvg = ExpAverage(data = Value, MACDLen);
def HistoBar = Value - nextAvg[1];
def HiScale = HighestAll(HistoBar);
def LoScale = AbsValue(LowestAll(HistoBar));
def BarScale = if HiScale > LoScale then HiScale else LoScale;

plot macd_plot = If (showMACD, If( invertNegMACD, If ( HistoBar < 0, ( -1 * HistoBar * MACDHeight / BarScale ), ( HistoBar * MACDHeight / BarScale )), HistoBar * MACDHeight / BarScale), Double.NaN);

macd_plot.AssignValueColor(if invertNegMACD then if HistoBar >= 0 then Color.CYAN else Color.MAGENTA else Color.CYAN);
macd_plot.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
macd_plot.SetLineWeight(MACDWidth);

def hiDiff = high - high[1];
def loDiff = low[1] - low;
def plusDM = if hiDiff > loDiff and hiDiff > 0 then hiDiff else 0;
def minusDM = if loDiff > hiDiff and loDiff > 0 then loDiff else 0;
def ATR = WildersAverage(TrueRange(high, close, low), DMI_Len);
plot "DI+" =
if showADX_DMI then 100 * WildersAverage(plusDM, DMI_Len) / ATR
else Double.NaN;
plot "DI-" =
if showADX_DMI then 100 * WildersAverage(minusDM, DMI_Len) / ATR
else Double.NaN;
def DX =
if ("DI+" + "DI-" > 0) then 100 * AbsValue("DI+" - "DI-") / ("DI+" + "DI-")
else 0;
plot ADX = if showADX_DMI then WildersAverage(DX, DMI_Len) else Double.NaN;
plot ADXAvg = if showADX_DMI then ExpAverage(ADX, ADX_Avg) else Double.NaN;
plot NoTrend = No_Trend;
plot LowTrend = Low_Trend;
plot StrngTrend = Strong_Trend;

plot ArrowUp = if "DI-" crosses above ADXAvg
               then  "DI-"
               else double.nan;
     ArrowUP.SetPaintingStrategy(PaintingStrategy.Arrow_UP);
     ArrowUP.SetLineWeight(3);
     ArrowUP.SetDefaultColor(Color.White);
plot ArrowDN = if "di+" crosses above ADXAvg
               then "di+"
               else double.nan;
     ArrowDN.SetPaintingStrategy(PaintingStrategy.Arrow_DOWN);
     ArrowDN.SetLineWeight(3);
     ArrowDN.SetDefaultColor(Color.Yellow);
Alert(ArrowUp, " ", Alert.Bar, Sound.Chimes);
Alert(ArrowDN, " ", Alert.Bar, Sound.Bell);

plot ArrowUpTrend = if "DI+" crosses above "DI-"
               then "DI-"
               else Double.NaN;
ArrowUpTrend.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
ArrowUpTrend.SetLineWeight(3);
ArrowUpTrend.SetDefaultColor(Color.green);
plot ArrowDNTrend = if  "DI+" crosses below "DI-"
               then "DI-"
               else Double.NaN;
ArrowDNTrend.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
ArrowDNTrend.SetLineWeight(3);
ArrowDNTrend.SetDefaultColor(Color.red);

"DI+".SetDefaultColor(Color.GREEN);
"DI-".SetDefaultColor(Color.RED);
ADX.SetDefaultColor(Color.WHITE);
ADXAvg.SetDefaultColor(Color.YELLOW);
 
D

dmkoch

New member
Thanks for getting this started. This is a nice start - but it is showing way too many arrows for entry and exit. Line chart is good.

For the bull case - my thinking is it would only show an entry(up) arrow when ADX crosses above DI- and exit (down) arrow when the ADX begins its decline by one bar. The entry will occur only when the ADX is starting from a position below both the DI+ and DI- before making the crossover to the upside of the DI-.

The bear case is just the opposite.
 
V

VicD

Member
Thanks for getting this started. This is a nice start - but it is showing way too many arrows for entry and exit. Line chart is good.

For the bull case - my thinking is it would only show an entry(up) arrow when ADX crosses above DI- and exit (down) arrow when the ADX begins its decline by one bar. The entry will occur only when the ADX is starting from a position below both the DI+ and DI- before making the crossover to the upside of the DI-.

The bear case is just the opposite.

What are you using that has a bar? The actual Advance-Decline? NYSE or which?

It's just an indicator set to my preferences, Basically when the current DI crosses ADX on 5 minute it is end of trend IF the other DI is rising above 15 and especially above 20.

Post a picture of what you are actually looking at
 
D

dmkoch

New member
Maybe "bar" is not the right language. I was calling a price bar the high-low range - for one segment of time (a 30min) shown on the price chart.

So the idea is to stay in the trade when you see the ADX continue its ascent through the DI- but when you see the ADX start to decline - meaning the current ADX value shown under the 30 minute price bar is lower than the prior ADX value under the previous 30 min price bar - this would signal it is time to exit from the trade. The intent is to start the trend when a low ADX value (any where below 20 and below both DI+ and DI-) crosses above either of the DIs to enter the trade (long entry when ADX crosses above DI- and short entry when ADX crosses above DI+). We want to show the exit on both conditions when the ADX begins its decline. The exit signal is only dependent on the ADX reversing trend to the downside. The exit is not dependent on the DI+ or DI+.

How do I post a picture? I was trying to post PNG file to this message but not sure how.
 
D

dmkoch

New member
 
D

dmkoch

New member
In this picture you see the white ADX line (below 20) crossing the declining red DI- at 16.95. The white ADX begins rolling over between 13:00 (1pm) and 13:30 (1:30pm). The code should show an exit arrow at 13:30. This is the first move down from the incline of the ADX. This example shows a long entry.

The short entry trade would be the the same as above - only when the white ADX line is crossing above a declining green DI+ below 20 - this would be the signal to enter a short trade with an exit when ADX begins to trend down after reach its peak. This trade could play out anywhere from 1 day - 3 days.

I really appreciate your assistance in refining this code.
 
D

dmkoch

New member
Any thoughts on how I might modify VicD's thinkscript to only show arrows for entry and exit under the conditions shown in the chart above. I think VicD is on to something, but his script generates way too many entry arrows. I think the logic for script should be much more simple and only show an "up arrow" for entry when the ADX is <20 and crossing above either a declining DI+ or a declining DI-. Then the code should publish a "down arrow" to exit when the ADX moves lower (or is less than) the previous ADX bar, It is most effective when this study set-up on a 30min bar chart. These trades will typically stay open between 1-3 days. Any help on how I could modify this code would very much appreciated!
 
V

VicD

Member
Maybe try a vertical line instead of an arrow (like on ready aim fire, called "AddVerticalLine")? and use the SMA cross over just when fast crossed below the slow on the ADX.
 
Last edited:
S

srinathgnath

New member
VIP
@VicD @dmkoch where are we on this script? Could you please share if you already achieved. Thanks
 
Thread starter Similar threads Forum Replies Date
H MTF ADX? Questions 2
R Consecutive bar count for DMI and ADX? Questions 0
J CM ADX System for ThinkorSwim Custom 6
H Request: Add arrows and alert to Smart ADX Questions 0
B ADX Custom Watchlist Column for ThinkorSwim Indicators 0

Similar threads

Top