closeAllLong() closeAllShort() or closeAllPositions example for my EA

 

I am new to MT5 and am still trying to figure a few things out!

Can someone please share a function that shows me how you would close all positions as per the subject line above?  I will always only have open positions of either type OP_BUY or OP_SELL and never both.

I have an input parameter on my EA called MaxTrades to limit the number of trades with a single currency pair.  However, I expect that I will be running my EA on multiple currency pairs and will need to throttle how many open orders each Currency Pair is allowed..

I don't yet understand the structures which I must use in order to successfully run a "for loop".

Any help would be much appreciated.

Thank you in advance,


CipherPips

 
You want to do something like this:
      int total=OrdersTotal();
      int i = 0;
      for(i = 0; i < total; i++)
      {
         if(OrderSelect(i,SELECT_BY_POS))
         {
            if((OrderType()==OP_BUY || OrderType()==OP_SELL) && OrderSymbol() == _Symbol)
            {
               if(OrderType()==OP_BUY)
               {
                  if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet))
                     Print("OrderClose error ",GetLastError());
               }
               else
               {
                  if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet))
                     Print("OrderClose error ",GetLastError());
               }
               Sleep(100);
            }
         }
      }

 

Forum on trading, automated trading systems and testing trading strategies


Hello,

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.


Reason: