Skip to main content

How to Write a NinjaScript Strategy with ChatGPT

ChatGPT plus CrossTrade MCP turns a plain-English strategy spec into NinjaScript that compiles in your NT8 install and backtests through Strategy Analyzer. This tutorial uses ChatGPT specifically; for Claude, see How to Write a NinjaScript Strategy with Claude.

Prerequisites

RequirementDetail
CrossTrade subscriptionElite
CrossTrade Add-Onv1.13.0 or higher
NinjaTrader 8Running
ChatGPTA plan that supports remote MCP connectors
Scopemcp:trade
AccountSim101

If you have not set up ChatGPT with CrossTrade MCP yet, see How to Connect ChatGPT to NinjaTrader 8 first.

Sim101 only

Run the entire workflow on Sim101. Do not move generated strategies to a live or funded account during this tutorial.

Step 1: Write the strategy spec

In trader language, not C#.

Strategy: SampleEmaCross
Instrument: MES 06-26
Bars: 5-minute
Entry: Long when 9-EMA crosses above 21-EMA.
Exit: ATR-based trailing stop, multiplier 2.0.
Risk: One contract. Max two trades per session. No averaging down.
Time: 09:30 to 11:00 ET only.
Account: Sim101.

Step 2: Ask ChatGPT to ground the draft in real symbols

I want to prototype an educational NinjaScript strategy for Sim101 only.
Before drafting, call GetNinjaScriptHelp on EMA, ATR, CrossAbove, and
SetTrailStop. Restate the overloads you intend to use.

ChatGPT calls the help tools and reports the overloads. Read the result. Push back on anything wrong.

Step 3: Generate the strategy

Draft SampleEmaCross from the spec above. Show me the source. Do not compile
yet.

ChatGPT writes the source. Compare against the spec.

Step 4: Compile and repair

CompileNinjaScript(in_memory: true). For each error, explain it in plain English.
For every unresolved identifier, LookupNinjaScriptSymbol and rewrite the offending
line. Recompile until green. Do not write the file yet.

Two or three iterations usually enough.

Step 5: Write the file with confirmation

After I confirm, WriteNinjaScriptFile to Strategies/SampleEmaCross.cs. Then run
one more CompileNinjaScript without in_memory to confirm the disk version
compiles.

Step 6: Backtest on Sim101

RunStrategyBacktest on Sim101, last 30 trading days, 5-minute bars, commission
$1.27 per round-trip, slippage 1 tick. Apply gates: profit factor > 1.25, max
drawdown < $500, trade count > 40. Report pass or fail per gate.

Read the metrics. Read the trade list. If gates fail, iterate the prompt, not just the parameters.

Step 7: Devil's advocate review

List five reasons this generated strategy should not go to a live or funded
account. Be specific.

If the agent cannot produce five reasons, you have not given it enough context.

Troubleshooting

SymptomFix
403 insufficient_scopeReauthorize at mcp:trade
Compile keeps failingForce LookupNinjaScriptSymbol on each unresolved identifier
Backtest returns no tradesCheck the time filter and instrument symbol