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.
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
| Tool | Use it for |
|---|---|
GetNinjaScriptHelp | Read file layout, templates, workflow notes, compile pitfalls, and C# language constraints before generating source. |
SearchNinjaScriptSymbols | Search NinjaTrader types, methods, and properties by name fragment. |
LookupNinjaScriptSymbol | Inspect the actual signatures available in the user's NT8 installation. This is the source of truth for overloads and property names. |
CompileNinjaScript | Compile source in the sandbox. Use in_memory:true for syntax checks before writing to disk. Returns a job_id. |
WriteNinjaScriptFile | Write a .cs file under the NT8 NinjaScript folder. The response tells you whether reflection-based compile triggering succeeded. |
ReadNinjaScriptFile | Read an existing user-authored NinjaScript file. |
ListNinjaScriptFiles | List user-authored files under Documents\NinjaTrader 8\bin\Custom\Indicators, Strategies, or AddOns. |
ListCompiledSnippets | List compiled sandbox .dll files. |
DeleteCompiledSnippet | Remove a compiled sandbox snippet. |
Recommended Compile Loop
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 causesCS0433ambiguity with vendor DLLs. - Class name must equal file name and live in
namespace NinjaTrader.NinjaScript.{Indicators|Strategies|AddOns}matching thekindargument. compile_enginefield inWriteNinjaScriptFile'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./langversionis rejected by NT8's legacyCSharpCodeProvider. 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
| Tool | Use it for |
|---|---|
DeployStrategy | Instantiate a compiled NinjaScript strategy. Use mode:"account" for headless deployment or mode:"chart" to attach to a visible chart. |
ListDeployedStrategies | List the MCP deployment registry with fresh live state from NT8. |
GetDeployedStrategyState | Verify a single deployment by deployment_id. Trust the live block over cached deploy-time fields. |
StopStrategy | Close a deployed strategy and optionally remove it from the registry. |
GetStrategyState | Inspect strategy parameters, indicator values, and recent trades. Useful for explaining why a strategy did or did not enter. |
OpenChart | Open a chart window for an instrument. Returns already_open:true if a matching chart is already visible. |
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.