Skip to content
All library documents

Trailing Stops Using Bid and Ask Prices in MQL5

Article MQL5 code base

Summary

This document describes an MQL5 method for trailing stop losses on open positions. A function runs on each tick, loops through positions, and uses bid prices for long positions and ask prices for short positions to calculate a stop level at a configurable distance. It preserves the take-profit value when modifying a position and attempts to set an initial stop after price moves favorably, then advance the stop as the market continues to move.

The material is an implementation example rather than a tested trading strategy: it provides code and setup notes, but no performance evidence or market-specific guidance. The logic includes an apparent weakness for short positions: its test for a missing stop loss compares the stop with the open price in a way that may fail when no stop is set. Users should also account for broker stop-distance rules, symbol point size, and trade modification errors, none of which the document discusses.

Key ideas

  • The example checks for open positions on every price tick and applies trailing logic to each one.
  • It calculates long-position stops from the bid and short-position stops from the ask.
  • A configurable distance controls how far the stop trails the market price.
  • The position modification calls retain the existing take-profit level.
  • The code does not address broker constraints or report whether stop modifications succeed.

Tags

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