//+------------------------------------------------------------------+
//| Close 1 Close All.mq4 |
//| Copyright © 2011, Aiman |
//| www.carigold.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, Aiman"
#property link "www.carigold.com"
int totalBuy=0;
int totalSell=0;
int deinit ()
{
Comment("");
return(0);
}
int start()
{
int buy=0, sell=0, i;
for (i=0; i<OrdersTotal(); i++)
{
OrderSelect(i, SELECT_BY_POS);
if (OrderType()==OP_BUY) buy++;
if (OrderType()==OP_SELL) sell++;
}
Comment ("Total Buy : ", DoubleToStr(buy,0), "\nTotal Sell: ", DoubleToStr(sell,0));
if (buy<totalBuy || sell<totalSell)
{
Comment ("\n\nClosing all positions....");
while (OrdersTotal()>0)
{
for (i=OrdersTotal()-1; i>= 0; i--)
{
OrderSelect(i, SELECT_BY_POS);
bool res = false;
switch(OrderType())
{
case OP_BUY : res = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,Blue); break;
case OP_SELL : res = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,Pink); break;
case OP_BUYLIMIT :
case OP_BUYSTOP :
case OP_SELLLIMIT :
case OP_SELLSTOP : res = OrderDelete( OrderTicket() ); break;
default : break;
}
if(!res) Print ("Failed to close, Ticket: " , OrderTicket() , " Code:" , GetLastError() );
Sleep(300);
}
}
buy = 0;
sell = 0;
}
totalBuy=buy;
totalSell=sell;
//----
return(0);
}