Skip to content
All library documents

Building and Backtesting an EMA Crossover EA with Simulink

Article MQL5 articles

Summary

This tutorial shows how to use MATLAB Simulink and Stateflow to model a simple automated trading system, test it on MetaTrader 5 historical prices, and translate its logic into an MQL5 Expert Advisor. It explains the visual modeling tools, discrete solver setup, and the role of states, transitions, events, and actions in representing trading rules.

The example trades EURUSD on an hourly chart using 21- and 55-period exponential moving averages. A crossover sets the direction, entry waits a specified number of bars, and a single position remains open until its stop loss or take profit is reached. The author describes loading price and spread data from MetaTrader 5 and using MATLAB arrays in the model. The article presents the workflow as a way to make system logic visible and reduce handwritten code, but calls the strategy primitive and acknowledges that the simulation simplifies real execution. It provides no performance statistics or evidence that the rules are profitable.

Key ideas

  • Simulink block diagrams and Stateflow charts can represent an automated trading system's logic visually.
  • The example enters long or short after a 21/55 EMA crossover, with entry delayed by a specified number of bars.
  • The model uses MetaTrader 5 historical data and MATLAB to prepare prices and moving averages for backtesting.
  • The article simplifies trading execution and offers no evidence of strategy profitability.

Tags

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.