CloseAll_FIFO

Download script
Author: Copyright 2021, David J. Díez Munilla

The CloseAll_FIFO script is a specialized utility designed for MetaTrader 5 (MT5) users who need to rapidly manage their open trading positions. Its primary purpose is to provide a clean, automated way to exit all current active trades simultaneously with high precision.

Core Functionality

The script operates on a "First-In, First-Out" (FIFO) logic principle when processing your open positions. When executed, it performs the following steps:

  • Comprehensive Scan: The script immediately identifies every active position currently held in your trading account across all symbols.
  • Sequential Closing: It systematically moves through your open positions one by one to execute a closing command for each.
  • Reliability Handling: The logic is designed with a built-in "safety check." If the script encounters an error while trying to close a specific position (for example, if a trade was already closed manually or a connection hiccup occurred), it captures that error message in the system log and continues attempting to close the remaining trades.
  • Loop Correction: To ensure no positions are skipped during the closing process, the script intelligently adjusts its counting logic. If a position is successfully closed, it re-verifies the list to ensure every single active trade is accounted for until the list is completely empty.

Key Benefits for the User

  • Efficiency: Instead of manually right-clicking and closing dozens of individual trades one at a time, this script automates the entire process in seconds.
  • Error Reporting: If a specific trade cannot be closed for any reason, the script provides a "Result Retcode," which allows you to see exactly what went wrong in the terminal's history rather than the script simply crashing or stopping midway.
  • Speed: It is designed to execute as quickly as the platform allows, making it ideal for emergency risk management or clearing out a portfolio at the end of a trading session.
8 Views
0 Downloads
0 Favorites
CloseAll_FIFO
ÿþ//+------------------------------------------------------------------+

//|                                                CloseAll_FIFO.mq5 |

//|                                    Copyright 2021, David J. Díez |

//|                         https://www.mql5.com/es/users/davidjdiez |

//+------------------------------------------------------------------+

#property copyright "Copyright 2021, David J. Díez Munilla"

#property link      "https://www.mql5.com/en/users/davidjdiez"

#property version   "1.00"

//+--- Libraries ----------------------------------------------------!

#include <Trade\Trade.mqh>

//+------------------------------------------------------------------+

//| Script program start function                                    |

//+------------------------------------------------------------------+

void OnStart(){

   CTrade trade;

   for(int i=0;i<PositionsTotal();i++){

      ulong iTicket=PositionGetTicket(i);

      if(!trade.PositionClose(iTicket,ULONG_MAX)){

         Print("PositionClose error ",trade.ResultRetcode());

         return;

         }

      else{i--;}

      }

   }

//+--------------------------------------------------------- End. ---+

Comments