Files
nt8-sdk/.kilocode/rules/project_context.md
mo 9a28a49292
Some checks failed
Build and Test / build (push) Has been cancelled
Pre-cleanup baseline snapshot
2026-04-05 16:50:18 -04:00

164 lines
6.4 KiB
Markdown

# Project Context — NT8 SDK (Sprint 2: SIM Validation)
**Last Updated:** 2026-03-27
You are working on the **NT8 SDK** — an institutional-grade algorithmic futures trading system for NinjaTrader 8.
This is **production trading software**. Bugs cause real financial losses. Never take shortcuts.
---
## Critical Rules for Kilocode
1. **Only modify files listed in the task spec.** Never touch adjacent code.
2. **C# 5.0 only.** No `$""`, no `?.`, no `=>` bodies, no `nameof()`, no async/await.
3. **Never remove XML documentation or comments.**
4. **Never change interface signatures** — IStrategy, IRiskManager, IPositionSizer, INT8ExecutionBridge are frozen.
5. **Always build before deploying:** `dotnet build NT8-SDK.sln --configuration Release`
6. **Always deploy to BOTH paths** after every code change:
- Repo: `C:\dev\nt8-sdk\src\NT8.Adapters\Strategies\`
- NT8: `C:\Users\billy\Documents\NinjaTrader 8\bin\Custom\Strategies\`
7. **Never guess NT8 API signatures.** Verify at `https://developer.ninjatrader.com/docs/desktop`.
---
## Current State (2026-03-27)
**What works end-to-end:**
- SimpleORBStrategy with 10-factor confluence scoring
- NT8StrategyBase with session management, kill switch, connection recovery
- Dual-leg scaler + runner architecture (EntriesPerDirection=2 restored)
- PortfolioRiskManager singleton (kill switch, daily loss, contract cap)
- File logging (session log + settings export)
- Historical replay guard (_realtimeBarSeen)
- Execution confirmed in SIM on 2026-03-27
**Pending validation:**
- Runner leg dual-fill (Qty=2) — run backtest to confirm
- Breakeven + trail in live multi-bar scenario
---
## Architecture
```
SimpleORBNT8.cs NT8 entry point
NT8StrategyBase.cs Abstract base: bar routing, kill switch, breakeven, runner trail
SimpleORBStrategy.cs Signal: ORB detection, 10-factor confluence, _tradeTaken lock
NT8OrderAdapter.cs INT8ExecutionBridge: EnterLong/EnterShort/SetStopLoss
PortfolioRiskManager.cs Singleton: cross-strategy risk
NinjaTrader 8
```
## Key Files
| File | Path |
|---|---|
| NT8StrategyBase.cs | `src\NT8.Adapters\Strategies\` |
| SimpleORBNT8.cs | `src\NT8.Adapters\Strategies\` |
| SimpleORBStrategy.cs | `src\NT8.Strategies\Examples\` |
| NT8OrderAdapter.cs | `src\NT8.Adapters\NinjaTrader\` |
| PortfolioRiskManager.cs | `src\NT8.Core\Risk\` |
| deploy-to-nt8.bat | `deployment\` |
## Active Sprint Tasks
See `SPRINT_BOARD.md` (at `docs\architecture\phase1_sprint_plan.md`) for full task list.
Immediate next action: Run Strategy Analyzer backtest (NQ JUN26, Jan 1 2026 → Mar 27 2026) and confirm `PNL_UPDATE Position=Short Qty=2` in session log to validate runner leg.
---
## What Is Already Built (Do Not Touch)
All core trading logic is complete and has 240+ passing tests:
| Layer | Status | Key Files |
|---|---|---|
| Risk (Tier 1-3) | ✅ Complete | `src/NT8.Core/Risk/` |
| Position Sizing | ✅ Complete | `src/NT8.Core/Sizing/` |
| OMS / Order Lifecycle | ✅ Complete | `src/NT8.Core/OMS/` |
| Intelligence | ✅ Complete | `src/NT8.Core/Intelligence/` |
| Analytics | ✅ Complete | `src/NT8.Core/Analytics/` |
| Execution Utilities | ✅ Complete | `src/NT8.Core/Execution/` |
| Market Data | ✅ Complete | `src/NT8.Core/MarketData/` |
**NT8 Order Execution is ALREADY WIRED.**
`NT8StrategyBase.SubmitOrderToNT8()` calls `EnterLong`, `EnterShort`, `SetStopLoss`, and
`SetProfitTarget` directly. The execution path works end-to-end. Do not re-implement it.
---
## What You Are Fixing (The Active Task List)
### CRITICAL — `NT8StrategyBase.cs`
**Gap 1 — No kill switch**
`NT8StrategyBase` has no `EnableKillSwitch` NinjaScript parameter and no early-exit in `OnBarUpdate()`.
A runaway strategy cannot be stopped without killing NinjaTrader.
**Fix:** Add `EnableKillSwitch` (bool NinjaScript property) and `EnableVerboseLogging` property.
Add kill switch check as the FIRST thing in `OnBarUpdate()`.
→ See `TASK-01-kill-switch.md`
**Gap 2 — `ExecutionCircuitBreaker` not wired**
`src/NT8.Core/Execution/ExecutionCircuitBreaker.cs` is complete and tested.
It is never instantiated. Orders submit regardless of latency or rejection conditions.
**Fix:** Instantiate in `InitializeSdkComponents()`, gate orders in `SubmitOrderToNT8()`, wire rejections in `OnOrderUpdate()`.
→ See `TASK-02-circuit-breaker.md`
### HIGH — `TrailingStopManager.cs`
**Gap 3 — Placeholder stop math returns zero**
`CalculateNewStopPrice()` FixedTrailing branch: `marketPrice - (x - x)` = always zero movement.
ATRTrailing and Chandelier also have meaningless placeholder formulas.
**Fix:** Replace with real calculations using `TrailingStopConfig.TrailingAmountTicks` and `AtrMultiplier`.
→ See `TASK-03-trailing-stop.md`
### HIGH — `BasicLogger.cs`
**Gap 4 — No log-level filter**
Every log statement writes to console unconditionally. Cannot suppress debug noise in production.
**Fix:** Add `MinimumLevel` property (defaults to `Information`). Suppress messages below threshold.
→ See `TASK-04-log-level.md`
### MEDIUM — `SessionManager.cs`
**Gap 5 — No holiday awareness**
`IsRegularTradingHours()` checks session times only. Will attempt to trade on Christmas, Thanksgiving, etc.
**Fix:** Add static CME holiday set for 2025/2026. Return `false` on those dates.
→ See `TASK-05-session-holidays.md`
---
## Architecture (Read Before Touching Anything)
```
SimpleORBStrategy.OnBar()
↓ returns StrategyIntent
NT8StrategyBase.OnBarUpdate()
↓ [TASK-01: kill switch check here, first]
↓ calls ProcessStrategyIntent()
↓ calls _riskManager.ValidateOrder()
↓ calls _positionSizer.CalculateSize()
↓ calls SubmitOrderToNT8()
↓ [TASK-02: circuit breaker gate here]
↓ calls EnterLong/EnterShort/SetStopLoss/SetProfitTarget (already works)
NT8 callbacks → OnOrderUpdate / OnExecutionUpdate
↓ [TASK-02: record rejections in circuit breaker here]
```
---
## Technology Constraints
- **C# 5.0 only** — no `$""`, no `?.`, no `=>` on methods/properties, no `nameof()`, no `out var`
- **.NET Framework 4.8** — not .NET Core/5+/6+
- **NinjaScript managed orders** — `EnterLong`, `EnterShort`, `SetStopLoss`, `SetProfitTarget`
- `string.Format()` everywhere, never string interpolation
- All `Dictionary`, `HashSet` access inside `lock (_lock)` blocks
- XML doc comments on all public members
- `try/catch` on all public methods with `LogError` in the catch