Skip to content
All library documents

Using Portfolio Profit and Loss Thresholds to Close Positions Automatically

Article MQL5 code base

Summary

This document describes a MetaTrader 5 expert advisor that monitors the combined profit or loss of open positions and closes them when user-set thresholds are reached. Traders can enable profit-triggered closing, loss-triggered closing, or both, and choose whether to receive alerts. The stated mechanism checks position profit, compares the total with the configured targets, then sends market orders to close positions and records activity in terminal logs.

The example illustrates how a profit target and a maximum-loss threshold would trigger liquidation, but it is not a test of strategy performance. The tool applies account-currency thresholds to all open positions, so the chosen limits determine how much exposure is closed together; the document does not explain per-symbol controls, partial exits, or portfolio-level risk calibration. It notes that market-order execution can incur slippage and that the advisor runs continuously until stopped or removed. As a result, threshold automation can enforce a predefined exit rule, but it cannot guarantee a fill at the threshold or that the chosen limits fit a trader’s strategy.

Key ideas

  • The advisor monitors aggregate profit and loss across open positions and closes positions when enabled thresholds are reached.
  • Profit and loss triggers can be configured independently, with optional alerts.
  • Closing uses market orders, so execution prices may differ from the trigger level due to slippage.
  • The tool continues running until manually stopped or removed, and the document gives no performance evidence.

Tags

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.