Download MetaTrader 5

Close open trades

To add comments, please log in or register
Ingvar Engelbrecht
4506
Ingvar Engelbrecht  

I have a multicurrency EA that operates on 27 pairs. It takes 4 trades for every trade opportunity 3 scale out and on trailer. Open orders quite often run up to 8-10 percent of the accountbalance rather fast and I like to take that

I am implementing a "Total System" type TP and SL EA that monitors the account and closes all trades if the value of total open orders is above a certain value or below a certain value.

I am using Alpari UK (five digits and a demo mico account) so I m not a victim of the FIFO rule

It seems that it works only for buy orders. When I run it I only have sell orders still open. The code is rather straight forward, I have tested variations and read several of the entries

here on the forum on the same subject. I get 2 types of error, 129 invalid price and 131 invalid trade volume. I have spent several hours going nowhere. Any help is appreciated.


2013.11.21 23:00:47 FXCMTFunc EURNZD,H1: Symbol: AUDUSD Ticket: 170655533 Price: 0.92359 Lots: 0.10 error(129): invalid price

2013.11.21 23:00:47 FXCMTFunc EURNZD,H1: Symbol: EURGBP Ticket: 170452329 Price: 0.83227 Lots: 0.11 error(129): invalid price

2013.11.21 23:00:49 FXCMTFunc EURNZD,H1: Symbol: NZDUSD Ticket: 170599563 Price: 0.82092 Lots: 0.11 error(131): invalid trade volume

code:

//+------------------------------------------------------------------+
//| CloseAll                                                CloseAll |
//+------------------------------------------------------------------+ 
bool CloseAll(double SystemTakeProfit,double SystemStopLoss,int StopDay)
 {
    double AccProfit;
    double triggvalue;
    int ticket;
    double balance;
    double closeprice;
    double lots;
     
    if(SystemTakeProfit == 0) return;

    balance = AccountBalance();
    
    triggvalue = balance * SystemTakeProfit/100;   //percentage of acc. balance
    
    Print("Triggvalue: " + DoubleToStr(triggvalue,0));
       
    int total=OrdersTotal();
    for(int pos=0;pos<total;pos++)
     {
      if(OrderSelect(pos,SELECT_BY_POS)==false) continue;
      ticket = OrderTicket();
      AccProfit = AccProfit +OrderProfit();         //accumulate open orders
     }   
     
    Print("AccValue: " + DoubleToStr(AccProfit,0));
    if(AccProfit > triggvalue)                      //open profit above limit
     {
      for(pos=0;pos<total;pos++)
       {
        ticket = OrderTicket();
      //  Sleep(1000);                                            //Does not help
        if(OrderSelect(pos,SELECT_BY_POS)==false) continue;
        if(OrderType() == OP_BUY)
          closeprice = MarketInfo(OrderSymbol(),MODE_BID);
        
        if(OrderType() == OP_SELL)
          closeprice = MarketInfo(OrderSymbol(),MODE_ASK);
        
     //   closeprice = OrderClosePrice();                         //Does not help
        
        closeprice = NormalizeDouble(closeprice,5);
        lots = NormalizeDouble(OrderLots(),2);
         
     //   OrderClose(ticket,lots,closeprice,30,CLR_NONE);         //Does not help
        OrderClose(ticket,lots,closeprice,3,CLR_NONE);            //close order  

        int err=GetLastError();
        if(err != 0)
           Print("Symbol: "+OrderSymbol()+ " Ticket: "+ DoubleToStr(ticket,0) + " Price: " + DoubleToStr(closeprice,5) + " Lots: "+DoubleToStr(lots,2)+
           " error(",err,"): " + ErrorDescription(err) );
       }
      return(true);
     }
   return(false);
 }
Simon Gniadkowski
Moderator
18025
Simon Gniadkowski  
ingvar_e:

I have a multicurrency EA that operates on 27 pairs. It takes 4 trades for every trade opportunity 3 scale out and on trailer. Open orders quite often run up to 8-10 percent of the accountbalance rather fast and I like to take that

I am implementing a "Total System" type TP and SL EA that monitors the account and closes all trades if the value of total open orders is above a certain value or below a certain value.

I am using Alpari UK (five digits and a demo mico account) so I m not a victim of the FIFO rule

It seems that it works only for buy orders. When I run it I only have sell orders still open. The code is rather straight forward, I have tested variations and read several of the entries

here on the forum on the same subject. I get 2 types of error, 129 invalid price and 131 invalid trade volume. I have spent several hours going nowhere. Any help is appreciated.



You need to OrderSelect() before you can use OrderTiccket() . . .

      for(pos=0;pos<total;pos++)
       {
          //  <-------           where is the OrderSelect() ? ?

        ticket = OrderTicket();
      //  Sleep(1000);                                            //Does not help
        if(OrderSelect(pos,SELECT_BY_POS)==false) continue;

If you are closing or deleting Orders in a loop you MUST count down: Loops and Closing or Deleting Orders


Don't call GetLastError() unless you have an error or you may be getting the wrong error number . . . . What are Function return values ? How do I use them ?

        OrderClose(ticket,lots,closeprice,3,CLR_NONE);            //close order  

        int err=GetLastError();  // <---  did the OrderClose() fail ?  if not where is the error ?
        if(err != 0)
           Print("Symbol: "+OrderSymbol()+ " Ticket: "+ DoubleToStr(ticket,0) + " Price: " + DoubleToStr(closeprice,5) + " Lots: "+DoubleToStr(lots,2)+
           " error(",err,"): " + ErrorDescription(err) );
To add comments, please log in or register