Download MetaTrader 5

CloseBySequence() - Function need help - MQL5

To add comments, please log in or register
Publish your articles and improve your rating!
jon
465
jon 2016.06.29 08:30 

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;

     }

  }

} 
Muhammad Mudasir
6918
Muhammad Mudasir 2016.07.03 19:51  
same thing i want but still need help may be some one figure out soon waiting...........
Marco vd Heijden
Moderator
4685
Marco vd Heijden 2016.07.03 21:24  
muhammadmudasir:
same thing i want but still need help may be some one figure out soon waiting...........
Feel free to try freelance !
jon
465
jon 2016.07.03 21:52  
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? 

Marco vd Heijden
Moderator
4685
Marco vd Heijden 2016.07.03 22:40  
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.
jon
465
jon 2016.07.04 21:55  
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.

Marco vd Heijden
Moderator
4685
Marco vd Heijden 2016.07.04 21:58  
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
jon
465
jon 2016.07.04 23:38  

Thanks!

To add comments, please log in or register