Quick CloseAll

Author: Copyright � 2008, IBFX
Quick CloseAll
Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
Quick CloseAll
//+-------------------------------------------------------------------------+
//|                                               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 = 3;
      string    Commentary = " 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( " 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);
}
//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---