Partial close not working

 

Hi there, I'm having trouble with my partial close function, it seems to be not working as I get same results both if true or false. These are the lines:

      if(PartialClose==true){
         for(int i=0;i<PositionsTotal();i++){ // Same result with for(int i=PositionsTotal()-1;i>=0;i--){
            ulong iTicket=PositionGetTicket(i);
            if(PositionSelectByTicket(iTicket)&&
            PositionGetString(POSITION_SYMBOL)==SName){
               if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY){
                  double PClose=PositionGetDouble(POSITION_PRICE_OPEN)+ATR[0]/3;
                  if(PositionGetDouble(POSITION_PRICE_CURRENT)>PClose&&
                  (PositionGetDouble(POSITION_SL)>PositionGetDouble(POSITION_PRICE_OPEN))){
                     Print("PartialClose."); // <-- It's printing.
                     if(!trade.PositionClosePartial(iTicket,PositionGetDouble(POSITION_VOLUME)*0.618,ULONG_MAX)){ // It's not working...
                        Print("PositionClosePartial error ",trade.ResultRetcode()); // It's not printing...
                        return;
                        }

Hope someone can help on this matter, thanks in advance.

Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
 
David Diez :

Hi there, I'm having trouble with my partial close function , it seems to be not working as I get same results both if true or false. These are the lines:

Hope someone can help on this matter, thanks in advance.

Three errors:

  1. You send a non-existent lot as a volume (no check for a step, no check ...)
  2. You do not check the results of a trade (do not print return codes)
  3. You are not checking the account type: netting or hedge.
 
Vladimir Karputov:

Three errors:

  1. You send a non-existent lot as a volume (no check for a step, no check ...)
  2. You do not check the results of a trade (do not print return codes)
  3. You are not checking the account type: netting or hedge.

Was my fault, it was:

(PositionGetDouble(POSITION_SL)<PositionGetDouble(POSITION_PRICE_OPEN))){

Also I had to NormalizeDouble, thank you anyway.

 
David Diez: Also I had to NormalizeDouble,
  1. You can't just use OrderLots()/2 because that is not a multiple of LotStep, and you can't close or have remaining less than MinLot.

  2. You also must check if you have already done it, to avoid repeated closing. Alternatives:

    • Move SL to Break Even+1 before the partial close. That way you know that you already did it.

    • Set a flag in persistent storage (files, global variables w/flush)

    • Open two orders initially, and close one (manually or by TP.)

Reason: