|
Mehdi,
Put this code (procedure) outside start() section and when you if the condition is met to close all trades, just call the procedure like that:
int start()
{ // your code here .....
If (your condition here) ShutDownAllTrades();
}
void ShutDownAllTrades
{ int Retry;
While (OrdersTotal()>0)
{ for (int cnt=OrdersTotal()-1;cnt>=0;cnt--)
{ OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderType()==OP_BUY)
{ Retry=0; while (Retry<5 && !IsTradeAllowed()) { Retry++; Sleep(1000); }
RefreshRates();
OrderClose(OrderTicket(),OrderLots(),Bid,Slippage, Yellow);
Sleep(1000);
}
else if (OrderType()==OP_SELL)
{ Retry=0; while (Retry<5 && !IsTradeAllowed()) { Retry++; Sleep(1000); }
RefreshRates();
OrderClose(OrderTicket(),OrderLots(),Ask,Slippage, Yellow);
Sleep(1000);
}
else
{ Retry=0; while (Retry<5 && !IsTradeAllowed()) { Retry++; Sleep(1000); }
OrderDelete(OrderTicket());
Sleep(1000);
}
}
}
}
HTH
Yannis
|