Problem with close all orders

 

I'm working on an EA that trade more then one currency and everything is fine with placing orders, but when i try to close all orders it seems to only close the orders of the currency the EA is attached to. I use this code:

void CloseAll()
{
int total = OrdersTotal();
for(int cnt = 0; cnt < total; cnt++)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
}
}

i've also tryed

void CloseAll()
{
for(int trade=OrdersTotal()-1; trade>=0; trade--){
OrderSelect(trade,SELECT_BY_POS);

if( OrderType() == OP_BUY ){
if( IsTradeAllowed() ){
OrderClose(OrderTicket(),OrderLots(), Bid, 3,Blue);
}
}

if( OrderType() == OP_SELL ){
if( IsTradeAllowed() ){
OrderClose(OrderTicket(),OrderLots(), Ask, 3,Blue);
}
}

if( OrderType() == OP_BUYSTOP || OrderType() == OP_BUYLIMIT || OrderType() == OP_SELLSTOP || OrderType() == OP_SELLLIMIT ){
if( IsTradeAllowed() ){
OrderDelete(OrderTicket());
}
}
}
}

but non of the functions close all orders. Do i have to use Symbol or something to close order on other currencies then the chart EA is attached to?

 
You need to check selected order for Symbol. If OrderSymbol() differs from Symbol() then You should use closeprice=MarketInfo(OrderSymbol(), MODE_ASK) (or MODE_BID)
 
stringo wrote:
You need to check selected order for Symbol. If OrderSymbol() differs from Symbol() then You should use closeprice=MarketInfo(OrderSymbol(),MODE_ASK) (or MODE_BID)

Thanks for the answer stringo, it works fine now... But there is still one problem, how do i prevent only one order to stop when i get requoted... I did watch a trade and when trade started to close the orders it only stopped on and the other did lagg, which give me less profit. Is it possible to check before closeing the trades?
 
Nebex:

I'm working on an EA that trade more then one currency and everything is fine with placing orders, but when i try to close all orders it seems to only close the orders of the currency the EA is attached to. I use this code:

void CloseAll()
{
int total = OrdersTotal();
for(int cnt = 0; cnt < total; cnt++)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
}
}

i've also tryed

void CloseAll()
{
for(int trade=OrdersTotal()-1; trade>=0; trade--){
OrderSelect(trade,SELECT_BY_POS);

if( OrderType() == OP_BUY ){
if( IsTradeAllowed() ){
OrderClose(OrderTicket(),OrderLots(), Bid, 3,Blue);
}
}

if( OrderType() == OP_SELL ){
if( IsTradeAllowed() ){
OrderClose(OrderTicket(),OrderLots(), Ask, 3,Blue);
}
}

if( OrderType() == OP_BUYSTOP || OrderType() == OP_BUYLIMIT || OrderType() == OP_SELLSTOP || OrderType() == OP_SELLLIMIT ){
if( IsTradeAllowed() ){
OrderDelete(OrderTicket());
}
}
}
}

but non of the functions close all orders. Do i have to use Symbol or something to close order on other currencies then the chart EA is attached to?

Hi Nebex, I am finding it difficult to Close all trades at a time, please can you give me the full complete code that you use to do this? I will be greatful if you can also explain how to implement it and call it when it is needed. Thanks. Ayo
 

when closing you can't use Bid, as Bid is for current chart currency only.

Instead use MarketInfo(OrderSymbol(),MODE_BID or MODE_ASK) this will give you correct bid for each pair.