Skip to main content

How to Vibe Code a NinjaScript Strategy Without Skipping the Backtest

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. This page is a step-by-step tutorial for doing that on NinjaTrader 8 through CrossTrade MCP, without skipping any of the verification steps that turn a draft into something you can actually run.

You can move faster than hand-writing C#. You still need verification.

What this tutorial builds

A simple example strategy on Sim101. No live trading. No funded accounts. The point is to walk through the full loop on a controlled account before you scale.

Strategy spec for the tutorial:

  • Instrument: MES 06-26.
  • Bars: 5-minute.
  • Entry: Long on the break above the first 15-minute range after the regular session open.
  • Exit: ATR-based trailing stop, multiplier 2.0.
  • Constraints: One contract. Max two trades per session. No averaging down.
  • Account: Sim101.

Prerequisites

RequirementDetail
CrossTrade subscriptionElite
CrossTrade Add-Onv1.13.0 or higher
NinjaTrader 8Running, add-on connected
MCP clientClaude Desktop, Claude Code, Cursor, or another remote-MCP client
Scopemcp:trade (compile, file write, and backtest all require it)
Default accountSim101

Start read-only on first runs if you want. Promote to mcp:trade only when you are ready to compile.

Step 1: Write the strategy spec in trader language

Before you involve the agent, write the spec yourself. Not as C#. As a trader.

Strategy: OpeningRangeMES
Instrument: MES 06-26
Bars: 5-minute
Entry: Long on break above first 15-minute range high after the
regular session open. Long only.
Exit: ATR-based trailing stop, multiplier 2.0.
Risk: One contract. Max two trades per session. No averaging down.
Time: Trade only between 09:30 and 11:00 ET.
Account: Sim101.

The spec is short. The spec is unambiguous. The agent's job is to translate it into NinjaScript; you do not need to invent that step.

Step 2: Ask the agent to turn the spec into a NinjaScript plan

The plan is not the code. The plan is the list of types and methods the agent intends to use, with overloads verified against your install.

Turn the following spec into a NinjaScript implementation plan. List the NT8 types, methods, and overloads you intend to use. Call GetNinjaScriptHelp on each one. Restate the plan before writing any code.

<spec>

The agent should call GetNinjaScriptHelp on time filters, ATR, EnterLong, SetTrailStop, and similar. After it restates the plan, read it. If anything looks wrong, push back here, not after the compile fails.

Step 3: Ask for the first NinjaScript draft

Draft the NinjaScript strategy from the plan. Do not call CompileNinjaScript yet. Show me the source.

Read the source. Look for obvious issues: wrong instrument, missing time filter, wrong exit logic. The agent will produce something workable, but not necessarily correct.

Step 4: Compile in NinjaTrader through CrossTrade MCP

CompileNinjaScript(in_memory: true). Do not WriteNinjaScriptFile.

In-memory compile runs in NT8's AppDomain without touching your disk. If it fails, you have not corrupted anything.

Step 5: Repair compile errors

For every error, explain it in plain English. For every unresolved identifier, LookupNinjaScriptSymbol and tell me the correct shape. Repair the source. Recompile. Repeat until green.

Typical repair iterations: two or three. After green compile, you should still not have written anything to disk.

Step 6: Run Strategy Analyzer backtest

WriteNinjaScriptFile only after I confirm. After confirm, RunStrategyBacktest on Sim101, the last 30 trading days, 5-minute bars, commission $1.27 per contract round-trip, slippage 1 tick. Show me the metrics block.

Realistic commission and slippage matter. A zero-cost backtest is a lie about live conditions.

Step 7: Review trade distribution, drawdown, and session behavior

The metrics tell you the overall picture. The trade list tells you whether the strategy behaves the way the spec described.

Questions to ask:

  • Does every trade fall inside the time filter (09:30 to 11:00)?
  • Did any session produce more than the configured max trades?
  • Are the entry prices near the high of the first 15-minute range?
  • Are the exits driven by the trailing stop, not by a session close?
  • Is the max drawdown consistent with the position size?

If any answer is off, the agent's draft does not match the spec. Iterate.

Step 8: Run parameter sweep carefully

Sweep the ATR multiplier from 1.5 to 3.0 in 0.25 steps. Show top three. Re-run a full single backtest on the winner. Flag any combination that looks like a noise fit (very narrow profitability cluster, tiny trade count, or extreme parameter values).

A wide sweep on a small data window almost always overfits. Bound the sweep to ranges your spec allows.

Step 9: Sim101 deployment checklist

Even on Sim101, walk the checklist.

  • Compile is green and the file is on disk.
  • Backtest gates passed.
  • Trade list matches the spec's intent.
  • Sweep winner is plausible, not a noise fit.
  • You can describe the strategy from memory.

Prepare a Sim101 deployment checklist. Restate the proposed DeployStrategy with account, instrument, quantity, and parameters. Wait for me to type "go". After deploy, GetDeployedStrategyState and confirm is_trading: true.

If is_trading: false, do not call success. Read the errors field.

Step 10: Human review before any live or funded account use

Before you consider moving a Sim101-tested strategy to a live or funded account:

  • Verify the firm's automation rules. Apex prohibits automation; Topstep permits with caveats; other firms vary.
  • Confirm your daily loss buffer math against the firm's drawdown rule.
  • Encode news window rules in the prompt.
  • Test on Sim101 for several sessions before considering anything else.
  • Decide what would make you stop the strategy.

If you cannot answer the last question, you are not ready to move beyond Sim101.

Common mistakes

  • Optimizing to the sample period. Wide sweeps on small windows. Re-run on a fresh out-of-sample window.
  • Ignoring fees and slippage. A zero-cost backtest is a lie.
  • Ignoring news windows. Strategy backtests across FOMC look great until the live session.
  • Ignoring prop firm max contracts. The strategy might generate orders that breach the per-account cap.
  • Assuming backtest fills match live fills. They sometimes do not, especially in fast markets.

Copy-paste prompts

Reusable prompts for each step are gathered in the NinjaTrader AI Prompt Library under the Vibe Coding category.

FAQ

Can I skip the compile loop and go straight to backtesting?

No. RunStrategyBacktest runs against a compiled class. Without a green compile, there is nothing to backtest.

Can I skip the backtest and deploy to Sim101?

You can. You should not. A backtest is cheap. Sim101 sessions are slower to evaluate than a backtest range.

What if the agent's plan is wrong?

Push back at the plan step, before the draft. Plan-level mistakes are far cheaper to fix than compile-level mistakes.

How do I know the strategy actually matches my spec?

Read the trade list. The metrics block can pass while the trades are happening at the wrong time or on the wrong instrument. The trade list is the truth source.