Download MetaTrader 5

Simple close all symbol orders but not working.. Need help!

To add comments, please log in or register
Do you know that MQL5 can create custom graphical tools?
mianhae71
48
mianhae71 2014.07.01 03:43 

Hi, 

I would like someone to take a little peek at what i have done. this function tends to very simply close all symbol order but after function has been called. It doesn't close all trades with the same currency pair. Always 1 trade is left behind.

A little help would be appreciated. Thanks!  

 

// ++------------------------------------------------------------------------------------------++ 

// Closesymbol: Function to close all trades with the current currency pair

// ++------------------------------------------------------------------------------------------++ 

int closesymbol()

{

bool Ans;

for (int j=1; j <= OrdersTotal(); j++)

{

if (OrderSelect(j-1,SELECT_BY_POS)==true)

{

if (OrderSymbol() != Symbol()) continue;

closelot = OrderLots();

closeticket = OrderTicket();

closetype = OrderType();


while(true)

  {


  string Text;

         

  switch (closetype)

     {

     case 0: closeprice = MarketInfo(OrderSymbol(), MODE_BID);

             Ans= OrderClose(closeticket,closelot,closeprice,3);                              

             Text = "Buy";

             break;               

     case 1: closeprice = MarketInfo(OrderSymbol(), MODE_ASK);

             Ans= OrderClose(closeticket,closelot,closeprice,3);    

             Text = "Sell"; 

             break;

     default:

              Text = "Error";

     }  

Alert("Closing ",Text," ",closeticket,".");

//   bool Ans= OrderClose(closeticket,closelot,closeprice,3);

         if (Ans == true)

  {

  Alert("Closed",Text," ",closeticket);

  tradecount = tradecount - 1;

  break; 

  }

int Error=GetLastError();                 // Failed :(

      switch(Error)                             // Overcomable errors

        {

        case 135:Alert("The price has changed. Retrying..");

              RefreshRates();                     // Update data

            continue;                           // At the next iteration

        case 146:Alert("Trading subsystem is busy. Retrying..");

            Sleep(500);                         // Simple solution

            RefreshRates();                     // Update data

            continue;                           // At the next iteration

        }

      switch(Error)                             // Critical errors

        {

          case 2 : Alert("Common error.");

            break;                              // Exit 'switch'

        case 5 : Alert("Old version of the client terminal.");

                  break;                              // Exit 'switch'

          case 64: Alert("Account is blocked.");

            break;                              // Exit 'switch'

          case 133:Alert("Trading is prohibited");

            break;                              // Exit 'switch'

          default: Alert("Occurred error ",Error);//Other alternatives   

        }

        break;                  

  }

  }

}

return(0);

Boeing747
241
Boeing747 2014.07.01 07:24  
//+------------------------------------------------------------------+
//|                                                    Close_All.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property show_confirm
bool Work=true;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
//-------------------------------------------------------------closesymbol()
   string symbol=Symbol();
   bool result;
   int i,cmd,e,type;
   int slippage=10;
   double price;

   while(true)
     {
      e=0;
      if(IsStopped()==true || Work==false)break;
      RefreshRates();
      if(OrderSelect(OrdersTotal()-1-i,SELECT_BY_POS,MODE_TRADES)==true)
        {
         if(OrderSymbol()!=symbol)
           {
            i++;
            continue;
           }
         cmd=OrderType();
         if(cmd>1)
           {
            i++;
            continue;
           }
         e=-1;
         type=MODE_ASK;
         if(cmd==0)type=MODE_BID;
         RefreshRates();
         price=MarketInfo(symbol,type);
         result=OrderClose(OrderTicket(),OrderLots(),price,slippage,CLR_NONE);
         if(result==false && Fun_Error(GetLastError())==1)continue;
        }
      if(e==-1)continue;
      i++;
      if(e==0)break;
     }
//--------------------------------------------------------------------closesymbol()



   return(0);
  }
//------------------------------------ Error-handling function

int Fun_Error(int Error)
  {
   switch(Error)
     {
      //----------------------------------- Overcomable errors
      case   4: Comment("Trade server is busy. Retry");
      Sleep(3000);
      return(1);
      case 135: Comment("Price changed. Retry.");
      RefreshRates();
      return(1);
      case 136: Comment("Off quotes. Waiting for a new tick.");
      while(RefreshRates()==false && IsStopped()==false)
         Sleep(300);
      return(1);
      case 137: Comment("Broker is busy.");
      Sleep(3000);
      return(1);
      case 138: Comment("Requote. Retry.");
      Sleep(500);
      RefreshRates();
      return(1);
      case 146: Comment("Trade context is busy. Retry.");
      Sleep(2000);
      return(1);
      case   6: Comment("No connection with trade server. Waiting for connection.");
      while(IsConnected()==false && IsStopped()==false)
         Sleep(1000);
      return(1);
      case   8: Comment("Too frequent requests.");
      Sleep(10000);
      return(1);
      case 128: Comment("Trade timeout.");
      Sleep(2000);
      return(1);
      //-------------------------------------- Critical errors
      case   2: Comment("Common error.");
      return(0);
      case   3: Comment("Invalid trade parameters.");
      return(0);
      case   5: Comment("Old version of the client terminal.");
      Work=false;
      return(0);
      case   7: Comment("Not enough rights.");
      return(0);
      case  64: Comment("Account disabled.");
      Work=false;
      return(0);
      case 133: Comment("Trade is disabled.");
      return(0);
      case 134: Comment("Not enough money.");
      Work=false;
      return(0);
      case 129: Comment("Invalid price.");
      return(0);
      case 130: Comment("Invalid stops.");
      return(0);
      case 131: Comment("Invalid trade volume.");
      return(0);
      case 132: Comment("Market is closed.");
      return(0);
      case 139: Comment("Order is locked.");
      return(0);
      case 147: Comment("Expirations are denied by broker.");
      Work=false;
      return(0);
      case 148: Comment("Amount of open and pending orders has reached the limit.");
      Work=false;
      return(0);
      case 149: Comment("Hedging is prohibited.");
      Work=false;
      return(0);
      case 150: Comment("Prohibited by FIFO rules.");
      Work=false;
      return(0);
      case   0: Comment("Error = 0.");
      return(0);
      case   1: Comment("No error.");
      return(0);
      default:  Comment("Error: "+IntegerToString(Error)+".");
      return(0);
     }
  }
//+------------------------------------------------------------------+
qjol
3243
qjol 2014.07.01 11:52  

to close orders you must count down

for (int j=OrdersTotal() -1; j >= 0; j--)
whroeder1
14470
whroeder1 2014.07.01 11:58  
mianhae71: I would like someone to take a little peek at what i have done.
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Loops and Closing or Deleting Orders - MQL4 forum
To add comments, please log in or register