Skip to main content

RunStrategyBacktest

Runs a compiled NinjaScript strategy through NT8's actual Strategy Analyzer engine. Single backtests are bit-identical to the NT8 UI. Optional parameter sweeps return a ranked grid.

PropertyValue
Required scopemcp:trade
RiskMedium. No live trading. Can be long-running.
Side effectsSpawns a backtest job in NT8.

When an agent should use it

  • After CompileNinjaScript succeeds.
  • Before any DeployStrategy call.
  • For parameter sweeps before deploying the winner.

When an agent should not use it

  • As a substitute for live trading. Backtests are diagnostics, not predictions.
  • On uncompiled strategies. The class must exist and compile.

Parameters

Single backtest:

{
"strategy": "SampleMACrossOver",
"instrument": "MES 06-26",
"from": "2026-04-01",
"to": "2026-04-30",
"bars_period": { "type": "Minute", "value": 5 },
"account": "Sim101",
"parameters": { "Fast": 10, "Slow": 25 },
"commission": "0",
"slippage_ticks": 0
}

Parameter sweep:

{
"strategy": "MyEmaCross",
"instrument": "MES 06-26",
"from": "2026-04-01",
"to": "2026-04-30",
"bars_period": { "type": "Minute", "value": 5 },
"optimization": {
"parameters_sweep": [
{ "name": "FastPeriod", "min": 5, "max": 15, "step": 1 },
{ "name": "SlowPeriod", "min": 20, "max": 30, "step": 1 }
],
"fitness": "NetProfit"
}
}

Response shape

Single backtest (truncated):

{
"job_id": "b-9001",
"status": "completed",
"metrics": {
"NetProfit": 800.00,
"ProfitFactor": 1.14,
"MaxDrawdown": -340.00,
"TradeCount": 264,
"Sharpe": 0.42
},
"trades": [
{"entry_ts": "2026-04-01T13:32:00Z", "exit_ts": "2026-04-01T13:55:00Z", "side": "Long", "pnl": 24.50}
]
}

Sweep response includes a ranked grid and a top-level best block.

Example user prompt

Run a single backtest of SampleMACrossOver on MES 06-26, April 1 to April 30, 2026,
5-minute bars, Fast=10, Slow=25, Sim101, no commission, 0 slippage. Return the
metrics block. After that, sweep Fast 5..15 and Slow 20..30 and tell me the
top three parameter sets by NetProfit.

Example tool call

{
"name": "RunStrategyBacktest",
"arguments": {
"strategy": "SampleMACrossOver",
"instrument": "MES 06-26",
"from": "2026-04-01",
"to": "2026-04-30",
"bars_period": { "type": "Minute", "value": 5 },
"account": "Sim101",
"parameters": { "Fast": 10, "Slow": 25 }
}
}

Common errors

SymptomLikely causeFix
strategy_not_compiledThe class is not compiled.Compile first.
data_not_availableHistorical data not downloaded for the range.Download historical data in NT8 or shorten the range.
job_timeoutVery large sweep.Reduce the sweep range or split into multiple jobs.

Safety notes

Backtests are not predictions. Treat them as one input among many.

  • A great backtest with a tiny trade count is luck.
  • Overfit sweeps look like profit factories and trade badly live.
  • Slippage and commission must match the live environment.

Safe workflow placement

CompileNinjaScript → WriteNinjaScriptFile → RunStrategyBacktest
→ user reviews metrics → DeployStrategy (gated)

Verified parity reference

For the current parity benchmark (SampleMACrossOver on MES 06-26, 5-minute bars, April 1 to April 30, 2026, Fast=10, Slow=25, Sim101, no commission, 0 slippage), see the Backtesting and Optimization section and the NinjaScript Backtest Benchmark tool page.

Use in AI-generated NinjaScript workflows

  • When the agent should call it: after WriteNinjaScriptFile. Single backtest first to confirm the strategy compiles into a runnable class; optional bounded sweep after; full single backtest on the sweep winner.
  • Required scope: mcp:trade.
  • Confirmation required: no for backtests (no live orders), but the prompt should require an explicit "go" before any sweep with a wide parameter range.
  • What the agent should summarize: the metrics block, the pass/fail of the user's gates, the trade count caveat (low trade counts mean the metrics are not meaningful), and the sweep winner's plausibility (wide winning region vs noise fit).
  • Failure mode it helps catch: strategies that compile but do not behave the way the prompt described. The trade list reveals it before the deploy decision does.

For the broader vibe coding workflow, see NinjaScript AI Workflow.