NinjaTrader automated trading: 5 steps from import to sim to live
Import the NinjaScript file, enable the strategy on a chart, set the account, data series, session template and order handling, then run it on a simulation account and compare its trades with the backtest before you switch to live. The sim step is the one most traders skip and the one that catches the most setup errors.
By Soren Huang · Published · updated · Editorial policy
Contents
- How do you run an automated strategy in NinjaTrader?
- How do you import a NinjaScript strategy and enable it on a chart?
- Which settings decide what the strategy actually trades?
- Why run on a sim account before going live, and what do you compare?
- What happens on a connection loss or restart?
- Why do live results differ from the backtest, and how much is normal?
- Frequently asked questions
- References
How do you run an automated strategy in NinjaTrader?
NinjaTrader automated trading takes five steps: import the NinjaScript file, enable the strategy on a chart, set the account, data series, session and order handling, run it on a simulation account and compare its trades with the backtest, then switch to a live account. The sim step catches most setup errors before they cost money.
A NinjaScript strategy is a program written in NinjaTrader's own language that reads bars and places orders on your behalf. Once it is enabled, nobody confirms each order. That is the whole point of automation, and it is also why the order of the five steps matters. Think of a pilot's checklist: the engine run-up on the ground is slow and boring, but it is where loose parts show up. The simulation run is that ground test.
- Import the NinjaScript file so NinjaTrader compiles it and lists it as a strategy.
- Enable it on a chart with the bar type and size you want it to trade.
- Set the account, the data series, the session template and the order-handling options.
- Run it on a simulation account and compare its trades with the backtest, trade by trade. Fix every difference you do not understand before moving on.
- Switch to the live account with the same settings, and keep comparing.
How do you import a NinjaScript strategy and enable it on a chart?
A strategy reaches the chart in two moves: import the file so NinjaTrader compiles it into its strategy list, then add it to a chart and tick Enabled. Importing alone places nothing; enabling is the switch that lets it send orders.
There are two common ways to get the code in. A packaged export (a zip file) goes through the Control Center menu Tools, then Import, then NinjaScript Add-On. A plain source file goes into the NinjaScript Editor, where you compile it. If the compile fails, the editor's output window names the line, and the strategy does not appear on any list until the error is fixed. If the strategy was generated in MultiAlphas (Valynox), the step-by-step route in exporting a MultiAlphas strategy to NinjaTrader 8 covers the file and the import, and the supported platforms for strategy export page lists NinjaTrader alongside the other targets if you also run the same rules elsewhere.
To enable it, open the chart you want it to trade, right-click, choose Strategies, pick the strategy from the list and set its parameters. The last control in that dialog is the Enabled box. Tick it, and the strategy starts calculating on the chart's bars and submitting orders to the account you selected. The Strategies tab of the Control Center shows every enabled strategy with its account and current position, which is the first place to look when something does not match what you expected.
Which settings decide what the strategy actually trades?
Four settings control the result more than any line of code: the account, the data series and bar size, the session template, and the order-handling options. If one of them differs from the backtest, the live strategy is trading a different test, even though the rules are the same.
A session template (NinjaTrader calls it Trading hours) is the list of days and times the data series treats as the trading session. The same rule on the same bar size produces different trades on a day-session template and on an all-hours template, because bars are built differently and the strategy sees different closes. The templates that ship with the platform cover common cases such as the regular and the extended session for CME index futures; the hours behind them are the exchange's, and you can check what a given session covers in your own time zone with the free futures market hours tool. Exchanges change session hours from time to time, so the exchange's contract page is the final word. The reasoning behind the choice is explained in day-only versus full-session bars in a futures backtest.
| Setting | Set it to | If it differs from the backtest |
|---|---|---|
| Account | Sim101 for the test, your live account only after the sim comparison | Orders go to the wrong account; on a live account that is real exposure during a test |
| Data series and bar size | The same bar type and size the backtest used | Every indicator value changes; trade count and timing no longer match |
| Session template | The same session as the backtest | Trades appear in hours the backtest never traded, and bar closes move |
| Calculate | On bar close if the backtest decided at the bar close | On each tick, signals fire mid-bar at prices the backtest never saw |
| Start behavior | Decided before the first enable, and written down | A restart can leave you flat, doubled or out of sync (next sections) |
Order handling has three more options worth reading before you enable anything. Entry handling and Entries per direction decide whether a second signal in the same direction adds to the position; a backtest that assumed one contract at a time needs Entries per direction set to one. Exit on session close flattens the position before the session ends, which is correct for a day-trading rule and wrong for a swing rule that was tested holding overnight. A mismatch here produces exits that exist in live trading and never existed in the backtest.
Why run on a sim account before going live, and what do you compare?
A simulation run with the same bar size and session as the backtest lets you line up entry times, fill prices and trade counts trade by trade, and any difference is cheaper to understand on sim than on real money. NinjaTrader's Sim101 account fills orders against live market data without sending them to a broker, so the strategy runs exactly as it would live, minus the money.
Run the strategy on Sim101 for long enough to collect a set of completed trades, then export the backtest trade list for the same dates and put the two lists side by side. Compare four things in this order:
- Trade count. Same number of trades on the same days. A different count means a settings mismatch, usually the session template or Calculate, and nothing else is worth checking until it is fixed.
- Entry and exit times. Same bar for each trade. A one-bar shift usually means the backtest filled on the signal bar and the live strategy fills on the next bar, or the reverse.
- Entry and exit prices. Small differences are fill slippage. Large or one-sided differences point to a different order type or a different price series.
- Direction and size. Long where the backtest was long, one contract where the backtest had one.
Hypothetical example
An NQ strategy on 15-minute bars shows three backtest entries at 18,250.00, 18,310.00 and 18,272.00, each on the open of the bar after the signal. The sim run enters on the same three bars at 18,250.25, 18,310.25 and 18,272.25, one tick worse each time. One NQ tick is 0.25 index points and worth $5.00 per contract, so the sim paid $5.00 more per entry than the backtest assumed with zero slippage. If the backtest charged one tick of slippage per side, the sim is inside the assumption. If it charged nothing, the round-trip cost per trade is $10.00 higher than tested, and the average trade must be checked again with that cost in.
The point of the exercise is the last sentence of the example. The gap between sim fills and backtest fills is a measured slippage figure, and commission and slippage in a backtest explains how to compare it with the average trade. Many traders skip the sim step because it feels slow. It is the only place where every difference is free to study.
What happens on a connection loss or restart?
When the connection drops or NinjaTrader restarts, the strategy stops running and does not know about your open position until it is enabled again, so the start-behavior setting and a written restart routine decide whether you end up flat, doubled or correctly in sync. This is the part of automation that no backtest contains, because a backtest never loses its connection.
Start behavior is the setting that tells a strategy what to do about a position that already exists in the account when it is enabled. Picture coming back to a car you left running: you can wait until it stops, drive off as if it were parked, or check the gear it is in and take over. NinjaTrader offers the same three choices, plus a fourth, Immediately submit synchronize account, which places orders to bring the account position in line with the strategy's own calculated position.
| Start behavior | What the strategy does with an existing long | Position after restart |
|---|---|---|
| Wait until flat | Stays idle until the account is flat, then begins trading from its next signal | One long, unmanaged until you close it by hand |
| Immediately submit | Trades from its own signals as if the account were flat | Can become two longs: yours and the strategy's |
| Adopt account position | Takes over the existing long as its own and manages its exit | One long, managed by the strategy |
Disconnect handling is the companion setting: it decides whether the strategy recalculates its state from historical bars when the data feed returns or keeps running from where it stopped, and the Max restart attempts and Max restart minutes settings cap how many times it may restart itself in a window. Read the description of each option in NinjaTrader's help before choosing. Then write a restart routine and keep it next to the screen:
- Open the Control Center Positions tab and write down the real position per account.
- Open the Strategies tab and compare the strategy's position with the account position.
- If they differ, decide which one is right and either flatten by hand or let the chosen start behavior resolve it.
- Re-enable the strategy and confirm on the Strategies tab that it shows the position you expect.
This routine takes one minute and removes the single most expensive automation mistake, the doubled position after a reconnect. The wider list of things that break in an unattended system is in six failure points of fully automated futures trading.
Why do live results differ from the backtest, and how much is normal?
Some gap between backtest and live is expected from fill timing, slippage, session edges and data differences; the question is whether the gap stays inside the cost assumptions you tested with. Separate the causes into two groups before you judge the gap, because one group is noise you already paid for and the other is a setup error you can fix today.
- Expected: one or two ticks of slippage on market and stop orders, a fill on the next bar after the signal, a partial fill on a larger order, and a gap on the day the contract rolls, which Futures backtesting done right: specs, rollover, tick value, costs covers.
- Setup error: a different session template, a different bar size, the wrong account, or Calculate set to each tick when the backtest decided on the bar close. Each of these shows up as a trade-count mismatch in the sim comparison.
How much expected gap is normal depends on how much room the strategy has. The direct way to measure the room is to rerun the backtest with twice the slippage you observed on sim. A strategy whose average trade after doubled slippage is still clearly positive has room for a bad week of fills; one that turns flat was depending on cheap execution. Bailey, Borwein, López de Prado and Zhu (2014) add a second reason to expect a drop: the backtest you chose was the best of many tries, and part of its result is selection, not edge, so live trading starts from a lower base than the report shows. Pardo (2008) treats the ongoing comparison of real-time results with the backtest as part of the evaluation itself, not something that stops at go-live. If the strategy came out of MultiAlphas, rerun it with the sim-measured slippage doubled and check that it still clears the thresholds you set in strategy validation before you enable it on the live account; a candidate that passes on worse costs than it will probably pay is the one worth automating.
The next thing to do: enable the strategy on Sim101 with the backtest's bar size and session, collect at least 30 completed trades, and compute the average price difference per side between sim fills and backtest fills. Put that figure, doubled, into the slippage setting of the backtest and read the average trade again. That one number decides whether step five is ready.
Frequently asked questions
Can NinjaTrader trade automatically?
Yes. A NinjaScript strategy enabled on a chart places entry and exit orders on the selected account without manual confirmation. The same strategy can run on the Sim101 simulation account against live data, which is how most traders test the setup before connecting it to a live account.
What is the difference between Calculate on bar close and on each tick?
On bar close evaluates the rules once when a bar completes, so a signal is placed with the next bar. On each tick evaluates them on every price update, so a signal can fire and fill in the middle of a bar. A backtest that decided at the bar close only matches a live strategy set to On bar close.
Which start behavior should I use for a NinjaTrader strategy?
Choose it from your restart routine, not from habit. Wait until flat is the safest when you close positions by hand after a disconnect. Adopt account position fits a strategy that should manage whatever position is open. Immediately submit can create a second position if one already exists, so use it only when you are sure the account is flat.
How long should I run a strategy on the sim account?
Long enough to collect a set of completed trades you can compare with the backtest, around 30 trades as a minimum for a trade-by-trade check. For an intraday strategy that is often one to two weeks; for a swing strategy it can take months, in which case compare each trade as it completes rather than waiting for the full set.
Why does my live strategy trade more often than the backtest?
The most common causes are a different session template, a different bar size, or Calculate set to each tick instead of bar close. Each one changes the bars the strategy sees and produces signals the backtest never had. Check the trade count on sim against the backtest for the same dates; a count mismatch points to one of these settings.
References
- Bailey, D. H., Borwein, J., López de Prado, M., & Zhu, Q. J. (2014). Pseudo-Mathematics and Financial Charlatanism: The Effects of Backtest Overfitting on Out-of-Sample Performance. Notices of the American Mathematical Society, 61(5), 458–471.
- Pardo, R. (2008). The Evaluation and Optimization of Trading Strategies (2nd ed.). John Wiley & Sons.
- CME Group. E-mini Nasdaq-100 Futures Contract Specs. https://www.cmegroup.com/markets/equities/nasdaq/e-mini-nasdaq-100.contractSpecs.html