Reputation: 203
Is this the correct implementation of OrderClose()
?
for(int ii = 0; ii < OrdersTotal(); ii++)
{
if(OrderSelect(ii, SELECT_BY_POS, MODE_TRADES))
{
if(OrderType() == OP_BUY && OrderMagicNumber() == MagicStart && OrderSymbol() == symb)
{
int tikt = OrderTicket();
if(!OrderClose(tikt,OrderLots(),bid,4,clrPurple))
{
Print("Close Error", GetLastError());
}
}
if(OrderType() == OP_SELL && OrderMagicNumber() == MagicStart && OrderSymbol() == symb)
{
int tikt = OrderTicket();
if(!OrderClose(tikt,OrderLots(),ask,4,clrPurple))
{
Print("Close Error", GetLastError());
}
}
}
}
I am using this code right before opening a trade. For example, if there is a signal to buy then it closes the sell first and then opens a buy, and if there is a sell signal then it closes a sell first and then opens a buy.
But it only does this for the first time and wont work after that. Let's say there is a sell signal. Then it will close the buy and open the sell, but when there is a second signal for a buy then it won't close the sell neither will it open a buy.
There is no error in the experts tab. The only thing I receive in the experts tab is a message like this: Positions order mismatch
. It does not appear like an error or a warning, it just appears as a message.
Upvotes: 2
Views: 1479
Reputation: 1383
Try the following code which is more robust and does not assume the orders to be closed are of the same symbol of the chart the EA is running on. This will close all orders on the account. If you want to restrict this to orders that are on the chart the EA is running on, or orders only opened by the EA, you should use OrderSymbol()
and OrderMagicNumber()
to filter the orders before closing them.
if(OrdersTotal()>0)
{
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderType()==OP_BUY || OrderType()==OP_SELL)
{
double price;
if(OrderType()==OP_BUY) price=MarketInfo(OrderSymbol(),MODE_BID); else price=MarketInfo(OrderSymbol(),MODE_ASK);
int ticket=OrderTicket();
bool res=OrderClose(ticket, OrderLots(), price, 50, clrNONE);
if(!res) Print("Error Closing Ticket #", IntegerToString(ticket));
}
else
{
int ticket=OrderTicket();
bool res=OrderDelete(ticket);
if(!res) Print("Error Closing Ticket #", IntegerToString(ticket));
}
}
}
}
Upvotes: 0
Reputation: 309
If you want to close all orders, you need to start iteration from the latest order position to the first position. Try this:
for(int i = OrdersTotal()-1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS))
{
if(OrderType() == OP_BUY)
{
if(!OrderClose(OrderTicket(), OrderLots(), Bid, 0))
{
Print("Error closing order: ", GetLastError());
}
}
if(OrderType() == OP_SELL)
{
if(!OrderClose(OrderTicket(), OrderLots(), Ask, 0))
{
Print("Error closing order: ", GetLastError());
}
}
}
}
You shouldn't start iterating from 0 when you are closing orders, because OrdersTotal()
function decrement its value while you loop through all orders and closing them one by one.
For example (in your loop) you have 5 orders:
Additionally, instead of int tikt= OrderTicket();
, just use OrderTicket()
in OrderClose()
function.
Upvotes: 2