Code migration guide
MQL4 to cTrader migration guide
An MQL4 EA or indicator can be ported to cTrader C# by replacing callbacks, MT4 trade functions, lots, and buffers with Robot or Indicator classes, typed Bars, volume units, and cTrader trade results.
Generate a cTrader C# draft, then build it in cTrader Algo and test it with the intended symbol and account settings.
Conversion map
What changes between MetaTrader 4 and cTrader
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 | Expert Advisors and indicators respond to predefined callbacks such as OnInit, OnTick, and OnCalculate. | Robots use lifecycle methods including OnStart, OnTick, OnBar, OnStop, and OnException; indicators calculate by index. | Choose when each MetaTrader 4 calculation should run in cTrader; matching syntax alone does not preserve timing. |
| Market data and history | Price series use terminal arrays and shift-based access; indicators commonly expose calculated values through buffers. | Bars and DataSeries expose typed C# collections, while multi-timeframe data is requested explicitly. | Recheck bar indexing, warm-up requirements, timeframe requests, and behavior on an unfinished bar. |
| Indicators and chart output | Custom indicators bind buffers, while built-in indicator functions can return values directly for a requested shift. | Indicator classes expose output series and use the cTrader Algo indicators API. | Map calculations before recreating plots, labels, colors, and platform-specific drawing objects. |
| Orders and positions | Classic trade functions such as OrderSend work with order tickets, lots, prices, stops, and a shared order pool. | Robot methods submit and manage trades through Symbol, Positions, PendingOrders, and volume measured in units. | Define fill timing, sizing, pyramiding or hedging, stops, and position ownership for the target platform. |
| Native validation | Compile the .mq4 file in MetaEditor, attach it in MetaTrader 4, and test the correct EA or indicator artifact. | 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. | Treat generated cTrader C# as a draft until it passes the target editor, chart, and backtest workflow. |
Code example
Moving-average cross: MQL4 to cTrader C#
The MQL4 fragment includes the current bar at shift 0. The cTrader fragment checks Last(2) and Last(1), which are completed bars when called from OnBar. Align the confirmation policy before comparing signals, and initialize the moving averages in the correct cBot or indicator lifecycle.
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;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);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 .mq4 Expert Advisor or custom indicator.
- 2
Choose the target artifact
Decide whether the result is a C# cBot or custom indicator project. This choice controls lifecycle methods and available APIs.
- 3
Rewrite through target APIs
Translate state, series, indicators, drawings, and orders into cTrader concepts instead of replacing function names.
- 4
Validate behavior natively
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.
Manual decisions
Decisions code replacement cannot make
- Choose cBot or custom indicator before building the C# class and access rights.
- Map OnTick and new-bar detection to cTrader OnTick or OnBar with the same timing intent.
- Convert MT4 lots, ticket ownership, and magic numbers into cTrader units, labels, and Positions filters.
Limitations
What the draft cannot prove
- MetaTrader 4 and cTrader 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 cTrader.
- Pineify can generate and revise a cTrader C# 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 EA into a cTrader cBot.
- Move an MQL4 indicator into a typed cTrader indicator.
- Adopt C# structure while preserving the strategy rules that are actually portable.
FAQ
MQL4 to cTrader C# questions
Official references
- MQL4 event handling functions
MetaQuotes
- MQL4 OrderSend reference
MetaQuotes
- cBot lifecycle
Spotware
- Introduction to cTrader cBots
Spotware
Related converters
Browse all convertersBuild the cTrader C# draft with its native model in view
Bring the complete source and your artifact, timing, data, and order requirements. Validate the result in cTrader.