Discussing the article: "Building Your Personal Expert Advisor (Part 7): Risk Management VI (Final)—Consolidating the Risk Framework"

Check out the new article: Building Your Personal Expert Advisor (Part 7): Risk Management VI (Final)—Consolidating the Risk Framework.

The article consolidates trade, basket, and account risk into a modular framework for MQL5 EAs. Risk logic moves into reusable classes and shared types, leaving the EA with its inputs, signal, configuration, and call sequence. The result is a shorter strategy file and a second EA that reuses the basket tier and the shared conversions.

Over the course of this series, risk checks and safety logic have accumulated until the moving average crossover EA has become a thousand-line monolith in which the actual trading idea is difficult to find and even harder to reuse. The problem is practical: understanding the signal requires navigating dozens of unrelated functions, reusing the same risk controls in another Expert Advisor means copying and adapting large blocks of code, and validation and configuration are scattered across initialization logic rather than being managed by the subsystems they affect.

The five risk-management parts of the series introduced position sizing, free-margin checks, order-type selection and distance validation, basket loss caps, and account-level screening before new trades. These mechanisms were progressively built into one EA file, FixedMACrossover_Part6.mq5, apart from the account-level class, which has had a header of its own since it was introduced. That EA file now exceeds a thousand lines, while only about one hundred lines implement the moving average crossover strategy itself.

The goal of this article is not to introduce new risk controls or to change the crossover strategy. It is to move the risk code out of the strategy file, so that the strategy can be read and a second EA can include that code instead of copying it. We will arrange the risk-management machinery in four header files: one for shared types and conversions, and one for each of three levels of risk management: trade, basket, and account. Three of the files are new, and the account-level file already exists from the previous part. The trade and basket levels each carry their own configuration structure, validation, and defaults. The account level keeps the setter interface it was published with: no configuration structure, no validation, and every limit switched off until the EA sets it. An EA can include only the levels it uses.


Author: Solomon Anietie Sunday