Close All Trades In All Charts Simultaneously Immediately

To add comments, please log in or register
strontiumDog
301
strontiumDog  

Hello

I've searched around and I've not found a script for MT4 specifically which closes all trades on all charts immediately at whatever price.  Has anyone got one?

This will help with testing EAs which work in strategies which work across multiple charts per terminal.

Thanks

qjol
3196
qjol  
strontiumDog:


Close All Trades In All Charts Simultaneously Immediately 

I've searched around and I've not found


because there isn't

never was and never will

Daniel Petrovai
412
Daniel Petrovai  
strontiumDog:

Hello

I've searched around and I've not found a script for MT4 specifically which closes all trades on all charts immediately at whatever price.  Has anyone got one?

This will help with testing EAs which work in strategies which work across multiple charts per terminal.

Thanks


Are you looking to close all orders placed by the EA on all symbols or do you want to close all orders regardless of who opened them ?

If you're looking to close one symbol regardless of who opened the order :

 

If you want to close all symbols, same function without the symbol condition :

 

void CloseAll()
  {
   int trade=0;
   closed=false;
   double price=0.0;
   for(trade=OrdersTotal()-1;trade>=0;trade--)
     {
      if(!OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))//if order select fails
        {
         Print("Order select failed in CloseAll() with error : "+(string)GetLastError());//tell me the error
         break;
        }
      // else if order select didn't fail
      else
        {
         price=0.0;
         if(OrderType()==OP_BUY)
           {
            price=Bid;
           }
         else
         if(OrderType()==OP_SELL)
           {
            price=Ask;
           }
         else
           {
            continue;
           }
         closed=OrderClose(OrderTicket(),OrderLots(),price,slip,Blue);
         if(!closed)
           {
            Print("Close order failed with error : "+(string)GetLastError());//tell me the error
           }
        }
     }
  }

 I believe you get the idea.

Note that pending orders have to be deleted not closed (Use OrderDelete() instead of OrderClose() ) .

qjol
3196
qjol  

@ thrdel your code doesn't do what strontiumDog wants

he want to close all the orders "Simultaneously Immediately"

so with your code ("or any code")  it's not gonna happen

B.T.W 

you don't need to write:

 

      else
        {
            price=0.0;
            if(OrderType()==OP_BUY)
              {
               price=Bid;
              }
            else
            if(OrderType()==OP_SELL)
              {
               price=Ask;
              }
            else
              {
               continue;
              }
         closed=OrderClose(OrderTicket(),OrderLots(),price,slip,Blue);

 

you can simple write

 

      else
        {
         closed=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slip,Blue);
Daniel Petrovai
412
Daniel Petrovai  
qjol:

@ thrdel your code doesn't do what strontiumDog wants

he want to close all the orders "Simultaneously Immediately"

so with your code ("or any code")  it's not gonna happen

Hi qjol:,

Long time, how are we ?

Sure one cannot close " all orders simultaneously immediately " but hey, one can only assume he meant ASAP.

In regards to  using OrderClosePrice(), I will test it  to see if it works. So far I only used OrderClosePrice() after selecting the order from history .

If it works , it's new for me.

William Roeder
20207
William Roeder  
thrdel: If it works , it's new for me.
OCP() works fine, has for years.
It's value is set at the OrderSelect, and doesn't update, just like Bid/Ask is without a RefreshRates.
Cromo
259
Cromo  

Hi,

How can I close all open orders in all chart by simbols?

William Roeder
20207
William Roeder  
kajironpu: How can I close all open orders in all chart by simbols?

Asked and answered. You have only four choices:

  1. Search for it,
  2. Beg at
    Coding help - MQL4 and MetaTrader 4 - MQL4 programming forum Requests & Ideas (MQL5 only!) - Trade FX - Expert Advisors and Automated Trading - MQL5 programming forum
    Free coding for your trading system.. - Easy Trading Strategy - General - MQL5 programming forum I will code & automate your strategy for free - Options Trading Strategies - General - MQL5 programming forum

  3. learn to code it. If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into yours.
  4. or pay (Freelance) someone to code it.
We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using CODE button) and state the nature of your problem.
          No free help
          urgent help.
Donald Gibson
494
Donald Gibson  
strontiumDog:

Hello

I've searched around and I've not found a script for MT4 specifically which closes all trades on all charts immediately at whatever price.  Has anyone got one?

This will help with testing EAs which work in strategies which work across multiple charts per terminal.

Thanks

This my emergency close function(not a script) I have used this for years without fail in the tester and demo trading. Since I don't use MT4 for my live trading I can't attest to how it will work there.

Good Luck.

void Close_AllOrders()
  {
/*-----------------------------------------------------------------------------------------------*/
//Close Orders according to FIFO Rule
   for(i=0; i<OrdersTotal(); i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol())
           {
            if(OrderType()==OP_BUY)
              {
               Close_Result=OrderClose(OrderTicket(),OrderLots(),Bid,0,clrNONE);
               if(Close_Result) i--;
              }
            if(OrderType()==OP_SELL)
              {
               Close_Result=OrderClose(OrderTicket(),OrderLots(),Ask,0,clrNONE);
               if(Close_Result) i--;
              }
            if(OrderType()==OP_SELLLIMIT ||
               OrderType()==OP_SELLSTOP ||
               OrderType()==OP_BUYLIMIT ||
               OrderType()==OP_BUYSTOP)
              {
               Close_Result=OrderDelete(OrderTicket(),clrNONE);
               if(Close_Result) i--;
              }
           }
     }
/*-----------------------------------------------------------------------------------------------*/
   return;
  }
nicholi shen
2392
nicholi shen  

There is a way to close all trades asynchronously for up to eight symbols at once.  Here are steps. 


  1. Make EA to apply a custom template to all charts upon init. This will remove any existing EAs that could interfere and replace it with an instance of itself to act as a worker bot. 
  2. Create a collection of symbols to be managed from the order pool. 
  3. Iterate over the symbols, sending them asynchronously to the worker EAs as strings via EventChartCustom
  4. Each worker EA then calculates the net position of its symbol and sends the appropriate hedge order to flatten the position. 
  5. Since all workers can compute values and send hedge orders async -- all positions regardless of the quantity of orders will be netted to 0 lots at the same time provided the number of workers is greater than or equal to the number symbols and the number of symbols to close is less than or equal to eight. 
  6. All risk is eliminated instantly but there will be offset orders on the books to be dealt with. The manager EA will need to loop through all offsetting orders using closeby to reconcile the transactions as "closed by hedge" with the broker server. There is no rush to do so since all risk has been eliminated by step 5. 
Note: It should go without saying that this method will not work with FIFO brokers.
To add comments, please log in or register