Close All Positions and Orders (Filtered) - MT4/MT5 Script
Info
The Close All Positions and Orders (Filtered) - MT4/MT5 Script is a Script for MetaTrader 5 that close all (filtered) is a script designed to close open positions and delete pending orders efficiently. This script offers robust filtering capabilities, allowing you to precisely control which trades are affected.
Usage
This tool is typically used for enhancing chart analysis and decision making.
Platform
This Script works exclusively on MetaTrader 5 (both build 600+ and newer versions).
Setup
Place the downloaded file in MQL5/Scripts folder via File ? Open Data Folder in MetaTrader 5.
How to Install and Use Close All Positions and Orders (Filtered) - MT4/MT5 Script
1. Installation: Move your script file into the MQL/Scripts directory and restart the platform.
2. Execution: Drag the script onto a chart; it will perform a one-time action, such as closing all open orders or clearing chart objects.
3. Editing: Use MetaEditor (F4) to modify code, click "Compile," and verify no errors appear in the terminal before running.
4. Removing: Scripts stop automatically, but you can remove them manually by right-clicking the chart and choosing "Remove Script."
Frequently Asked Questions
Q: How are scripts different from EAs? A: Scripts execute a single action and then stop; EAs monitor the market and trade continuously.
Q: Can I assign a hotkey to a script? A: Yes, right-click the script in the Navigator, select "Set Hotkey," and define your preferred keyboard shortcut.
Q: Why did my script stop? A: Scripts are designed to stop immediately after finishing their programmed command; this is normal behavior.
What this tool does
Close All (Filtered) is a script designed to close open positions and delete pending orders efficiently.
Typical Use Case
This Script excels in automated trading and technical analysis on MetaTrader 5.
Compatible Platform & Setup
This Script works on MetaTrader 5. Place the file in the MQL5/Scripts folder and restart the terminal.
Description & Settings
Related: Export Closed Trades to CSV with Summary by Magic Number - another powerful script for MetaTrader 5 traders.
Close All (Filtered) is a script designed to close open positions and delete pending orders efficiently.Also recommended: Trade History Exporter: Detailed Closed Trade Analysis to CSV - similar script with strong performance on MetaTrader 5.
This script offers robust filtering capabilities, allowing you to precisely control which trades are affected. For instance, you can close only losing sell trades from a specific EA, or exclusively close profitable trades on the current chart.
Before executing any actions, the script first counts the positions and orders that meet your specified filters and prompts for confirmation. Positions that encounter issues during closure, such as requotes or server busy errors, will be retried. The return code for each failure is logged in the Experts tab. A summary report is provided at the end, detailing the number of items closed or deleted versus the total number that matched the filters.
Confirmation Prompt Before Closing
Input Parameters:
Current Chart Symbol Only
- If true, the script operates only on the current chart's symbol. If false, it applies to all symbols.
Magic Number
- -1: Applies to any magic number.
- 0: Applies only to manual trades.
- Any other value: Applies only to trades with that specific magic number.
Side
- Options include: All (buys and sells), Buys Only, or Sells Only.
Which Positions
- Options include: All positions, Only in Profit, or Only in Loss (profit plus swap).
Close Positions
- Enable or disable the closing of open positions.
Delete Pending Orders
- Enables the deletion of matching pending orders. This option is only applicable when 'Which Positions' is set to 'All'.
Ask for Confirmation First
- Displays a Yes/No confirmation box showing the number of items to be acted upon.
Retries
- Specifies the number of passes to retry closing positions that initially failed.
Max Deviation
- Defines the maximum allowed slippage in points.
The script utilizes the standard CTrade class and is compatible with both netting and hedging accounts. Ensure that Algo Trading is enabled in your terminal settings. This script was updated in 2026.
You may also like: Close Positions by Custom Filters: Side, Symbol, Magic Number, and Profit - excellent alternative for script users on MetaTrader 5.
Source Code
#property copyright "RobotFX"
#property link "https://robotfx.org"
#property version "1.00"
#property description "This code is educational. Feel free to use, modify and upgrade as you wish. Visit ROBOTFX.ORG for professional trading tools"
#property script_show_inputs
#include <Trade\Trade.mqh>
enum ENUM_CA_WHAT
{
CA_ALL = 0,
CA_PROFIT = 1,
.......
⚠ Limitations & Risk Warning
- This tool is provided for educational and testing purposes only.
- Past performance does not guarantee future results.
- Trading involves substantial risk of loss. Use on a demo account first.
- Results may vary depending on market conditions, broker, and settings.
- We recommend thorough backtesting and forward testing before using with real funds.