Scanning MACD crossing the zero line in a range of periods for the first time


Category:
0
0

Hello Mr. Hahn, can you create a scanner in which the macd is being crossed for the first time in a range of time? Say 60 days or beyond?

declare lower;

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;

plot Value = MovingAverage(averageType, close, fastLength) – MovingAverage(averageType, close, slowLength);
plot Avg = MovingAverage(averageType, Value, MACDLength);

plot Diff = Value – Avg;
plot ZeroLine = 0;

plot UpSignal = if Diff crosses above ZeroLine then ZeroLine else Double.NaN;
plot DownSignal = if Diff crosses below ZeroLine then ZeroLine else Double.NaN;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);

Value.SetDefaultColor(GetColor(1));
Avg.SetDefaultColor(GetColor(8));
Diff.SetDefaultColor(GetColor(5));
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
Diff.DefineColor(“Positive and Up”, Color.GREEN);
Diff.DefineColor(“Positive and Down”, Color.DARK_GREEN);
Diff.DefineColor(“Negative and Down”, Color.RED);
Diff.DefineColor(“Negative and Up”, Color.DARK_RED);
Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.color(“Positive and Up”) else Diff.color(“Positive and Down”) else if Diff < Diff[1] then Diff.color(“Negative and Down”) else Diff.color(“Negative and Up”));
ZeroLine.SetDefaultColor(GetColor(0));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

 

 

Marked as spam
Posted by (Questions: 1, Answers: 2)
Asked on June 6, 2017 11:22 pm
505 views
0

For a standard indicator included with Thinkorswim, there is no need to include the code in your post. Unless you have made an attempt at solving the problem and would like me to explain why your code doesn’t work.

So for this particular question, you simply want to scan for stocks that have not crossed the zero line, in either direction, for 60 or more days?

( at June 7, 2017 7:47 am)
0

Yes, I need a scanner that shows the macd crossing for the first time in 60 days. Please

( at June 7, 2017 9:13 am)
0

Sorry, forgot to ask which plot of the MACD you want to test. There are three. The MACD Value, the MACD Average, and the MACD Difference (histogram).

( at June 7, 2017 11:25 am)
0

No worries, MACD average

( at June 7, 2017 12:19 pm)
0
Private answer

Ok, now that we’ve cleared up a minor detail I have the code ready to go. For those trying to learn from this, be sure to check out a previous post: https://www.hahn-tech.com/ans/how-to-filter-a-scan-for-the-d-crossing-the-d-on-the-dmi-for-the-first-time-in-many-periods/

Each of these solutions contain a technique for testing if a condition has been true over a specified period of time, excluding the current bar. This is what makes it all possible. It’s used right there in the very last line of code:

def firstCrossInXBars = Highest(crossesZero[1], lookBack) < 1 and crossesZero;

If you are trying to learn and you don’t understand how this works, just post a new question in the forum asking for an explanation. Otherwise, just copy the code below and get some use out of it:

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input lookBack = 60;
input averageType = AverageType.EXPONENTIAL;
def Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
def Avg = MovingAverage(averageType, Value, MACDLength);
def Diff = Value - Avg;
def crossesZero = (Avg[1] > 0 and Avg < 0) or (Avg[1] < 0 and Avg > 0);
plot firstCrossInXBars = Highest(crossesZero[1], lookBack) < 1 and crossesZero;

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on June 7, 2017 3:43 pm