MoneyManagement and round off

 

Hi,

I have calculated my lotposition and if the result is f.e.

0.047 Lot, I want to round it of to 0.04.

This systematic should work on a micro- and a minilot account.

 
DoubleToStr(0.047 - 0.005, 2);
 
VirtualReal:

And that gives you a string . . .

You can use MathFloor . . .

double lot = 0.047;

lot = MathFloor(lot * 100)/100;

You can use basic mathematics

double lot = 0.047;

lot = (lot*100 - ((lot*100) % 1)) / 100;
 
Using by 100 ASSUMES lot step is 0.01. Broker dependent.
double NormalizeLots(double lots, string pair=""){
    if (pair == "") pair = Symbol();
    double  lotStep = MarketInfo(pair, MODE_LOTSTEP),
            minLot  = MarketInfo(pair, MODE_MINLOT);
    lots            = MathRound(lots/lotStep) * lotStep;
    if (lots < minLot) lots = 0;    // or minLot
    return(lots);
}
 

I use this routine. This "0.000000001" value is because there is some error that happen when using ">" around 0.1 lots. If you don't like it, you can try to Normalize values.

double VolumeSize=0.047; // Initial value

double LotStep=MarketInfo(Symbol(),MODE_LOTSTEP);
double LotSize=MarketInfo(Symbol(),MODE_LOTSIZE);
double MinLots=MarketInfo(Symbol(),MODE_MINLOT);
double MaxLots=MarketInfo(Symbol(),MODE_MAXLOT);

double VolumeStepSize=0;
while(VolumeSize+0.000000001>VolumeStepSize){VolumeStepSize+=LotStep;}
VolumeSize=VolumeStepSize-LotStep;

if (VolumeSize<MinLots) {VolumeSize=MinLots;}
if (VolumeSize>MaxLots) {VolumeSize=MaxLots;}
 
RaptorUK:

And that gives you a string . . .

You can use MathFloor . . .

You can use basic mathematics


Spot on, I've never used the MathFloor in MQL, good to see it has one
Reason: