I take the ATR and half it. I add these two values to a given level and subtract them from the same. Now I need to compare these values to the current price.
So if I add them, then in some situations the value is lower than the price even if it (when rounded correctly) weren't. So it's off by a fraction. But this leads to wrong assumptions.
Also if (I know it's not the case for this Examen, but there are situations in which this applies) I divide 0.00009, I get as result 0.000044999999 so this rounds to 0.00004, which is wrong for my use case. It would need to be 0.00005.
I didn't expect this to be so precisely working, but if I ignore this error, I get bad results. If I manage this, results are correct.
Well, that's the source.
So I was thinking how to solve this and therefore I wrote my own function to "Normalize Price".
So, yes, I need to round the value. Especially because ATR values are way off from being "compatible" with price values.
That's my issue at the moment.
The issue is that you still trying to deal with doubles like integers when comparing them, they are not precise, you have to define 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:
bool is_equal(double a, double b) {
returnMathAbs(a - b) < .00001;
}
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.
//+------------------------------------------------------------------+//| Converting numeric value into the shortest string representation |//| that round-trips into the same numeric value. The result may hold|//| up to the first 17 significant digits, discarding trailing zeros.|//| The round-trip ("%.17g") format specifier ensures that a numeric |//| value converted to a string is always parsed back into the same |//| numeric value, StringToDouble(Repr(f)) == f. |//+------------------------------------------------------------------+// toString()string Repr(doublevalue)
{
string repr = "";
//--- First format with 15 spaces of precision. If round-trip fails,//--- format with 17 spaces of precision.if(value == StringToDouble(repr = StringFormat("%.15g", value)))
return repr;
return StringFormat("%.17g", value);
}
Thank you. I know about that issue. I always use printf(), and I always use formatting strings...
It's not about displaying. It's about the precision compromise and applying the right granularity to calculations and comparison.
Thank you for your support.
When comparing doubles, I only use greater/less than to be sure to get consistent results. But still there stays a margin or error due to the calculations and their results and the given granularity.
Sometimes this is proned to an offset as described above and therefore gives false results.
Thank you. I know about that issue. I always use printf(), and I always use formatting strings...
It's not about displaying. It's about the precision compromise and applying the right granularity to calculations and comparison.
Thank you for your support.
To check your functions for correct rounding (up, down or truncating) you can use this:
//+------------------------------------------------------------------+//| Checks if a number has a specified number of fractional digits. |//+------------------------------------------------------------------+bool IsRound(double num, int digits)
{
// return num == StringToDouble(DoubleToString(num, digits));double p = MathPow(10, digits);
returnMathRound(num * p) / p == num;
}
I will take into account all given hints and advices and try to solve the issue for me.
Thank you everyone for their contribution.
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:
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.
Alain Verleyen #: 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.
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
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.