Files
nt8-sdk/TASK_GAP050_ADVANCED_RISK_QTY_PROMPT.md
mo a99ce1d0e7 [GAP-050]: quantity-aware risk contract complete
- IRiskManager.cs: add quantity-aware ValidateOrder contract

- AdvancedRiskManager.cs: delegate Tier 1 through sized BasicRiskManager overload and preserve 3-arg behavior

- NT8StrategyBase.cs: remove BasicRiskManager type-check workaround and call sized interface method

- NT8OrderAdapter.cs: size before risk validation and pass actual contracts

- tests: add AdvancedRiskManager, NT8OrderAdapter, and NT8StrategyBase coverage

Build: clean | Tests: 566 passing; Core 476 passing | Dual-deployed: no - skipped by prompt
2026-08-25 16:39:56 -04:00

10 KiB

TASK: GAP-050 — MaxTradeRisk quantity term missing from IRiskManager contract

Staged: 2026-08-18 (v7.0 review, "raised, not yet prompted"), decided 2026-08-25 (Option A)

Sprint: Stage 1 — risk-interface hardening, no live-behaviour change (see THE DEFECT)

Owner: Kilocode

Deploy: COMMIT ONLY — do NOT run deploy-to-nt8.bat or copy build output to

the live NT8 install as part of this task. Verify-build and commit

as usual; leave the deploy step for the operator to run manually

once ready.

─────────────────────────────────────────────────────────────────

THE DEFECT

MaxTradeRisk was originally flagged as "no quantity term — worst case is $600 against a $1,000 daily limit." That framing is stale for the live path — verify this yourself in READ-FIRST before touching anything — but the underlying interface gap is real and worth closing properly.

Current state, confirmed by direct source read on 2026-08-25:

  1. BasicRiskManager has TWO overloads: ValidateOrder(intent, context, config) (3-arg, hardcodes candidateContracts=1 by calling the 4-arg version) and ValidateOrder(intent, context, config, candidateContracts) (4-arg, real quantity — CalculateTradeRisk multiplies StopTicks * tickValue * candidateContracts).
  2. IRiskManager (src/NT8.Core/Risk/IRiskManager.cs) only declares the 3-arg ValidateOrder(intent, context, config). The 4-arg overload is NOT part of the interface — it only exists as a concrete method on BasicRiskManager.
  3. The live path is already safe, but only via a workaround, not the interface: SimpleORBNT8/NT8StrategyBase (NT8StrategyBase.cs:1763) constructs _riskManager = new BasicRiskManager(...) directly, and the order-eval flow (~2131-2136) does var basicRiskManager = _riskManager as BasicRiskManager; var riskDecision = basicRiskManager != null ? basicRiskManager.ValidateOrder(intent, context, _riskConfig, sizingResult.Contracts) : _riskManager.ValidateOrder (intent, context, _riskConfig); — a runtime type-check working around the interface's own gap, only correct today because nothing else is plugged into _riskManager.
  4. AdvancedRiskManager (src/NT8.Core/Risk/AdvancedRiskManager.cs) implements IRiskManager but has no 4-arg ValidateOrder at all. Its 3-arg ValidateOrder delegates Tier 1 to its inner _basicRiskManager via the 1-contract-assumed 3-arg overload. Confirmed not currently wired to anything live — new AdvancedRiskManager( does not appear anywhere in src/ outside its own file; the only places it is constructed are tests/NT8.Core.Tests/Risk/AdvancedRiskManagerTests.cs and tests/NT8.Integration.Tests/RiskSizingIntegrationTests.cs, both of which build it directly and call it in isolation.
  5. NT8OrderAdapter.ExecuteIntent (src/NT8.Adapters/NinjaTrader/ NT8OrderAdapter.cs) calls _riskManager.ValidateOrder(intent, context, config.RiskSettings) (3-arg) before calling _positionSizer. CalculateSize(...) — backwards even if the interface supported quantity, since the real size isn't known yet at the point risk is checked. NT8OrderAdapter/NT8Adapter are, in turn, only constructed by BaseNT8StrategyWrapper (src/NT8.Adapters/Wrappers/ BaseNT8StrategyWrapper.cs), whose own doc comment says: "This is a template that would be extended in actual NT8 strategy files" — nothing in src/ extends it. Confirmed not on the live path.

Net: today's SIM/live trading is not exposed. The gap is an interface contract hole that happens to be papered over by a type-check in the one place that matters right now, plus two pieces of unwired scaffolding that would silently reintroduce the 1-contract assumption the moment either is ever wired up. Fix it at the interface so this can't recur by accident.

READ-FIRST

  1. src/NT8.Core/Risk/IRiskManager.cs — current interface, 3-arg only.
  2. src/NT8.Core/Risk/BasicRiskManager.cs (~lines 40-90) — the existing 3-arg-calls-4-arg pattern to mirror.
  3. src/NT8.Core/Risk/AdvancedRiskManager.cs (full file) — constructor wraps a real BasicRiskManager; existing 3-arg ValidateOrder runs Tier 1 via the inner basic manager, then Tier 2 (weekly loss, trailing drawdown), Tier 3 (cross-strategy exposure, time restrictions, correlation), and Tier 3.5 (portfolio heat, GAP-019) — all unchanged by this task.
  4. src/NT8.Adapters/Strategies/NT8StrategyBase.cs ~2095-2145 — the sizing-then-risk-check flow and the as BasicRiskManager ternary this task removes.
  5. src/NT8.Adapters/NinjaTrader/NT8OrderAdapter.cs (full file, short) — the ExecuteIntent validate-before-size ordering this task fixes.
  6. Re-run the greps yourself before starting — confirm new AdvancedRiskManager( and new NT8OrderAdapter( / new NT8Adapter( / new BaseNT8StrategyWrapper( / new SimpleORBNT8Wrapper( still don't appear anywhere in src/ outside their own definitions and test files. If that has changed since 2026-08-25, stop and flag it — the risk profile of this task changes if either is now live.

THE CHANGE — Option A (decided by operator 2026-08-25)

  1. IRiskManager.cs: add RiskDecision ValidateOrder(StrategyIntent intent, StrategyContext context, RiskConfig config, int candidateContracts); to the interface. Keep the existing 3-arg member too (still required by both implementations for source/test compatibility).

  2. BasicRiskManager.cs: no logic change — it already has both signatures with the exact right behaviour. Just confirm it still compiles as an IRiskManager after step 1 (it should, with zero edits).

  3. AdvancedRiskManager.cs: add the 4-arg ValidateOrder(intent, context, config, candidateContracts). It should run Tier 1 via _basicRiskManager.ValidateOrder(intent, context, config, candidateContracts) (the real, quantity-aware overload) instead of the 3-arg one, then fall through to the exact same Tier 2/3/3.5 body the existing 3-arg method already has (do not duplicate that logic — factor it into a shared private method both the 3-arg and 4-arg public methods call, or have the 3-arg method call the new 4-arg one with candidateContracts=1, mirroring BasicRiskManager's own pattern exactly — your call which is cleaner, but no behavioural duplication/ drift between the two).

  4. NT8StrategyBase.cs ~2131-2136: remove the as BasicRiskManager cast and fallback entirely. Replace with a direct call: var riskDecision = _riskManager.ValidateOrder(intent, context, _riskConfig, sizingResult.Contracts); — now safe for any IRiskManager implementation, not just BasicRiskManager. This must produce byte- identical behaviour for the live path (currently always a BasicRiskManager instance) — same underlying 4-arg call, just reached without the type-check.

  5. NT8OrderAdapter.cs ExecuteIntent: reorder so _positionSizer. CalculateSize(...) runs first, then call the 4-arg ValidateOrder with the real sizingResult.Contracts instead of the 3-arg call before sizing. Keep the existing sizingResult.Contracts <= 0 early-return, just move it ahead of the risk check.

DO NOT

  • Do not change BasicRiskManager's existing logic — it's already correct and is the class actually protecting live trading today.
  • Do not change any of AdvancedRiskManager's Tier 2/3/3.5 check bodies (ValidateWeeklyLimit, ValidateTrailingDrawdown, ValidateCrossStrategyExposure, ValidateTimeRestrictions, ValidateCorrelationLimits, ValidatePortfolioHeat) — only how Tier 1 is invoked changes.
  • Do not wire AdvancedRiskManager or NT8OrderAdapter/NT8Adapter/ BaseNT8StrategyWrapper into the live SimpleORBNT8/NT8StrategyBase construction path. That is a separate, much larger decision (switching which risk manager actually governs live trading) and is explicitly out of scope for this task — this task only guarantees correctness if that ever happens, it does not make it happen.
  • Do not touch src/_archive/legacy-orders/OrderManager.cs — archived, out of scope.
  • Do not touch RiskConfig/AdvancedRiskConfig field shapes.

TESTS (required)

  1. New test(s) in AdvancedRiskManagerTests.cs proving the 4-arg ValidateOrder correctly scales rejected/allowed decisions by candidateContracts (mirror the existing quantity-aware tests in BasicRiskManagerTests.cs if any exist — same shape, e.g. a trade that passes at qty=1 but is correctly rejected at qty=3 once StopTicks * tickValue * 3 > MaxTradeRisk).
  2. A test proving AdvancedRiskManager's 3-arg ValidateOrder still behaves exactly as before (backward-compatibility / no regression for existing callers, including RiskSizingIntegrationTests.cs).
  3. A test proving NT8OrderAdapter.ExecuteIntent now calls ValidateOrder with the actual sized quantity, not a hardcoded 1 (construct with a test-double bridge/risk manager/sizer and assert the call).
  4. Confirm the full existing suite still passes, including RiskSizingIntegrationTests.cs and anything covering the NT8StrategyBase.cs ternary area (source-text assertion is fine there, consistent with this codebase's established precedent for NinjaScript-derived classes that can't be instantiated directly).

VALIDATION (operator)

None required for correctness — the live path (BasicRiskManager reached directly by SimpleORBNT8) is unchanged in behaviour, just reached via the interface instead of a type-check. AdvancedRiskManager/NT8OrderAdapter remain unwired to live trading after this task. No SIM/gate validation needed before committing; deploy is still an explicit operator step per the header above.

GOVERNANCE

Track as [GAP-050] in SPRINT_BOARD.md — promote out of the "operator decision needed" catch-all line into its own entry, Option A implemented, noting the live path was already safe and this closes the interface contract gap plus the two unwired call sites. Update docs/00-governance/decisions.md with a new dated entry recording Option A and the implementation, referencing this task. Reference: this task, DEC-20260825-03 (operator decision + investigation findings), original v7.0 review staging (SPRINT_BOARD.md "Also raised by the review, not yet prompted").