I have a lot of open positions.How can I closeall at the same time?

 

Hi

I have a lot of open positions.How can I closeall at the same time?

If I use :

   while(PositionsTotal()>0)
     {
      for(int i=PositionsTotal()-1;i>=0; i--)

        {

         PositionSelect(PositionGetSymbol(i));

         while(false==m_trade.PositionClose(PositionGetInteger(POSITION_TICKET),deviation_))
           {
            m_trade.PositionClose(PositionGetInteger(POSITION_TICKET),deviation_);
           }

        }
     }

it will take a lot of time

And I do not have margin for buying and selling, unlike the direction.

 

Why don't you use an EA to close them all?

 
Ali Poormomen:

Hi

I have a lot of open positions.How can I closeall at the same time?



Hi Ali,

Maybe you can use my simply logic,

and you just complete it a bit :)

......
......

     //+--------------------------+
     //| Get positions            |
     //+--------------------------+
     ulong totalpos=PositionsTotal();
     Print("!* Total running positions now: ",totalpos);
     for (ulong i=0; i < totalpos; i++) 
     {
      if(!PositionSelectByTicket(PositionGetTicket(i))) continue;
      if(PositionGetTicket(i)>0
         && ((PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) || (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY))
         )
      {
        Print("* ",i+1,". Closing ticket #",PositionGetTicket(i)); //--Displays the order of closing tickets
        
        while(true)
        {
          if(trade_close_position(PositionGetTicket(i))==true)
          {
             Print("* Done.",PositionGetTicket(i)," was closed.");
             break;
          }
          else
          {
             Print("* Retry closing ...",PositionGetTicket(i));
          }
          Sleep(1000);
        }//-while
      }//--if
     }//--for

.......
.......

//+----------------------------------------+
//| Close Position                         |
//+----------------------------------------+
bool trade_close_position(ulong ticket)
{
      ResetLastError();
      ZeroMemory(request);
      ZeroMemory(result);
      if(!PositionSelectByTicket(ticket))       //--check ticket does not exist
      {
        Print("*! Can't find ticket: ",ticket);
        return(false);
      }
      else  //--ticket exist
      {

         //--do something .. closing ticket here ...
         if(.........)  //---condition 1
         {
            //--do something .. close position done
            return(true);
         }
         else //--condition 2
         {
           //--do something .. close position failed
           return(false);
         }
      }
}

Good luck,
Yo.  (^̮^)

 
Yohana Parmi:

Hi Ali,

Maybe you can use my simply logic,

and you just complete it a bit :)

Good luck,
Yo.  (^̮^)

thank you for your attention


But that also takes a long time.
There is a way to close everyone at a time?

 
Ali Poormomen:

thank you for your attention


But that also takes a long time.
There is a way to close everyone at a time?


Ali Poormomen:

Hi

I have a lot of open positions.How can I closeall at the same time?



Yes, I understand you :)
but I don't think so, as I knew we should close one by one.

maybe another expert coder here can help us :)

 

With mql5, you can use asynchronous requests. It's still one by one, but it's very fast as you don't have to wait server answers.

You could also try to "CloseBy" approach. Search on the forum about it.

 
Ali Poormomen: I have a lot of open positions.How can I closeall at the same time? If I use, it will take a lot of time. And I do not have margin for buying and selling, unlike the direction.

You can also use the OrderCloseBy() concept that has been used in MT4 as described below, but you will have to use the MT5 equivalent, namely "ORDER_TYPE_CLOSE_BY".

Forum on trading, automated trading systems and testing trading strategies

OrderCloseBy Question

Ahmad Mehdiyev, 2014.03.22 12:13

1. You cannot open opposite positions at the same time if your broker is in US. That is true. With other brokers it is possible.

2. The way you get opposite positions (if they are allowed by your broker) is quite simple :). You buy and then sell, or vice versa.

3. The difference in lot size or the time at which each order was opened does not matter as far as OrderCloseBy is concerned.

4. OrderCloseBy is ment to substitute the use of OrderSend, so submitting OrderSend is not required. Simpy use "Close By" or "Multiple Close By" from within the Order Entry window, or use a script if you have one.

5. You do save a spread. But once! Here how it works. When you buy you pay a spread. When you sell you pay a spread. If you close them separately you loose both of those spreads (well they are in your P&L already), but if you do OrderCloseBy you are able to get one of those spreads back since it matches the closing price with opening price of the second trade. Simply put it assumes that when you opened the opposite trade you closed out the first one at the price at the opening price of the matching order.

They way I use OrderCloseBy is when I have a multiple one-sided positions and need to close them all quick I just lock the complete position with one trade, and then OrderCloseBy them with that new matching order. Obviously if you don't have an ability to lock the position you must close the initial multiple positions one-by-one which in a fast market could be quite damaging.

Hope this helps, and I did not simply repeat the things you know already.

Forum on trading, automated trading systems and testing trading strategies

what is your design/strategy for 1 or more order close failures (retries exhausted) on 100% unattended multisym EA ?

JC, 2010.02.12 11:46

The following brought to mind by a discussion on hedging which gordon and I have been participating in...

If you are closing multiple orders then - provided your broker supports it - I'd consider placing an opposing order for the size of your total position, and then repeatedly pairing things off. In other words, if you've got 5 buy orders for 0.2 lots, you place a sell order for 1 lot. And then repeatedly use OrderCloseBy().

This isn't pretty to do, because OrderCloseBy() keeps spawning new ticket numbers. However, as far as I'm concerned, it reduces the execution risk in MT4. If you're closing 5 orders, then there's the potential for the price to move quite strongly while those 5 separate requests are being handled one after the other. And there's 5 times the chance of encountering something like error #128. Of course, the price could move in your favour rather than against you, but personally I don't like the uncertainty. If you go down the pairing-and-offsetting route then your position is locked from the moment the closing order goes in (except for the effect of variations in the spread, but that's less significant than the risk from the price moving).

 

I have script for closed all. do PM me if you are interested. tq.

 
Alain Verleyen:

With mql5, you can use asynchronous requests. It's still one by one, but it's very fast as you don't have to wait server answers.

You could also try to "CloseBy" approach. Search on the forum about it.

seems there is no function such that for mq4 ?
 
Mehrdad Shiri:
seems there is no function such that for mq4 ?
Correct!
 
Mehrdad Shiri:
seems there is no function such that for mq4 ?

You may search in the market. There is at least 1 product with similar functionality.

Reason: