Code migration guide

MQL4 to NinjaScript migration guide for OnTick and OrderSend

An MQL4 Expert Advisor reacts in OnInit, OnTick, and OnCalculate, then sends trades with OrderSend tickets. NinjaScript configures that work in OnStateChange, calculates in OnBarUpdate, reads prices as bars-ago Series such as Close[0], and replaces each ticket with 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 4 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
LifecycleExpert Advisors and indicators respond to predefined callbacks such as OnInit, OnTick, and OnCalculate.OnStateChange configures lifecycle states and OnBarUpdate handles bar-driven calculations at the selected Calculate frequency.Choose when each MetaTrader 4 calculation should run in NinjaTrader 8; matching syntax alone does not preserve timing.
Market data and historyPrice series use terminal arrays and shift-based access; indicators commonly expose calculated values through buffers.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 outputCustom indicators bind buffers, while built-in indicator functions can return values directly for a requested shift.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 positionsClassic trade functions such as OrderSend work with order tickets, lots, prices, stops, and a shared order pool.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 .mq4 file in MetaEditor, attach it in MetaTrader 4, and test the correct EA or indicator 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: MQL4 to NinjaScript

MQL4 shift 0 refers to the current bar. NinjaScript CrossAbove runs at the frequency selected by Calculate and on the series identified by BarsInProgress. Match these settings and the completed-bar policy before comparing signals. Neither fragment shown here submits an order.

MQL4 source fragment
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;
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, and OnCalculate work in the .mq4 Expert Advisor or custom indicator, including buffers and every OrderSend that uses tickets, lots, prices, stops, and the shared order pool.

  2. 2

    Choose the target artifact

    Choose a NinjaScript Indicator or Strategy and configure it in OnStateChange. Order replacement belongs on a Strategy that uses EnterLong and ExitLong, or on a deliberate unmanaged design.

  3. 3

    Rewrite through target APIs

    Turn shift-based prices into bars-ago Series such as Close[0]. Route each extra MQL4 timeframe through a data series and BarsInProgress, and run the calculation in OnBarUpdate at the selected Calculate frequency.

  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

  • Choose a NinjaScript Indicator or Strategy and configure it in OnStateChange.
  • Match each MQL4 timeframe request with a NinjaScript data series and BarsInProgress branch.
  • Translate MT4 orders, stops, and magic numbers into managed signals or a deliberate unmanaged design.

Limitations

What the draft cannot prove

  • MQL4 work arrives through OnInit, OnTick, and OnCalculate. NinjaScript configures lifecycle in OnStateChange and calculates in OnBarUpdate at the selected Calculate frequency, so the bar policy has to be chosen before signals can be compared.
  • OrderSend works with tickets, lots, prices, stops, and a shared order pool. A NinjaScript Strategy uses managed methods such as EnterLong and ExitLong, or unmanaged handling, so magic numbers and ticket ownership need a new order design.
  • Pineify can generate and revise a NinjaScript draft. Compilation in NinjaScript Editor, loading the Indicator or Strategy, and Strategy Analyzer remain the checks before runtime use.

Use cases

When this conversion is useful

  • Port an MQL4 Expert Advisor to a NinjaTrader Strategy.
  • Rebuild an MT4 indicator as a NinjaScript Indicator.
  • Move shift-based MT4 signals into NinjaTrader series and analysis tools.

FAQ

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