Code migration guide
cTrader to MQL4 migration guide
A C# cBot or cTrader indicator can be rewritten for MQL4, but typed Robot classes, units, labels, and trade results must become MT4 callbacks, lots, magic numbers, and classic ticket-based order handling.
Generate an MQL4 draft, then compile it in MetaEditor and test it in MetaTrader 4.
Conversion map
What changes between cTrader 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 | Robots use lifecycle methods including OnStart, OnTick, OnBar, OnStop, and OnException; indicators calculate by index. | Expert Advisors and indicators respond to predefined callbacks such as OnInit, OnTick, and OnCalculate. | Choose when each cTrader calculation should run in MetaTrader 4; matching syntax alone does not preserve timing. |
| Market data and history | Bars and DataSeries expose typed C# collections, while multi-timeframe data is requested explicitly. | 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 classes expose output series and use the cTrader Algo indicators API. | 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 | Robot methods submit and manage trades through Symbol, Positions, PendingOrders, and volume measured in units. | 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 | Build the C# project in cTrader Algo and resolve build errors. Check an indicator on the intended chart; for a cBot, run a backtest with explicit symbol, timeframe, spread, and volume settings before demo testing. | 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: cTrader C# to MQL4
The cTrader fragment checks Last(2) and Last(1); when called from OnBar, these are completed bars. The MQL4 fragment includes shift 0, the current bar, so the two snippets do not yet share a confirmation policy. Place each fragment in the correct lifecycle, align bar-close timing, and normalize volume units to broker lot steps before testing.
var fast = Indicators.SimpleMovingAverage(Bars.ClosePrices, 10);
var slow = Indicators.SimpleMovingAverage(Bars.ClosePrices, 20);
bool longSignal = fast.Result.Last(2) <= slow.Result.Last(2)
&& fast.Result.Last(1) > slow.Result.Last(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 a C# cBot or custom indicator project.
- 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
- Flatten C# project structure and dependencies into MQL4-compatible files and includes.
- Convert cTrader volume units and Symbol rules to MT4 lot steps and broker limits.
- Replace position labels and events with magic-number filters and explicit ticket loops.
Limitations
What the draft cannot prove
- cTrader 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
- Port a cTrader cBot to an MT4 Expert Advisor.
- Rebuild a cTrader custom indicator for MetaTrader 4.
- Move core signals to MT4 while replacing unsupported C# dependencies.
FAQ
cTrader C# to MQL4 questions
Official references
- cBot lifecycle
Spotware
- Introduction to cTrader cBots
Spotware
- 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.