Implement NinjaTrader 8 adapter for integration
Some checks failed
Build and Test / build (push) Has been cancelled

This commit is contained in:
Billy Valentine
2025-09-09 17:19:14 -04:00
parent 92f3732b3d
commit 63200fe9b4
7 changed files with 671 additions and 0 deletions

View File

@@ -0,0 +1,159 @@
using System;
using System.Collections.Generic;
using NT8.Core.Common.Interfaces;
using NT8.Core.Common.Models;
using NT8.Core.Risk;
using NT8.Core.Sizing;
using NT8.Adapters.NinjaTrader;
namespace NT8.Adapters.Wrappers
{
/// <summary>
/// Base wrapper class for NT8 strategies that integrate with the SDK
/// This is a template that would be extended in actual NT8 strategy files
/// </summary>
public abstract class BaseNT8StrategyWrapper
{
#region SDK Components
protected IStrategy _sdkStrategy;
protected IRiskManager _riskManager;
protected IPositionSizer _positionSizer;
protected NT8Adapter _nt8Adapter;
protected StrategyConfig _strategyConfig;
#endregion
#region Properties
/// <summary>
/// Stop loss in ticks
/// </summary>
public int StopTicks { get; set; }
/// <summary>
/// Profit target in ticks (optional)
/// </summary>
public int TargetTicks { get; set; }
/// <summary>
/// Risk amount per trade in dollars
/// </summary>
public double RiskAmount { get; set; }
#endregion
#region Constructor
/// <summary>
/// Constructor for BaseNT8StrategyWrapper
/// </summary>
public BaseNT8StrategyWrapper()
{
// Set default values
StopTicks = 10;
TargetTicks = 20;
RiskAmount = 100.0;
// Initialize SDK components
InitializeSdkComponents();
}
#endregion
#region Abstract Methods
/// <summary>
/// Create the SDK strategy implementation
/// </summary>
protected abstract IStrategy CreateSdkStrategy();
#endregion
#region Public Methods
/// <summary>
/// Process a bar update (would be called from NT8's OnBarUpdate)
/// </summary>
public void ProcessBarUpdate(BarData barData, StrategyContext context)
{
// Call SDK strategy logic
var intent = _sdkStrategy.OnBar(barData, context);
if (intent != null)
{
// Convert SDK results to NT8 actions
ExecuteIntent(intent, context);
}
}
#endregion
#region Private Methods
/// <summary>
/// Initialize SDK components
/// </summary>
private void InitializeSdkComponents()
{
// In a real implementation, these would be injected or properly instantiated
// For now, we'll create placeholder instances
_riskManager = null; // This would be properly instantiated
_positionSizer = null; // This would be properly instantiated
// Create NT8 adapter
_nt8Adapter = new NT8Adapter();
_nt8Adapter.Initialize(_riskManager, _positionSizer);
// Create SDK strategy
_sdkStrategy = CreateSdkStrategy();
}
/// <summary>
/// Create SDK configuration from parameters
/// </summary>
private void CreateSdkConfiguration()
{
// Create risk configuration
var riskConfig = new RiskConfig(
dailyLossLimit: 500.0,
maxTradeRisk: RiskAmount,
maxOpenPositions: 5,
emergencyFlattenEnabled: true
);
// Create sizing configuration
var sizingConfig = new SizingConfig(
method: SizingMethod.FixedDollarRisk,
minContracts: 1,
maxContracts: 100,
riskPerTrade: RiskAmount,
methodParameters: new Dictionary<string, object>()
);
// Create strategy configuration
_strategyConfig = new StrategyConfig(
name: "NT8Strategy",
symbol: "Unknown",
parameters: new Dictionary<string, object>(),
riskSettings: riskConfig,
sizingSettings: sizingConfig
);
}
/// <summary>
/// Execute strategy intent through NT8
/// </summary>
private void ExecuteIntent(StrategyIntent intent, StrategyContext context)
{
// Calculate position size
var sizingResult = _positionSizer != null ?
_positionSizer.CalculateSize(intent, context, _strategyConfig.SizingSettings) :
new SizingResult(1, RiskAmount, SizingMethod.FixedDollarRisk, new Dictionary<string, object>());
// Execute through NT8 adapter
_nt8Adapter.ExecuteIntent(intent, sizingResult);
}
#endregion
}
}

View File

@@ -0,0 +1,118 @@
using System;
using System.Collections.Generic;
using NT8.Core.Common.Interfaces;
using NT8.Core.Common.Models;
using NT8.Core.Logging;
namespace NT8.Adapters.Wrappers
{
/// <summary>
/// Simple ORB (Opening Range Breakout) strategy wrapper for NT8
/// This demonstrates how to implement a strategy that works with the SDK
/// </summary>
public class SimpleORBNT8Wrapper : BaseNT8StrategyWrapper
{
#region Strategy Parameters
/// <summary>
/// Opening range period in minutes
/// </summary>
public int OpeningRangeMinutes { get; set; }
/// <summary>
/// Number of standard deviations for breakout threshold
/// </summary>
public double StdDevMultiplier { get; set; }
#endregion
#region Strategy State
private DateTime _openingRangeStart;
private double _openingRangeHigh;
private double _openingRangeLow;
private bool _openingRangeCalculated;
private double _rangeSize;
#endregion
#region Constructor
/// <summary>
/// Constructor for SimpleORBNT8Wrapper
/// </summary>
public SimpleORBNT8Wrapper()
{
OpeningRangeMinutes = 30;
StdDevMultiplier = 1.0;
_openingRangeCalculated = false;
}
#endregion
#region Base Class Implementation
/// <summary>
/// Create the SDK strategy implementation
/// </summary>
protected override IStrategy CreateSdkStrategy()
{
return new SimpleORBStrategy();
}
#endregion
#region Strategy Logic
/// <summary>
/// Simple ORB strategy implementation
/// </summary>
private class SimpleORBStrategy : IStrategy
{
public StrategyMetadata Metadata { get; private set; }
public SimpleORBStrategy()
{
Metadata = new StrategyMetadata(
name: "Simple ORB",
description: "Opening Range Breakout strategy",
version: "1.0",
author: "NT8 SDK Team",
symbols: new string[] { "ES", "NQ", "YM" },
requiredBars: 20
);
}
public void Initialize(StrategyConfig config, IMarketDataProvider dataProvider, ILogger logger)
{
// Initialize strategy with configuration
// In a real implementation, we would store references to the data provider and logger
}
public StrategyIntent OnBar(BarData bar, StrategyContext context)
{
// This is where the actual strategy logic would go
// For this example, we'll just return null to indicate no trade
return null;
}
public StrategyIntent OnTick(TickData tick, StrategyContext context)
{
// Most strategies don't need tick-level logic
return null;
}
public Dictionary<string, object> GetParameters()
{
return new Dictionary<string, object>();
}
public void SetParameters(Dictionary<string, object> parameters)
{
// Set strategy parameters from configuration
}
}
#endregion
}
}