Some missed crossovers with classic moving average detection

 

Hello,

I recently realized that my code for detecting moving average crossovers was not perfect. Instead, it is the classic code that is generally used by EAs.

Here is an example for US30 (backtested in visual mode in order to see the values ​​of the moving averages at each bar opening):

Crossover

- At point 1: in reality, the crossover has not yet occurred;
- At point 2: as the US30 falls, the MA1 also falls. Then a crossover occurs, but it's just before point 1. As the EA is looking for a crossover between point 1 and point 2, it doesn't see the crossover before point 1.


Here is the code of the function:

bool maCrossCheck(int f_symbol, string f_SymbolToTrade, bool &f_buyOrSell)
  {
   double actualMa1, previousMa1;
   double actualMa2, previousMa2;
   bool crossOK = false;

   actualMa1 = iMA(f_SymbolToTrade,ma_timeframe,ma1_period,0,ma1_method,ma1_applied_price,ma1_shift);
   actualMa2 = iMA(f_SymbolToTrade,ma_timeframe,ma2_period,0,ma2_method,ma2_applied_price,ma2_shift);
   previousMa1 = iMA(f_SymbolToTrade,ma_timeframe,ma1_period,0,ma1_method,ma1_applied_price,ma1_shift+1);
   previousMa2 = iMA(f_SymbolToTrade,ma_timeframe,ma2_period,0,ma2_method,ma2_applied_price,ma2_shift+1);

   if((actualMa1 > actualMa2) && (previousMa1 < previousMa2))    //buy signal
     {
      f_buyOrSell = reversed_logic? false : true;
      crossOK = true;
     }
   else
      if((actualMa1 < actualMa2) && (previousMa1 > previousMa2))    //sell signal
        {
         f_buyOrSell = reversed_logic? true : false;
         crossOK = true;
        }

   return crossOK;
  }

All orders are properly opened except when a case like this appears.

I had the idea to do an additional check to see if a crossover occurs between bar 0 and bar 1 at the open of bar 2, but I wonder if a crossover can, depending on the volatility , occur before the bar 0.


Have you ever noticed this 'bug'?

Do you have an idea to fix it without making the code too complex?


Thanks in advance.

 

Please don't post randomly in any section. MT4/mql4 has it's own section on the forum.

I have moved your topic to the correct section, so please don't create another topic.

 
Fernando Carreiro #:

Please don't post randomly in any section. MT4/mql4 has it's own section on the forum.

I have moved your topic to the correct section, so please don't create another topic.

Hello Fernando,

Sorry, I hadn't noticed that there are several sections for EAs.

 
Matthieu Jean Baptiste Wambergue: - At point 1: in reality, the crossover has not yet occurred;

When the candle closes, the crossover has occurred. There is no “this case.” You said it yourself:

Matthieu Jean Baptiste Wambergue: but it's just before point 1.
double aPrev = …(i+1), aCurr = …(i),
       bPrev = …(i+1), bCurr = …(i);
bool   wasUp = aPrev > bPrev,
        isUp = aCurr > bCurr,
     isCross = isUp != wasUp;
 
William Roeder #:

When the candle closes, the crossover has occurred. There is no “this case.” You said it yourself:

When you make live backtests, you realize that the value of a MA can change a few candles after if there is volatility. That's why sometimes you can see a crossover on live but a few candles later, the crossover disappeared because of a change in the trend.

That's hard to explain but here the problem is caused by the same phenomenon: the change of 2 MA values 1 or 2 candles after the time of their calculation.
 
First of all just to avoid misunderstanding, candle 0 would be current candle, and you have to compare the prices of MA on close prices of candle 1 and candle 2. Also in your picture, you can see that on candle 1, faster MA is below slower MA. And on your candle 0 faster MA is above … 
 
Daniel Cioca #:
First of all just to avoid misunderstanding, candle 0 would be current candle, and you have to compare the prices of MA on close prices of candle 1 and candle 2. Also in your picture, you can see that on candle 1, faster MA is below slower MA. And on your candle 0 faster MA is above … 
What I try to make understand is that when the candle 1 opens, the crossover didn't occurred, but when the candle 2 opens a crossover appears before the candle 1. You can't see that on the picture but that's the reality.
 
Matthieu Jean Baptiste Wambergue #: What I try to make understand is that when the candle 1 opens, the crossover didn't occurred, but when the candle 2 opens a crossover appears before the candle 1. You can't see that on the picture but that's the reality.

Candle one never opens. It is closed. All indicators should be fixed. If an indicator changes after the fact, that is called repainting and is useless.

Only candle zero (right most) opens and signals can come and then go.

 

Hello, sorry, I was busy these last days, but I finally took the time to make a visual backtest with screenshots:

Crossover

As you can see, when checking only at candle opening, the classic method for ma crossing detection doesn't work here: when a candle opens at 10:15, a crossover occurs between 10:05 and 10:10, but not between 10:10 and 10:15. In reality, the crossover occurs during the 10:10 candle.

Finally, for checking at opening, I think the best is to check a crossover between:

  • Candle 1 and candle 0
  • Candle 2 and candle 1
Reason: