Building a Simple Moving Average Indicator in MQL5
Summary
This beginner guide explains how a custom MQL5 indicator is structured by walking through a simple moving average. It describes the average as the sum of recent closing prices divided by the chosen period, and shows how the period and chart shift are exposed as user inputs. The example uses an indicator buffer to store plotted values and a calculation loop to fill the buffer for each bar.
The article also explains how MetaTrader interacts with indicator code. Global declarations and properties define shared settings and data, OnInit runs during setup, and OnCalculate performs the recurring calculations as market data arrives. It outlines the roles of comments, input variables, buffers, and optional cleanup through OnDeinit. The example is instructional rather than a trading strategy: it focuses on code structure and platform lifecycle, and does not evaluate the moving average as a signal or provide performance evidence. Readers are directed to platform documentation for details beyond this introductory example.
Key ideas
- A simple moving average is calculated by averaging closing prices over a user-selected period.
- An indicator buffer stores the calculated values that MetaTrader plots on the chart.
- OnInit handles setup, while OnCalculate updates indicator values as data arrives.
- The previous calculation count can be used to limit recalculation to new or updated bars.
- The example teaches MQL5 indicator structure and does not assess trading performance.
Tags
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.