Finding a Macd Crossover below the zero line


Category:
0
0

Hi Peter,

I want to know if there is a way to modify the macdhistogramCrossover indicator to allow it to scan or screen for a bullish crossover (macd crossing over its signal)  that takes place specifically below the macd zero line ? Is there also a way to find out how deep is the crossover below the zero line ?  Thanks so much and happy new year by the way.

Marked as spam
Posted by (Questions: 2, Answers: 6)
Asked on January 3, 2017 11:29 pm
5603 views
0
How about if you want just crossover both above/beliw zero line
( at October 3, 2019 5:03 pm)
0
A scan for MACD crossovers can very easily be constructed without writing a single line of code. You can use the Condition Wizard to build such a scan. Details here: https://www.hahn-tech.com/thinkorswim-condition-wizard/
( at October 3, 2019 7:19 pm)
0
Hello Pete, awesome info and thanks for the help you bring to all the traders. I honestly don't have a clue on coding and I try watching the video you mentioned above for the condition wizard where MACD explanation starts around min 43. However, I'm still trying to figure out how to get a specific code for the conditions I'm looking for on my scan for MACD and I don't know how to create it. What I have noticed is that these scans are showing the crosses but way above/below the zero line or anytime a cross happens. I'm basically looking for a scanner, for an intraday 5 min chart, that shows stocks that are approaching the zero line (3 candles) before it crosses over the zero line. It can be in either direction, bull/bear (separate scans would be better). Is it possible? thanks in advance, Luis
( at May 5, 2020 6:14 pm)
0
"3 candles before it crosses the zero line".... ok so you want a scan that can read the future. Sure, we can do that. Just close your eyes and dream. That's as close as we can get. Best we can do in reality is to capture the cross above the zero line or below. Which is exactly what the solutions for this post provide.
( at May 5, 2020 7:15 pm)
1
Private answer

In response to question from Yvens Junior Ulysse

can this be made into a scanner? been trying and couldnt get it to work

Yes, and instead of just giving you the code I will try to explain the process so you can apply this method to other studies.

You see in the code there are 6 statements that begin with the word “plot”. A scan will only permit a single “plot” statement. Start at the top of the code, and change the first four “plot” to “def”. So we convert the first four to “def” because we still need those values. Once you do this, you will notice several statements immediately below these that turn red. Those are style statements for the plots. We changed them from “plot” to “def” so they will not accept style statements. We simply delete those lines. All the way from Value.SetDefaultColor(GetColor(1)); down to ZeroLine.SetDefaultColor(GetColor(0));

Next, we need to get rid of the two plot statements at the bottom. Likewise, each of these are followed by style statements. Once you delete the last two plot statements those style statements will turn red and need to be deleted.

So having followed these steps the last two lines of code should be:

def ValueCrossBelowZeroline = Value[1] < Avg[1] and Value[1] < 0 and Value > Avg;
def ValueCrossAboveZeroline = Value[1] > Avg[1] and Value[1] > 0 and Value < Avg;

These are the signals we’ll use in the scan.

If you want to scan for both conditions at the same time you will use:

plot scan = ValueCrossBelowZeroline or ValueCrossAboveZeroline;

And if you want to scan for each signal separately you will use one of the following:

plot scan = ValueCrossBelowZeroline;

or

plot scan = ValueCrossAboveZeroline;

Don’t worry if it doesn’t work out the first time. If you don’t get a working scan the first time through, don’t try and patch it together. Just erase all the code and start over from the beginning.

Hope it helps.

Marked as spam
Posted by (Questions: 37, Answers: 4117)
Answered on January 21, 2017 7:18 pm
0

Thanks Pete. That explanation helped a lot in helping us convert indicators into scans.

( at January 28, 2017 12:06 pm)
0
Private answer

In response to the original question from Obelix Gaulois

Yes and this is pretty simple. You didn’t provide a link or the name of which video you are referring too so I’ll just post a couple lines of code that will express the conditions specified in your question.

Before we get into that. You asked if it were possible to tell “how deep is the crossover below the zero line”. Very important to note that MACD does not have true “overbought/oversold” levels. The amount the lines move above or below zero is directly related to price of the underlying instrument. A 5 dollar stock will have a much lower range above and below zero then a 500 dollar stock. So, we’ll just skip that request in our solution. I suggest you combine this with an RSI or a Stochastic if you want to cover the overbought/oversold conditions.

Ok, so let’s get to the code:

This is taken from a standard MACD so we have the same inputs and variable names. Screenshot attached.

declare lower;

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
plot Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
plot Avg = MovingAverage(averageType, Value, MACDLength);
plot Diff = Value - Avg;
plot ZeroLine = 0;
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));


# now for the signal
def ValueCrossBelowZeroline = Value[1] < Avg[1] and Value[1] < 0 and Value > Avg;
plot belowZeroCross = if ValueCrossBelowZeroline then Value else Double.NaN;
belowZeroCross.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
belowZeroCross.SetDefaultColor(Color.CYAN);
def ValueCrossAboveZeroline = Value[1] > Avg[1] and Value[1] > 0 and Value < Avg;
plot aboveZeroCross = if ValueCrossAboveZeroline then Value else Double.NaN;
aboveZeroCross.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
aboveZeroCross.SetDefaultColor(Color.MAGENTA);

Attachments:
Marked as spam
Posted by (Questions: 37, Answers: 4117)
Answered on January 4, 2017 9:47 am
0
For the above code, for the signal. How to code a signal, if macd line crosses above zero line or if macd line crosses above -1 or if macd line crosses above -2 should generate only 1 buy signal. Thanks in advance.
( at November 17, 2019 4:07 pm)
0
Private answer

Hi Pete,

Sorry for the late reply. I really appreciate the prompt reply. I haven’t had the chance to play with the answer you posted but I’m sure I will be able to transform it into a TOS scan which I was looking for.  I now have an indicator and I can try to turn in into a scan. After I do, I can post a follow up. Thanks again Pete.

Marked as spam
Posted by (Questions: 2, Answers: 6)
Answered on January 11, 2017 6:35 pm
0
Hi! Can you please help me here. I have followed those steps but still its not working for scan stock. Its giving me an error at the bottom about "Exactly one plot expected" and the "OK" Button is still gray.
( at September 23, 2020 9:23 am)
0
Private answer

can this be made into a scanner? been trying and couldnt get it to work

Marked as spam
Posted by (Questions: 6, Answers: 8)
Answered on January 21, 2017 6:28 pm
0
Private answer

wow! i wasn’t expecting you to reply so quickly, especially over the weekend. thanks! it works. Can the same be done for bearish percentDLength percentKLength crosses above 40 and bullish percentDLength percentKLength crosses below -40 for stochastic momentum index indicator? fought with it for a while and clearly didnt win haha (i’m new at this).

Marked as spam
Posted by (Questions: 6, Answers: 8)
Answered on January 22, 2017 7:24 am
0

Sure can. But I suggest you post that as a new question to make is easier for others to find. This thread is starting to wander a bit. Please include whatever code you have, even if it doesn’t work. There may be a learning opportunity there.

( at January 22, 2017 9:41 am)
0
hello. Im trying to learn how to enter macd crossovers into the TOS scanner, and how to qualify that event as happening below the zero line. Have used this code below, and not getting the results I was wanitng. [MACD(12,26,9). "Value" is greater than MACD(12,26,9). "AVG"] Do you have a guide to do this, for someone who does not know coding?
( at May 12, 2020 5:27 pm)
0
Step-by-step instructions here: https://www.hahn-tech.com/ans/macd-value-line-crosses-avg-line-with-close-above-200-ema/
( at May 12, 2020 6:00 pm)
0
Private answer

peter, thank you for these great tutorials!! In regards to this scan is it possible to put a code in that this happened “Within” a certain period? I attempted to add “within 3 bars;” but it did not like that code.


Marked as spam
Posted by (Questions: 1, Answers: 4)
Answered on March 8, 2017 1:34 pm
0

If you are speaking about the scan. We can test to see if this occurred exactly three bars ago:
plot scan = ValueCrossBelowZeroline[3];
We can also test to see if it occurred in any of the most recent three bars, including the current:
plot scan = Highest(ValueCrossBelowZeroline, 3) > 0;

( at March 8, 2017 4:36 pm)
0
Private answer

Hello Pete,

While searching through the forum, I found exactly what I was looking for.  However, I think there is something wrong with my computer (a Mac Mini) or the TOS scanner.  I included a screenshot of my scan results and it is indicating that out of 521 stocks, 498 meet this scan criteria and I seriously doubt that.  The scan (Bobs_MACD_Scan) I’m using is the one you have posted and I have included it below.  The strange thing is that any new scan I create comes up with the same results.  If I use older and different kinds of  scans, they work perfectly.  I closed TOS and deleted the ‘User GUI’ file and loaded TOS back up so I am running the current software.  When you run this scan, what kind of results are you receiving?

declare lower;
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
plot Value = MovingAverage(averageType, close, fastLength) – MovingAverage(averageType, close, slowLength);
plot Avg = MovingAverage(averageType, Value, MACDLength);
plot Diff = Value – Avg;
plot ZeroLine = 0;
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));

# now for the signal
def ValueCrossBelowZeroline = Value[1] < Avg[1] and Value[1] < 0 and Value > Avg;
plot belowZeroCross = if ValueCrossBelowZeroline then Value else Double.NaN;
belowZeroCross.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
belowZeroCross.SetDefaultColor(Color.CYAN);
def ValueCrossAboveZeroline = Value[1] > Avg[1] and Value[1] > 0 and Value < Avg;
plot aboveZeroCross = if ValueCrossAboveZeroline then Value else Double.NaN;
aboveZeroCross.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
aboveZeroCross.SetDefaultColor(Color.MAGENTA);

plot scan = ValueCrossBelowZeroline;

Attachments:
Marked as spam
Posted by (Questions: 2, Answers: 21)
Answered on November 24, 2017 1:02 pm
0

From your screenshots, I see you are placing a reference to the saved custom study in the study filter. I never recommend this, nor do I demonstrate doing so unless working with a licensed study. (for licensed studies we have no choice). The problem is that your saved custom study as more than a signal plot. And only one plot is acceptable for a study filter in a scan. If you were to paste the entire code in the study filter (which is the only way I demonstrate when working with custom studies), you will get a complier warning, stating that only one plot was expected.

There is a way to do this while using a reference to your saved custom study. But you need to carefully construct the code to define which of the many plots you want to pull from the saved custom study. I don’t recommend this, and only use it when working with licensed studies.

( at November 24, 2017 1:47 pm)
0
Private answer

Thanks for your input Pete as I now have the scan working correctly.

Marked as spam
Posted by (Questions: 2, Answers: 21)
Answered on November 24, 2017 2:52 pm