Avoiding Common MQL4 Compile-Time and Runtime Errors
Summary
This programming guide explains how to modernize older MQL4 programs for the newer compiler and reduce failures during execution. It recommends strict compilation and covers reserved identifiers, invalid name characters, integer-only switch expressions, missing or mismatched function return values, and passing arrays by reference. It also notes that built-in price series are constant and may need copying before being passed to a function that expects a mutable array reference.
For runtime safety, the article shows how to reason about indicator-buffer bounds, adjust loop limits when using historical bar offsets, guard divisions against zero, and use the correct null value for the current symbol. It also discusses Unicode strings and DLL interfaces, explicit file-sharing flags, and date-time conversion. Compiler warnings such as shadowed variables, implicit conversions, and unused variables are presented as issues worth correcting. The examples are practical migration guidance rather than a trading method, and the article does not compare compiler versions or provide measured reliability results.
Key ideas
- Strict compilation helps expose errors and potential problems in legacy MQL4 code.
- Switch expressions require integer types, and non-void functions must return values of their declared type.
- Array arguments are passed by reference, while constant price series may need to be copied before use.
- Loop bounds should account for bar offsets and available buffer sizes to prevent out-of-range access.
- Check external or computed divisors before division, and handle string, symbol, and file-interface changes explicitly.
Tags
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.