Download MetaTrader 5

Close all trades and take profit

To add comments, please log in or register
Cuong Truong
4280
Cuong Truong 2015.01.21 08:24 

Hi,

I was wondering if there is a script or EA that will automatically close all of my currently open orders if profit for each order is above $10?

If so, please reply with a link to download that script or EA. Thank you.

Cuong Truong
4280
Cuong Truong 2015.01.21 08:49  

Here is the code I've found online. It's not working. Does anyone know how to fix it?


int start()
{
  int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
  {
    OrderSelect(i, SELECT_BY_POS);
    int type   = OrderType();

    bool result = false;
    
    switch(type)
    {
      //Close opened long positions
      case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
                          break;
      
      //Close opened short positions
      case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
                          
    }
    
    if(result == false)
    {
      Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
      Sleep(3000);
    }  
  }
  
  return(0);
}
Alain Verleyen
Moderator
31085
Alain Verleyen 2015.01.21 09:08  
cryptocurrency:

Here is the code I've found online. It's not working. Does anyone know how to fix it?


What is not working ?
Biantoro Kunarto
15221
Biantoro Kunarto 2015.01.21 09:09  

Try this script:

void OnStart()
  {
   int total = OrdersTotal();
   for(int i=total-1;i>=0;i--)
      {
          bool result = false;
          if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
            {
               if ( OrderProfit() > 10 )
               {          
                int type   = OrderType();
                
                switch(type)
                {
                  //Close opened long positions
                  case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
                                      break;
                  //Close opened short positions
                  case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
                }
               }
            }
          if(result == false)
          {
            Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
            Sleep(3000);
          } 
      }
  }
Stuart Browne
5882
Stuart Browne 2015.01.21 09:10  

Please use the SRC button when posting code. It would also help if you could explain why the code isn't working. But I can see it's not checking for any conditions, just closing all open orders. You would need something more like this:

 

int start()
{
  int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
  {
    OrderSelect(i, SELECT_BY_POS);
    
    if (OrderProfit()>10) {      // Our order has more than $10 profit
       int type   = OrderType();
   
       bool result = false;
       
       switch(type)
       {
         //Close opened long positions
         case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
                             break;
         
         //Close opened short positions
         case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
                             
       }
       
       if(result == false)
       {
         Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
         Sleep(3000);
       } 
    } 
  }
  
  return(0);
}

 The above code also does not take Commission or Swap into the equation

To add comments, please log in or register