Skip to main content

NinjaScript and Strategies

MCP lets an AI trading agent write NinjaScript, compile it against the user's live NT8 install, deploy compiled strategies, and verify whether they are actually trading.

For practical examples of how to ask for this safely, see NinjaScript and Strategy Prompts. For the full agent loop including backtesting, see Backtesting and Optimization.

Code and Trade Power

These tools can write .cs files, compile code, deploy strategies, and stop live strategies. Use mcp:trade only with AI clients you trust.

NinjaScript Authoring

ToolUse it for
GetNinjaScriptHelpRead file layout, templates, workflow notes, compile pitfalls, and C# language constraints before generating source.
SearchNinjaScriptSymbolsSearch NinjaTrader types, methods, and properties by name fragment.
LookupNinjaScriptSymbolInspect the actual signatures available in the user's NT8 installation. This is the source of truth for overloads and property names.
CompileNinjaScriptCompile source in the sandbox. Use in_memory:true for syntax checks before writing to disk. Returns a job_id.
WriteNinjaScriptFileWrite a .cs file under the NT8 NinjaScript folder. The response tells you whether reflection-based compile triggering succeeded.
ReadNinjaScriptFileRead an existing user-authored NinjaScript file.
ListNinjaScriptFilesList user-authored files under Documents\NinjaTrader 8\bin\Custom\Indicators, Strategies, or AddOns.
ListCompiledSnippetsList compiled sandbox .dll files.
DeleteCompiledSnippetRemove a compiled sandbox snippet.
GetNinjaScriptHelp(topic="strategy_template")
SearchNinjaScriptSymbols(query="EnterLong")
LookupNinjaScriptSymbol(name="Strategy", include_inherited=true)
CompileNinjaScript(source=..., in_memory=true)
GetMcpJob(job_id="...")
WriteNinjaScriptFile(name="...", kind="strategy", source=..., overwrite=true)

If WriteNinjaScriptFile reports that compile triggering was not available, the user must press F5 in the NinjaScript Editor or restart NT8 so NinjaTrader compiles the file.

NinjaScript Compile Gotchas

These trip up code generators frequently. The addon's GetNinjaScriptHelp(topic: "compile_pitfalls") returns the up-to-date list, but the highlights:

  • C# 6 target: no out var, no local functions, no tuples, no expression-bodied properties with => on the property itself. NinjaTrader's NinjaScript Editor parser rejects newer C# syntax.
  • Do NOT add using NinjaTrader.NinjaScript.Indicators; at file scope. It causes CS0433 ambiguity with vendor DLLs.
  • Class name must equal file name and live in namespace NinjaTrader.NinjaScript.{Indicators|Strategies|AddOns} matching the kind argument.
  • compile_engine field in WriteNinjaScriptFile's response: "reflection.<something>" means NT8 was recompiled immediately; "file_only" means the file was written but compile is deferred until the user presses F5 or restarts NT8.
  • /langversion is rejected by NT8's legacy CSharpCodeProvider. Do not pass it.

Always pre-flight with CompileNinjaScript(in_memory: true) before WriteNinjaScriptFile. Iterating on a syntax error 5 times costs ~5 seconds of in-memory compile and zero filesystem churn.

Strategy Deployment

ToolUse it for
DeployStrategyInstantiate a compiled NinjaScript strategy. Use mode:"account" for headless deployment or mode:"chart" to attach to a visible chart.
ListDeployedStrategiesList the MCP deployment registry with fresh live state from NT8.
GetDeployedStrategyStateVerify a single deployment by deployment_id. Trust the live block over cached deploy-time fields.
StopStrategyClose a deployed strategy and optionally remove it from the registry.
GetStrategyStateInspect strategy parameters, indicator values, and recent trades. Useful for explaining why a strategy did or did not enter.
OpenChartOpen a chart window for an instrument. Returns already_open:true if a matching chart is already visible.
REST Counterparts

The public REST and WebSocket APIs include strategy lifecycle endpoints such as StartStrategy, EnableStrategy, DisableStrategy, and CloseStrategy. MCP deployment tools add registry tracking, live-state interpretation, and reconnect-aware verification.

See Strategies for the public endpoints.

Deployment Check

After deploying, do not rely on the initial response alone.

DeployStrategy(...)
GetDeployedStrategyState(deployment_id="...")

is_trading:true means the strategy is active and processing bars. A zero trade count can be normal if no entry signal has fired yet.

Backtest Before You Deploy

Once a strategy compiles cleanly, run it through NT8's actual Strategy Analyzer engine before deploying live. RunStrategyBacktest accepts the same parameters object you would pass to DeployStrategy, so the same configuration can be backtested then deployed without modification.

CompileNinjaScript(source=..., in_memory=true) // syntax check
WriteNinjaScriptFile(name="MyStrategy", kind="strategy", source=..., overwrite=true)
RunStrategyBacktest(
strategy_class: "MyStrategy",
instrument: "MES 06-26",
bars_period: {period_type: "Minute", value: 5},
from: "2026-04-01T00:00:00Z",
to: "2026-04-30T00:00:00Z",
parameters: {Fast: 10, Slow: 25}
)
GetMcpJob(job_id="...") // poll until completed
DeployStrategy(strategy_class: "MyStrategy", ..., parameters: {Fast: 10, Slow: 25})

Backtest results are bit-identical to running the same configuration through NT8 Strategy Analyzer's UI. See Backtesting and Optimization for parameter sweeps, fitness selection, and the full result schema.