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.
William Roeder: Code breaks if lot
step is not a power of 10.
When I suggested the -log10() function I was giving a solution referring to his very limited "switch" list, that also only allowed for the
values 0.01, 0.1 and 1. For these cases the function works. If you include other values, e.g.
-log10(0.02)=1.698
-log10(0.3)=0.523
-log10(0.4)=0.398
rounding UP (not MathRound) is the correct way to obtain the number of digits, as can be seen with the example -log10(0.4), because the result is
below 0.5, so MathRound should return 0, whereas the number of digits after the decimal point for 0.4 is 1
so I guess
ceil(-log10(step)) should be correct, not round(-log10(step));
But suppose you have other steps (like 0.25 or 0.5) then I have an unconventional (but working) solution, which I use for long time: the function GetNoDigits.
Please see -
#property strictint GetNoDigits (double step) {
int d = 0;
double f = 1.0;
// Emergency stop at 10while (d <= 10) {
double m = f*step;
if (MathFloor(m) == m) {
return d;
}
f = f * 10.0;
d++;
}
return0;
}
voidOnStart() {
PrintFormat("START\n");
// For test of GetNoDigitsdouble steps[] = {1.0, 0.1, 0.25, 0.5, 0.01, 0.025, 0.05, 0.001, 0.0001};
for (int i = 0; i < ArraySize(steps); i++) {
PrintFormat ("step %6.4f ===> ndigits %4d", steps[i], GetNoDigits(steps[i]));
}
}
But suppose you have other steps (like 0.25 or 0.5) then I have an unconventional (but working) solution, which I use for long time: the function GetNoDigits.
Dr Matthias Hammelsbeck: But suppose you have other steps (like 0.25 or 0.5) then I have an unconventional (but working) solution, which I use for long time: the function GetNoDigits.
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.
amrali: Some prices from the terminal are off by 1 Epsilon (ulp) from the rounded value. It may be due to some post processing inside the terminal software.
Of course, they are. Reread my post, especially № 1 and № 2.
NormalizeDouble, It's use is usually wrong,
Double-precision floating-point format - Wikipedia, the free encyclopedia
See also The == operand. - MQL4 programming forum
MT4:NormalizeDouble - General - MQL5 programming forum
How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum
Code breaks if lot step is not a power of 10.
When I suggested the -log10() function I was giving a solution referring to his very limited "switch" list, that also only allowed for the values 0.01, 0.1 and 1. For these cases the function works. If you include other values, e.g.
-log10(0.02)=1.698
-log10(0.3)=0.523
-log10(0.4)=0.398
rounding UP (not MathRound) is the correct way to obtain the number of digits, as can be seen with the example -log10(0.4), because the result is below 0.5, so MathRound should return 0, whereas the number of digits after the decimal point for 0.4 is 1
so I guess
ceil(-log10(step)) should be correct, not round(-log10(step));
or am I getting this wrong?
What is wrong is doing it at all. "If that is not a power of 1/10," then Lotdecimal_0_1_2 is meaningless.
Forget about switch. Use if.
No need to make it complicated.
Forget about switch. Use if.
No need to make it complicated.
Yes, my opinion too: keep it simple.
But suppose you have other steps (like 0.25 or 0.5) then I have an unconventional (but working) solution, which I use for long time: the function GetNoDigits.
Please see -
Output
Enjoy your life
Matthias
------------------------------------------------------
"Is it more important to feel strong than to be strong."
Yes, my opinion too: keep it simple.
But suppose you have other steps (like 0.25 or 0.5) then I have an unconventional (but working) solution, which I use for long time: the function GetNoDigits.
Please see -
Output
Enjoy your life
Matthias
------------------------------------------------------
"Is it more important to feel strong than to be strong."
Now, you have the number of digits,
great!
so what then?
It seems to be a correct answer, but for a different question.
Stop using NormalizeDouble, It's use is usually wrong, as it is in your case.
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 2013.06.07
Print out your values to the precision you want with DoubleToString - Conversion Functions - MQL4 Reference.
SL/TP (stops) need to be normalized to tick size (not Point) — code fails on metals. (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 - MQL5 programming forum
How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum
What is a TICK? - MQL4 programming forum 2014.08.03
Prices you get from the terminal are already normalized.
Of course, they are. Reread my post, especially № 1 and № 2.