Files
nt8-sdk/NT8_INTEGRATION_IMPLEMENTATION_PLAN.md
mo 0e36fe5d23
Some checks failed
Build and Test / build (push) Has been cancelled
feat: Complete Phase 5 Analytics & Reporting implementation
Analytics Layer (15 components):
- TradeRecorder: Full trade lifecycle tracking with partial fills
- PerformanceCalculator: Sharpe, Sortino, win rate, profit factor, expectancy
- PnLAttributor: Multi-dimensional attribution (grade/regime/time/strategy)
- DrawdownAnalyzer: Period detection and recovery metrics
- GradePerformanceAnalyzer: Grade-level edge analysis
- RegimePerformanceAnalyzer: Regime segmentation and transitions
- ConfluenceValidator: Factor validation and weighting optimization
- ReportGenerator: Daily/weekly/monthly reporting with export
- TradeBlotter: Real-time trade ledger with filtering
- ParameterOptimizer: Grid search and walk-forward scaffolding
- MonteCarloSimulator: Confidence intervals and risk-of-ruin
- PortfolioOptimizer: Multi-strategy allocation and portfolio metrics

Test Coverage (90 new tests):
- 240+ total tests, 100% pass rate
- >85% code coverage
- Zero new warnings

Project Status: Phase 5 complete (85% overall), ready for NT8 integration
2026-02-16 21:30:51 -05:00

23 KiB

NinjaTrader 8 Integration - Complete Implementation Plan

Project: NT8 SDK
Phase: NT8 Integration Layer
Date: February 17, 2026
Status: Planning → Implementation Ready
Estimated Time: 12-16 hours total


🎯 Objective

Build a complete, production-ready NinjaTrader 8 integration layer that enables the NT8 SDK to run strategies inside NinjaTrader 8 with full order execution, risk management, and performance tracking.

Success Criteria:

  • SimpleORB strategy compiles in NinjaTrader 8
  • Strategy can be enabled on a chart
  • Orders submit correctly to simulation account
  • Risk controls trigger appropriately
  • All 240+ existing tests still pass
  • Zero compilation warnings in NT8
  • Strategy runs for 1+ hours without errors

📋 Current State Assessment

What We Have

  • Core SDK: 20,000 lines of production code (Phases 0-5 complete)
  • Strategy Logic: SimpleORBStrategy fully implemented
  • Risk System: Multi-tier validation operational
  • Position Sizing: Multiple sizing methods working
  • Analytics: Complete performance tracking
  • Test Coverage: 240+ tests passing (100% pass rate)

What's Missing

  1. NT8 Strategy Base Class - Inherits from NinjaTrader's Strategy class
  2. Real Order Adapter - Actual NT8 order submission (not stubs)
  3. Data Adapter - NT8 bar/market data conversion
  4. Execution Adapter - Fill/update callback handling
  5. Deployment Automation - Script to copy files to NT8
  6. Minimal Test Strategy - Simple validation strategy

🏗️ Implementation Architecture

Layer Separation Strategy

┌─────────────────────────────────────────────────────────────┐
│                  NinjaTrader 8 Platform                      │
│  (Strategy base class, Order objects, Instrument, etc.)     │
└────────────────────┬────────────────────────────────────────┘
                     │
                     ↓ Inherits & Implements
┌─────────────────────────────────────────────────────────────┐
│              NT8StrategyBase (NEW)                           │
│  • Inherits: NinjaTrader.NinjaScript.Strategies.Strategy    │
│  • Implements: NT8 lifecycle (OnStateChange, OnBarUpdate)   │
│  • Bridges: NT8 events → SDK components                     │
│  • Location: Deployed directly to NT8 (not in DLL)          │
└────────────────────┬────────────────────────────────────────┘
                     │
                     ↓ Uses
┌─────────────────────────────────────────────────────────────┐
│              NT8ExecutionAdapter (NEW)                       │
│  • Order submission: SDK OrderRequest → NT8 EnterLong/Short │
│  • Order management: NT8 Order tracking                     │
│  • Fill handling: NT8 Execution → SDK OrderStatus           │
│  • Location: NT8.Adapters.dll                               │
└────────────────────┬────────────────────────────────────────┘
                     │
                     ↓ Coordinates
┌─────────────────────────────────────────────────────────────┐
│                  NT8.Core.dll                                │
│  • All SDK business logic (already complete)                │
│  • Risk, Sizing, OMS, Analytics, Intelligence               │
│  • Location: NT8 Custom\bin folder                          │
└─────────────────────────────────────────────────────────────┘

Why This Architecture?

  1. NT8StrategyBase deployed as .cs file - NT8 must compile it to access platform APIs
  2. NT8ExecutionAdapter in DLL - Reusable adapter logic, testable
  3. Core SDK in DLL - All business logic stays in tested, versioned SDK

📦 Deliverables (6 Major Components)

Component 1: NT8ExecutionAdapter.cs

Location: src/NT8.Adapters/NinjaTrader/NT8ExecutionAdapter.cs
Purpose: Bridge between SDK OrderRequest and NT8 Order objects
Time: 3-4 hours

Key Responsibilities:

  • Accept SDK OrderRequest, create NT8 Order objects
  • Submit orders via NT8 EnterLong(), EnterShort(), ExitLong(), ExitShort()
  • Track NT8 orders and map to SDK order IDs
  • Handle NT8 OnOrderUpdate() callbacks
  • Handle NT8 OnExecutionUpdate() callbacks
  • Thread-safe order state management

Interface:

public class NT8ExecutionAdapter
{
    // Submit order to NT8
    public string SubmitOrder(
        NinjaTrader.NinjaScript.Strategies.Strategy strategy,
        OrderRequest request);
    
    // Cancel order in NT8
    public bool CancelOrder(
        NinjaTrader.NinjaScript.Strategies.Strategy strategy,
        string orderId);
    
    // Process NT8 order update
    public void ProcessOrderUpdate(
        NinjaTrader.Cbi.Order order,
        double limitPrice,
        double stopPrice,
        int quantity,
        int filled,
        double averageFillPrice,
        NinjaTrader.Cbi.OrderState orderState,
        DateTime time,
        NinjaTrader.Cbi.ErrorCode errorCode,
        string nativeError);
    
    // Process NT8 execution
    public void ProcessExecution(
        NinjaTrader.Cbi.Execution execution);
    
    // Get order status
    public OrderStatus GetOrderStatus(string orderId);
}

Dependencies:

  • Requires reference to NinjaTrader.Core.dll
  • Requires reference to NinjaTrader.Cbi.dll
  • Uses SDK OrderRequest, OrderStatus, OrderState

Component 2: NT8DataAdapter.cs

Location: src/NT8.Adapters/NinjaTrader/NT8DataAdapter.cs
Purpose: Convert NT8 market data to SDK format
Time: 2 hours

Key Responsibilities:

  • Convert NT8 bars to SDK BarData
  • Convert NT8 account info to SDK AccountInfo
  • Convert NT8 position to SDK Position
  • Convert NT8 instrument to SDK Instrument

Interface:

public class NT8DataAdapter
{
    // Convert NT8 bar to SDK format
    public static BarData ConvertBar(
        NinjaTrader.Data.Bars bars,
        int barsAgo);
    
    // Convert NT8 account to SDK format
    public static AccountInfo ConvertAccount(
        NinjaTrader.Cbi.Account account);
    
    // Convert NT8 position to SDK format
    public static Position ConvertPosition(
        NinjaTrader.Cbi.Position position);
    
    // Build strategy context
    public static StrategyContext BuildContext(
        NinjaTrader.NinjaScript.Strategies.Strategy strategy,
        AccountInfo account,
        Position position);
}

Component 3: NT8StrategyBase.cs

Location: src/NT8.Adapters/Strategies/NT8StrategyBase.cs
Purpose: Base class for all NT8-integrated strategies
Time: 4-5 hours
Deployment: Copied to NT8 as .cs file (not compiled into DLL)

Key Responsibilities:

  • Inherit from NinjaTrader.NinjaScript.Strategies.Strategy
  • Implement NT8 lifecycle methods
  • Create and manage SDK components
  • Bridge NT8 events to SDK
  • Handle errors and logging

Lifecycle Implementation:

public abstract class NT8StrategyBase 
    : NinjaTrader.NinjaScript.Strategies.Strategy
{
    protected IStrategy _sdkStrategy;
    protected IRiskManager _riskManager;
    protected IPositionSizer _positionSizer;
    protected NT8ExecutionAdapter _executionAdapter;
    protected ILogger _logger;
    
    protected override void OnStateChange()
    {
        switch (State)
        {
            case State.SetDefaults:
                // Set strategy defaults
                break;
                
            case State.Configure:
                // Add data series, indicators
                break;
                
            case State.DataLoaded:
                // Initialize SDK components
                InitializeSdkComponents();
                break;
                
            case State.Historical:
            case State.Transition:
            case State.Realtime:
                // Strategy ready for trading
                break;
                
            case State.Terminated:
                // Cleanup
                break;
        }
    }
    
    protected override void OnBarUpdate()
    {
        if (CurrentBar < BarsRequiredToTrade) return;
        
        // Convert NT8 bar to SDK
        var barData = NT8DataAdapter.ConvertBar(Bars, 0);
        var context = NT8DataAdapter.BuildContext(this, account, position);
        
        // Call SDK strategy
        var intent = _sdkStrategy.OnBar(barData, context);
        
        if (intent != null)
        {
            ProcessIntent(intent, context);
        }
    }
    
    protected override void OnOrderUpdate(
        Order order, double limitPrice, double stopPrice,
        int quantity, int filled, double averageFillPrice,
        OrderState orderState, DateTime time,
        ErrorCode errorCode, string nativeError)
    {
        _executionAdapter.ProcessOrderUpdate(
            order, limitPrice, stopPrice, quantity, filled,
            averageFillPrice, orderState, time, errorCode, nativeError);
    }
    
    protected override void OnExecutionUpdate(
        Execution execution, string executionId,
        double price, int quantity,
        MarketPosition marketPosition, string orderId,
        DateTime time)
    {
        _executionAdapter.ProcessExecution(execution);
    }
    
    // Abstract methods for derived strategies
    protected abstract IStrategy CreateSdkStrategy();
    protected abstract void ConfigureStrategyParameters();
}

Component 4: SimpleORBNT8.cs

Location: src/NT8.Adapters/Strategies/SimpleORBNT8.cs
Purpose: Concrete SimpleORB implementation for NT8
Time: 1-2 hours
Deployment: Copied to NT8 as .cs file

Implementation:

public class SimpleORBNT8 : NT8StrategyBase
{
    #region User-Configurable Parameters
    
    [NinjaScriptProperty]
    [Display(Name = "Opening Range Minutes", GroupName = "Strategy")]
    public int OpeningRangeMinutes { get; set; }
    
    [NinjaScriptProperty]
    [Display(Name = "Std Dev Multiplier", GroupName = "Strategy")]
    public double StdDevMultiplier { get; set; }
    
    [NinjaScriptProperty]
    [Display(Name = "Stop Ticks", GroupName = "Risk")]
    public int StopTicks { get; set; }
    
    [NinjaScriptProperty]
    [Display(Name = "Target Ticks", GroupName = "Risk")]
    public int TargetTicks { get; set; }
    
    [NinjaScriptProperty]
    [Display(Name = "Daily Loss Limit", GroupName = "Risk")]
    public double DailyLossLimit { get; set; }
    
    #endregion
    
    protected override void OnStateChange()
    {
        if (State == State.SetDefaults)
        {
            Name = "Simple ORB NT8";
            Description = "Opening Range Breakout with SDK integration";
            Calculate = Calculate.OnBarClose;
            
            // Default parameters
            OpeningRangeMinutes = 30;
            StdDevMultiplier = 1.0;
            StopTicks = 8;
            TargetTicks = 16;
            DailyLossLimit = 1000.0;
        }
        
        base.OnStateChange();
    }
    
    protected override IStrategy CreateSdkStrategy()
    {
        return new NT8.Strategies.Examples.SimpleORBStrategy(
            OpeningRangeMinutes,
            StdDevMultiplier);
    }
    
    protected override void ConfigureStrategyParameters()
    {
        _strategyConfig.RiskSettings.DailyLossLimit = DailyLossLimit;
        _strategyConfig.RiskSettings.MaxTradeRisk = StopTicks * Instrument.MasterInstrument.PointValue;
    }
}

Component 5: MinimalTestStrategy.cs

Location: src/NT8.Adapters/Strategies/MinimalTestStrategy.cs
Purpose: Simple test strategy to validate integration
Time: 30 minutes

Implementation:

public class MinimalTestStrategy 
    : NinjaTrader.NinjaScript.Strategies.Strategy
{
    protected override void OnStateChange()
    {
        if (State == State.SetDefaults)
        {
            Name = "Minimal Test";
            Description = "Validates NT8 integration without SDK";
            Calculate = Calculate.OnBarClose;
        }
    }
    
    protected override void OnBarUpdate()
    {
        if (CurrentBar < 20) return;
        
        // Just log, no trading
        Print(string.Format("{0}: O={1:F2} H={2:F2} L={3:F2} C={4:F2} V={5}",
            Time[0].ToString("HH:mm:ss"),
            Open[0], High[0], Low[0], Close[0], Volume[0]));
    }
}

Component 6: Deploy-To-NT8.ps1

Location: deployment/Deploy-To-NT8.ps1
Purpose: Automate deployment to NinjaTrader 8
Time: 1 hour

Script:

# NT8 SDK Deployment Script
param(
    [switch]$BuildFirst = $true,
    [switch]$RunTests = $true,
    [switch]$CopyStrategies = $true
)

$ErrorActionPreference = "Stop"
$sdkRoot = "C:\dev\nt8-sdk"
$nt8Custom = "$env:USERPROFILE\Documents\NinjaTrader 8\bin\Custom"
$nt8Strategies = "$nt8Custom\Strategies"

Write-Host "NT8 SDK Deployment Script" -ForegroundColor Cyan
Write-Host "=" * 60

# Step 1: Build
if ($BuildFirst) {
    Write-Host "`n[1/5] Building SDK..." -ForegroundColor Yellow
    
    Push-Location $sdkRoot
    dotnet clean --configuration Release | Out-Null
    $buildResult = dotnet build --configuration Release
    
    if ($LASTEXITCODE -ne 0) {
        Write-Host "Build FAILED!" -ForegroundColor Red
        Pop-Location
        exit 1
    }
    
    Write-Host "Build succeeded" -ForegroundColor Green
    Pop-Location
}

# Step 2: Run Tests
if ($RunTests) {
    Write-Host "`n[2/5] Running tests..." -ForegroundColor Yellow
    
    Push-Location $sdkRoot
    $testResult = dotnet test --configuration Release --no-build
    
    if ($LASTEXITCODE -ne 0) {
        Write-Host "Tests FAILED!" -ForegroundColor Red
        Pop-Location
        exit 1
    }
    
    Write-Host "All tests passed" -ForegroundColor Green
    Pop-Location
}

# Step 3: Copy Core DLL
Write-Host "`n[3/5] Copying SDK DLLs..." -ForegroundColor Yellow

$coreDll = "$sdkRoot\src\NT8.Core\bin\Release\net48\NT8.Core.dll"
$corePdb = "$sdkRoot\src\NT8.Core\bin\Release\net48\NT8.Core.pdb"

Copy-Item $coreDll $nt8Custom -Force
Copy-Item $corePdb $nt8Custom -Force

Write-Host "Copied NT8.Core.dll and .pdb" -ForegroundColor Green

# Step 4: Copy Dependencies
Write-Host "`n[4/5] Copying dependencies..." -ForegroundColor Yellow

$depsPath = "$sdkRoot\src\NT8.Core\bin\Release\net48"
$deps = @(
    "Microsoft.Extensions.*.dll",
    "System.Memory.dll",
    "System.Buffers.dll"
)

foreach ($dep in $deps) {
    Get-ChildItem "$depsPath\$dep" -ErrorAction SilentlyContinue | 
        Copy-Item -Destination $nt8Custom -Force
}

Write-Host "Copied dependencies" -ForegroundColor Green

# Step 5: Copy Strategies
if ($CopyStrategies) {
    Write-Host "`n[5/5] Copying strategies..." -ForegroundColor Yellow
    
    $strategyFiles = @(
        "$sdkRoot\src\NT8.Adapters\Strategies\NT8StrategyBase.cs",
        "$sdkRoot\src\NT8.Adapters\Strategies\SimpleORBNT8.cs",
        "$sdkRoot\src\NT8.Adapters\Strategies\MinimalTestStrategy.cs"
    )
    
    foreach ($file in $strategyFiles) {
        if (Test-Path $file) {
            Copy-Item $file $nt8Strategies -Force
            Write-Host "  Copied $(Split-Path $file -Leaf)" -ForegroundColor Green
        }
    }
}

Write-Host "`n" + ("=" * 60) -ForegroundColor Cyan
Write-Host "Deployment Complete!" -ForegroundColor Green
Write-Host "`nNext steps:" -ForegroundColor Yellow
Write-Host "1. Open NinjaTrader 8"
Write-Host "2. Tools -> NinjaScript Editor (F5)"
Write-Host "3. Compile -> Compile All (F5)"
Write-Host "4. Verify compilation succeeds"
Write-Host "5. Create new strategy instance on chart"

🔄 Implementation Sequence

Phase A: Foundation (4-5 hours)

Goal: Build adapter infrastructure

  1. Create NT8DataAdapter.cs (2 hours)

    • Implement bar conversion
    • Implement account conversion
    • Implement position conversion
    • Implement context builder
    • Write unit tests (20+ tests)
  2. Create NT8ExecutionAdapter.cs (2-3 hours)

    • Implement order submission logic
    • Implement order state tracking
    • Implement callback processing
    • Write unit tests (30+ tests)

Verification:

dotnet test --filter "FullyQualifiedName~NT8DataAdapter"
dotnet test --filter "FullyQualifiedName~NT8ExecutionAdapter"

Phase B: Strategy Base (4-5 hours)

Goal: Build NT8 strategy base class

  1. Create NT8StrategyBase.cs (3-4 hours)

    • Implement state change lifecycle
    • Implement OnBarUpdate integration
    • Implement order callback handling
    • Add error handling and logging
    • Add component initialization
  2. Create SimpleORBNT8.cs (1 hour)

    • Implement concrete strategy
    • Add NT8 property decorators
    • Configure strategy parameters

Manual Verification:

  • Copy to NT8 Strategies folder
  • Open NinjaScript Editor
  • Verify no compilation errors

Phase C: Testing & Deployment (3-4 hours)

Goal: Validate and deploy

  1. Create MinimalTestStrategy.cs (30 min)

    • Simple logging strategy
    • No SDK dependencies
    • Validates NT8 integration basics
  2. Create Deploy-To-NT8.ps1 (1 hour)

    • Automate build
    • Automate file copying
    • Add verification steps
  3. Integration Testing (2-3 hours)

    • Deploy to NT8
    • Compile in NT8
    • Enable MinimalTestStrategy on chart (verify basic NT8 integration)
    • Enable SimpleORBNT8 on chart (verify full SDK integration)
    • Run on sim data for 1 hour
    • Verify risk controls
    • Verify order submission
    • Document any issues

Verification Checklist

Build Verification

  • dotnet build --configuration Release succeeds
  • dotnet test --configuration Release all 240+ tests pass
  • Zero build warnings for new adapter code
  • NT8.Core.dll builds successfully
  • Dependencies copy correctly

NT8 Compilation Verification

  • NinjaScript Editor opens without errors
  • "Compile All" succeeds with zero errors
  • Zero warnings for NT8StrategyBase.cs
  • Zero warnings for SimpleORBNT8.cs
  • MinimalTestStrategy.cs compiles
  • All strategies visible in strategy dropdown

Runtime Verification (Simulation)

  • MinimalTestStrategy enables on chart without errors
  • MinimalTestStrategy logs bars correctly
  • SimpleORBNT8 enables on chart without errors
  • SimpleORBNT8 initializes SDK components
  • Opening range calculated correctly
  • Risk validation triggers
  • Orders submit to simulation account
  • Fills process correctly
  • Stops and targets placed correctly
  • Strategy runs for 1+ hours without errors
  • Daily loss limit triggers correctly
  • Emergency flatten works

Performance Verification

  • OnBarUpdate executes in <200ms
  • Order submission in <5ms (excluding NT8)
  • No memory leaks over 1+ hour run
  • Thread-safe operation confirmed

📊 Success Metrics

Must Have (Release Blockers)

  • Zero compilation errors in NT8
  • Zero runtime exceptions for 1+ hours
  • All risk controls working correctly
  • Orders execute as expected
  • Position tracking accurate
  • All 240+ SDK tests still passing

Should Have (Quality Targets)

  • <200ms tick-to-trade latency
  • <5ms order submission time
  • 95%+ test coverage on new adapters
  • Zero memory leaks
  • Comprehensive error logging

Nice to Have (Future Enhancements)

  • Automated NT8 integration tests
  • Performance profiling tools
  • Replay testing framework
  • Multi-strategy coordination

🚨 Risk Mitigation

Critical Risks

Risk 1: NT8 API Changes

  • Mitigation: Reference exact NT8 version (8.0.20.1+)
  • Fallback: Version compatibility matrix

Risk 2: Thread Safety Issues

  • Mitigation: Comprehensive locking in adapters
  • Testing: Stress test with rapid order submission

Risk 3: Order State Synchronization

  • Mitigation: Correlation IDs for SDK↔NT8 mapping
  • Testing: Partial fill scenarios

Risk 4: Memory Leaks

  • Mitigation: Proper disposal in OnStateTerminated
  • Testing: Long-running tests (4+ hours)

Contingency Plans

If NT8 Compilation Fails:

  1. Deploy MinimalTestStrategy only (no SDK)
  2. Verify NT8 setup is correct
  3. Add SDK components incrementally
  4. Check DLL references

If Orders Don't Submit:

  1. Check connection status
  2. Verify account is in simulation
  3. Check NT8 error logs
  4. Validate order request format

If Performance Issues:

  1. Profile OnBarUpdate
  2. Reduce logging verbosity
  3. Optimize hot paths
  4. Consider async processing

📝 Development Notes

NT8-Specific Constraints

  1. Must use .NET Framework 4.8 (not .NET Core)
  2. Must use C# 5.0 syntax (no modern features)
  3. Strategy classes must be public and in correct namespace
  4. Properties need [NinjaScriptProperty] attribute for UI
  5. No async/await in OnBarUpdate (performance)
  6. Must not block NT8 UI thread (<200ms execution)

Coding Standards

All code must follow existing SDK patterns:

  • XML documentation on all public members
  • Comprehensive error handling
  • Defensive validation
  • Thread-safe operations
  • Logging at appropriate levels
  • Unit tests for all logic

📚 Reference Documentation


🎯 Next Steps

Immediate Actions (Today)

  1. Review this implementation plan
  2. Confirm approach and estimates
  3. ⏭️ Begin Phase A: Foundation (NT8DataAdapter)

This Week

  • Day 1: Phase A - Adapters (4-5 hours)
  • Day 2: Phase B - Strategy Base (4-5 hours)
  • Day 3: Phase C - Testing & Deployment (3-4 hours)
  • Day 4: Bug fixes and refinement (2-3 hours)
  • Day 5: Documentation and handoff (1-2 hours)

Success Criteria Met When:

  • SimpleORBNT8 runs successfully in NT8 simulation for 24+ hours
  • All risk controls validated
  • Zero critical bugs
  • Complete documentation
  • Deployment automated

Total Estimated Time: 12-16 hours
Critical Path: Phase A → Phase B → Phase C
Can Start Immediately: Yes, all dependencies documented


Let's build this properly and get NT8 SDK running in NinjaTrader! 🚀