Order_Close_All

Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
0 Views
0 Downloads
0 Favorites
Order_Close_All
//+------------------------------------------------------------------+
//| Order_Close_Old.mq4                                              |
//|   Immediate close of the oldest order.                           |
//+------------------------------------------------------------------+
#property strict
//--- description
#property description "Immediate close of the oldest order."

// Global names for this iteration (currency pair)
string   GMaxSlip    = "GMaxSlip"+Symbol();
string   GStatus     = "GStatus"+Symbol();

double   MaxSlip     = 3;
int      iMaxSlip;


//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
// Can we create an order?
   if (!IsTradeAllowed()) return; 

   
// Load GLOBAL variables
   MaxSlip = GlobalVariableGet(GMaxSlip);

//--- Create the Close order
   iMaxSlip = int(NormalizeDouble(MaxSlip,0));
   
   int total  = OrdersTotal();

   if (total < 1)
   {
      Print("No orders to delete for ",Symbol());
      return;
   }
//--- Keep looping until all orders have been processed
   for (int pos = 0; pos < total; pos++)                            // Start with oldest
   {
      if (!OrderSelect(pos,SELECT_BY_POS,MODE_TRADES)) continue;    // May not still be valid
      if (OrderSymbol() == Symbol())                                // Is this our trade?
      {
         while (IsTradeContextBusy()) Sleep(100);
         if (OrderType()==OP_BUY)                  // Close One or All
            if (!OrderClose(OrderTicket(),OrderLots(),Bid,iMaxSlip,clrViolet)) 
               Print("Error closing OP_BUY order : ",GetLastError());
            else
               pos--;      // Back up one for deleted item
         else
         if (OrderType()==OP_SELL)                 // Close One or All
            if (!OrderClose(OrderTicket(),OrderLots(),Ask,iMaxSlip,clrViolet)) 
               Print("Error closing OP_SELL order : ",GetLastError());
            else
               pos--;
      }
   }
   // Tell Order_EA that we tried to delete EVERYTHING
   datetime Temp = GlobalVariableSet(GStatus, 3.0);

//--- Exit
}

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 ---