This article implements a flag and pennant detector in MQL5 using swing-structure data from a higher timeframe. The pole is validated by ATR-based height and a maximum bar duration, and the subsequent consolidation is replayed bar by bar on the chart timeframe with limits on minimum length, lifetime, and retracement. A three-state machine trades a fresh breakout once and projects a measured-move target from the pole's height.
A reversal pattern asks whether a trend is ending. A continuation pattern asks the opposite question: whether a pause in a trend is temporary. The flag and pennant are the classic examples. Both begin with a pole, a fast, steep, high-momentum price move that signals strong directional conviction. Furthermore, both are followed by a brief consolidation, a pause where the market absorbs the move without giving up much of the ground it gained. Both resolve with a breakout in the original direction, and both project a target using the height of the pole itself.
In Part 1 of this series, we built "CSwingEngine," a swing detection engine that reads market structure from a higher timeframe and labels every confirmed swing HH, LH, HL, or LL. Parts 2 and Part 3 used that engine to detect reversal patterns: the double top, double bottom, and head and shoulders. This article turns to a different kind of pattern entirely.
The classical distinction between the two is geometric. A flag consolidates in a shallow channel of two roughly parallel lines. A pennant consolidates in a converging wedge. The detector in this article does not measure geometry. Instead, it tests what both patterns share: a single impulsive leg, a bounded pause of sufficient length, and a close beyond the pause boundary. In plain terms it is a pole-and-consolidation breakout detector, and the flag and pennant names are used for the family of patterns it targets, not for shapes it distinguishes. The known limitations section returns to this.
A pole is also simpler to find than a reversal. A reversal needs several swings arranged in a specific sequence. A pole needs exactly two adjacent swings, one low and one high, and the distance between them must be large and quick. The consolidation that follows is then watched on the EA's own chart timeframe, because a pole's aftermath plays out over a handful of chart bars, not over a full higher-timeframe swing cycle.
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
Check out the new article: Automating Chart Patterns in MQL5 (Part 4): The Flag and Pennant.
A reversal pattern asks whether a trend is ending. A continuation pattern asks the opposite question: whether a pause in a trend is temporary. The flag and pennant are the classic examples. Both begin with a pole, a fast, steep, high-momentum price move that signals strong directional conviction. Furthermore, both are followed by a brief consolidation, a pause where the market absorbs the move without giving up much of the ground it gained. Both resolve with a breakout in the original direction, and both project a target using the height of the pole itself.
In Part 1 of this series, we built "CSwingEngine," a swing detection engine that reads market structure from a higher timeframe and labels every confirmed swing HH, LH, HL, or LL. Parts 2 and Part 3 used that engine to detect reversal patterns: the double top, double bottom, and head and shoulders. This article turns to a different kind of pattern entirely.
The classical distinction between the two is geometric. A flag consolidates in a shallow channel of two roughly parallel lines. A pennant consolidates in a converging wedge. The detector in this article does not measure geometry. Instead, it tests what both patterns share: a single impulsive leg, a bounded pause of sufficient length, and a close beyond the pause boundary. In plain terms it is a pole-and-consolidation breakout detector, and the flag and pennant names are used for the family of patterns it targets, not for shapes it distinguishes. The known limitations section returns to this.
A pole is also simpler to find than a reversal. A reversal needs several swings arranged in a specific sequence. A pole needs exactly two adjacent swings, one low and one high, and the distance between them must be large and quick. The consolidation that follows is then watched on the EA's own chart timeframe, because a pole's aftermath plays out over a handful of chart bars, not over a full higher-timeframe swing cycle.
Author: Tola Moses Hector