problem with partial close

 

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--)
   {
    select=OrderSelect(n,SELECT_BY_POS,MODE_TRADES);
     {
      RiskAmount =  AccountBalance()*RiskPercent/100;
      double LotSizeBuy=NormalizeDouble(RiskAmount/(((OrderOpenPrice()-OrderStopLoss())/pips)*10),2);
      double LotSizeSell=NormalizeDouble(RiskAmount/(((OrderStopLoss()-OrderOpenPrice())/pips)*10),2);
      
       {
        if(OrderLots() == LotSizeBuy)
       //
        if (OrderType()==OP_BUY)
        if((Ask - OrderOpenPrice())>((PartialClosePercent/100)*(OrderTakeProfit() - OrderOpenPrice())))
        close=OrderClose(OrderTicket(),OrderLots()/2,Bid,3);
       }
      
       {  
        if(NormalizeDouble(OrderLots(),1) == NormalizeDouble(LotSizeSell,1))
        if (OrderType()==OP_SELL)
        if((OrderOpenPrice()-Ask)>((PartialClosePercent/100)*(OrderOpenPrice()-OrderTakeProfit())))
        close=OrderClose(OrderTicket(),OrderLots()/2,Ask,3);
       }
     }
   }  
}

 

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.

 
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
 

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 :

if(LotSizeBuy /  OrderLots() > 1.01 && LotSizeBuy /  OrderLots() < 2.9)
Reason: