WriteNinjaScriptFile
Writes a NinjaScript source file to the NT8 user directory. The file becomes part of the user's NT8 install and will participate in subsequent compiles and Strategy Analyzer runs.
Touching the user directory affects every chart and strategy that references the file. Only call this after a green in-memory compile and an explicit user confirmation.
| Property | Value |
|---|---|
| Required scope | mcp:trade |
| Risk | Medium. Modifies the user's NT8 install. |
| Side effects | Writes a source file to disk. |
When an agent should use it
- Only after
CompileNinjaScript(in_memory: true)succeeds. - Only after the user has approved the write.
When an agent should not use it
- As a quick way to save scratch code. Use in-memory compile first.
- Without overwrite protection if the file already exists.
Parameters
{
"name": "MyEmaCross",
"kind": "strategy",
"source": "namespace NinjaTrader.NinjaScript.Strategies { ... }",
"overwrite": true
}
Response shape
{
"name": "MyEmaCross",
"kind": "strategy",
"path": "...\\bin\\Custom\\Strategies\\MyEmaCross.cs",
"bytes_written": 2480,
"compile_triggered": false,
"compile_engine": "file_only",
"note": "File written (compile_engine=file_only). ..."
}
The compile_engine field tells you whether NT8 actually rebuilt itself after the write. See Compile trigger is best-effort below before you assume the strategy is ready.
Compile trigger is best-effort
WriteNinjaScriptFile writes the .cs file into the appropriate NinjaScript source folder. The file write is deterministic. After writing, the add-on makes a best-effort attempt to trigger NinjaTrader's own NinjaScript recompile via reflection. This trigger is not guaranteed on every NT8 build or runtime.
Read compile_engine in the response:
compile_engine: "reflection.<...>"means the recompile was triggered. Check the NT8 Logs panel for compile errors.compile_engine: "file_only"means the file was written successfully but NT8 has not rebuiltNinjaTrader.Custom.dllyet. This is an expected fallback, not an error.
When you see file_only, press F5 in the NinjaScript Editor, or restart NT8, before expecting the strategy to appear or to deploy through NT8's compiled strategy path (DeployStrategy, RunStrategyBacktest).
CompileNinjaScript(in_memory: true) and the WriteNinjaScriptFile recompile trigger are two different compile paths. The in-memory compile validates your source through the MCP sandbox and can succeed even when NT8's own Custom assembly has not been rebuilt. WriteNinjaScriptFile writes the source into NT8's folder and only makes a best-effort attempt to trigger NT8's own recompile. A green in-memory compile does not mean NT8 has the new class loaded.
Example user prompt
Compile MyEmaCross in memory. If compile succeeds, show me the source you plan
to write and the target path. Wait for me to say "go" before WriteNinjaScriptFile.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
compile_required | Tried to write without a recent successful compile. | Call CompileNinjaScript first. |
file_exists | overwrite was false. | Either set overwrite: true or choose a new path. |
Safe workflow placement
CompileNinjaScript(in_memory: true) → user confirms → WriteNinjaScriptFile → check compile_engine → F5 if file_only
After writing, check compile_engine in the response. If it is file_only, the file is on disk but NT8 has not recompiled: press F5 in the NinjaScript Editor or restart NT8 before backtesting or deploying. See Compile trigger is best-effort.
Use in AI-generated NinjaScript workflows
- When the agent should call it: only after a green
CompileNinjaScript(in_memory: true)and an explicit user confirmation. This is the door between "trying things" and "the file is on disk." - Required scope:
mcp:trade. - Confirmation required: yes. Always.
- What the agent should summarize: the path written, the bytes written, and what the agent expects the next step to be (backtest, sweep, review). Should not auto-trigger any next step.
- Failure mode it helps catch: the wrong file overwriting an existing strategy. The confirmation gate is what prevents it.
For the broader vibe coding workflow, see NinjaScript AI Workflow.