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?

input double   iStartLots     = 0.25;   // 1st Trade Lot
double   iStart2Lots     = .4*iStartLots; //0.10;   // 2nd Trade Lot

what have i done wrong?

Michael Charles Schefe:

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?

The problem is the data type double precision floating point.

Do a research on that topic.

0.25*0.4 = 0.0999999999

However you round your price, you seem to end up with a floor operation.

NormalizePrice() is usually 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.

Dominik Egert #:
The problem is the data type double precision floating point.

Do a research on that topic.

0.25*0.4 = 0.0999999999

prove it please. i have tried many calculators, and all have same result: 0.1

Kazuma Uezono #:


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!

Kazuma Uezono #:


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?

Kazuma Uezono #:


Thanks for the solution. i already had similar code, but i like yours as it has 2 less lines than mine.

But i would still like to understand why my original code did not work. i am sure it is a coding issue that i have forgotten about.

Dominik Egert #:
The problem is the data type double precision floating point.

Do a research on that topic.

0.25*0.4 = 0.0999999999

Michael Charles Schefe:

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.
0.1 cannot be represented exactly becaus it is not an exact sum of fractions of 2 (the binary floating point system used by MQL). So, 0.1 could be either something like 0.099999999999 or .1000000000006, depending on the mathematical expression or computation yielding that result.
Alain Verleyen #:
You should show all the relevant code, the one calculating the lot size.
               LastLot[sel] = NormalizeDouble(iStart2Lots,2);
that is what it was, now i have added the code by @Kazuma Uezono