Skip to content
All library documents

Sharing Expert Advisor Code Across MQL4 and MQL5

Article MQL5 articles

Summary

The document presents a software design approach for maintaining expert advisors that target both MetaTrader 4 and MetaTrader 5. Because each platform compiles its own source format, the article proposes keeping thin platform-specific source files while sharing common implementation in header files. It then develops the idea further: place shared behavior in base classes and use inherited, platform-specific classes for features or conventions that differ.

A simple greeting example illustrates shared headers, while discussion of conditional compilation and split implementations addresses language differences. The motivation is to reduce duplicated code and the burden of synchronizing fixes across separate versions. The method is an architectural tutorial rather than evidence from a production trading system, and it does not show improved strategy returns or execution. It also describes real limits: some MetaTrader 5 features may lack a direct MetaTrader 4 counterpart, and trade operations differ enough that developers may need separate implementations or a chosen common convention.

Key ideas

  • Separate platform source files can include a shared header containing code common to both MQL versions.
  • Base classes can hold shared behavior while derived classes handle platform-specific differences.
  • Conditional compilation and separate implementations help manage incompatible language features or function signatures.
  • Code reuse can reduce duplicated maintenance, but does not eliminate differences in platform capabilities or trading operations.
  • The article teaches software structure rather than a trading strategy or tested performance result.

Tags

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