The NormalizeDouble function almost always rounds to 4 places, but every once in a while I get (ROC is: 0.0005999999999999999) some non-rounded number.
Can someone enlighten me why this is and how to fix?
Thanks.
Try this
Comment("ROC is: ", NormalizeDouble(ROC_Rounded,4));
some fractional numbers cannot be accurately stored on a computer.
#include <stdlib.mqh>
void OnTick()
{
double Price1 = iClose("USDCHF",1,1);
double Price2 = iClose("USDCHF",1,2);
double ROC;
if(Price1 > Price2) {
ROC = Price1 - Price2;
}
if(Price1 < Price2) {
ROC = Price2 - Price1;
}
double ROC_Rounded = NormalizeDouble(ROC, 4);
Comment("ROC is: ", DoubleToStrMorePrecision(ROC_Rounded,4));
}
this
will solve your issue. :)
- No it does not. It rounds to the digits specifies and returns a double. Floating point has infinite number of decimals, it's your not
understanding floating point and that some numbers can't be represented exactly. (like 1/10.)
Double-precision floating-point format - Wikipedia, the free encyclopedia
See also The == operand. - MQL4 programming forum
Print out your values to the precision you want with DoubleToString - Conversion Functions - MQL4 Reference.
- You used NormalizeDouble, It's use is usually
wrong, as it is in your case.
- SL/ TP(stops) need to be normalized to tick size (not Point.) (On 5Digit Broker Stops are only allowed to be placed on full pip values. How to find out in mql? - MQL4 programming forum) and abide by the limits Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial and that requires understanding floating point equality Can price != price ? - MQL4 programming forum
- Open price for pending orders need to be adjusted. On Currencies, Point == TickSize, so you will get the same answer, but it won't work on Metals. So do it right: Trailing Bar Entry EA- MQL4 programming forum or Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 programming forum
- Lot size must also be adjusted to a multiple of LotStep and check against min and max. If that is not a power of 1/10 then NormalizeDouble is wrong. Do it right.
- MathRound() and NormalizeDouble() are rounding in a different way. Make it explicit.
MT4:NormalizeDouble - General - MQL5 programming forum
How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum
The NormalizeDouble function almost always rounds to 4 places, but every once in a while I get (ROC is: 0.0005999999999999999) some non-rounded number.
Can someone enlighten me why this is and how to fix?
Thanks.
This is the way how 0.0006 is represented internally in memory. Try this code:
Print(0.0006); // 0.0005999999999999
The solution for your code is:
Print(DoubleToString(0.0006,4)) //0.0006
The above problem is not related to NormalizeDouble() or MathRound() in any way.
This is the way how 0.0006 is represented internally in memory. Try this code:
The solution for your code is:
The above problem is not related to NormalizeDouble() or MathRound() in any way.
Thanks you all for your help.
I will need a few days to study all of your comments.
Thanks you all for your help.
I will need a few days to study all of your comments.
Comment("ROC is: ", DoubleToStrMorePrecision(ROC_Rounded,4));
what about when i am normalising lotsizes, take profit and stop losses? In this case i cant use strings (DoubleToString)..!
what about when i am normalising lotsizes, take profit and stop losses? In this case i cant use strings (DoubleToString)..!
You only use DoubleToString to print, not in calculations.
You only use DoubleToString to print, not in calculations.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
The NormalizeDouble function almost always rounds to 4 places, but every once in a while I get (ROC is: 0.0005999999999999999) some non-rounded number.
Can someone enlighten me why this is and how to fix?
Thanks.