|
EA - closing all positions at once.
I'm trying to run the following subroutine to close all my short positions at once. It works great with the backtester as it closes all short positions as expected. But when I run against a Demo account and leave the EA on all the time, it only appears to close 1 open order at each bar (since I only analyze charts when a new bar appears). Also, I don't see the !!!Warning message in the journal. So I'm thinking it's only executing the routine once or not looping properly.
Any ideas what I'm doing wrong.
void CloseAllShort() {
int total = OrdersTotal();
for (int cnt = 0 ; cnt < total ; cnt++) {
int order_type;
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
order_type = OrderType();
if(order_type == 1) {
if (!(OrderClose(OrderTicket(),OrderLots(),Ask,0,Blue ))) {
Print("!!!Warning - Order could not be closed, " + OrderTicket() + " " + GetLastError());
}
}
}
total = OrdersTotal();
Alert("<<<Short - All short orders were closed - " + total + " - $" + iClose(NULL,0,0));
}
|