4.9 KiB
4.9 KiB
Purpose
A single source of truth to ensure first-time compile success for all NinjaTrader 8 strategies, indicators, and add-ons generated by an LLM.
Golden Rules (Pin These)
- NT8 only. No NT7 APIs. If NT7 concepts appear, silently upgrade to NT8 (proper
OnStateChange()andprotected overridesignatures). - One file, one public class. File name = class name. Put at the top:
// File: <ClassName>.cs. - Namespaces:
- Strategies →
NinjaTrader.NinjaScript.Strategies - Indicators →
NinjaTrader.NinjaScript.Indicators
- Strategies →
- Correct override access: All NT8 overrides are
protected override(neverpublicorprivate). - Lifecycle: Use
OnStateChange()withState.SetDefaults,State.Configure,State.DataLoadedto set defaults, add data series, and instantiate indicators/Series. - Indicator creation: Instantiate indicators once in
State.DataLoaded. Add to chart (if desired) inState.ConfigureviaAddChartIndicator(). - Managed orders by default: Use
SetStopLoss/SetProfitTargetbefore entries on the same bar. Do not mix Managed & Unmanaged in the same file. - MTF discipline: Add secondary series only in
State.Configure. InOnBarUpdate(), gate logic withBarsInProgressandCurrentBars[i]. - No Order Flow+ by default: Assume unavailable. If VWAP is needed, implement a local fallback or feature flag (OFF by default).
- Valid enums only: Use real NT8 members for
OrderState,MarketPosition, etc. - Starter header in every strategy
OnBarUpdate():
if (BarsInProgress != 0) return;
if (CurrentBar < BarsRequiredToTrade) return;
Required Using Block (Strategy)
Always show details
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Windows.Media;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Gui;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Gui.Tools;
using NinjaTrader.NinjaScript;
using NinjaTrader.NinjaScript.Strategies;
using NinjaTrader.NinjaScript.Indicators;
using NinjaTrader.NinjaScript.DrawingTools;
Indicators omit some GUI usings unless needed.
Inputs Pattern
Use DataAnnotations so properties render properly in the UI:
Always show details
[NinjaScriptProperty, Range(1, int.MaxValue)]
[Display(Name = "Quantity", GroupName = "Parameters", Order = 0)]
public int Quantity { get; set; } = 1;
[NinjaScriptProperty]
[Display(Name = "DebugMode", GroupName = "Parameters", Order = 999)]
public bool DebugMode { get; set; } = false;
Managed Order Rules
Call SetStopLoss and SetProfitTarget before you place an entry order.
Re-arm stops/targets when intended direction changes (e.g., flat→long or flat→short).
Use unique signal names per direction to bind OCO correctly (e.g., "L1", "S1").
Multi-Timeframe Rules
Add secondary series in State.Configure:
Always show details
AddDataSeries(BarsPeriodType.Minute, 5);
Guard in OnBarUpdate():
Always show details
if (BarsInProgress == 1) { /* 5-min logic */ }
if (CurrentBars[0] < BarsRequiredToTrade || CurrentBars[1] < 20) return;
Price Rounding & Tick Math
Always round to tick size to avoid rejections:
Always show details
private double Rt(double p) => Instrument.MasterInstrument.RoundToTickSize(p);
double target = Rt(Close[0] + 10 * TickSize);
Historical vs. Realtime
Always show details
private bool IsLive => State == State.Realtime;
Avoid timers/threads/file I/O by default.
Common Safety Defaults
Always show details
Calculate = Calculate.OnBarClose;
IsOverlay = false;
BarsRequiredToTrade = 20;
IsSuspendedWhileInactive = true;
IsInstantiatedOnEachOptimizationIteration = true;
Valid NT8 Signatures (paste as needed)
Always show details
protected override void OnOrderUpdate(
Order order, double limitPrice, double stopPrice, int quantity,
int filled, double averageFillPrice, OrderState orderState,
DateTime time, ErrorCode error, string nativeError) { }
protected override void OnExecutionUpdate(
Execution execution, string executionId, double price, int quantity,
MarketPosition marketPosition, string orderId, DateTime time) { }
protected override void OnMarketData(MarketDataEventArgs e) { }
protected override void OnMarketDepth(MarketDepthEventArgs e) { }
protected override void OnPositionUpdate(Position position, double averagePrice, int quantity, MarketPosition marketPosition) { }
Compile Checklist (Preflight)
NT8 only; no OnStartUp() or NT7 methods.
Exactly one public class; file name matches class name.
Required usings present.
Indicators/Series created in State.DataLoaded.
Starter header present in OnBarUpdate().
Managed orders only (unless explicitly asked otherwise).
Secondary series added only in State.Configure.
Enums & members verified against NT8.
Price rounding helper present for any custom prices.
DebugMode gating for Print() calls.
""").format(date=datetime.date.today().isoformat())
files["NT8_Templates.md"] = textwrap.dedent("""