Code migration guide

MQL4 to MQL5 converter

An MQL4 EA or indicator needs an API migration for MQL5, not just syntax edits: direct indicator calls become handles and CopyBuffer reads, classic order-pool logic becomes orders, deals, and positions, and account mode affects trade behavior.

Generate an MQL5 draft, then compile it in MetaEditor 5 and test the target account mode in MetaTrader 5.

Conversion map

What changes between MetaTrader 4 and MetaTrader 5

The migration is a model translation. Use this map to decide where the source behavior belongs before writing target syntax.

Source and target platform conversion map
ConcernSource modelTarget modelMigration decision
LifecycleExpert Advisors and indicators respond to predefined callbacks such as OnInit, OnTick, and OnCalculate.Programs respond to typed events such as OnInit, OnTick, OnTradeTransaction, and OnCalculate.Choose when each MetaTrader 4 calculation should run in MetaTrader 5; matching syntax alone does not preserve timing.
Market data and historyPrice series use terminal arrays and shift-based access; indicators commonly expose calculated values through buffers.Timeseries and indicator values are commonly copied into arrays; built-in indicators return handles read with CopyBuffer.Recheck bar indexing, warm-up requirements, timeframe requests, and behavior on an unfinished bar.
Indicators and chart outputCustom indicators bind buffers, while built-in indicator functions can return values directly for a requested shift.Indicator handles, buffers, plots, and OnCalculate separate setup from repeated calculation.Map calculations before recreating plots, labels, colors, and platform-specific drawing objects.
Orders and positionsClassic trade functions such as OrderSend work with order tickets, lots, prices, stops, and a shared order pool.Trade requests and CTrade operate with orders, deals, and account-dependent position models.Define fill timing, sizing, pyramiding or hedging, stops, and position ownership for the target platform.
Native validationCompile the .mq4 file in MetaEditor, attach it in MetaTrader 4, and test the correct EA or indicator artifact.Compile the .mq5 file in MetaEditor 5, load it in MetaTrader 5, and use Strategy Tester for the intended artifact.Treat generated MQL5 as a draft until it passes the target editor, chart, and backtest workflow.

Code example

Moving-average cross: MQL4 to MQL5

These fragments show where the same signal starts in each API. They are not complete standalone programs; lifecycle setup, inputs, plots, and order handling still depend on the selected artifact.

MQL4 source fragment
double fast0 = iMA(NULL, 0, 10, 0, MODE_SMA, PRICE_CLOSE, 0);
double fast1 = iMA(NULL, 0, 10, 0, MODE_SMA, PRICE_CLOSE, 1);
double slow0 = iMA(NULL, 0, 20, 0, MODE_SMA, PRICE_CLOSE, 0);
double slow1 = iMA(NULL, 0, 20, 0, MODE_SMA, PRICE_CLOSE, 1);
bool longSignal = fast1 <= slow1 && fast0 > slow0;
MQL5 target fragment
double fast[2], slow[2];
CopyBuffer(fastHandle, 0, 0, 2, fast);
CopyBuffer(slowHandle, 0, 0, 2, slow);
bool longSignal = fast[0] <= slow[0] && fast[1] > slow[1];

Workflow

A four-step migration path

Keep generation, target-platform validation, and behavior comparison as separate stages.

  1. 1

    Inventory the source behavior

    Separate calculations, plots, alerts, orders, external dependencies, and assumptions in an .mq4 Expert Advisor or custom indicator.

  2. 2

    Choose the target artifact

    Decide whether the result is an .mq5 Expert Advisor or custom indicator. This choice controls lifecycle methods and available APIs.

  3. 3

    Rewrite through target APIs

    Translate state, series, indicators, drawings, and orders into MetaTrader 5 concepts instead of replacing function names.

  4. 4

    Validate behavior natively

    Compile the .mq5 file in MetaEditor 5, load it in MetaTrader 5, and use Strategy Tester for the intended artifact.

Manual decisions

Decisions code replacement cannot make

  • Choose the MQL5 netting or hedging assumptions that replace the MQL4 ticket workflow.
  • Move built-in indicator setup into handles with explicit buffer copying and cleanup.
  • Replace OrderSend and order-pool loops with CTrade or explicit MqlTradeRequest logic.

Limitations

What the draft cannot prove

  • MetaTrader 4 and MetaTrader 5 do not share one execution, data, or order model, so behavior can change even when both versions compile.
  • Broker, symbol, session, timezone, history, and fill settings can change signals and backtest results on MetaTrader 5.
  • Pineify can generate and revise a MQL5 draft, but the target platform remains the authority for compilation, loading, backtesting, and runtime behavior.

Use cases

When this conversion is useful

  • Port an MT4 Expert Advisor to an MT5 Expert Advisor.
  • Migrate an MQL4 custom indicator to MQL5 handles and buffers.
  • Modernize shared trading logic while keeping platform-specific execution separate.

FAQ

MQL4 to MQL5 questions

Build the MQL5 draft with its native model in view

Bring the complete source and your artifact, timing, data, and order requirements. Validate the result in MetaTrader 5.