Topic: MACD does not really close when Crossing the zero line downward ?

Dear Popov and Staff,
the EA has the following conditions:

Open Long when Macd Line crosses the zero line upward
Close Long when Macd Line crosses the zero line downward

but it has not closed, I had to close the position manually:

https://i.postimg.cc/wMnBBTw7/image.png

PLease find EA studio EA attached.

Thanks

Post's attachments

EA Studio XAUUSD M5 1238842970 MACD Cross Sopra Sotto 0 Traling S.mq5 56.24 kb, 3 downloads since 2026-02-25 

You don't have the permssions to download the attachments of this post.

Re: MACD does not really close when Crossing the zero line downward ?

This behaviour results from how EA Studio and the exported Expert Advisors calculate signals.

Both EA Studio and the Expert Advisors use "sigma" (minimum value) when calculating the signals.

In this case: "Open Long when Macd Line crosses the zero line upward".
Long  signal = MACD[1] > 0 + sigma AND  MACD[2] < 0 - sigma
Short signal = MACD[1] < 0 - sigma AND  MACD[2] > 0 + sigma

Where:
- MACD[1] - previous bar MACD value
- MACD[2] - prev - previous bar MACD value
- sigma = 0.000001

This formula is the same in EA Studio and in the experts.

The purpose of "sigma" is to filter out false signals due to the way computers calculate the fractional numbers.
The drawback of this formula is that it ignores signals when crossovers occur with very small indicator values.

Compared to that, FSB Pro offers more advanced calculations. It goes back through values until it proves or disproves a significant signal.

We have more precise, but slower computations in FSB Pro. EA Studio is good enough and much faster.

I'll try to make both fast and reliable signal formulas in my next backtester.

Re: MACD does not really close when Crossing the zero line downward ?

Thanks for replying, but my EA studio licence expired and I have a not functioning EA on MACD, downloaded with my last license.

As remedy, I believe you should send me via email the functioning version of the EA I shared here, without the bug.

Thanks!

Popov wrote:

This behaviour results from how EA Studio and the exported Expert Advisors calculate signals.

Both EA Studio and the Expert Advisors use "sigma" (minimum value) when calculating the signals.

In this case: "Open Long when Macd Line crosses the zero line upward".
Long  signal = MACD[1] > 0 + sigma AND  MACD[2] < 0 - sigma
Short signal = MACD[1] < 0 - sigma AND  MACD[2] > 0 + sigma

Where:
- MACD[1] - previous bar MACD value
- MACD[2] - prev - previous bar MACD value
- sigma = 0.000001

This formula is the same in EA Studio and in the experts.

The purpose of "sigma" is to filter out false signals due to the way computers calculate the fractional numbers.
The drawback of this formula is that it ignores signals when crossovers occur with very small indicator values.

Compared to that, FSB Pro offers more advanced calculations. It goes back through values until it proves or disproves a significant signal.

We have more precise, but slower computations in FSB Pro. EA Studio is good enough and much faster.

I'll try to make both fast and reliable signal formulas in my next backtester.

Re: MACD does not really close when Crossing the zero line downward ?

This is the designed behaviour of the logical rule.

In your case, MACD does not cross the zero line in two consecutive bars, and the Expert Advisor does not raise a close signal.

MACD falls below zero after being effectively zero on the previous bar, where "zero" means: |MACD| < 0.000001

EA Studio computes signals more precisely than MetaQuotes' example formula. At least, we use "sigma".

https://image-holder.forexsb.com/store/metatrader-macd-expert-example-thumb.png

As you see, the given "standard" code example does not manage the case where the values are equal. (m_macd_previous and m_signal_previous in that case). However, there is no strict definition of "equality" for real numbers.

5 (edited by poteree 2026-03-20 00:29:25)

Re: MACD does not really close when Crossing the zero line downward ?

Thanks for quick replying even if it's not clear to me but it's not your responsability.

Just adding a couple of things:
1) if the EA works on EA_STUDIO showing (on the chart of the backtest) continuos opening and closing without missing trades, as user I would think the EA will act the same way on Metatrader in live trading. Without going into technical aspects.

Ea Studio Closed the trade correctly for the case I talk about in the first post:
https://i.postimg.cc/L6ys6JWX/image.png



So, I would expect, in general, for all EAs, they will work the same way on MT5 (instead I closed it manually):
https://i.postimg.cc/wMnBBTw7/image.png

Ok for a technical reason behing the "bug", but it's hard to accept in my opinion for clients seeing EA studio opening and closing the trade correctly, believing the EA will work the same on Mt5 and then it won't.

2) deactivating the EA but leaving it on chart, the EA still keep logging and very frequently (4 logs per second each pair):

2026.03.19 23:53:25.561    EA Studio Macd Sopra Sotto Zero SL e TP 1279100259 (XAUUSD,M1)    Trade context is busy! Waiting...
2026.03.19 23:53:30.658    EA Studio Macd Sopra Sotto Zero SL e TP 1279100259 (XAGUSD,M1)    The waiting limit exceeded!
2026.03.19 23:53:30.767    EA Studio Macd Sopra Sotto Zero SL e TP 1279100259 (XAGUSD,M1)    Order Send retry: 2
2026.03.19 23:53:30.767    EA Studio Macd Sopra Sotto Zero SL e TP 1279100259 (XAGUSD,M1)    Trade context is busy! Waiting...
2026.03.19 23:53:55.634    EA Studio Macd Sopra Sotto Zero SL e TP 1279100259 (XAUUSD,M1)    The waiting limit exceeded!
2026.03.19 23:53:55.745    EA Studio Macd Sopra Sotto Zero SL e TP 1279100259 (XAUUSD,M1)    Order Send retry: 3
2026.03.19 23:53:55.745    EA Studio Macd Sopra Sotto Zero SL e TP 1279100259 (XAUUSD,M1)    Trade context is busy! Waiting...
2026.03.19 23:54:00.869    EA Studio Macd Sopra Sotto Zero SL e TP 1279100259 (XAGUSD,M1)    The waiting limit exceeded!
2026.03.19 23:54:00.976    EA Studio Macd Sopra Sotto Zero SL e TP 1279100259 (XAGUSD,M1)    Order Send retry: 3
2026.03.19 23:54:00.976    EA Studio Macd Sopra Sotto Zero SL e TP 1279100259 (XAGUSD,M1)    Trade context is busy! Waiting...

Re: MACD does not really close when Crossing the zero line downward ?

> 1) if the EA works on EA_STUDIO showing (on the chart of the backtest) continuos opening and closing without missing trades, as user I would think the EA will act the same way on Metatrader in live trading.

The EA Studio chart shows "Premium" data. These data are composed from tick files from Dukascopy. These are different data from the ones in your MetaTrader 5.
You can import the actual MT data into EA Studio to check whether the signal corresponds. Please note that the Premium data quotes XAUUSD to 3 decimal digits, whereas your MT Terminal most likely quotes it to 2 decimal digits.

I tested all EA Studio indicators against MT. I'm sure EA Studios' MACD matches MT's MACD to the 10th decimal place.

> Ok for a technical reason behind the "bug", but it's hard to accept in my opinion, for clients seeing

This is absolutely reasonable!

The signals, like "X crosses below the zero line", sound simple and are easy to implement programmatically from programmers' viewpoint.  MetaTrader 5 shows MACD up to the 3rd decimal digit. To be sound from the "customers" point of view, the calculations should be up to the 3rd digit as well, but that is not precisely enough.

There are also discrepancies between the indicator values and the chart's visual representation. It is because the chart uses rounded pixels after scaling the actual values.

I chose 6 digits for the signal calculations and a simplified formula as a compromise between speed and precision. But, as I already said, it will be further improved.

...

> 2) deactivating the EA but leaving it on the chart, the EA still keeps logging and very frequently (4 logs per second, each pair):

I almost agree:

> 4 logs per second

These are actually 3 logs smile, but yes, one log would be enough.

> ...each pair

This is the correct behaviour. Each expert is independent.

> deactivating the EA but leaving it on the chart

You are right here!

There is no point in waiting 0.1 seconds for the user to enable the trading. I'll cancel the signal entirely if the trade is disabled.

Thank you for the report!

Re: MACD does not really close when Crossing the zero line downward ?

You can easily disable the "noise" protection by setting the "sigma" value to 0.

It is on line 101 of your expert.

This is the original code:

https://image-holder.forexsb.com/store/macd-xauusd-sigma-settings-mql-editor.png

Set it like that:

const double sigma = 0.0;

Backtest the expert in the MetaTrader tester before and after the change and compare the results.
I did it on Dukascopy MT5, and it shows no difference.

It may or may not show differnce in your data.

...

If you want, you may slightly modify the closing signal formula. It is on line 370 of your expert.

Change it from:

void ManageClose(void)
  {
   // MACD (Close, 12, 26, 9)
   double ind2buffer[]; CopyBuffer(indHandlers[0][2][0], 0, 1, 3, ind2buffer);
   double ind2val1  = ind2buffer[2];
   double ind2val2  = ind2buffer[1];
   bool   ind2long  = ind2val1 < 0 - sigma && ind2val2 > 0 + sigma;
   bool   ind2short = ind2val1 > 0 + sigma && ind2val2 < 0 - sigma;

   if((posType == OP_BUY  && ind2long) ||
      (posType == OP_SELL && ind2short))
      ClosePosition();
  }

To:


void ManageClose(void)
  {
   // MACD (Close, 12, 26, 9)
   double ind2buffer[]; CopyBuffer(indHandlers[0][2][0], 0, 1, 3, ind2buffer);
   double ind2val1  = ind2buffer[2];
   double ind2val2  = ind2buffer[1];
   bool   ind2long  = ind2val1 <= 0.0 && ind2val2 > 0.0;
   bool   ind2short = ind2val1 >= 0.0 && ind2val2 < 0.0;

   if((posType == OP_BUY  && ind2long) ||
      (posType == OP_SELL && ind2short))
      ClosePosition();
  }

The change is from:

   bool   ind2long  = ind2val1 < 0 - sigma && ind2val2 > 0 + sigma;

to:

   bool   ind2long  = ind2val1 <= 0.0 && ind2val2 > 0.0;

After the modification, the signal will sound like: MACD becomes greater than zero after being below or equal to zero.