Rejoignez notre page de fans

AutoCloseOnProfitLoss Expert - Automatically Close All Positions on Profit/Loss - expert pour MetaTrader 5
- Vues:
- 641
- Note:
- Publié:
- 2025.05.14 04:17
-
Besoin d'un robot ou d'un indicateur basé sur ce code ? Commandez-le sur Freelance Aller sur Freelance
Introduction
The AutoCloseOnProfitLoss Expert Advisor (EA) is a powerful automation tool for MetaTrader 5, designed to close all open positions when predefined profit or loss targets are reached. This EA is perfect for traders who want to manage risk and secure profits without manual intervention. Whether you're a scalper targeting quick gains or a long-term trader protecting capital, this EA offers a reliable, hands-free solution.
Features
- Automated Position Closing: Automatically closes all open positions when the total profit or loss meets the specified threshold.
- Customizable Targets: Set profit and loss targets in your account currency (e.g., USD, EUR).
- Flexible Options: Independently enable or disable closing based on profit or loss.
- User Notifications: Displays alerts and logs in the terminal when positions are closed.
- Efficient and Lightweight: Runs continuously with minimal CPU usage, ideal for ongoing trade management.
How It Works
The EA monitors the total profit or loss of all open positions using the PositionGetDouble(POSITION_PROFIT) function in MetaTrader 5. It compares this value against the user-defined TargetProfit and MaxLoss thresholds. If either condition is met (and the corresponding feature is enabled), the EA closes all positions using market orders and notifies the user through alerts (if enabled) and terminal logs.
Installation
- Download the AutoCloseOnProfitLoss.mq5 file from the MQL5 CodeBase.
- Open MetaTrader 5 and go to File > Open Data Folder.
- Navigate to MQL5 > Experts and copy the .mq5 file into this folder.
- Restart MetaTrader 5 or refresh the Navigator panel.
- Locate the EA in the Navigator under Expert Advisors, then drag it onto a chart to activate.
Input Parameters
- TargetProfit (default: 100.0): The profit target in your account currency to close all positions.
- MaxLoss (default: -50.0): The maximum loss in your account currency (must be a negative value) to close all positions.
- EnableProfitClose (default: true): Enable or disable closing positions when the target profit is reached.
- EnableLossClose (default: true): Enable or disable closing positions when the maximum loss is reached.
- ShowAlerts (default: true): Enable or disable alerts in MetaTrader 5 when positions are closed.
Usage
- Drag the AutoCloseOnProfitLoss EA onto any chart in MetaTrader 5.
- In the dialog box that appears, configure the input parameters:
- Set your desired TargetProfit and MaxLoss values.
- Enable or disable profit/loss closing as needed.
- Choose whether to display alerts.
- Click OK to start the EA.
- The EA will continuously monitor all open positions and close them if the specified conditions are met.
- Check the Experts or Journal tab in MetaTrader 5 for logs and results.
Example
Suppose you set TargetProfit = 100.0, MaxLoss = -50.0, EnableProfitClose = true, and EnableLossClose = true. If your open positions reach a total profit of $120, the EA will close all positions and display an alert: "All positions closed! Reason: Target profit reached: 120.00". If the total loss reaches -$60, the EA will close all positions with an alert: "All positions closed! Reason: Max loss reached: -60.00".
Notes
- The EA requires open positions to take action; otherwise, it will log the current profit/loss and continue monitoring.
- Positions are closed using market orders, so slippage may occur depending on market conditions.
- Test the EA on a demo account first to ensure it suits your trading strategy.
- Unlike a script, this EA runs continuously until manually stopped or removed from the chart.
Feedback
If you have suggestions or encounter issues, please leave a comment on the MQL5 CodeBase page or contact me through the MQL5 community. Your feedback is valuable for improving this tool!
Author: Duy Van Nguy
Contact: MQL5 Link
Updated: May 14, 2025
Platform: MetaTrader 5
Category: Expert Advisors

A simple utility for monitoring long and short swaps of a single symbol. If your broker-dealer's swaps are specified in points instead of account currency, this utility automatically converts points into account currency. Swaps are tripled on Wednesday. Horizontal and vertical alignment can be adjusted in the inputs.

It is taken into account that there are 30 days in a month, 365 days in a year The script demonstrates the counter of time until the beginning of the new year

The Acceleration/Deceleration Indicator (AC) measures acceleration and deceleration of the current driving force.

Indicator edition for new platform.