Download MetaTrader 5

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

To add comments, please log in or register
Join MQL5 Cloud Network. Millions of traders are waiting for this!
CipherPips
393
CipherPips 2016.01.13 22:55 

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

Marc Boggs
583
Marc Boggs 2016.01.14 02:28  
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);
            }
         }
      }

Alain Verleyen
Moderator
30723
Alain Verleyen 2016.01.14 07:57  

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.


To add comments, please log in or register