lotsize 0.09 for code .4*.25(==.10)
my code below iStart2Lots should equate to 0.10 -- however when trade opens, the lot is 0.09. why oh why? please explain, and how to ensure the result is correct -- 0.10?
what have i done wrong?
Small addition: in this particular case the multiplication itself is not the culprit. Multiplying by 0.25 (a power of two) is exact, so .4*0.25 gives exactly the same double as the literal 0.1, and both NormalizeDouble(x,2) and MathFloor(x/0.01)*0.01 return 0.10 for it. So the 0.09 is produced somewhere else. Two usual suspects:
1. Global variable initialized from an input. double iStart2Lots = .4*iStartLots; at global scope is evaluated when the EA is loaded. If you change iStartLots in the inputs dialog of an EA that is already on the chart, the EA only gets OnDeinit/OnInit (REASON_PARAMETERS) and is not reloaded, so the global keeps the value calculated from the old input (e.g. 0.4*0.23 = 0.092 -> 0.09). Calculate it inside OnInit instead.
2. The volume is changed later - a lot-normalization routine that floors, a margin/risk check, or a different variable passed to the second OrderSend. Print the exact volume right before OrderSend with %.8f to see where it changes.
input double iStartLots = 0.25; // 1st Trade Lot
double iStart2Lots; // computed in OnInit
int OnInit()
{
iStart2Lots = NormalizeLots(_Symbol, 0.4 * iStartLots);
PrintFormat("iStart2Lots = %.8f", iStart2Lots);
return(INIT_SUCCEEDED);
}
double NormalizeLots(const string sym, double lots)
{
double step = SymbolInfoDouble(sym, SYMBOL_VOLUME_STEP);
double minv = SymbolInfoDouble(sym, SYMBOL_VOLUME_MIN);
double maxv = SymbolInfoDouble(sym, SYMBOL_VOLUME_MAX);
lots = MathRound(lots / step) * step; // nearest step, not floor
lots = MathMax(minv, MathMin(maxv, lots));
int digits = (int)MathMax(0, MathCeil(-MathLog10(step)));
return(NormalizeDouble(lots, digits));
} Rounding to the nearest step (MathRound) instead of truncating also protects you from cases like 0.29/0.01 = 28.999999999999996, which MathFloor or (int) would turn into 0.28.
prove it please. i have tried many calculators, and all have same result: 0.1
Rounding to the nearest step (MathRound) instead of truncating also protects you from cases like 0.29/0.01 = 28.999999999999996, which MathFloor or (int) would turn into 0.28.
wt! all the same calculators come to same result as my head: 29. proof please!
1. Global variable initialized from an input. double iStart2Lots = .4*iStartLots; at global scope is evaluated when the EA is loaded. If you change iStartLots in the inputs dialog of an EA that is already on the chart, the EA only gets OnDeinit/OnInit (REASON_PARAMETERS) and is not reloaded, so the global keeps the value calculated from the old input (e.g. 0.4*0.23 = 0.092 -> 0.09). Calculate it inside OnInit instead.
was 0.23 a typo, or intentional?
my code below iStart2Lots should equate to 0.10 -- however when trade opens, the lot is 0.09. why oh why? please explain, and how to ensure the result is correct -- 0.10?
what have i done wrong?
You should show all the relevant code, the one calculating the lot size.
LastLot[sel] = NormalizeDouble(iStart2Lots,2);
my code below iStart2Lots should equate to 0.10 -- however when trade opens, the lot is 0.09. why oh why? please explain, and how to ensure the result is correct -- 0.10?
what have i done wrong?