Code migration guide

MQL5 to cTrader converter

An MQL5 EA can be rebuilt as a cTrader cBot by mapping MQL events to Robot lifecycle methods, indicator handles to cTrader indicator objects, and MT5 position sizing to cTrader symbol volume units.

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 5 and cTrader

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
LifecyclePrograms respond to typed events such as OnInit, OnTick, OnTradeTransaction, and OnCalculate.Robots use lifecycle methods including OnStart, OnTick, OnBar, OnStop, and OnException; indicators calculate by index.Choose when each MetaTrader 5 calculation should run in cTrader; matching syntax alone does not preserve timing.
Market data and historyTimeseries and indicator values are commonly copied into arrays; built-in indicators return handles read with CopyBuffer.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 outputIndicator handles, buffers, plots, and OnCalculate separate setup from repeated calculation.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 positionsTrade requests and CTrade operate with orders, deals, and account-dependent position models.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 validationCompile the .mq5 file in MetaEditor 5, load it in MetaTrader 5, and use Strategy Tester for the intended artifact.Build the C# project in cTrader Algo, resolve build output, attach it to the intended symbol, and run a cTrader backtest.Treat generated cTrader C# as a draft until it passes the target editor, chart, and backtest workflow.

Code example

Moving-average cross: MQL5 to cTrader C#

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.

MQL5 source 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];
cTrader C# target fragment
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. 1

    Inventory the source behavior

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

  2. 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. 3

    Rewrite through target APIs

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

  4. 4

    Validate behavior natively

    Build the C# project in cTrader Algo, resolve build output, attach it to the intended symbol, and run a cTrader backtest.

Manual decisions

Decisions code replacement cannot make

  • Map OnInit, OnTick, timers, and trade events to cBot lifecycle methods without merging distinct responsibilities.
  • Convert lots and account risk rules to cTrader units using Symbol normalization and limits.
  • Rebuild MT5 netting or hedging assumptions around cTrader Positions, labels, and trade results.

Limitations

What the draft cannot prove

  • MetaTrader 5 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 MT5 Expert Advisor to a cTrader cBot.
  • Move an MQL5 indicator into a typed cTrader C# indicator.
  • Retain strategy rules while adopting cTrader-native execution and backtesting.

FAQ

MQL5 to cTrader C# questions

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