Code migration guide

NinjaScript to MQL4 migration guide for bars-ago and OrderSend

A NinjaScript Indicator or Strategy configures lifecycle in OnStateChange and calculates in OnBarUpdate with bars-ago Series such as Close[0]. MQL4 maps that work to OnInit, OnTick, or OnCalculate, shift-based buffers, and OrderSend tickets for managed EnterLong or ExitLong orders.

Generate an MQL4 draft, then compile it in MetaEditor and test it in MetaTrader 4.

Conversion map

What changes between NinjaTrader 8 and MetaTrader 4

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
LifecycleOnStateChange configures lifecycle states and OnBarUpdate handles bar-driven calculations at the selected Calculate frequency.Expert Advisors and indicators respond to predefined callbacks such as OnInit, OnTick, and OnCalculate.Choose when each NinjaTrader 8 calculation should run in MetaTrader 4; matching syntax alone does not preserve timing.
Market data and historySeries use bars-ago indexing such as Close[0]; multi-series scripts must route work with BarsInProgress.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 outputIndicator classes configure plots and update their Series values inside the NinjaScript lifecycle.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 positionsStrategies can use managed methods such as EnterLong and ExitLong or opt into lower-level unmanaged handling.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 validationCompile in NinjaScript Editor, load the Indicator or Strategy, and test strategies with Strategy Analyzer before runtime use.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: NinjaScript to MQL4

NinjaScript CrossAbove follows Calculate and BarsInProgress settings. The MQL4 iMA calls shown include shift 0, the unfinished bar. Choose equivalent calculation timing and data series before comparing signals. The fragments define a condition only; order management and indicator output require additional code.

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

Workflow

A four-step migration path

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

  1. 1

    Inventory the source behavior

    List OnStateChange setup, OnBarUpdate calculations, BarsInProgress branches, bars-ago Series such as Close[0], and EnterLong, ExitLong, or unmanaged orders in the NinjaScript class.

  2. 2

    Choose the target artifact

    Choose an .mq4 Expert Advisor or custom indicator. Map setup, historical processing, realtime work, and cleanup to OnInit, OnTick, or OnCalculate.

  3. 3

    Rewrite through target APIs

    Convert BarsInProgress and bars-ago indexes into explicit symbol, timeframe, and shift requests. Replace managed signal names and OCO rules with OrderSend tickets, lots, prices, stops, and manual coordination.

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

  • Map setup, historical processing, realtime work, and cleanup to the appropriate MQL4 event handlers.
  • Convert BarsInProgress and bars-ago series into explicit MT4 symbol, timeframe, and shift requests.
  • Replace managed signal relationships and OCO rules with order tickets and manual coordination.

Limitations

What the draft cannot prove

  • OnBarUpdate runs at the selected Calculate frequency. MQL4 Expert Advisors and indicators respond to OnInit, OnTick, and OnCalculate, so historical processing and realtime work need a chosen callback.
  • Bars-ago indexing such as Close[0], plus BarsInProgress routing, becomes symbol, timeframe, and shift access. Custom indicator output uses buffers.
  • EnterLong and ExitLong, or unmanaged handling, become OrderSend tickets in a shared order pool. OCO signal relationships need manual ticket coordination.

Use cases

When this conversion is useful

  • Port a NinjaTrader Strategy to an MT4 Expert Advisor.
  • Rebuild a NinjaScript Indicator with MQL4 buffers.
  • Move signal logic to MT4 while documenting lifecycle and order-model differences.

FAQ

NinjaScript to MQL4 questions

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