NinjaScript ยท Compile ยท Backtest ยท Deploy

Build and Backtest NinjaScript Strategies with AI

A typical AI-generated NinjaScript file does not compile. The model invents methods. It picks the wrong overload. CrossTrade MCP changes that: the agent inspects your real NT8 symbol surface, compiles in memory against your install, repairs on failure, and then backtests through NT8's actual Strategy Analyzer engine. The result is a compile loop that converges instead of looping.

The trader version of vibe coding

From plain-English idea to verified NinjaScript

In this context, vibe coding means describing a trading strategy in plain English and letting an AI agent draft the NinjaScript, then using compile and backtest feedback to refine it. CrossTrade MCP turns that draft into something a futures trader can actually run.

  1. A trader describes a setup in plain English.
  2. The agent translates it into a NinjaScript implementation plan with NT8 types and method overloads checked against your install.
  3. The first draft is compiled inside NT8 with CompileNinjaScript(in_memory: true).
  4. Errors are repaired with tool feedback (LookupNinjaScriptSymbol, GetNinjaScriptHelp), not by another round of guessing.
  5. The strategy is tested in Strategy Analyzer with realistic commission and slippage.
  6. Deployment stays gated behind explicit approval and a post-deploy GetDeployedStrategyState check.

For the dedicated vibe coding page with prompts and FAQ, see Vibe Coding NinjaScript Strategies with CrossTrade MCP.

Direct answer

The compile loop belongs in the conversation

CrossTrade MCP exposes four tools that turn AI-written NinjaScript from a brittle one-shot into a converging loop:

  • GetNinjaScriptHelp returns help text for a symbol or topic.
  • SearchNinjaScriptSymbols fuzzy-searches the live NT8 type and member surface.
  • LookupNinjaScriptSymbol resolves a single name to its overloads.
  • CompileNinjaScript(in_memory: true) compiles against the running NT8 AppDomain without touching disk.

With those, the agent reads, writes, compiles, and repairs in the same conversation. WriteNinjaScriptFile only fires after compile is green and you confirm. RunStrategyBacktest drives NT8's real engine; results are bit-identical to the desktop UI for single backtests.

The old AI NinjaScript problem

What goes wrong without MCP

Ask a generic model to write NinjaScript. You get plausible C# that:

  • References indicators that don't exist in your install.
  • Picks method overloads that were removed two versions ago.
  • Misspells CrossAbove or invents EmaCross.
  • Calls SetTrailStop with the wrong argument types.

You compile, you paste the errors back, the model swears it understands, and it makes a new set of mistakes. Iteration is slow because the model has no way to look at the truth.

The CrossTrade MCP loop

The loop in eight steps

GetNinjaScriptHelp / Search / Lookup    โ†’  ground the model in real signatures
       โ”‚
       โ–ผ
Author NinjaScript source
       โ”‚
       โ–ผ
CompileNinjaScript(in_memory: true)
       โ”‚
       โ”œโ”€โ”€โ”€โ”€ on failure โ”€โ”€โ”€โ”€โ”
       โ”‚                    โ–ผ
       โ”‚              LookupNinjaScriptSymbol on unknown identifier
       โ”‚                    โ”‚
       โ”‚                    โ–ผ
       โ”‚              Repair source
       โ”‚                    โ”‚
       โ””โ”€โ”€โ”€โ”€ recompile โ—„โ”€โ”€โ”€โ”€โ”˜
       โ”‚
       โ–ผ  on success
WriteNinjaScriptFile (with user confirmation)
       โ”‚
       โ–ผ
RunStrategyBacktest
       โ”‚
       โ–ผ
Review metrics; gate by profit factor, drawdown, trade count
       โ”‚
       โ–ผ
DeployStrategy (with explicit user confirmation)
       โ”‚
       โ–ผ
GetDeployedStrategyState (verify is_trading)
Example prompt

One prompt; a full compile-backtest-sweep session

Compile loop + sweep prompt On Sim101, write a NinjaScript strategy called MyEmaCross for MES. Behavior: long when 9-EMA crosses above 21-EMA. Exit on cross below. ATR-based trailing stop with multiplier 2.0. Maximum 4 contracts. No averaging down. Max two trades per session. 1. Before writing, call GetNinjaScriptHelp on EMA, ATR, CrossAbove, SetTrailStop. 2. Compile in memory. If compile fails, LookupNinjaScriptSymbol on the unresolved identifier and repair. Recompile until green. 3. WriteNinjaScriptFile after I confirm. 4. RunStrategyBacktest April 1 to April 30, 2026, 5-minute bars, no commission, 0 slippage. Return the metrics block. 5. If profit factor > 1.25, max drawdown < $500, trade count > 40, sweep FastPeriod 5..15 step 1 and SlowPeriod 20..30 step 1, fitness NetProfit. Show top three. 6. Re-run a full single backtest on the winning parameter set. Show every trade. 7. Stop. Do not deploy unless I explicitly approve.
Compile error repair

What a healthy iteration looks like

A typical first compile after AI-generated NinjaScript:

CompileNinjaScript(in_memory: true)
   โ†’ success: false
   โ†’ errors:
       MyEmaCross.cs(42,18): CS0103 The name 'EmaCross' does not exist in the current context.

The agent reads the error, calls SearchNinjaScriptSymbols("ema cross") and LookupNinjaScriptSymbol("CrossAbove"), identifies the right call shape, and rewrites the offending line. Recompile. Often green on the second try.

Strategy Analyzer backtest

Bit-identical to NT8 UI for single backtests

The reference parity case lives at the NinjaScript Backtest Benchmark: SampleMACrossOver on MES 06-26, 5-minute bars, April 1 to April 30, 2026, Fast=10, Slow=25, Sim101, no commission, 0 slippage. Running through NT8's UI and running through RunStrategyBacktest produce matching NetProfit, ProfitFactor, Sharpe, and trade counts.

This matters because the agent's decisions on parameter sweeps and deployment gates are only as good as the backtests they depend on. If the backtest is not faithful, the agent's "go" is meaningless.

Parameter sweeps

Sweep responsibly

A sweep is cartesian. Two 11-step ranges produce 121 iterations. The risk is overfitting, not throughput:

  • The wider the sweep, the easier it is to find parameters that fit noise.
  • Strategy Analyzer treats the entire range as in-sample by default; build your own out-of-sample window.
  • A great single backtest with 12 trades is luck, not edge.

Defenses inside the prompt: bound the sweep, require a minimum trade count, require profit factor on an out-of-sample window, refuse to deploy if any gate fails.

Funded account constraints

The deploy gate is the safety system

On a funded futures account, never let the agent deploy a strategy without:

  • The firm's automation policy verified.
  • Profit factor above a minimum on the in-sample window.
  • Profit factor above a minimum on an out-of-sample window.
  • Max drawdown below a hard dollar cap.
  • Trade count above a minimum.
  • An explicit user confirmation.
  • A post-deploy GetDeployedStrategyState that returns is_trading: true.
Deployment safety

After deploy, verify

The agent must call GetDeployedStrategyState after DeployStrategy. is_trading: true is the only acceptable post-deploy state. If false, the strategy is registered but not running, and the agent should report the errors field.

One operational note: chart-attached strategies cannot be toggled via StrategiesGrid.StrategyEnable. Deploy through MCP's DeployStrategy rather than chart-strategy enable for that case.

FAQ

Frequently asked questions

Can AI write NinjaScript that compiles?

Yes when grounded in your real NT8 install. The agent calls help/search/lookup tools and compiles in memory before any file write.

Are MCP backtests faithful to NT8's UI?

Yes for single backtests. The reference parity case is SampleMACrossOver on MES with matching metrics.

Can the agent deploy without my approval?

Only if your prompt allows it. Always require explicit confirmation and quantitative gates.

What is the difference between in_memory: true and writing a file?

In-memory compile runs in NT8's AppDomain without touching disk. Use it during iteration. Write the file only after compile is green and you confirm.

Why are parameter sweeps risky?

Cartesian search fits noise. Require trade-count minimums and out-of-sample validation before deploying the winner.

The compile loop that traders actually needed

Inspect the symbol surface, compile in memory, repair, backtest, deploy with gates. One conversation.