Indicator rounding and cross (x> and x<) logic
-
After some extensive testing, it seems there is a bug with how X> and X< behaves related to indicator values... especially when those indicator values are very close together. Based on observation, this appears to be due to rounding. The result is that not all X> and X< events are captured. Let me explain.
I have this condition block, which checks to see if EMA(8) crossed above EMA(13) in the previous candle (Candle ID = 1)...

When I backtest this EA in MT5 I see indicator values that look like this (last four bars)...
Candle ID = 3 (3 candles ago)

Candle ID = 2 (2 candles ago)

Candle ID = 1 (1 candle ago)

Candle ID = 0 (current candle)

Notice how EMA(8) is below EMA(13) for candle 3 and candle 2, but then climbs above it on candle 1 and further above on candle 0. The cross happens on candle 1. That is... it SHOULD happen. But it does not. The condition is never met in the EA. And based on reviewing several such examples, it seems to be related to rounding. It seems that the EA treats EMA(8) and EMA(13) as the same value since they both round to 1.17582, and as a result the "greater than" (>) comparison in the MQL code results in a FALSE value. As such, the cross is never detected.
At least... that's what SEEMS to be going on. Thoughts? How do I make sure ALL of my EMA crosses produce a trigger every time... even when the difference between the EMA values is a fraction of a point? Do I need custom MQL code block and/or some variables to store the EMA values so that this rounding doesn't occur?
-
@sidmcfarland you have a example EA proving this bug? Maybe draw a vertical line every time the MAs cross?
-
I do have an EA, but I'm not at liberty to share it. However, I did confirm the issue is due to rounding of the EMA value. I added a comment block to output the EMA values onto the chart. It looks like this...

However, on that same candle MT5 reports the EMA values like this...

So the EA seems to be ignoring that last digit or rounding (pretty sure it's rounding based on other examples I've seen). As a result, the cross is never detected.
If needed, I could create a very simple EA that just looks for an EMA cross and demonstrate how it doesn't capture ALL crosses.
-
Here's a good example.
https://fxdreema.com/shared/zL7SQMC4I ran that EA in strategy tester in MT5 on EURUSD 1-hour chart for the current month (Oct 1, 2020 to Oct 16, 2020). Here are the results.

Notice how there are several crosses that have no lines.
-
@sidmcfarland I think you are on to something... I even modified the example so that there is no cross operator, just two separate conditions with candle ID 1 and 2 (= how the cross theoritically works).
Even then I dont get a line on one particular cross. -
You're getting more lines than me after making your changes to define the cross explicitly with two conditions (< on candle 1 and > on candle 2, and vice versa). However, you're still not getting them all (as you mentioned/circled). I think that's probably because you're using < and > instead of <= and >=. Just a guess.
So I suppose I could replace my X> and X< conditions with two conditions...
Instead of A (candle 1) X> B (candle 1), use A (candle 1) > B (candle 1) AND A (candle2) <= B (candle 2)
Instead of A (candle 1) X< B (candle 1), use A (candle 1) < B (candle 1) AND A (candle2) >= B (candle 2)A little more complex, but it works (I got all the crosses now)...
https://fxdreema.com/shared/aTk06CFM

Unfortunately, it defeats the purpose of X> and X<, and is a little more complex (requires two condition blocks instead of one).
-
@sidmcfarland yeah, maybe that is the bug inside the cross operator: it forgets to check the = case, just like I did
-
Lesson learned. Steer clear of X> and X<
... at least until they work correctly. Use two blocks instead explicitly specifying <,>= or >,<= Thanks for the help @roar. -
i knew my logic wasn't flawed...thanks for the workaround too.
