Skip to content
All library documents

Logging and Assertions for Diagnosing MQL4 Expert Advisor Errors

Article MQL5 articles

Summary

This article offers practical methods for finding runtime and logic errors in MQL4 Expert Advisors. It distinguishes errors that can often be prevented during development, such as invalid stop distances, from conditions that must be handled at runtime, such as a busy trade context. A logging helper records the function, a descriptive message, and an error code with its platform description. The examples show capturing an error before it is cleared and selectively ignoring an expected no-change result from order modification.

For logic bugs, the article recommends recording useful state information and adding assertions for conditions that should always hold, such as a long trade's stop being below the current bid. Prefixes for information, errors, and failed assertions make messages easier to locate and filter in terminal logs. It also suggests reviewing tester logs over long periods, then periodically examining live account logs to catch environment-specific issues.

These techniques improve diagnosis and can expose problems before deployment, but they do not automatically correct faults. The examples are specific to MQL4 and require developers to choose relevant conditions and handle each error appropriately.

Key ideas

  • Log the calling function, context, and error description when a built-in trading function fails.
  • Capture error codes before another call clears them, and suppress only known acceptable outcomes.
  • Use assertions to flag conditions that should hold whenever a function runs.
  • Distinct log prefixes make diagnostic messages easier to search and filter.
  • Review tester and live operation logs to detect errors that arise in different environments.

Tags

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