Expert Advisor Programming: Map MQL4 and MQL5 Before You Code

Expert advisor programming turns a specification into event-driven MQL4 for MT4 or MQL5 for MT5. The languages share a C-like look, but MQL5 separates orders, deals, and positions, and renaming an EX4 file does not make it an EX5. You also have to plan state after a restart, the trade request, and what the program does when that request fails.

Key Takeaways

  • Choose MT4/MQL4 or MT5/MQL5 based on the target account and platform. Their executables and APIs are not interchangeable.
  • Treat events, signals, orders, fills and positions as distinct stages.
  • Use broker symbol properties and account mode rather than universal lot, pip or position assumptions.
  • Compile the source, test expected behavior and then assess historical performance on documented settings.

Design the event lifecycle and persistent state

Initialization sets up inputs, resources and state. Tick, timer or other handlers process events; cleanup releases resources when the program is deinitialized. In MQL5, input or chart changes can trigger deinitialization and reinitialization. Plan how the EA discovers existing positions after a restart rather than assuming every initialization starts with an empty account.

  • A pending or processing NewTick event prevents another NewTick from being queued. Keep handlers bounded and choose explicitly between quote-based and completed-bar rules.
  • Define which account positions and orders belong to this EA. A strategy identifier can help distinguish them, but ownership checks must be applied consistently.
  • Keep temporary signal state separate from broker-confirmed positions and from settings needed after restart.

Pick MQL4 or MQL5 before any code is shared

MQL4 is the language for an MT4 expert advisor. MQL5 is the language for an MT5 expert advisor. Both use C-like syntax, which is why a pasted port fails: the trading and indicator APIs differ. MQL5 keeps orders, deals, and positions as separate objects, and the code must follow netting or hedging on that account. Moving the program means mapping each operation and opening a new test record. Changing the file extension does not perform that mapping.

  • MQL4 exposes Digits and Point for symbol precision. It is incorrect to claim that MT4 cannot support five-digit forex pricing.
  • MT5 supports multi-symbol testing and optimization. Choose test inputs and available data deliberately rather than assuming every tester mode models the same path.
  • Preserve platform-specific source and dependencies. Renaming an extension cannot translate code or executable behavior.

Program the MQL5 order, deal, and position apart

A signal is only a decision from data. In MQL5 the following steps are different objects: the order you submit, the deal the server records, and the position the account holds. MQL4 does not split a trade into those three objects, so a port has to map each MQL4 order action onto them. Review a changed price, invalid volume, and insufficient funds as failed requests. An accepted request is not always a completed deal.

  • Use symbol properties for volume steps, price increments, stop restrictions and supported filling modes.
  • Check results and record failures with enough context to reproduce them. Define retry limits and duplicate protection.
  • For multi-position or multi-symbol systems, calculate aggregate exposure as well as a nominal limit on each new order.

Make the risk arithmetic explicit

Position sizing should state its capital base, assumed exit loss, conversion to account currency and broker volume constraints. A planned percentage risk is conditional on execution near the assumed exit; gaps and costs can exceed it. No fixed percentage prevents all damaging drawdowns.

  • For a hypothetical account that loses exactly 5% of remaining equity on each of four trades, equity becomes 0.95^4 = 81.45% of the starting value: an 18.55% loss before any additional costs. This is arithmetic, not an observed trading record.
  • If sizing is based on a fixed initial capital amount instead, four 5%-of-initial losses total 20%. State which convention applies.
  • Test zero or invalid stop distances, insufficient margin, minimum lot constraints and overlapping positions before using a sizing rule.

Treat a Pineify MQ5 draft as MQL5, not as MQL4

Pineify can draft or revise MQ5 source from the written specification and show syntax diagnostics. That file is MQL5 for MT5, not an MQL4 expert advisor for MT4. Download it, compile it in MetaEditor, and inspect entry, exit, restart, and rejection cases in the MT5 tester. Keep the source version with the input set. Generated comments do not prove the handlers follow the order, deal, and position split you specified.

  • Use deterministic cases for entry, no entry, exit, restart and rejection handling. Then run broader historical evaluation.
  • MT5 can model costs and execution delays depending on settings. Record them; do not label all backtests perfect-fill simulations.
  • Reserve later data for a frozen choice. Optimization and resampling describe selected assumptions and do not certify future returns.
  • A separate Pine Script implementation requires its own review. One strategy description does not automatically produce equivalent platform behavior.

This page is educational and does not provide investment advice. Check MQL4 and MQL5 behavior in a tester or on demo before live use. Automated trading can result in substantial loss.

Frequently Asked Questions