MACD Two Line Zero Cross


Hi Pete,

I’m referencing the solution you provided previously for a scan ..

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;

I am seeking a few modifications…

  1.  I would like to apply this to a Chart Study for MACD TwoLines.  I already use your MTF MACD study (which is wonderful, but I want to avoid the histogram on this one for clutter’s sake).
  2. I would like to apply the study to the MACD Fast line crossing the Zero line rather than the slower line.
  3. And lastly, can we apply an alert?

I thank you in advance, I appreciate any help you can offer.

Marked as spam
Posted by (Questions: 6, Answers: 11)
Asked on October 28, 2020 9:46 am
Private answer

Whenever you reference a previous post or some other resource from another site it's very polite to include a link to that resource. Many of our viewers will be wondering where that code came from and would like to be able to click straight through to view the solution in full context.

So before we go any further I will provide a link to that post here:

In that post we find the request was to find the first cross within a specified number of bars. Since you are including that code as a reference point then we will make sure to include that in this solution. Viewers can click that link I just provided to get a full explanation of how the code works.

I see that you want this solution to work as a custom chart study, but display the two lines without the histogram. So I will:

  1. copy the code from that previous post,
  2. remove the lines pertaining to the histogram,
  3. change the def statements for Value and Avg, to display as plots,
  4. add a new plot for the zero line,
  5. then add new lines of code to facilitate the crossover signals and alerts


declare lower;
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input lookBack = 60;
input averageType = AverageType.EXPONENTIAL;
plot Value = MovingAverage(averageType, close, fastLength) – MovingAverage(averageType, close, slowLength);
plot Avg = MovingAverage(averageType, Value, MACDLength);
plot centerLine = 0;
def crossAbove = value[1] < 0 and value > 0;
def crossBelow = value[1] > 0 and value < 0;
plot crossAboveSignal = if Highest(crossAbove[1], lookBack) < 1 and crossAbove then centerLine else Double.NaN;
plot crossBelowSignal = if Highest(crossBelow[1], lookBack) < 1 and crossBelow then centerLine else Double.NaN;
Alert(Highest(crossAbove[1], lookBack) < 1 and crossAbove, "Value Crossing Above Zero", Alert.BAR, Sound.RING);
Alert(Highest(crossBelow[1], lookBack) < 1 and crossBelow, "Value Crossing Below Zero", Alert.BAR, Sound.RING);

Marked as spam
Posted by (Questions: 39, Answers: 3664)
Answered on October 28, 2020 12:26 pm
Thank you so much. This is exactly what I was looking for. A real thing of beauty.
( at October 28, 2020 12:46 pm)
Everything seems to be working well except the alerts are not sounding. th boolean arrows appear as expected but no accompanying alert sound.
( at October 29, 2020 7:17 am)
The alerts in this code use the exact same signals which generates the arrows. So if you are not getting pop-up and sound alerts on your platform the issue is most likely in the application settings. But that would mean that none of your other chart studies are able to generate alerts.
( at October 29, 2020 7:56 am)
I have other alerts active on the chart and they are working fine. Not sure why these alerts are not firing .
( at October 29, 2020 8:31 am)
I've noticed that the arrows sometimes appear then disappear and reappear again as the scenario is forming. Perhaps this is why the alert is not sounding?
( at October 29, 2020 8:39 am)
The alerts work fine for me when I test it. Yes, the arrow and come and go until the bar closes. But using the default settings for the alerts of this code it should trigger the very moment the arrow appears the first time. ONCE per bar.
( at October 29, 2020 9:17 am)
okay. I'm trying to troubleshoot. I did change the MACD inputs for fast er response. from 12,26,9 to 9,12,9. I also changed the look back to 20. I made no adjustments to the alerts or boolean. I would think those adjustments should have no bearing on the alerts sounding.
( at October 29, 2020 9:38 am)
I cant seem to figure this out. I tried deleting the code and reloading it. still no alerts. tried adjusting the inputs still nothing. I'm stumped. Other alerts of boolean arrows on my charts working fine. I am on an iMac. Seems like there are some TOS glitches with mac. For example, custom sounds for alerts is a known issue and still has not been resolved.
( at October 29, 2020 1:04 pm)
I use a Mac for everything I do in Thinkorswim and the alerts work on mine. I don't have any other ideas for you, sorry. They still haven't fixed custom sounds yet? Must be they are too busy with the Schwab integration.
( at October 29, 2020 1:30 pm)
Well, I’ll keep working on it and if I find some sort of solution a reason why I’m having difficulty I will post it here. Thanks for your help and consideration on this.
( at October 29, 2020 3:00 pm)
Oops, found an issue. I decided to let this run in the background to check if it was working correctly. I discovered one change I made after testing this that ended up causing the alert function to break. Terribly sorry about that. I have updated the code in my solution to correct the issue that prevented alerts from being triggered.
( at October 29, 2020 4:46 pm)
Thank you for continuing to work on this. It's operating perfectly now. phew. This issue definitely had my mind spinning. Your solution saved me considerable future headache.
( at October 30, 2020 7:57 am)