Closing Tagged Forex Orders at Profit or Loss Thresholds
Summary
This MetaTrader 4 example describes an expert advisor that monitors the combined floating profit or loss of open market orders sharing a configured magic number. It exposes a profit target, an optional loss limit, and the magic number used to identify the orders. On each price tick, it sums profit, commission, and swap, then closes matching buy and sell orders when the total reaches either threshold.
The close routine selects bid or ask according to order direction and logs a warning if an order close fails. The example illustrates a group-level exit rule rather than separate stops for each position. It does not discuss testing, execution failures beyond the warning, or how thresholds should be chosen. The loss limit is disabled at zero, and the logic only closes market buy and sell orders, so pending orders are not covered.
Key ideas
- The advisor aggregates floating profit, commission, and swap across orders with a matching magic number.
- A profit target is required, while a zero loss limit disables the loss exit.
- When a threshold is reached, the advisor attempts to close all matching market buy and sell orders.
- Closing uses the appropriate bid or ask price for each order direction.
- The example provides no evidence about performance or guidance for calibrating exit thresholds.
Tags
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.