Code migration guide

MQL5 to NinjaScript migration guide for CopyBuffer and OnBarUpdate

An MQL5 Expert Advisor or indicator can move to NinjaScript after its event handlers, indicator handles, and position logic have a NinjaTrader home. OnInit setup goes into OnStateChange, CopyBuffer reads become bars-ago Series in OnBarUpdate, and CTrade or trade requests become EnterLong, ExitLong, or unmanaged handling.

Generate a NinjaScript draft, then compile it in NinjaScript Editor and test it in NinjaTrader 8.

Conversion map

What changes between MetaTrader 5 and NinjaTrader 8

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.OnStateChange configures lifecycle states and OnBarUpdate handles bar-driven calculations at the selected Calculate frequency.Choose when each MetaTrader 5 calculation should run in NinjaTrader 8; 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.Series use bars-ago indexing such as Close[0]; multi-series scripts must route work with BarsInProgress.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 configure plots and update their Series values inside the NinjaScript lifecycle.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.Strategies can use managed methods such as EnterLong and ExitLong or opt into lower-level unmanaged handling.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.Compile in NinjaScript Editor, load the Indicator or Strategy, and test strategies with Strategy Analyzer before runtime use.Treat generated NinjaScript as a draft until it passes the target editor, chart, and backtest workflow.

Code example

Moving-average cross: MQL5 to NinjaScript

The MQL5 fragment requires valid handles and both CopyBuffer calls to return 2 before reading the fixed arrays. Its index 1 contains the current bar. NinjaScript CrossAbove follows Calculate and BarsInProgress settings. Match the same bar and series policy; the fragments shown do not submit orders.

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];
NinjaScript target fragment
protected override void OnBarUpdate()
{
    if (CurrentBar < 20) return;
    bool longSignal = CrossAbove(SMA(10), SMA(20), 1);
}

Workflow

A four-step migration path

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

  1. 1

    Inventory the source behavior

    List OnInit, OnTick, OnTradeTransaction, and OnCalculate work in the .mq5 Expert Advisor or custom indicator, including indicator handles, CopyBuffer reads, and CTrade or trade-request orders, deals, and positions.

  2. 2

    Choose the target artifact

    Choose a NinjaScript Indicator or Strategy. Move one-time handle and resource setup into the correct OnStateChange states before any OnBarUpdate calculation.

  3. 3

    Rewrite through target APIs

    Align CopyBuffer array order with bars-ago indexing such as Close[0] and the selected Calculate frequency. Route extra series with BarsInProgress, and choose EnterLong and ExitLong or unmanaged handling for the MT5 trade workflow.

  4. 4

    Validate behavior natively

    Compile in NinjaScript Editor, load the Indicator or Strategy, and test strategies with Strategy Analyzer before runtime use.

Manual decisions

Decisions code replacement cannot make

  • Move one-time handle and resource setup into the correct OnStateChange states.
  • Align CopyBuffer array order with NinjaScript bars-ago indexing and Calculate frequency.
  • Choose managed or unmanaged NinjaScript orders based on the complexity of the MT5 trade workflow.

Limitations

What the draft cannot prove

  • MQL5 programs respond to OnInit, OnTick, OnTradeTransaction, and OnCalculate. NinjaScript configures lifecycle in OnStateChange and calculates in OnBarUpdate at the selected Calculate frequency, so handle setup and bar policy are part of the port.
  • CopyBuffer reads indicator handles into arrays. NinjaScript series use bars-ago indexing, so array order and Calculate frequency have to be aligned before signals are compared.
  • CTrade and trade requests work with orders, deals, and account-dependent position models. A NinjaScript Strategy uses EnterLong and ExitLong or unmanaged handling, and Strategy Analyzer applies its own fill assumptions.

Use cases

When this conversion is useful

  • Port an MT5 Expert Advisor into a NinjaTrader Strategy.
  • Rebuild an MQL5 custom indicator as a NinjaScript Indicator.
  • Compare MT5 signals in Strategy Analyzer with explicit fill assumptions.

FAQ

MQL5 to NinjaScript questions

Build the NinjaScript draft with its native model in view

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