Inconsistent of Closing Tickets

 

if more than one tickets are opened at any time (not opened at the same time), MQL5 wasn't able to close all of the tickets (see enclosed figure).

Enclosed:

  • Call to close long and short
  • Code of closing functions
  • Figure show not all are closing

void closeLong(){
   
   int total   = PositionsTotal();
   ulong ticket  = 0;
   
   
   if (total<1) return; 
   for(int i=0;i<total;i++)
     {
         PositionSelect(_Symbol);
         ulong type = PositionGetInteger(POSITION_TYPE);
         if(type == POSITION_TYPE_BUY)
           {
               ticket  = PositionGetTicket(i);
               if(ticket>0)
                 {
                     trade.PositionClose(ticket,10); 
                     TimeToCloseLong = false;                 
                 }
           
           }
   
     }
}

//+------------------------------------------------------------------+
//| closeShort                                                       |
//+------------------------------------------------------------------+ 

void closeShort(){
   
   int total   = PositionsTotal();
   ulong ticket  = 0;
   
   
   if (total<1) return; 
   for(int i=0;i<total;i++)
     {
         PositionSelect(_Symbol);
         ulong type = PositionGetInteger(POSITION_TYPE);
         if(type == POSITION_TYPE_SELL)
           {
               ticket  = PositionGetTicket(i);
               if(ticket>0)
                 {
                     trade.PositionClose(ticket,10); 
                     TimeToCloseShort = false;
                 }                 
           }
   
     }
}

Improperly formatted code edited by moderator. Please ALWAYS use the CODE button (Alt-S) when inserting code.

Code button in editor

 
You should select positions by tickets, not by symbol.
Selecting by symbol works fine only for netting account, where all trades are embedded into a single position.

I also strongly recommend to use CTrade classes libraries, for easier codes and proper error handling.
 
black_building:

if more than one tickets are opened at any time (not opened at the same time), MQL5 wasn't able to close all of the tickets (see enclosed figure).

Enclosed:

  • Call to close long and short
  • Code of closing functions
  • Figure show not all are closing

Improperly formatted code edited by moderator. Please ALWAYS use the CODE button (Alt-S) when inserting code.

I was using selected by ticket but I ran to issue of I can only close one ticket. Program ignores other ticket (return ticket not found). I will modify my code to use Trade class. Does the existing "return ticket not found" issue has been resolved? If it does. Please shoot me the link.


Thanks 

 
Have you tried counting loop in reverse?
 
Yashar Seyyedin #:
Have you tried counting loop in reverse?

once a single trade is closed, then wont "total" be different to PositionsTotal? since that total wont be updated until the next tick or nex time that the function is called again, correct?