Moving averages with same value

 

Could you kinldy explain the below situation?

I write the following:

Fast_Ema = iMA(NULL,NULL,9,0,MODE_EMA,PRICE_CLOSE,1) ;

Slow_Ema = iMA(NULL,NULL,18,0,MODE_EMA,PRICE_CLOSE,1) ;

In my expert I write the entry condition as follow:

... && Fast_Ema>Slow_Ema && ... ;

There are some circumstances when the moving averages assume the same value (so the condition to trade is not true) but the expert opens the trade.

For instance, I see on the monitor that both moving averages on the previous candle assume the value of 1.2273, but the expert opens the trade.

I think there is a problem of digits but I don’t know how to solve.

 
Alberto_jazz:

I think there is a problem of digits but I don’t know how to solve.

See here for the most common problems and solutions related to working with doubles -> https://www.mql5.com/en/articles/1561.
 

i got around the lines being equal scenario in my ea by doing if lines are equal take value from previous bar, that way it keeps looking at previous bars untill it finds the last one before they were equal, that still makes the eventual completion of the cross a valid trade, if it doesnt cross and goes back the other way the scenario ends by itself.

 
Alberto_jazz:

Could you kinldy explain the below situation? ....

There are some circumstances when the moving averages assume the same value (so the condition to trade is not true) but the expert opens the trade.

For instance, I see on the monitor that both moving averages on the previous candle assume the value of 1.2273, but the expert opens the trade.

I think there is a problem of digits but I don’t know how to solve.

What (double values) is being displayed can be truncated by IndicatorDigits, NormalizeDouble, & the default limitation of Comment for examples. While the calculation always use the stored values in memory. So if you see they're equal doesn't mean they are (in the n'th digit after point)...

... && Fast_Ema>Slow_Ema && ... ; 

I think your cross criteria can also gives problem in this matter (regardless of what the ommited parts are). I use these :

if( Fast_Ema[i+1] < Slow_Ema[i+1] && Fast_Ema[i+2] >= Slow_Ema[i+2] ) // checking Down-cross on last completed bar

if( Fast_Ema[i+1] > Slow_Ema[i+1] && Fast_Ema[i+2] <= Slow_Ema[i+2] ) // checking Up-cross on last completed bar

HTH

 
if( (Fast_Ema[i+1] - Slow_Ema[i+1]) * (Fast_Ema[i+2] - Slow_Ema[i+2]) <0) {
   // EMA's crossed
   if( Fast_Ema[i+1] < Slow_Ema[i+1] ) { // Sell...
 
That's a new one to add to the arsenal WH, Thanks.