Skip to content
All library documents

Closing Trading Positions at a Target Aggregate Profit

Article MQL5 code base

Summary

The article explains an Expert Advisor approach that closes trades when current profit reaches a currency-denominated threshold, instead of setting a take-profit distance in pips. It shows how to count open orders and sum their profits, optionally filtering both operations by a magic number so that an EA instance can manage its own trades. It then describes checking eligible buy and sell orders and closing them when the profit condition is met.

This method can be applied to a group of positions and may make profit targets less dependent on entry-price distance or broker slippage. The article also suggests adapting the logic for a profit-based trailing stop or removing the magic-number filter to manage trades across EAs. Its examples are implementation guidance, not evidence of trading performance. The described profit total is accumulated across matching orders, while the closing loop checks that total against each eligible position; readers should account for that distinction when implementing the behavior. Execution costs and order-close failures are not analyzed.

Key ideas

  • A take-profit condition can be based on account-currency profit rather than a fixed pip distance.
  • A magic number can scope profit tracking and trade closure to a particular EA instance.
  • The method sums profit across open orders and uses a threshold to trigger closures.
  • The same concept can support group-level exits or a profit-based trailing rule.
  • The article provides code guidance but no performance study and does not analyze execution failures or costs.

Tags

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