# Article-23576-Automating-Trading-Strategies-MQL5-Double-Top-Double-Bottom This repository is an article-derived reference project based on the original MQL5 article. It does not claim to reproduce the full original source code unless files are explicitly attached. ![Article preview](https://c.mql5.com/2/237/Screenshot_2026-07-27_210600_1200x628.jpg) ## Overview This repository is derived from the MQL5 article about automating the Double Top and Double Bottom reversal model in MetaTrader 5. The article explains how to convert a visually interpreted chart pattern into a rules-based Expert Advisor that detects confirmed swing pivots, validates the pattern with tolerance and structure filters, and executes trades on neckline breakout or optional pullback. The project serves as a reference implementation source for traders and MQL5 developers who want to study objective pattern detection, state-machine driven entry logic, and trade management around reversal setups. ## Original Article - **Article ID:** 23576 - **Title:** Automating Trading Strategies in MQL5 (Part 53): Double Top and Double Bottom Reversal Model - **Author:** Allan Munene Mutiiria - **Author Profile:** https://www.mql5.com/en/users/29210372 - **Publication Date:** Not available - **Categories:** Trading, Trading Systems, Expert Advisors, Examples Experts - **Article URL:** https://www.mql5.com/en/articles/23576 ## Repository Purpose This repository exists to preserve and organize the technical ideas from the article in repository form. A reader can use it to: - understand how double top and double bottom patterns can be formalized in MQL5 - study swing-pivot based pattern detection - learn how to structure a pattern-trading EA with setup states such as idle, armed, and retest - reuse the article’s architecture for experimentation, testing, or further extension in MetaTrader 5 Because this is article-derived, the practical completeness of the repository depends on the files actually attached by the original author. ## Key Concepts - Trading - Trading Systems - Expert Advisors - Examples Experts - Double top pattern detection - Double bottom pattern detection - Swing pivot confirmation - Neckline breakout confirmation - Pullback entry logic - Pattern validation filters - Measured-move targeting - Reward-to-risk target selection - Trailing stop management - State machine based setup handling ## Algorithm / Architecture Summary The article describes an Expert Advisor that processes price action bar by bar and builds mechanical trading rules around reversal patterns. 1. **Detect confirmed swing pivots** - The system confirms swing highs and swing lows using a configurable pivot length. - Only confirmed pivots are stored, so provisional right-edge pivots are avoided. 2. **Store recent pivots in a bounded buffer** - Confirmed swings are appended to a rolling history. - Duplicate pivots are ignored. - The search works only on recent pivot history. 3. **Search for a double top or double bottom** - The newest pivot is treated as the potential second top or second bottom. - The algorithm scans backward for a prior pivot of the same type. - The neckline is defined by the strongest opposite pivot between the pair: - lowest low between two highs for a double top - highest high between two lows for a double bottom 4. **Validate the pattern** - Peak or trough similarity is checked using a tolerance relative to pattern height. - Minimum spacing in bars can be enforced. - Leg balance between the first leg and second leg can be enforced. - An optional prior-trend requirement can reject patterns that do not reverse a meaningful preceding move. 5. **Arm the setup** - Once validated, the EA stores the pattern direction, peaks, neckline, extreme, and optional leading trend leg. - The setup enters an armed state, waiting for neckline confirmation. 6. **Wait for breakout confirmation** - The pattern is confirmed only when price closes through the neckline. - If price invalidates the structure by exceeding the pattern extreme first, the setup is discarded. - A timeout prevents stale setups from remaining active indefinitely. 7. **Optional pullback workflow** - The EA can either enter immediately on breakout or wait for a retest of the neckline. - In pullback mode, the setup transitions into a retest state and tracks the pullback path until price touches the neckline again or the setup expires. 8. **Open and protect the trade** - Entry is placed at market. - Stop loss is placed beyond the pattern extreme with a configurable buffer. - Take profit can be calculated in two ways: - measured move projected from the neckline - reward-to-risk multiple based on stop distance 9. **Manage active positions** - The EA can trail the stop once profit exceeds a configured activation threshold. - Trailing applies only to positions associated with the EA’s symbol and magic number. 10. **Visualize the pattern** - The article also describes drawing pattern legs, neckline, labels, breakout path, and pullback annotations on the chart for interpretation and debugging. ## Mentioned or Attached Files ### Explicitly attached files - An attached ZIP archive is available: https://www.mql5.com/en/articles/download/23576_264386.zip?s=53ca505847b5bd14c085f94ec9cd7b658d340fceb49638e7a82c206d70f5a3bd&t=1788799540 - The internal file list of the ZIP archive was not provided in the processed input. ### Files mentioned in the article text - `Double Top and Double Bottom.mq5` - `Trade\Trade.mqh` ## Statistics - **Word count:** Not available - **Reading time:** Not available - **Image count:** Not available - **Code block count:** Not available - **File count:** Not available ## Tags - mql5 - metatrader-5 - trading - trading-systems - expert-advisors - examples-experts - difficulty-advanced ## Difficulty **Advanced** Reason: the article covers non-trivial pattern formalization, pivot-based signal extraction, multi-stage setup handling, configurable validation filters, order management, and trailing-stop logic in an event-driven MQL5 Expert Advisor. ## Limitations - This repository is based on article analysis and attached-material availability rather than a guaranteed full standalone project dump. - The full original source structure is not confirmed from the processed input beyond the file references explicitly visible in the article. - Although a ZIP archive is attached, its internal contents were not listed in the processed input. - Backtesting screenshots in the article do not guarantee live trading performance. - The article explicitly presents the work for educational purposes and emphasizes testing and risk management before live deployment. - Production readiness should not be assumed unless verified from the actual attached source files. ## Reference - Original article: https://www.mql5.com/en/articles/23576 - Author profile: https://www.mql5.com/en/users/29210372