Harden AI agent rules and compile learning workflow

This commit is contained in:
2026-04-05 17:59:34 -04:00
parent 4453ff552a
commit de6e150655
9 changed files with 301 additions and 963 deletions

View File

@@ -1,176 +1,45 @@
# Project Context NT8 SDK (Sprint 2: SIM Validation)
**Last Updated:** 2026-03-27
# Project Context - NT8 SDK
**Last Updated:** 2026-04-05
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.
Mission: ship safe, compile-stable NT8 trading software with strict scope control and durable governance documentation.
---
## Session Start (Mandatory)
- [ ] Read these files first, in order:
1. `docs/00-governance/executive_summary.md`
2. `docs/00-governance/current_status.md`
3. `docs/00-governance/active_work.md`
4. `docs/00-governance/architecture.md`
5. `docs/00-governance/roadmap.md`
- [ ] Treat `docs/00-governance/*` as canonical for current direction.
## Onboarding / First Read (Canonical Order)
## Historical/Contextual Sources (Non-Canonical)
Use only for background unless explicitly requested in task scope:
- `README.md`
- `docs/README.md`
- `PROJECT_HANDOVER.md`
- `DESIGNED_VS_IMPLEMENTED_GAP_ANALYSIS.md`
- `docs/INDEX.md`
For every new Kilo session, start with `docs/00-governance/` as the primary source of truth:
1. `docs/00-governance/executive_summary.md`
2. `docs/00-governance/current_status.md`
3. `docs/00-governance/active_work.md`
4. `docs/00-governance/architecture.md`
5. `docs/00-governance/roadmap.md`
## File Touch Scope (Hard Rules)
- [ ] Modify only files explicitly listed in the task spec.
- [ ] Do not edit adjacent files for cleanup or style-only changes.
- [ ] If a required fix is outside scope, stop and record a scope blocker.
- [ ] Respect `.kilocode/rules/file_boundaries.md` at all times.
`PROJECT_HANDOVER.md` and `DESIGNED_VS_IMPLEMENTED_GAP_ANALYSIS.md` are historical/contextual references only. Use them for background, not for authoritative current direction.
## Conversation Separation (Hard Rules)
- [ ] Strategy conversations: entry/exit logic and behavior.
- [ ] Architecture conversations: cross-component design and interfaces.
- [ ] Coding conversations: implementation details and diffs.
- [ ] Do not mix decisions across tracks; record architecture decisions in `docs/00-governance/decisions.md`.
---
## Documentation Sync Triggers
Update governance docs as part of normal task completion:
- [ ] Update `docs/00-governance/active_work.md` when current priorities, status, blockers, or ownership change.
- [ ] Update `docs/00-governance/roadmap.md` when milestone sequence, scope, or timing changes.
- [ ] Update `docs/00-governance/decisions.md` when a durable tradeoff or architecture decision is made.
- [ ] Update `CLEANUP_LOG.md` when cleanup debt is created, resolved, or deferred.
## 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
## Compatibility and Safety Baselines
- [ ] C# 5.0 only (`.kilocode/rules/csharp_50_syntax.md`).
- [ ] NT8 compile safety (`.kilocode/rules/ninjascript_guardrails.md`, `.kilocode/rules/nt8compilespec.md`).
- [ ] Verification gates (`.kilocode/rules/verification_requirements.md`).