biqut2:
[...] my EAs seem to be running very slow [...] getting repeated error closing an order [...]
[...] 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:
- Trade pool index starts at 0; loop should be from OrdersTotal()-1 down to 0.
- Return of OrderSelect() should be checked and actions done
accordingly.
- A RefrashRate() is needed in your loop (ensure latest Bid, Ask are used).
- Alternatively, u can use OrderClosePrice() instead of Bid/Ask (and u can make the code much shorter by closing if OrderType()<=1).
- There is no reason to declare variable i on a global scope. Just declare it in the for loop header.
- Closing when OrderProfit()>0 with no 'margin' will not work properly in Live. See more info here -> https://www.mql5.com/en/forum/125528.
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); }
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
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);
}