evolution/README.md
82 lines
12 KiB
Markdown
Vladislav Boyko
2026-06-11 16:41:04 -07:00
# Evolution
Vladislav Boyko
2026-02-20 15:09:43 -08:00
Vladislav Boyko
2026-11-01 11:00:02 -08:00
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.
| Part | Article | Code at the End of the Part |
| :--- | :--- | :--- |
| 1 | [Stop Copy-Pasting and Start Writing Scalable, Reusable Code](https://www.mql5.com/en/articles/17566) | [`article-part1`](https://forge.mql5.io/modular-indicator-architecture/evolution/src/tag/article-part1) |
| 2 | [Standard Smoothing Methods as Interchangeable Modules](https://www.mql5.com/en/articles/23623) | [`article-part2`](https://forge.mql5.io/modular-indicator-architecture/evolution/src/tag/article-part2) |
## How to Follow the Articles
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) |
| | [fix: enable tester_everytick_calculate](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/1cac034cfecb9084d73f277919df7cd7c49369b4) |
| | [fix: add sanity checks for rates_total and prev_calculated](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/6d896358688cf28c633ee66e575ad3ff41e15f55) |
| [2. Key Ingredients: Header Files and Object-Oriented Programming](https://www.mql5.com/en/articles/17566#s2) | none |
| [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) |
| [5. Isolating Input Parameters](https://www.mql5.com/en/articles/17566#s5) | [refactor: replace CIndicator ctor args with params object](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/6a95a6c6a96febbc1873234e0ea5802ab99b372a) |
| [6. Extracting Common Traits: CSubIndiBase](https://www.mql5.com/en/articles/17566#s6) | [refactor: extract rates_total checks into checkRatesTotal](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/b72c7e7a8c6b0c1dcc733fdf37cac6a07f2b1b76) |
| | [refactor: introduce CSubIndiBase for sub-indicators](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/a8fa958ee46bcc40fddd774b341806500dd391c8) |
| [7. Centralizing Requirements: CSubIndiRegistry](https://www.mql5.com/en/articles/17566#s7) | [refactor: centralize rates_total checks in CSubIndiRegistry](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/96627473d2877cad61ec1f70743106058975cd1b) |
| [8. Hiding Infrastructure Boilerplate: CIndicatorBase](https://www.mql5.com/en/articles/17566#s8) | [refactor: extract infrastructure logic into CIndicatorBase](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/97982404ff6f1f6fc953ddbca21fbd57f15a5cb3) |
| [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: outline CSma sub-indicator](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/26132b07e5b3d2c756711bac09a520043cef6654) |
| | [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) |
| [10. Calculating the Simple Moving Average](https://www.mql5.com/en/articles/17566#s10) | [feat: implement CSma::onCalculate](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/9814531a45961fd66ec49bcc78d7d65108237c5d) |
| [11. DRYing Sub-Indicators: Pulling Logic Up](https://www.mql5.com/en/articles/17566#s11) | [refactor: introduce clearBuffersAt hook for subclasses](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/246b12b7504b2af484b5f10626c0786ca34149a7) |
| | [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) |
| [2. Extracting CSmoothingBase](https://www.mql5.com/en/articles/23623#s2) | [refactor: introduce CSmoothingBase for smoothing sub-indicators](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/4abf7c110c5c242c7451eccabdca574ab9ef2912) |
| [3. Selecting the Smoothing Method at Runtime](https://www.mql5.com/en/articles/23623#s3) | [feat: add smoothing method input parameter](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/849387bfbcf6362281b82c3ce2c11c792b3f8635) |
| | [feat: switch CIndicator to dynamic CSmoothingBase pointer](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/143be74c961a0c10e1da4953dbfd5740bf32caad) |
| [4. Exponential Moving Average](https://www.mql5.com/en/articles/23623#s4) | [feat: implement CEma smoothing sub-indicator](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/17ac61072cbdd8473fd6c8af2cf8d3e799e4a2cc) |
| [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) |
| [6. Smoothed Moving Average](https://www.mql5.com/en/articles/23623#s6) | [feat: implement CSmma smoothing sub-indicator](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/b276d139e1b30e2ea762419a7d9303343c14f296) |
| [7. Linear Weighted Moving Average](https://www.mql5.com/en/articles/23623#s7) | [feat: implement CLwma smoothing sub-indicator](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/22cf0e14ab43a4f33077165b499c38f770815e2c) |
| [8. Optimizing LWMA](https://www.mql5.com/en/articles/23623#s8) | [perf: calculate LWMA using sliding window sums](https://forge.mql5.io/modular-indicator-architecture/evolution/commit/eb1b43a87cc57ad9895ebb26bc223080ebb16539) |
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.