
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
an acceptable margin of error, in the case of 5 digit EURUSD, that would be 0.00001, so a simple function to solve the issue is:
You could of course add a precision parameter and improve this function.
So I have investigated on the rounding function of NormalizePrice
Here is my finding
std-lib:
will give as result: 1.3559999999999999
while my function will return: 1.3560000000000001
Now the question is, which is better?
Better use this function to display doubles instead of (string)dbl or Print(dbl);
It is equivalent to repr of python.
To check your functions for correct rounding (up, down or truncating) you can use this:
Welcome!
One final hint that might help you is that, I always find the most reliable way to debug any rounding function is to use some test cases that involve exact comparisons with the hard-coded values of the expected answers.
DO NOT rely on the visual output of Print or any string function, as this involves converting the double answer to string, which is another source of error in itself.
I include an example of how to correctly test your function:
if anytime you need to use Print(dbl), use Print(Repr(dbl)) instead;
because in MQL, this equality does not hold for a lot of numbers.
So, what you see is not aways what you get. For example, consider this simple calculation
One final hint that might help you is that, I always find the most reliable way to debug any rounding function is to use some test cases that involve exact comparisons with the hard-coded values of the expected answers.
DO NOT rely on the visual output of Print or any string function, as this involves converting the double answer to string, which is another source of error in itself.
I include an example of how to correctly test your function:
if anytime you need to use Print(dbl), use Print(Repr(dbl)) instead;
because in MQL, this equality does not hold for a lot of numbers
For example, consider this simple calculation
What is this "NormalizePrice" function ? There is no need for a second parameter "digits" or "decimals" to normalize a price in the mql sense.
Hi Alain, NormalizePrice is the proposed function of the OP.
I posted this answer on StackOverflow some time ago, for more in-depth handling of rounding with 64-bit doubles.
https://stackoverflow.com/a/48764436/4208440
Hi Alain, NormalizePrice is the proposed function of the OP.
Ah ok. Sooner or later this function will result in buggy values.