evolution/README.md

Evolution

This repository holds the code of the article series Modular Indicator Architecture in MQL5 by Vladislav Boyko 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 article-part1
2 Standard Smoothing Methods as Interchangeable Modules 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 The main indicator file acting as the entry point and containing the MQL5 event handlers.
src/Indicator.mqh The root object that wires all sub-indicators together, eliminating the need for global state.
src/IndicatorBase.mqh Abstracts away infrastructure boilerplate (such as rates_total checks) from the main indicator logic.
src/InputsAndRelated.mqh Defines the global input variables and CIndiParams, the derived class mapping them to the parameter object.
src/IndiParamsBase.mqh Defines the base class for indicator parameters, decoupling the core logic from global input variables.
src/SubIndiRegistry.mqh A static registry that centrally manages the barsRequired requirement across all modules.
src/SubIndiBase.mqh The base class for all sub-indicators that extracts common traits and handles routine boilerplate.
src/AppliedPrice.mqh A sub-indicator that calculates the applied price.
src/SmoothingBase.mqh The base class for all smoothing sub-indicators that defines their common interface.
src/Sma.mqh A sub-indicator that calculates the Simple Moving Average.
src/Ema.mqh A sub-indicator that calculates the Exponential Moving Average.
src/Smma.mqh A sub-indicator that calculates the Smoothed Moving Average.
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 Initial commit
refactor: unify buffer recalculation loop in OnCalculate
refactor: collapse switch cases in OnCalculate
fix: enable tester_everytick_calculate
fix: add sanity checks for rates_total and prev_calculated
2. Key Ingredients: Header Files and Object-Oriented Programming none
3. Encapsulating Logic: The First Module refactor: extract applied price into CAppliedPrice
4. Eliminating Global State: Introducing CIndicator refactor: introduce CIndicator as root object
5. Isolating Input Parameters refactor: replace CIndicator ctor args with params object
6. Extracting Common Traits: CSubIndiBase refactor: extract rates_total checks into checkRatesTotal
refactor: introduce CSubIndiBase for sub-indicators
7. Centralizing Requirements: CSubIndiRegistry refactor: centralize rates_total checks in CSubIndiRegistry
8. Hiding Infrastructure Boilerplate: CIndicatorBase refactor: extract infrastructure logic into CIndicatorBase
9. Building the Second Module: SMA refactor: rename applied price parameter to 'Apply to'
feat: add SMA period input parameter
feat: outline CSma sub-indicator
feat: integrate CSma into CIndicator
feat: reconfigure plots and buffers
refactor: rename barIdxToRecalcFrom to recalcStartIdx
refactor: replace 0 with drawBegin in CAppliedPrice
10. Calculating the Simple Moving Average feat: implement CSma::onCalculate
11. DRYing Sub-Indicators: Pulling Logic Up refactor: introduce clearBuffersAt hook for subclasses
refactor: pull up prepareBeforeCalculating to base class

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 fix: ensure recurrence seed is calculated when recalculation touches drawBegin
refactor: rename prepareBeforeCalculating to prepareCalculation
refactor: add prepareCalculation overload with isFullCalculation flag
2. Extracting CSmoothingBase refactor: introduce CSmoothingBase for smoothing sub-indicators
3. Selecting the Smoothing Method at Runtime feat: add smoothing method input parameter
feat: switch CIndicator to dynamic CSmoothingBase pointer
4. Exponential Moving Average feat: implement CEma smoothing sub-indicator
5. Setting the Plot Label Dynamically refactor: set plot label dynamically based on smoothing method
6. Smoothed Moving Average feat: implement CSmma smoothing sub-indicator
7. Linear Weighted Moving Average feat: implement CLwma smoothing sub-indicator
8. Optimizing LWMA perf: calculate LWMA using sliding window sums

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, before Section 1, and style: align pointer and reference symbols with type, between Sections 3 and 4.