Expert Advisor Programming: Design the Behavior Before the Code

Expert advisor programming turns a trading specification into event-driven software for MetaTrader. The work includes data handling, state, trade requests and recovery from failures as well as entry rules. Pineify can draft editable source, but the specification and behavior still need review.

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.

Choose the platform and trading model

MQL4 targets MT4; MQL5 targets MT5. Both use C-like syntax, but trading and indicator APIs differ. MQL5 distinguishes orders, deals and positions, and behavior must match the account netting or hedging mode. Porting requires an explicit mapping of those operations and a new test record.

  • 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.

Separate a signal from a successful trade

A signal is a decision made from data. A trade request is an instruction submitted to the server. A fill changes account state. Review these stages separately, including what happens when the requested price changes, volume is invalid or funds are insufficient. An accepted request is not always a completed fill.

  • 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.

Use generation and testing as separate review steps

Pineify can draft or revise MQ5 source from a written specification and provide syntax diagnostics. Download the source, compile it in MetaEditor, and inspect individual actions in the MT5 tester. Keep the source version with the input set and report. Generated comments do not independently prove that the code implements their description.

  • 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. Diagnose changes in a tester or demo environment before considering live use. Automated trading can result in substantial loss.

Frequently Asked Questions