How to speedup executing time of the closing process of many orders?

 

Hi, all.

Firstly, I am sorry with my poor English.

I have created an EA that open many orders on many currency pairs. When the total profit of all orders (in $) have reached the defined profit, the EA will closing all orders one by one. But this execution sequence takes more times and sometime finally profit after closing process finished came to LOSS.

My question is between

(1) Close all orders in the same execution sequence on one of chart, and

(2) Separate orders in 2-3 groups (or more) and close them separately on many chart (look like multi-thread)

, which method is more efficiency?


Thank you for advance.


Naret.D

 
hyperxeon:


My question is between

(1) Close all orders in the same execution sequence on one of chart, and

(2) Separate orders in 2-3 groups (or more) and close them separately on many chart (look like multi-thread)

You can only close one Order at a time . . . if you use several EAs to do that then you are going to have trade context is busy issues unless you use something like a Mutex . . . if you use one EA to close all orders you won't have any of the slight delays due to the Mutex.
 
hyperxeon:

Hi, all.

Firstly, I am sorry with my poor English.

I have created an EA that open many orders on many currency pairs. When the total profit of all orders (in $) have reached the defined profit, the EA will closing all orders one by one. But this execution sequence takes more times and sometime finally profit after closing process finished came to LOSS.

My question is between

(1) Close all orders in the same execution sequence on one of chart, and

(2) Separate orders in 2-3 groups (or more) and close them separately on many chart (look like multi-thread)

, which method is more efficiency?

Thank you for advance.

Naret.D

If you show the code where the trades get closed then it might be we can see where you can speed up the closing...

follow (1) use the chart where the EA is placed

(2) will get errors like tradecontext too busy

 
hyperxeon:

(1) Close all orders in the same execution sequence on one of chart, and

u can do it in "MT5"
 

Thank you for your answer.


=> RapterUK

Your are right, after checked expert's log file, I found that each execution started time was delayed.


=>deVries

EA use many lib, so it is difficult to show it, but it is easy concept like this,


<Case 1>Same execution sequence attached on EURUSD chart with below condition

if(totalProfit > TARGET_PROFIT){
while(OrdersTotal() > 0){
closeAllOrderAllCurrency(); // external function in individual lib file
}

}


<Case 2> Separate execution on many chart

2.1 Main EA attached on EURUSD chart with below condition

if(totalProfit > TARGET_PROFIT){

closeOnlyEURUSD(); // external function in individual lib file

}


2.2 Sub EA attached on AUDUSD, USDJPY chart with below condition

// This trigger will be true when totalProfit> TARGET_PROFIT and main EA starting close some of EURUSD order

// But it is must to wait the new TICK of each chart.

if(LastOrdersTotal > OrdersTotal()){

closeOnly(Symbol()); // on each chart

}

 
hyperxeon:

Thank you for your answer.

closeAllOrderAllCurrency(); // external function in individual lib file

Does this function count up or count down ?

Loops and Closing or Deleting Orders

 
RaptorUK:

Does this function count up or count down ?

Loops and Closing or Deleting Orders


Function is count down.

Full code as below.

Sorry, it is not professional coding and please ignore some constant value and print statement.

void closeAllOrderAllCurrency(){
   bool success;
   int myOP, maxErrorTime, total = 0;
   double myPrice, totalProfit = 0.0, totalLots = MY_ZERO;
   string mySymbol;
   double balance = AccountBalance()+ MY_ZERO;
   datetime stime, etime, maxTime = 0;
   for(int index = OrdersTotal() - 1; index >=0; index--){
      OrderSelect(index, SELECT_BY_POS, MODE_TRADES);
      myOP = OrderType();
      mySymbol = OrderSymbol();
      if(myOP == OP_SELL || myOP == OP_BUY){
         maxErrorTime = 5;
         while(maxErrorTime > 0){
            if(IsTradeAllowed()){
               RefreshRates();
               totalProfit += OrderProfit();
               totalLots += OrderLots();
               if(myOP == OP_BUY)
                  myPrice = NormalizeDouble(MarketInfo(mySymbol, MODE_BID), MarketInfo(mySymbol, MODE_DIGITS));
               else
                  myPrice = NormalizeDouble(MarketInfo(mySymbol, MODE_ASK), MarketInfo(mySymbol, MODE_DIGITS));
               maxTime = MathMax(maxTime, TimeCurrent() - OrderOpenTime());
               success=OrderClose(OrderTicket(), OrderLots(), myPrice, SLIP_PAGE * getMP(mySymbol), Aqua);
               if(!success) {
                  maxErrorTime--;
                  Print(getOperationName(myOP)+" order <" + mySymbol + "> closing error = "+ErrorDescription(GetLastError()));
                  Sleep(SLEEP);
                }
               else{
                  total++;
                  maxErrorTime = -1;
               }
            }
         }
      }
   }
   Print("::==============================================::");
   Print(":: TOTAL PROFIT = " + totalProfit+ " @ " + DoubleToStr(totalProfit/totalLots,2) + " < " + DoubleToStr(100*totalProfit/balance,2) + " % >::");   
   Print(":: MAXIMUM ESLAPED TIME = " + TimeToStr(maxTime, TIME_SECONDS)+ " ::");
   Print("::==============================================::");
}

This function is work perfectly for me, but it only use more times to finished.

As the previous result, some time it use more than 2 minutes to close about 40 orders. But average is approx. 2 sec/order.

 
qjol:
u can do it in "MT5"

Thank you qjol.

I know it can in MT5, but I cannot programing in MQL5, now ONLY basic MQL4.

 
hyperxeon:

1. Function is count down.

Full code as below.

Sorry, it is not professional coding and please ignore some constant value and print statement.


This function is work perfectly for me, but it only use more times to finished.

As the previous result, some time it use more than 2 minutes to close about 40 orders. But average is approx. 2 sec/order.

1. Good :-)

I don't think 2 secs per order is that bad . . . your code can be optimised and simplified a little, not sure it will help the speed much if at all.

You can use OrderClosePrice() then there is no need to distinguish between OP_BUY and OP_SELL, you can use the same line of code for both.

You are using MarketInfo so you don't need to use RefreshRates(), see documentation, RefreshRates is for . . "Refreshing of data in pre-defined variables and series arrays." predefined variables such as Bid, Ask, etc . . .

You can dispense with the NormalizeDouble, it isn't needed . . . really, it isn't. :-)

 
hyperxeon:

(1) Close all orders in the same execution sequence on one of chart, and

(2) Separate orders in 2-3 groups (or more) and close them separately on many chart (look like multi-thread)

, which method is more efficiency?

On one chart, you can only close orders one by one.

On multiple charts, you use a mutex to avoid busy context, you are closing one by one.

They close as fast as the server allows.

If you know where you want to close them, set the TP or SL level and they close on the server.

Reason: