Code: Select all
void CloseAllTrades(bool CloseAll, int DontClose)
{
if (CloseAll == false) return;
Alert (Symbol() + " Closing All Trades");
for(int i=OrdersTotal();i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
int type = OrderType();
bool result = false;
if ( (OrderTicket() == DontClose) || (OrderSymbol() != Symbol()) )
{
result = true;
type = 999;
}
switch(type)
{
//Close opened long positions
case OP_BUY : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
break;
//Close opened short positions
case OP_SELL : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
break;
//Close pending orders
case OP_BUYLIMIT :
case OP_BUYSTOP :
case OP_SELLLIMIT :
case OP_SELLSTOP : result = OrderDelete( OrderTicket() );
}
if(result == false)
{
Alert(Symbol() + " Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
Sleep(3000);
}
}
return;
}
CloseAllTrades(true,902101321); note the 902101321 is just a value to pass.. if you know the ticket number it will close all trades except for that.