//+-------------------------------------------------------------------------+ //| IBFX - Quick CloseAll.mq4 | //+-------------------------------------------------------------------------+ #property copyright "Copyright © 2008, IBFX" #property link "www.ibfx.com" //---- int start() { /*+-------------------------------------------------------------------------+ Because these scripts are meant to execute fast there are no user external inputs. Make sure to modify the settings below, then compile the script before assigning a hot key to it and using it. The magicNumber HAS TO TO BE THE SAME ON ALL SCRIPTS if you change it here make sure to change it on all scripts!!! Do not forget to click on COMPILE once your changes have been made!!! +-------------------------------------------------------------------------+*/ int MagicNumber = 915; int Slippage = 1; string Commentary = " IBFX - Quick CloseAll "; string FontName = "Arial"; int FontSize = 12; //+-------------------------------------------------------------------------+ //| DO NOT MODIFY ANYTHING BELOW THIS LINE!!! | //+-------------------------------------------------------------------------+ //---- A few checks before we get started if( !IsConnected() ) { Alert( Commentary + " - No Connection!!" ); return(0); } //+------------------------------------------------------------------+ // CloseAllOrders | //+------------------------------------------------------------------+ int type = 0; int ErrorCode = 0; bool result = false; Comment( "IBFX - QuickCloseAll | Closing ALL Orders, please wait ..." ); if( CountAll( MagicNumber ) > 0 ) { while( CountAll( MagicNumber ) > 0 ) { for(int i = OrdersTotal()-1; i>=0; i--) { OrderSelect(i,SELECT_BY_POS,MODE_TRADES); if( MagicNumber == OrderMagicNumber() && OrderSymbol() == Symbol() ) { type = OrderType(); result = false; ErrorCode = 0; //-- Switch switch(type) { case OP_BUY : Wait(); result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 0, CLR_NONE ); if( result < 0 ) { ErrorCode = GetLastError(); } break; case OP_SELL : Wait(); result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 0, CLR_NONE ); if( result < 0 ) { ErrorCode = GetLastError(); } break; case OP_BUYLIMIT : case OP_BUYSTOP : case OP_SELLLIMIT : case OP_SELLSTOP : Wait(); result = OrderDelete( OrderTicket() ); break; default : break; } // Switch } }// For }// While } Comment(""); //---- return(0); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //+ Wait + //+------------------------------------------------------------------+ void Wait() { while( IsTradeContextBusy() ) { Sleep(50); } } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ // CountAll | //+------------------------------------------------------------------+ int CountAll( int MagicNumber ) { int Count=0; for(int i = OrdersTotal()-1; i>=0; i--) { OrderSelect(i,SELECT_BY_POS,MODE_TRADES); if( OrderMagicNumber() == MagicNumber ) { Count++; } } return(Count); } //+------------------------------------------------------------------+