Source code
OnTick vs OnTimer in MQL5: structure a responsive EA
An EA is an event-driven program. A clear division between setup, market decisions and maintenance helps both a human developer and an AI coding assistant make smaller, safer changes.
Give each handler a clear responsibility
Use OnInit for setup and input validation, and OnDeinit for cleanup. OnTick receives new-quote events for the chart symbol. OnTimer handles timer events after a timer is subscribed. These entry points are a useful architectural map when you open an unfamiliar source project.
Keep the event queue responsive
MQL5 processes events sequentially for a program. If a NewTick event is already queued or being handled, another NewTick is not added. Long blocking work inside a handler therefore deserves attention. A loop that waits indefinitely for a condition is not a substitute for an event-aware design.
Use periodic work intentionally
A timer can suit a status panel or periodic housekeeping. A shorter timer period can also increase tester work, so choose a frequency that serves a visible purpose. A timer is not a guarantee that a fresh market quote exists, and it does not automatically improve execution quality.
Sketch the flow before editing
- List what happens once at startup.
- List what requires every relevant quote.
- List what only needs a periodic refresh.
- Define how the program reacts when data is unavailable.
- Test shutdown and reinitialization as well as normal operation.
Make it yours with MQLume
Bring a clear architecture to your next custom build. MQLume gives you editable projects that you can read, compile and refine, including Quantum Titan MT5.