Code migration guide
MQL5 to MQL4 migration guide
MQL5 indicator handles and its orders, deals, and account-dependent position model require different APIs in MQL4. MQL4 also supports classes; inspect each library dependency instead of assuming object-oriented code must be removed.
Generate an MQL4 draft, then compile it in MetaEditor and test it in MetaTrader 4.
Conversion map
What changes between MetaTrader 5 and MetaTrader 4
The migration is a model translation. Use this map to decide where the source behavior belongs before writing target syntax.
| Concern | Source model | Target model | Migration decision |
|---|---|---|---|
| Lifecycle | Programs respond to typed events such as OnInit, OnTick, OnTradeTransaction, and OnCalculate. | Expert Advisors and indicators respond to predefined callbacks such as OnInit, OnTick, and OnCalculate. | Choose when each MetaTrader 5 calculation should run in MetaTrader 4; matching syntax alone does not preserve timing. |
| Market data and history | Timeseries and indicator values are commonly copied into arrays; built-in indicators return handles read with CopyBuffer. | Price series use terminal arrays and shift-based access; indicators commonly expose calculated values through buffers. | Recheck bar indexing, warm-up requirements, timeframe requests, and behavior on an unfinished bar. |
| Indicators and chart output | Indicator handles, buffers, plots, and OnCalculate separate setup from repeated calculation. | Custom indicators bind buffers, while built-in indicator functions can return values directly for a requested shift. | Map calculations before recreating plots, labels, colors, and platform-specific drawing objects. |
| Orders and positions | Trade requests and CTrade operate with orders, deals, and account-dependent position models. | Classic trade functions such as OrderSend work with order tickets, lots, prices, stops, and a shared order pool. | Define fill timing, sizing, pyramiding or hedging, stops, and position ownership for the target platform. |
| Native validation | Compile the .mq5 file in MetaEditor 5, load it in MetaTrader 5, and use Strategy Tester for the intended artifact. | Compile the .mq4 file in MetaEditor, attach it in MetaTrader 4, and test the correct EA or indicator artifact. | Treat generated MQL4 as a draft until it passes the target editor, chart, and backtest workflow. |
Code example
Moving-average cross: MQL5 to MQL4
The MQL5 fixed arrays store the older copied bar first and the current bar second; valid SMA handles and CopyBuffer return values of 2 are prerequisites. MQL4 reads current and previous values directly through iMA shifts 0 and 1. Both fragments include the unfinished bar, so choose a completed-bar rule explicitly if required.
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];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;Workflow
A four-step migration path
Keep generation, target-platform validation, and behavior comparison as separate stages.
- 1
Inventory the source behavior
Separate calculations, plots, alerts, orders, external dependencies, and assumptions in an .mq5 Expert Advisor or custom indicator.
- 2
Choose the target artifact
Decide whether the result is an .mq4 Expert Advisor or custom indicator. This choice controls lifecycle methods and available APIs.
- 3
Rewrite through target APIs
Translate state, series, indicators, drawings, and orders into MetaTrader 4 concepts instead of replacing function names.
- 4
Validate behavior natively
Compile the .mq4 file in MetaEditor, attach it in MetaTrader 4, and test the correct EA or indicator artifact.
Manual decisions
Decisions code replacement cannot make
- Replace MQL5 indicator handles and CopyBuffer access with MQL4 calls or custom buffers.
- Check each library and API for MQL4 support, retaining compatible class structure and explicit resource cleanup.
- Define how MT5 netting or hedging positions map to independent MT4 order tickets.
Limitations
What the draft cannot prove
- MetaTrader 5 and MetaTrader 4 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 4.
- Pineify can generate and revise a MQL4 draft, but the target platform remains the authority for compilation, loading, backtesting, and runtime behavior.
Use cases
When this conversion is useful
- Backport an MT5 Expert Advisor for an MT4-only broker environment.
- Rebuild an MQL5 indicator with MQL4-compatible buffers and calls.
- Identify MT5-specific APIs before planning an MT4 implementation.
FAQ
MQL5 to MQL4 questions
Official references
- MQL5 event handling
MetaQuotes
- MQL5 indicator handles and data access
MetaQuotes
- MQL4 event handling functions
MetaQuotes
- MQL4 OrderSend reference
MetaQuotes
Related converters
Browse all convertersBuild the MQL4 draft with its native model in view
Bring the complete source and your artifact, timing, data, and order requirements. Validate the result in MetaTrader 4.