Nested if-else logic inside OnTick() creates implicit states that are hard to isolate, debug, and extend without regressions. A formal finite state machine in MQL5 uses an IState interface, a CStrategyContext mediator, and four concrete states to separate detection from behavior. A three-file include structure resolves circular dependencies and keeps declarations, definitions, and instantiation clean, making changes safer and debugging faster.
A typical Expert Advisor begins with a manageable OnTick() function. There is a condition to check whether a position is open, a condition to evaluate the entry signal, a condition to verify the spread is acceptable, and a condition to check whether the session is active. Four conditions, clearly readable, easily debugged.
After six months of feature additions, the same function grows to forty-three nested conditionals across 230 lines. To add a trailing stop, a new developer must first identify the exact condition set that indicates an open, profitable position before the first take-profit level. This requires reading the entire function, mapping the execution paths, and choosing an insertion point that does not break existing behavior. The probability of introducing a regression is not low.
The root problem is that the EA has implicit states — idle, looking for entry, in a trade, managing exit — but encodes them as scattered boolean flag combinations rather than explicit structural entities. With implicit state, state-handling code is interleaved with boundary-detection code. As a result, the function must answer two questions at each evaluation point: the current state and the action to perform. These are two distinct responsibilities, and combining them is what produces the fragility.
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
Check out the new article: Designing a Strategy State Machine in MQL5: Replacing Nested If-Else Logic with Formal States.
Nested if-else logic inside OnTick() creates implicit states that are hard to isolate, debug, and extend without regressions. A formal finite state machine in MQL5 uses an IState interface, a CStrategyContext mediator, and four concrete states to separate detection from behavior. A three-file include structure resolves circular dependencies and keeps declarations, definitions, and instantiation clean, making changes safer and debugging faster.
A typical Expert Advisor begins with a manageable OnTick() function. There is a condition to check whether a position is open, a condition to evaluate the entry signal, a condition to verify the spread is acceptable, and a condition to check whether the session is active. Four conditions, clearly readable, easily debugged.
After six months of feature additions, the same function grows to forty-three nested conditionals across 230 lines. To add a trailing stop, a new developer must first identify the exact condition set that indicates an open, profitable position before the first take-profit level. This requires reading the entire function, mapping the execution paths, and choosing an insertion point that does not break existing behavior. The probability of introducing a regression is not low.
The root problem is that the EA has implicit states — idle, looking for entry, in a trade, managing exit — but encodes them as scattered boolean flag combinations rather than explicit structural entities. With implicit state, state-handling code is interleaved with boundary-detection code. As a result, the function must answer two questions at each evaluation point: the current state and the action to perform. These are two distinct responsibilities, and combining them is what produces the fragility.
Author: Ushana Kevin Iorkumbul