This repository holds the code of the article series **Modular Indicator Architecture in MQL5** by [Vladislav Boyko](https://www.mql5.com/en/users/boyvlad) on MQL5.com. The series builds an object-oriented framework for MetaTrader 5 custom indicators, in which an indicator is assembled from reusable modules. Each article explains its changes to the code through the commits of this repository.
The code grows in single-purpose commits, and the articles explain them in the order of the history. The code snippets of an article have captions with links to the commits that the code comes from. Under each part below, a table lists the commits of each section of the article.
The tag of a part marks the code at the end of that part. The `main` branch has the latest code and can be ahead of the latest tag: its commits after that tag belong to no published part yet.
## The Code
| File | Description |
| :--- | :--- |
| [`Evolution.mq5`](Evolution.mq5) | The main indicator file acting as the entry point and containing the MQL5 event handlers. |
| [`src/Indicator.mqh`](src/Indicator.mqh) | The root object that wires all sub-indicators together, eliminating the need for global state. |
| [`src/IndicatorBase.mqh`](src/IndicatorBase.mqh) | Abstracts away infrastructure boilerplate (such as `rates_total` checks) from the main indicator logic. |
| [`src/InputsAndRelated.mqh`](src/InputsAndRelated.mqh) | Defines the global input variables and `CIndiParams`, the derived class mapping them to the parameter object. |
| [`src/IndiParamsBase.mqh`](src/IndiParamsBase.mqh) | Defines the base class for indicator parameters, decoupling the core logic from global input variables. |
| [`src/SubIndiRegistry.mqh`](src/SubIndiRegistry.mqh) | A static registry that centrally manages the `barsRequired` requirement across all modules. |
| [`src/SubIndiBase.mqh`](src/SubIndiBase.mqh) | The base class for all sub-indicators that extracts common traits and handles routine boilerplate. |
| [`src/AppliedPrice.mqh`](src/AppliedPrice.mqh) | A sub-indicator that calculates the applied price. |
| [`src/SmoothingBase.mqh`](src/SmoothingBase.mqh) | The base class for all smoothing sub-indicators that defines their common interface. |
| [`src/Sma.mqh`](src/Sma.mqh) | A sub-indicator that calculates the Simple Moving Average. |
| [`src/Ema.mqh`](src/Ema.mqh) | A sub-indicator that calculates the Exponential Moving Average. |
| [`src/Smma.mqh`](src/Smma.mqh) | A sub-indicator that calculates the Smoothed Moving Average. |
| [`src/Lwma.mqh`](src/Lwma.mqh) | A sub-indicator that calculates the Linear Weighted Moving Average. |
## Part 1: Stop Copy-Pasting and Start Writing Scalable, Reusable Code
The article develops an object-oriented framework for MQL5 indicators by evolving a primitive example into reusable modules. It formalizes partial buffer recalculation in `OnCalculate`, moves logic into header-based classes (`CAppliedPrice`, `CSma`), and introduces `CSubIndiBase`, `CIndicatorBase`, and a registry to centralize requirements. You get portable components, isolated inputs, and clean buffers with minimal boilerplate, making new indicators faster to assemble and easier to maintain.
| Section | Commit |
| :--- | :--- |
| [1. The Starting Point: A Primitive Implementation](https://www.mql5.com/en/articles/17566#s1) | [Initial commit](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/6a73b7ac7916a9f66ff9851e6016a7efdf4cf757) |
| | [refactor: unify buffer recalculation loop in OnCalculate](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/bdf3183c28fb3d60bc362e38403ae59f8d595bad) |
| | [refactor: collapse switch cases in OnCalculate](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/db1e57732e78d24b736a64d6795b69909a311acb) |
| [3. Encapsulating Logic: The First Module](https://www.mql5.com/en/articles/17566#s3) | [refactor: extract applied price into CAppliedPrice](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/256ad48fefedccd084e29dcba618184315d786a0) |
| [4. Eliminating Global State: Introducing CIndicator](https://www.mql5.com/en/articles/17566#s4) | [refactor: introduce CIndicator as root object](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/a73c54201e1a7248ffc9e98256eb8103ab2991ae) |
| [9. Building the Second Module: SMA](https://www.mql5.com/en/articles/17566#s9) | [refactor: rename applied price parameter to 'Apply to'](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/c37f8382a6064ca3e7633fdefcc494a0fa6b9812) |
| | [feat: add SMA period input parameter](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/7e121a660672c58e2154e4251a9ae59394c35733) |
| | [feat: integrate CSma into CIndicator](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/db294e800a36fc0df94aaaa220f55590ebbc35e7) |
| | [feat: reconfigure plots and buffers](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/654e54bb271e3cedd823239880ea6f97f48928e6) |
| | [refactor: rename barIdxToRecalcFrom to recalcStartIdx](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/1507c1be8d5aa6e8948a01f7cf8db6b84e92920d) |
| | [refactor: replace 0 with drawBegin in CAppliedPrice](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/73413fcd5fae9942c39f9bdbc736322580b3e175) |
| | [refactor: pull up prepareBeforeCalculating to base class](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/acfbfa108f101e6ed77f040cd96e4d2a0985c73a) |
## Part 2: Standard Smoothing Methods as Interchangeable Modules
EMA, SMMA, and LWMA join SMA as modules of the indicator framework from Part 1, all four derived from a new base class, `CSmoothingBase`. The root object, `CIndicator`, dynamically creates the module of the chosen smoothing method and works with it through a base-class pointer. Besides the code, the article explains the smoothing methods themselves, with formulas and worked examples, and compares the four: how they weight the bars, what they need to start, and what their calculation costs.
| Section | Commit |
| :--- | :--- |
| [1. Fixing a Bug in CSma](https://www.mql5.com/en/articles/23623#s1) | [fix: ensure recurrence seed is calculated when recalculation touches drawBegin](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/b589d8d09aa51b9428bd66a87da455d8acf03440) |
| | [refactor: rename prepareBeforeCalculating to prepareCalculation](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/4ec4cc98cad7e4f1e292280df2607dfaa1490b86) |
| | [refactor: add prepareCalculation overload with isFullCalculation flag](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/dfac980ebd956ff661b0419d025b62e353a27c0c) |
| [5. Setting the Plot Label Dynamically](https://www.mql5.com/en/articles/23623#s5) | [refactor: set plot label dynamically based on smoothing method](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/b6f1612c118b4498c3ae6964f7266cecee09122a) |
Apart from the commits that change only this README, two commits of this part are in no section, and neither changes what the code does: [docs: add code comments to all source files](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/9f9824611676958f2b490f3dc22bb6ad69ddb2c6), before Section 1, and [style: align pointer and reference symbols with type](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/cd50d18f993ae3cc75afd7cf2186d2eea188014a), between Sections 3 and 4.