CloseBySequence() - Function need help - MQL5

 

MQL5: 

I'm trying to write a function to close by sequence.

There is no error after compilation, but it does not run.

I could not figure out why. Could you help? 

Thank you! 

Closing by order sequence.

i.e.
Deal 1 + Deal 2 has profited - close
Deal 3 + Deal 4 has profited - close

5 + 6
7 + 8
9 + 10
and so on ...

Deal 1 has the smallest order number and is opened at the earliest time. 

Deal 10 has the biggest order number and is opened at the latest time. 

 

void CloseBySequence()

{

  datetime from=0; 

  datetime to=TimeCurrent(); 

  HistorySelect(from,to); 

  double Minimum_Profit=5.00;

  ulong ticket;

  uint x;

  double trades[][2];

  uint total=HistoryDealsTotal(); 

  if(total>1)

  {

  ArrayResize(trades,total);

  for(x=total-1;x>=0;x--)

     {

       if((ticket=HistoryDealGetTicket(x))>0)

        {

        trades[x][0]=HistoryDealGetInteger(ticket,DEAL_ORDER);    

        trades[x][1]=HistoryDealGetDouble(ticket,DEAL_PROFIT)

        +HistoryDealGetDouble(ticket,DEAL_COMMISSION)

        +HistoryDealGetDouble(ticket,DEAL_SWAP);

        }

     }

 

  ArraySort(trades);

  x=0;

  while(x<total-1)

     {

     double profit=trades[x][1]+trades[x+1][1];

     if(profit>=Minimum_Profit)

        { 

        if(HistoryDealSelect((int)trades[x][0]))

        if(!trade.PositionClose(PositionGetSymbol(x)))

        Print("Error closing (Deal 1/2) #",IntegerToString(OrderGetTicket(x))," Error code ",GetLastError());

        

        if(HistoryDealSelect((int)trades[x+1][0]))

        if(!trade.PositionClose(PositionGetSymbol(x)))

        Print("Error closing (Deal 2/2) #",IntegerToString(OrderGetTicket(x))," Error code ",GetLastError());

           }

        x+=2;

     }

  }

} 
 
same thing i want but still need help may be some one figure out soon waiting...........
 
muhammadmudasir:
same thing i want but still need help may be some one figure out soon waiting...........
Feel free to try freelance !
 
Marco vd Heijden:
Feel free to try freelance !

There is no error.

Strategy tester hangs.

It runs and opens 2 trades and then hangs.

I figured its with this function because when I disable this function, it runs. 

Bugs on MT5? 

 
jon:

There is no error.

Strategy tester hangs.

It runs and opens 2 trades and then hangs.

I figured its with this function because when I disable this function, it runs. 

Bugs on MT5? 

Sounds like its stuck in an endless while loop.
 
Marco vd Heijden:
Sounds like its stuck in an endless while loop.

Thanks, you are a genius! The problem is with one of the loop.

 

However now, I just keep getting error:


 "Error closing (Pair 1/2) #0, Error code 4753"

or 

It closes losing trading pairs instead of winning ones.

 
jon:

Thanks, you are a genius! The problem is with one of the loop.

 

Now I just keep getting error:

 "Error closing (Pair 1/2) #0, Error code 4753"

So you press F1 in the compiler, and type in the errorcode

https://www.mql5.com/en/docs/constants/errorswarnings/errorcodes

Documentation on MQL5: Standard Constants, Enumerations and Structures / Codes of Errors and Warnings / Runtime Errors
Documentation on MQL5: Standard Constants, Enumerations and Structures / Codes of Errors and Warnings / Runtime Errors
  • www.mql5.com
Standard Constants, Enumerations and Structures / Codes of Errors and Warnings / Runtime Errors - Reference on algorithmic/automated trading language for MetaTrader 5
 

Thanks!