Lot decimal places

 

Hello,

In my code I use this sentence:

LotValue = NormalizeDouble(LotValue, LotDecimalPlaces);


Question is:

How to know the right value for variable LotDecimalPlaces? I mean in an automatic way inside of the EA.

I thought that value could be equal to the decimal places of the Step (MODE_LOTSTEP), for example:

Step: 0.01 -> LotDecimalPlaces: 2

Step: 0.05 -> LotDecimalPlaces: 2

Step: 0.1 -> LotDecimalPlaces: 1

Step: 1.0 -> LotDecimalPlaces: 0

So the routine would be:

if (Step < 0.1) then LotDecimalPlaces: 2

if (Step >= 0.1 and Step < 1) then LotDecimalPlaces: 1

if (Step >= 1) then LotDecimalPlaces: 0

Do you see any problem to this?

Thanks in advance!

 
desert:

Hello,

In my code I use this sentence:

LotValue = NormalizeDouble(LotValue, LotDecimalPlaces);


I use . . .

LotValue = NormalizeDouble(LotValue, Digits);
 
RaptorUK:

I use . . .

LotValue = NormalizeDouble(LotValue, Digits);

Thanks for your answer, but in my humble opinion it is not correct at all, Digits has the number of decimal places in the price value (2 or 3, 4 or 5), these decimal places do not apply for the lot value. I could be wrong maybe.

Regards

 
LotValue = NormalizeDouble(LotValue, LotDecimalPlaces); 
This is WRONG. Depending on LDP (0, 1, 2) it assumes a lotstep is 1.0, 0.1, or 0.01. Any other BROKER DEFINED lotstep (like 0.05) can not be done by this code. Just do it right:
    double  lotStep         = MarketInfo(Symbol(), MODE_LOTSTEP),   //IBFX= 0.01
            minLot          = MarketInfo(Symbol(), MODE_MINLOT );   //IBFX= 0.10
    lotValue=MathCeil(lotValue/lotStep)*lotStep;
    if (lotValue < minLot) ...
 
WHRoeder:
This is WRONG. Depending on LDP (0, 1, 2) it assumes a lotstep is 1.0, 0.1, or 0.01. Any other BROKER DEFINED lotstep (like 0.05) can not be done by this code. Just do it right:


Hello, thank you for the advise.


The issue with MathCeil is the following:

if Lot = 0.833333, after MathCeil it is 0.9 (Step 0.1)

if Lot = 0.866666, after MathCeil it is 0.9 (Step 0.1)

However, if I use Lot = NormalizeDouble(Lot,1), the rounding I got is more exact:

if Lot = 0.833333, after NormalizeDouble(Lot,1)it is 0.8 (Step 0.1)

if Lot = 0.866666, after NormalizeDouble(Lot,1)it is 0.9 (Step 0.1)


See the difference? So, the issue I have is detecting the number of decimal places in the lot value, for that is what I thought to use the decimal places of the step value, could please tell me why this is wrong?

Thanks again.

 
I'm newbie in this, but I think you can use MathCeil, or MathFloor or MathRound depend on what you expect. ex: MathCeill(1.05) = 2 MathCeil(1.95) = 2 -------> UP MathFloor(1.05) = 1 MathFloor(1.95) = 1 ---------> DOWN MathRound(1.05) = 1 MathRound(1.95) = 2 I think you can use this one. Hope can help.
 

Does this work . . .

MathAbs((lots +( 0.5 *lotstep))/lotstep) *lotstep 

it rounds up/down to the nearest lotstep . . . I think. Looks like it works for a lotstep of 0.05 too

 

MathAbs will not work, you need truncation to an int before the *lotstep. int i=(lots+lotstep/2)/lotstep; lots=i*lotstep;

I think you're splitting hairs with the mathCeil, (1.12 vs 1.13 lots) but mathRound will work just as well.

 
WHRoeder:

MathAbs will not work, you need truncation to an int before the *lotstep. int i=(lots+lotstep/2)/lotstep; lots=i*lotstep;

I think you're splitting hairs with the mathCeil, (1.12 vs 1.13 lots) but mathRound will work just as well.

Sorry, yes . . . for some reason I thought MathAbs returned the Integer part . . oops.
 
Thanks a lot everybody, MathRound does what I need.
 

I know this is an old question.

Not sure if any drawbacks from a trading perspective...

MQL4:

int getDecimalPlaces(double number) {

   string decimalPlaces[] = {0};    

   StringSplit(number,46,decimalPlaces);

   if (ArraySize(decimalPlaces) == 2) {

      return StringLen(decimalPlaces[1]);

   }

   return 0;

}

getDecimalPlaces(MarketInfo(NULL,MODE_MINLOT));

Reason: