delay in execution of EA

 
I've got a very simple EA that I wrote because I noted something bugging me, my EAs seem to be running very slow. Is this something that is broken dependent? Where does the basic clock signal come from that executes the code? Below is my actual code, any time an order is profitable it will try to close it, problem in the order can be profitable for a matter of minutes before the EA attempts anything whether it is successful or not. Also at times when it is getting repeated error closing an order I can click close and its closes virtually instantly. Is all of this typical? I know my computer is not the problem 4 core 3.4Ghz, 8GB Ram, etc. and I dont have any latency issues elsewhere just with the execution times of the EA.

CODE:
int i;

int start(){
int totalTrades=OrdersTotal();
for(i=totalTrades;i>0;i--){
OrderSelect(i, SELECT_BY_POS);
if(OrderProfit()>0){
switch(OrderType()){
case 0:
// close long order
int order=OrderClose(OrderTicket(),OrderLots(),Bid,3,CLR_NONE);
if(order<1) Alert("Error closing long order: ",GetLastError());
break;
case 1:
// close short order
order=OrderClose(OrderTicket(),OrderLots(),Ask,3,CLR_NONE);
if(order<1) Alert("Error closing short order: ",GetLastError());
break;
default:
Alert("Error: Abnormal order type!");
break;
}
}
}
return(0);
}
 
biqut2:
[...] my EAs seem to be running very slow [...] getting repeated error closing an order [...]

Your errors are a result of buggy code and I am pretty sure that's also the reason it seems to be "slow" or not working...


Problems with your code:

  1. Trade pool index starts at 0; loop should be from OrdersTotal()-1 down to 0.
  2. Return of OrderSelect() should be checked and actions done accordingly.
  3. A RefrashRate() is needed in your loop (ensure latest Bid, Ask are used).
  4. Alternatively, u can use OrderClosePrice() instead of Bid/Ask (and u can make the code much shorter by closing if OrderType()<=1).
  5. There is no reason to declare variable i on a global scope. Just declare it in the for loop header.
  6. Closing when OrderProfit()>0 with no 'margin' will not work properly in Live. See more info here -> https://www.mql5.com/en/forum/125528.
And one more thing:
 
Ah thank you, that made all the difference. I did not know the OrderClosePrice() function worked like that, My take after reading the documentation was that it worked on orders already closed. This does simplify the code greatly. As for the trading pool I did miss the '-1' i normally put in there. The variable declared globally, that's just a preference of mine so that I can easily pick them out and make changes to the initial value easily by looking at the top of the source. As for the 'OrderProfit()>0' this actually works very well for me. changed code:
extern int slippage=3;

int start(){
   int totalTrades=OrdersTotal()-1;
   for(int i=totalTrades;i>=0;i--){
      OrderSelect(i, SELECT_BY_POS);
      RefreshRates();
      if(OrderProfit()>0 && OrderType()<=1){
         int order=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,CLR_NONE);
         if(order<1) Alert("Error closing order: ",GetLastError());
         else Alert("Order closed!");
      }
   }
   return(0);
}
Reason: