Code migration guide
NinjaScript to cTrader migration guide for OnBar and Positions
Shared C# syntax still leaves a NinjaScript Strategy on OnStateChange and OnBarUpdate. A cBot or indicator needs its own class, OnStart plus OnBar or OnTick, explicit Bars instances in place of BarsInProgress, and trades through Positions or PendingOrders with volume in 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 NinjaTrader 8 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 | OnStateChange configures lifecycle states and OnBarUpdate handles bar-driven calculations at the selected Calculate frequency. | Robots use lifecycle methods including OnStart, OnTick, OnBar, OnStop, and OnException; indicators calculate by index. | Choose when each NinjaTrader 8 calculation should run in cTrader; matching syntax alone does not preserve timing. |
| Market data and history | Series use bars-ago indexing such as Close[0]; multi-series scripts must route work with BarsInProgress. | 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 | Indicator classes configure plots and update their Series values inside the NinjaScript lifecycle. | 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 | Strategies can use managed methods such as EnterLong and ExitLong or opt into lower-level unmanaged handling. | 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 in NinjaScript Editor, load the Indicator or Strategy, and test strategies with Strategy Analyzer before runtime use. | 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: NinjaScript to cTrader C#
NinjaScript CrossAbove is evaluated according to Calculate and BarsInProgress. The cTrader Last(2)/Last(1) comparison uses completed bars when called from OnBar. Match the same series and confirmation policy, and initialize the moving averages in the correct lifecycle. Shared C# syntax does not establish equivalent signal timing.
protected override void OnBarUpdate()
{
if (CurrentBar < 20) return;
bool longSignal = CrossAbove(SMA(10), SMA(20), 1);
}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
List OnStateChange setup, OnBarUpdate calculations, BarsInProgress branches, and EnterLong or ExitLong calls in the NinjaScript Indicator or Strategy.
- 2
Choose the target artifact
Choose a cTrader custom indicator or cBot. Robots use OnStart, OnTick, OnBar, OnStop, and OnException. Indicators calculate by index.
- 3
Rewrite through target APIs
Replace BarsInProgress routing with explicit Bars instances. Translate managed EnterLong and ExitLong calls into Positions, PendingOrders, labels, and protection updates, with volume measured in units.
- 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
- Map NinjaScript states and OnBarUpdate to cTrader initialization plus OnBar or OnTick behavior.
- Replace BarsInProgress routing with explicit cTrader Bars instances for each market and timeframe.
- Translate managed order methods and signal names into cTrader positions, labels, and protection updates.
Limitations
What the draft cannot prove
- OnBarUpdate runs at the selected Calculate frequency and on the series identified by BarsInProgress. A cBot calculation from OnBar uses that lifecycle event, and the Last(2)/Last(1) comparison refers to completed bars when called from OnBar.
- EnterLong and ExitLong are NinjaTrader managed strategy methods, with unmanaged handling as the other option. cTrader robots submit trades through Symbol, Positions, and PendingOrders, and volume is measured in units.
- Pineify can generate and revise a cTrader C# draft. Building in cTrader Algo, checking the indicator chart, and backtesting a cBot with symbol, timeframe, spread, and volume settings remain the validation steps.
Use cases
When this conversion is useful
- Move a NinjaTrader Strategy to a cTrader cBot.
- Port a NinjaScript Indicator to a cTrader custom indicator.
- Keep reusable C# math while rebuilding platform services and trade execution.
FAQ
NinjaScript to cTrader C# questions
Official references
- NinjaScript OnStateChange
NinjaTrader
- NinjaScript OnBarUpdate
NinjaTrader
- NinjaScript managed order approach
NinjaTrader
- 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.