Close All Trades

Download script
Author: Pranav Soan

This script is designed as an automated tool for the MetaTrader 4 platform to provide users with a "panic button" or a quick exit strategy. Its primary purpose is to instantly identify and shut down every active trading position currently held in your account.

How the Logic Works:

The script follows a straightforward, three-step process once it is activated:

1. Scanning Your Portfolio As soon as the script is loaded onto a chart, it performs a comprehensive sweep of your open trades. It looks at your entire account to identify every live position, regardless of which currency pair or asset class they belong to.

2. Reverse Order Processing To ensure that the software doesn't skip over any trades while closing them, the logic processes the list in reverse order (starting from the most recent trade and working backward). This is a standard safety procedure in trading software to ensure that as one position is removed, the numbering of the remaining positions stays consistent for the computer to read.

3. Immediate Execution For every single open trade it finds during its scan, the script sends an immediate instruction to the server to close that position at the current market price. It handles this automatically, meaning you do not have to manually click "close" on individual trades.

Key Takeaways for the User:

  • Automation: You don't need to interact with the screen; once launched, the script does the work of clearing your positions for you.
  • Speed: The logic is designed to execute as quickly as possible, which is ideal for situations where you need to exit all market exposure instantly.
  • One-Time Action: Because the logic is contained within the initialization phase (the starting sequence), the script performs its task once upon being placed on a chart and then finishes its job.
Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
6 Views
0 Downloads
0 Favorites
Close All Trades
//+------------------------------------------------------------------+
//|                                             Close All Trades.mq4 |
//|                                                      Pranav Soan |
//|                                              pranasoan@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Pranav Soan"
#property link      "pranasoan@gmail.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
int total = OrdersTotal();
  for (int y=OrdersTotal()-1; y>=0; y--)
   {
     if (OrderSelect(y,SELECT_BY_POS,MODE_TRADES))
       {
        int ticket=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,Aqua);
       }
   }
   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {

   
  }
//+------------------------------------------------------------------+

Comments