Close all trades and take profit

 

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.

 

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);
}
 
cryptocurrency:

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


What is not working ?
 

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);
          } 
      }
  }
 

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