Files
nt8-sdk/docs/archive/completed-tasks/TASK-02-circuit-breaker.md
mo c9041df36a Sprint 3: Production Hardening — 9 tasks completed
## Summary
Implemented 9 production-hardening tasks across risk, execution, calendar, and governance.
All changes verified: build clean (0 errors), 492 tests passing (404 core + 78 integration + 10 perf).

## Core Changes

### Risk & Portfolio (3 tasks)
- **GAP-019** Portfolio Heat Tier 3.5: Added MaxHeatPct (default 3%) to AdvancedRiskConfig;
  ValidatePortfolioHeat() computes TotalOpenRisk + NewTradeRisk / Equity * 100; rejects when exceeded.
- **GAP-003** RiskPerTrade <= MaxTradeRisk Assertion: RiskPerTrade property in RiskConfig; throws
  ArgumentException at ValidateOrder() if RiskPerTrade > MaxTradeRisk; 3 unit tests.
- **GAP-005** GetRiskStatus() Live Config: _currentDailyLossLimit field updated from config.DailyLossLimit
  on each ValidateOrder(); GetRiskStatus() returns actual value; 3 unit tests.

### Execution & Position Management (3 tasks)
- **TASK-06a** Runner Mode Architecture: RunnerMode enum (Auto/Mode1DualLeg/Mode2RunnerOnly/
  Mode3ScalerOnly/Disabled); ResolveRunnerMode() logic; 4-mode SubmitOrderToNT8() behavior.
- **TASK-06b** ATR Dynamic Stop: EnableDynamicStop, AtrPeriod, AtrMultiplier, MinStopTicks, MaxStopTicks;
  computes dynStop = clamp(round(ATR/TickSize * Mult), Min, Max); overrides intent.StopTicks.
- **GAP-006** Position-Close Re-arm: OnPositionUpdate() sends clear_trade_taken when flat;
  EnableBarBasedRearm parameter gates legacy bar-driven re-arm (default false for live).

### Strategy Intelligence (2 tasks)
- **GAP-004** orbRangeTicks Fix: DailyBarContext.OrbRangeTicks field; computed from actual ORB
  high/low / TickSize; passed to Factor 7 (ORBvsATR).
- **GAP-010** Short-Side Regime Filter: EnableShortTrendFilter param; vetoes shorts when
  AVWAP slope > 0 (bullish); configurable, default true.

### Calendar & Trailing (2 tasks)
- **GAP-008** CME Holiday Awareness: NEW CmeHolidayCalendar.cs with 9 observed holidays +
  Good Friday (Easter calc); EnableCMEHolidayGuard skips ORB window on holidays.
- **GAP-009** ATR Trailing Real ATR: EnableATRTrailing, ATRTrailingPeriod (14), ATRTrailingMultiplier (2.0),
  ATRTrailingMinTicks (8), ATRTrailingMaxTicks (60); computes trailTicks from ATR(Close,Period)[0].

## Governance
- SPRINT_BOARD.md, current_status.md, decisions.md (DEC-20260711-01 through -09), NEXT_PROMPT.md updated
- All 9 tasks moved ACTIVE -> COMPLETED; GAP-011 (Gitea CI) now ACTIVE

## Verification
- verify-build.bat: 0 errors, 0 warnings
- Tests: 492 passing (404 core + 78 integration + 10 performance)

## Files
- Modified: 13 source, 3 test, 9 governance
- Deleted: 28 obsolete spec/docs
- Added: 1 new module (NT8.Core.Calendar), governance files
- Dual-deployed to NT8 Custom\Strategies\

## Rollback
Tag: sprint3-production-hardening-complete
To revert: git reset --hard sprint3-production-hardening-complete
2026-07-13 08:38:25 -04:00

4.4 KiB

TASK-02: Wire ExecutionCircuitBreaker into NT8StrategyBase

File: src/NT8.Adapters/Strategies/NT8StrategyBase.cs Priority: CRITICAL Depends on: TASK-01 must be done first (file already open/modified) Estimated time: 45 min


Background

ExecutionCircuitBreaker at src/NT8.Core/Execution/ExecutionCircuitBreaker.cs is complete and tested. Its public API is:

  • bool ShouldAllowOrder() — returns false when circuit is Open
  • void OnSuccess() — call after a successful order submission
  • void OnFailure() — call after a failed order submission
  • void RecordOrderRejection(string reason) — call when NT8 rejects an order
  • void Reset() — resets to Closed state

The ExecutionCircuitBreaker constructor:

public ExecutionCircuitBreaker(
    ILogger<ExecutionCircuitBreaker> logger,
    int failureThreshold = 3,
    TimeSpan? timeout = null,
    TimeSpan? retryTimeout = null,
    int latencyWindowSize = 100,
    int rejectionWindowSize = 10)

Problem: It is never instantiated. NT8StrategyBase submits orders with no circuit breaker gate.


Exact Changes Required

1. Add using statement at top of NT8StrategyBase.cs

using NT8.Core.Execution;
using Microsoft.Extensions.Logging.Abstractions;

2. Add private field alongside the other private fields

private ExecutionCircuitBreaker _circuitBreaker;

3. Initialize in InitializeSdkComponents(), after _positionSizer = new BasicPositionSizer(_logger);

_circuitBreaker = new ExecutionCircuitBreaker(
    NullLogger<ExecutionCircuitBreaker>.Instance,
    failureThreshold: 3,
    timeout: TimeSpan.FromSeconds(30));

4. Gate SubmitOrderToNT8() — add check at top of the method

private void SubmitOrderToNT8(OmsOrderRequest request, StrategyIntent intent)
{
    // Circuit breaker gate
    if (_circuitBreaker != null && !_circuitBreaker.ShouldAllowOrder())
    {
        var state = _circuitBreaker.GetState();
        Print(string.Format("[SDK] Circuit breaker OPEN — order blocked: {0}", state.Reason));
        if (_logger != null)
            _logger.LogWarning("Circuit breaker blocked order: {0}", state.Reason);
        return;
    }

    try
    {
        // ... EXISTING submit logic unchanged ...
        var orderName = string.Format("SDK_{0}_{1}", intent.Symbol, DateTime.Now.Ticks);
        _executionAdapter.SubmitOrder(request, orderName);

        if (request.Side == OmsOrderSide.Buy)
        { ... existing EnterLong/EnterLongLimit/etc ... }
        else if (request.Side == OmsOrderSide.Sell)
        { ... existing EnterShort/etc ... }

        if (intent.StopTicks > 0)
            SetStopLoss(orderName, CalculationMode.Ticks, (int)intent.StopTicks, false);

        if (intent.TargetTicks.HasValue && intent.TargetTicks.Value > 0)
            SetProfitTarget(orderName, CalculationMode.Ticks, (int)intent.TargetTicks.Value);

        // Mark success after submission
        if (_circuitBreaker != null)
            _circuitBreaker.OnSuccess();
    }
    catch (Exception ex)
    {
        if (_circuitBreaker != null)
            _circuitBreaker.OnFailure();

        Print(string.Format("[SDK] SubmitOrderToNT8 failed: {0}", ex.Message));
        if (_logger != null)
            _logger.LogError("SubmitOrderToNT8 failed: {0}", ex.Message);
        throw;
    }
}

5. Wire rejections in OnOrderUpdate()

In the existing OnOrderUpdate() override, after the null/name checks, add:

// Record NT8 rejections in circuit breaker
if (orderState == NinjaTrader.Cbi.OrderState.Rejected && _circuitBreaker != null)
{
    var reason = string.Format("{0} {1}", errorCode, nativeError ?? string.Empty);
    _circuitBreaker.RecordOrderRejection(reason);
    Print(string.Format("[SDK] Order rejected by NT8: {0}", reason));
}

Acceptance Criteria

  • ExecutionCircuitBreaker is instantiated in InitializeSdkComponents()
  • SubmitOrderToNT8() checks ShouldAllowOrder() before submitting — if false, prints message and returns
  • OnOrderUpdate() calls RecordOrderRejection() when orderState == OrderState.Rejected
  • OnSuccess() called after successful order submission
  • OnFailure() called in catch block
  • verify-build.bat passes with zero errors
  • Existing 240+ tests still pass: dotnet test NT8-SDK.sln --verbosity minimal

Do NOT Change

  • ExecutionCircuitBreaker.cs — already correct, just use it
  • Any Core layer files
  • Any test files