# problem with partial close

25

hi everyone,

I have an issue with my partial close code

The calculated LotSizeBuy = 0.04875487 and OrderLots() = 0.04

when i try to Normalize LotSizeBuy it gives 0.05 which isn't equal to 0.04

is there any suggestion so the condition ( OrderLots() == LotSizeBuy ) can be true ?

void Partial_Close()
{
for(int  n=OrdersTotal()-1; n>=0; n--)
{
{
RiskAmount =  AccountBalance()*RiskPercent/100;
double LotSizeSell=NormalizeDouble(RiskAmount/(((OrderStopLoss()-OrderOpenPrice())/pips)*10),2);

{
//
close=OrderClose(OrderTicket(),OrderLots()/2,Bid,3);
}

{
if(NormalizeDouble(OrderLots(),1) == NormalizeDouble(LotSizeSell,1))
if (OrderType()==OP_SELL)
}
}
}
}

Moderator
2297

Checking for equality with doubles is always troublesome.

If you must do so, define an acceptable deviation and check if the difference between the doubles is within that deviation.

Also remember that you must always check that your lotsize complies with min lot, max lot and lot step.

16275

Hany Taha:

The calculated LotSizeBuy = 0.04875487 and OrderLots() = 0.04

when i try to Normalize LotSizeBuy it gives 0.05 which isn't equal to 0.04

is there any suggestion so the condition ( OrderLots() == LotSizeBuy ) can be true ?

1. 0.048 rounded to two digits is 0.05. Why are you comparing the sizes? If you want to close it close it.
2. Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong
3. close=OrderClose(OrderTicket(),OrderLots()/2,Bid,3);
The close amount must be a multiple of LotStep and be between min and max. The remaining amount must be also. Normalize OrderLots()/2 properly and test.
4. Check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
25

Thnx all guys I tried honest_knave's idea and it worked fine

NOTES : (1) I changed plan and decided to close 2/3 of OrderLots instead of 1/2 OrderLots

(2) Numbers 1.01 & 2.9 are obtained by experiment and covers almost all LotSize probabilities

now the comparison code is :