How to filter a scan for the D+ crossing the D- on the DMI for the first time in many periods?


  • Questions
  • Stock Scanners
  • How to filter a scan for the D+ crossing the D- on the DMI for the first time in many periods?
Category:
0
0

Greetings Mr. Hahn,

I was attempting to filter your DMI study into scan that searched for stocks in which the D+ crosses the D- for the first time in “x” amount of periods on a daily chart, ideally, I see that when the D+ crosses for the first time the D- in 90 days is when you can anticipate a stock’s major move. I really wish I could figure this out, I attempted having something as a negative value and inputting “lookback period” however my scan did not work. I have attempted creating this scan for hours without any success. Here is your DMI study to see if you could help me out figuring this scanner .

input length = 14;
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), length);
plot “DI+” = 100 * WildersAverage(plusDM, length) / ATR;
“DI+”.SetDefaultColor(color.GREEN);
plot “DI-” = 100 * WildersAverage(minusDM, length) / ATR;
“DI-“.SetDefaultColor(color.RED);
def DX = if (“DI+” + “DI-” > 0) then 100 * AbsValue(“DI+” – “DI-“) / (“DI+” + “DI-“) else 0;
plot ADX = WildersAverage(DX, length);
ADX.SetDefaultColor(color.YELLOW);
plot line1 = 30;
line1.SetStyle(Curve.LONG_DASH);
line1.SetDefaultColor(color.MAGENTA);
plot line = 20;
line.SetdefaultColor(color.WHITE);
line.SetStyle(Curve.SHORT_DASH);

 

 

As always I greatly appreciate your help, share the wealth!!

Attachments:
Marked as spam
Posted by (Questions: 6, Answers: 14)
Asked on June 6, 2017 10:38 am
1704 views
0

On the first screenshot second to bottom study, around the center, you can notice how the D+ is always below the D- and barely two days ago the D+ crossed above the D-, and we could have anticipated that major move.

Same thing on the second image, the D+ was below the D- without ever crossing it, and when it did, again, we could have anticipated that move.
I hope we can all benefit from this scanner, as I believe this will be really effective.

( at June 6, 2017 10:49 am)
1
Private answer

So I see that you have modified your specifications from the original post on this topic. Located here: https://www.hahn-tech.com/ans/scan-dmi-plus-crosses-dmi-minus/

So rather than start with the code you provided in the current post, I will be modifying the code I provide in the solution to your previous post. That code simply scanned for stocks where the DI+ and DI- lines crossed.

What I will add here is an additional requirement that the cross is the first to have occurred in the previous X number of bars. Since you suggested a 90 day look back is preferred, I will provide an input for this parameter and set it’s default value to 90.

Here is the code, and a screenshot below to show how the signal is being picked up by the scan code.

input length = 14;
input averageType = AverageType.WILDERS;
input threshold = 20;
input lookBack = 90;
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 = MovingAverage(averageType, TrueRange(high, close, low), length);
def diPlus = 100 * MovingAverage(averageType, plusDM, length) / ATR;
def diMinus = 100 * MovingAverage(averageType, minusDM, length) / ATR;
def DX = if (diPlus + diMinus > 0) then 100 * AbsValue(diPlus - diMinus) / (diPlus + diMinus) else 0;
def diPlusCrossesAbove = diPlus[1] < diMinus[1] and diPlus > diMinus;
def diPlusCrossesBelow = diPlus[1] > diMinus[1] and diPlus < diMinus;
# use this scan to check for DI+ crossing above DI-
plot scan = Highest(diPlusCrossesAbove[1], lookBack) < 1 and diPlusCrossesAbove;
# use this scan to check for DI+ crossing below DI-
#plot scan = Highest(diPlusCrossesBelow[1], lookBack) < 1 and diPlusCrossesBelow;

The ability to check for the presence of a signal X number of bars in the past is very useful. It’s a tool that you should understand how to use. So practice with this and make sure you understand exactly what is taking place in the Plot Scan statements.

Attachments:
Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on June 6, 2017 12:04 pm
0

I tried it and it works perfectly, however, I got less results than I expected, I am more familiar with using the condition wizard, however, I went online to the thinkscript website to see what would the ”reserved phrase” would be for the lookback period to be 90 days or greater than 90 days so it shows more securities?
Lastly, would there be any condition that could make the scan search for stocks in which the D+ has crossed the D- WITHIN lets say 5 days or less than five days? As five days is more or less what it takes the trend to take place.

Mr. Hahn I really see potential in this scanner, you are the BEST!!!!!!
Thank you so much for your input.

( at June 6, 2017 12:53 pm)
0

The look back period of 90 days is controlled by the input line:
input lookBack = 90;

That appears at the very top of the code and you simply change it there.

( at June 6, 2017 1:18 pm)
0

Hey, this is a very interesting scanner. However, when I scan I only receive a static number of 90 days. Is there any way this scan could actually search a range of days? For example, if I wanted the scanner to search for a range of 90 to 120 days, would that be possible? It would save time and give me more stock results in a single search rather than searching one by one such as day 90, day 91, day 92, day 93…etc.

Thank you!

( at June 6, 2017 2:37 pm)
0

Hey Maria, that would actually be very efficient and less time consuming, would there actually be a condition to look for a range of days rather than a single lookback period Mr. Hahn?

( at June 6, 2017 2:49 pm)
0

Actually the lookback period already does this. It is not a static 90 days. It is 90 days or greater. It is testing to see if the condition has been absent for the past 90 or more bars. So changing the lookback period from 90 to 45 would pick up all the same stocks that were included with the 90 lookback bars, plus all the new stocks that are picked up using the 45.

( at June 6, 2017 3:07 pm)
0

Hey Mr. Hahn
I found your videos on youtube, and they blew my mind. You are the man. I am experimenting with building custom indicators, and it’s pretty cool to see trends appearing. I am starting to a build scanner in thinkorswim. It is bound by Parabolic SAR dots, ADX line increasing, and the code in this article. My question is similar to the precious question from “Macro venegas.”
When using this code in a “custom scan,” how can I limit the results of the scan to only find stocks that experienced a DI+ cross DI- in the last 5 days?
I get no signal when the input lookback = 90. I lowered the number to 9, and started seeing the indicator work. However, I think something needs to added to this code to be able to look back 1 or 2 or 5 bars (days) ago. Similar to scanning for a parabolic SAR dot shift from 1 day ago. Do you know what I’m saying?
Please help master.
Thanks, Phil

( at August 2, 2018 12:13 pm)
0

Thanks for that great feedback. Very glad you are finding value here in the Q&A forum just as in our videos. The best way I can answer your question is to explain these last two statements in the scan:

# use this scan to check for DI+ crossing above DI-
plot scan = Highest(diPlusCrossesAbove[1], lookBack) < 1 and diPlusCrossesAbove; # use this scan to check for DI+ crossing below DI- #plot scan = Highest(diPlusCrossesBelow[1], lookBack) < 1 and diPlusCrossesBelow;

Highest(diPlusCrossesAbove[1], lookBack) < 1 .... means that we look back x number of bars to make sure we have NOT had a di+ crossing above (excluding the current bar).

and diPlusCrossesAbove .... means we also have a di+ cross above on the current bar. The lookback period affects how far back we look to make sure there have not been any cross above. The cross below works exactly the same way. So I will omit duplicating this explanation.

What I get from your request is that you want to find any and all of these exact signals that have occurred in the previous x days. So at this point I hope you see that the lookback period can never give you that result. In fact we need to make some modifications to get to your goal. We have to turn the "plot" statements to "def" then add a new line for each to plot scan results including the previous x bars.

So we change:
plot scan = Highest(diPlusCrossesAbove[1], lookBack) < 1 and diPlusCrossesAbove;
To this:
def signal1 = Highest(diPlusCrossesAbove[1], lookBack) < 1 and diPlusCrossesAbove;

Then we add this:
plot scan = Highest(signal1, 5) > 0;

That new line is saying: “check the most recent 5 bars to see if signal1 has been greater than zero”. True equals 1, False equals 0. So the True/False output of signal1 is either a 1 or a zero. If any of the previous 5 bars is equal to 1 the scan returns that stock in it’s results output.

( at August 2, 2018 12:30 pm)
0

Thank you Sire.
I will try these modifications, and see what happens.
I just donated $50.

( at August 2, 2018 3:29 pm)
0

Thank you Phil. You are awesome. Very grateful for your generous contribution.

( at August 2, 2018 6:02 pm)
1
Private answer

Hey, this is a very interesting scanner. However, when I scan I only receive a static number of 90 days. Is there any way this scan could actually search a range of days? For example, if I wanted the scanner to search for a range of 90 to 120 days, would that be possible? It would save time and give me more stock results in a single search rather than searching one by one such as day 90, day 91, day 92, day 93…etc.

Thank you!

Marked as spam
Posted by (Questions: 1, Answers: 2)
Answered on June 6, 2017 2:37 pm