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,115 @@
using System;
using NT8.Core.Common.Models;
using NT8.Core.Risk;
using NT8.Core.Sizing;
namespace NT8.Adapters.NinjaTrader
{
/// <summary>
/// Order adapter for executing trades through NT8
/// </summary>
public class NT8OrderAdapter
{
private IRiskManager _riskManager;
private IPositionSizer _positionSizer;
/// <summary>
/// Initialize the order adapter with required components
/// </summary>
public void Initialize(IRiskManager riskManager, IPositionSizer positionSizer)
{
_riskManager = riskManager;
_positionSizer = positionSizer;
}
/// <summary>
/// Execute strategy intent through NT8 order management
/// </summary>
public void ExecuteIntent(StrategyIntent intent, StrategyContext context, StrategyConfig config)
{
if (_riskManager == null || _positionSizer == null)
{
throw new InvalidOperationException("Adapter not initialized. Call Initialize() first.");
}
// Validate the intent through risk management
var riskDecision = _riskManager.ValidateOrder(intent, context, config.RiskSettings);
if (!riskDecision.Allow)
{
// Log rejection and return
// In a real implementation, we would use a proper logging system
return;
}
// Calculate position size
var sizingResult = _positionSizer.CalculateSize(intent, context, config.SizingSettings);
if (sizingResult.Contracts <= 0)
{
// Log that no position size was calculated
return;
}
// In a real implementation, this would call NT8's order execution methods
// For now, we'll just log what would be executed
ExecuteInNT8(intent, sizingResult);
}
/// <summary>
/// Execute the order in NT8 (placeholder implementation)
/// </summary>
private void ExecuteInNT8(StrategyIntent intent, SizingResult sizing)
{
// This is where the actual NT8 order execution would happen
// In a real implementation, this would call NT8's EnterLong/EnterShort methods
// along with SetStopLoss, SetProfitTarget, etc.
// Example of what this might look like in NT8:
/*
if (intent.Side == OrderSide.Buy)
{
EnterLong(sizing.Contracts, "SDK_Entry");
SetStopLoss("SDK_Entry", CalculationMode.Ticks, intent.StopTicks);
if (intent.TargetTicks.HasValue)
{
SetProfitTarget("SDK_Entry", CalculationMode.Ticks, intent.TargetTicks.Value);
}
}
else if (intent.Side == OrderSide.Sell)
{
EnterShort(sizing.Contracts, "SDK_Entry");
SetStopLoss("SDK_Entry", CalculationMode.Ticks, intent.StopTicks);
if (intent.TargetTicks.HasValue)
{
SetProfitTarget("SDK_Entry", CalculationMode.Ticks, intent.TargetTicks.Value);
}
}
*/
}
/// <summary>
/// Handle order updates from NT8
/// </summary>
public void OnOrderUpdate(string orderId, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, string orderState, DateTime time, string errorCode, string nativeError)
{
// Pass order updates to risk manager for tracking
if (_riskManager != null)
{
// In a real implementation, we would convert NT8 order data to SDK format
// and pass it to the risk manager
}
}
/// <summary>
/// Handle execution updates from NT8
/// </summary>
public void OnExecutionUpdate(string executionId, string orderId, double price, int quantity, string marketPosition, DateTime time)
{
// Pass execution updates to risk manager for P&L tracking
if (_riskManager != null)
{
// In a real implementation, we would convert NT8 execution data to SDK format
// and pass it to the risk manager
}
}
}
}