Download MetaTrader 5

The danger of small numbers

To add comments, please log in or register
Deposit to your account in any convenient way. There are 4 of them here!
Ingvar Engelbrecht
4504
Ingvar Engelbrecht 2015.02.18 11:11 

Have some code that checks MA crossovers. I discovered that I only got one type (I think it was positive cross overs) but not the other one

Changed the code and it works ok

/*    
    last = ShrtMA[1] - LongMA[1];      // No good.  Yes, "last" is a double
    if(last>0) result = 1;
    if(last<0) result -1;
*/
 
    if(ShrtMA[1] > LongMA[1]) result = 1;     // works
    if(ShrtMA[1] < LongMA[1]) result = -1;    // OK
  
Alain Verleyen
Moderator
30742
Alain Verleyen 2015.02.18 12:43  
ingvar_e:

Have some code that checks MA crossovers. I discovered that I only got one type (I think it was positive cross overs) but not the other one

Changed the code and it works ok

It's not danger of small numbers but danger of double type data.
Vladimir Karputov
Moderator
46003
Vladimir Karputov 2015.02.19 09:08  
ingvar_e:

Have some code that checks MA crossovers. I discovered that I only got one type (I think it was positive cross overs) but not the other one

Changed the code and it works ok

If you still need to compare the double, read beginning with the words: "If you still need to compare the equality of two real numbers, then you can do this in two different ways. The first...".
Documentation on MQL5: Language Basics / Data Types / Real Types (double, float)
Documentation on MQL5: Language Basics / Data Types / Real Types (double, float)
  • www.mql5.com
Language Basics / Data Types / Real Types (double, float) - Reference on algorithmic/automated trading language for MetaTrader 5
Ingvar Engelbrecht
4504
Ingvar Engelbrecht 2015.03.07 11:30  

The code I have given above as "working" is not good.

I still found some inconcistencies in behavior so I made the following changes to the code.

Result:

Consistent behavior, a lot better results

   
double mult = 10000;

    w1 = ShrtMA[1] * mult;
    w2 = LongMA[1] * mult;
    if(w1 > w2) result = 1;
    if(w1 < w2) result = -1;

Dr.Trader
3781
Dr.Trader 2015.03.07 12:47  

I had similar problems in C++. In some cases comparing double value with 0 gave incorrect result. However, comparing with 0.0 was working fine and as expected.

I have no idea how mql5 compiler interprets code, but try this: 

double last = ShrtMA[1] - LongMA[1];
if(last>0.0) result = 1;
if(last<0.0) result -1;
Share results please, I am really interested about result of this in mql5.
Alain Verleyen
Moderator
30742
Alain Verleyen 2015.03.07 13:10  
Better to understand why, than try everything and anything.
Ingvar Engelbrecht
4504
Ingvar Engelbrecht 2015.03.07 16:34  
angevoyageur:
Better to understand why, than try everything and anything.
  Out of curiosity, how would you code it??
Ingvar Engelbrecht
4504
Ingvar Engelbrecht 2015.03.07 16:39  
Dr.Trader:

I had similar problems in C++. In some cases comparing double value with 0 gave incorrect result. However, comparing with 0.0 was working fine and as expected.

I have no idea how mql5 compiler interprets code, but try this: 

Share results please, I am really interested about result of this in mql5.
I can only say that as far as I can judge this code works ok. I have other areas to concentrate on, so I will leave this as is unless someone has some objections as to the validity of the code
Alain Verleyen
Moderator
30742
Alain Verleyen 2015.03.07 18:42  
ingvar_e:
  Out of curiosity, how would you code it??
    double epsilon=_Point/2.;
    if(ShrtMA[1]-LongMA[1] > epsilon) result = 1;
    if(LongMA[1]-ShrtMA[1] > epsilon) result = -1;
Ingvar Engelbrecht
4504
Ingvar Engelbrecht 2015.03.07 20:19  
angevoyageur:
Thanks.  Interesting. One thing.   You divide by  2.    Is that a typo or or does the decimal point signify something?
Alain Verleyen
Moderator
30742
Alain Verleyen 2015.03.07 20:49  
ingvar_e:
Thanks.  Interesting. One thing.   You divide by  2.    Is that a typo or or does the decimal point signify something?
It means 2 is a double, but it's not mandatory in this case, just an habit.
12
To add comments, please log in or register