Managing Buy and Sell Stops with a Trailing Stop Function
Summary
This document presents an MQL-style function for updating stop-loss levels on open buy and sell orders. It can filter which orders it manages by symbol and magic number, set an initial stop after a trade moves favorably by a start distance, and then adjust the stop to remain a chosen distance from the market. The examples use bid prices and instrument point and digit settings to calculate and normalize stop levels.
The code reports failed order modifications, but it supplies no tests, execution results, or guidance on choosing the distances. Its behavior also depends on broker rules and instrument quoting conventions. Several conditions, particularly in the sell-order branch, appear questionable, so the implementation should be reviewed carefully before use. A trailing stop can help lock in gains or limit losses, but it does not guarantee a fill at the requested level during gaps or fast markets.
Key ideas
- The function can apply stop updates only to orders matching optional symbol and magic-number filters.
- It sets an initial stop after price moves favorably by a configured trigger distance.
- Once the initial stop is in place, the function trails it at a set distance from the market.
- The code normalizes prices and logs failed order modifications.
- The sell-side conditions appear questionable, and broker execution rules can affect actual stop behavior.
Tags
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.