Closing A Large Number of Trades At One Time

 
Is there a good way to do this? Right now I am using the following code: int numOrders = OrdersTotal(); for (int i = 0; i numOrders; i++) { OrderSelect(0, SELECT_BY_POS, MODE_TRADES); if (Symbol() == OrderSymbol()) { if (OrderType() == OP_BUY) { OrderClose(OrderTicket(), OrderLots(), Bid, 3, Violet); } else { OrderClose(OrderTicket(), OrderLots(), Ask, 3, Violet); } } } But it doesn't seem to close them all in one tick... Is there any reason for this? Thanks -Shane
 
see 'Problem with close all orders'

note that close loop should be directed from end to begin
 
Can you do it without the 1 sec sleep?? When I am closing many trades, the price changes while they are closing. Can this be avoided?
 
You can remove sleep but You must check price changes
 
How do you do that? Would it work if I just made the sleep shorter?
 
'How to place (and close correctly) multiple orders in an EA'

Just remove or comment line with Sleep function call
 
rowand33 wrote:
Is there a good way to do this? Right now I am using the following code: int numOrders = OrdersTotal(); for (int i = 0; i numOrders; i++) { OrderSelect(0, SELECT_BY_POS, MODE_TRADES); if (Symbol() == OrderSymbol()) { if (OrderType() == OP_BUY) { OrderClose(OrderTicket(), OrderLots(), Bid, 3, Violet); } else { OrderClose(OrderTicket(), OrderLots(), Ask, 3, Violet); } } } But it doesn't seem to close them all in one tick... Is there any reason for this? Thanks -Shane

I took a completely different approach to this issue. I calculate where I want to exit and update the take profit value to that point. If the profit target changes use OrderModify to change the take profit value. You must be careful to do this prior to exceeding the minimum from the current price (usually around .0005 for the EURUSD). I have used this to exit dozens of orders in less than a second.

If you cannot easily calculate the take profit value well in advance, you can also put hedge orders in the opposite direction way above the market (in the case of longs), move them just prior to your exit and use OrderCloseBy for the actual exit.   You won't incure the extra spread cost because most brokers don't charge the spread on a closing order.
 
ScottB wrote:
rowand33 wrote:
Is there a good way to do this? Right now I am using the following code: int numOrders = OrdersTotal(); for (int i = 0; i numOrders; i++) { OrderSelect(0, SELECT_BY_POS, MODE_TRADES); if (Symbol() == OrderSymbol()) { if (OrderType() == OP_BUY) { OrderClose(OrderTicket(), OrderLots(), Bid, 3, Violet); } else { OrderClose(OrderTicket(), OrderLots(), Ask, 3, Violet); } } } But it doesn't seem to close them all in one tick... Is there any reason for this? Thanks -Shane

I took a completely different approach to this issue. I calculate where I want to exit and update the take profit value to that point. If the profit target changes use OrderModify to change the take profit value. You must be careful to do this prior to exceeding the minimum from the current price (usually around .0005 for the EURUSD). I have used this to exit dozens of orders in less than a second.

If you cannot easily calculate the take profit value well in advance, you can also put hedge orders in the opposite direction way above the market (in the case of longs), move them just prior to your exit and use OrderCloseBy for the actual exit. You won't incure the extra spread cost because most brokers don't charge the spread on a closing order.
 
you need to add RefreshRates in your loop. this will give you latest bid/ask spread for each trade.
 
ScottB wrote:
... If you cannot easily calculate the take profit value well in advance, you can also put hedge orders in the opposite direction way above the market (in the case of longs), move them just prior to your exit and use OrderCloseBy for the actual exit. You won't incure the extra spread cost because most brokers don't charge the spread on a closing order.

Hi Scott,

Could you please explain how works and how to use OrderCloseBy ?
I have not found a clear explanation in the documentation neither any detailled example on the site.

thanks
Reason: