[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
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace NT8.Core.Tests.Adapters
|
||||
{
|
||||
[TestClass]
|
||||
public class NT8StrategyBaseRiskContractTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void ProcessStrategyIntent_UsesQuantityAwareRiskInterfaceCall()
|
||||
{
|
||||
string source = File.ReadAllText(FindSourceFile());
|
||||
string method = ExtractMethodBlock(source, "private void ProcessStrategyIntent");
|
||||
|
||||
Assert.IsTrue(method.Contains("_riskManager.ValidateOrder(intent, context, _riskConfig, sizingResult.Contracts)"), "Sized risk validation must use the IRiskManager quantity-aware overload.");
|
||||
Assert.IsFalse(method.Contains("_riskManager as BasicRiskManager"), "Sized risk validation must not depend on a BasicRiskManager type check.");
|
||||
}
|
||||
|
||||
private static string FindSourceFile()
|
||||
{
|
||||
DirectoryInfo dir = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
|
||||
while (dir != null)
|
||||
{
|
||||
string candidate = Path.Combine(dir.FullName, "src", "NT8.Adapters", "Strategies", "NT8StrategyBase.cs");
|
||||
if (File.Exists(candidate))
|
||||
return candidate;
|
||||
dir = dir.Parent;
|
||||
}
|
||||
|
||||
throw new FileNotFoundException("Could not locate src\\NT8.Adapters\\Strategies\\NT8StrategyBase.cs from test output directory.");
|
||||
}
|
||||
|
||||
private static string ExtractMethodBlock(string source, string signature)
|
||||
{
|
||||
int start = source.IndexOf(signature, StringComparison.Ordinal);
|
||||
if (start < 0)
|
||||
Assert.Fail("Source does not contain method: " + signature);
|
||||
|
||||
int brace = source.IndexOf("{", start, StringComparison.Ordinal);
|
||||
if (brace < 0)
|
||||
Assert.Fail("Method has no opening brace: " + signature);
|
||||
|
||||
int depth = 0;
|
||||
for (int i = brace; i < source.Length; i++)
|
||||
{
|
||||
if (source[i] == '{')
|
||||
depth++;
|
||||
else if (source[i] == '}')
|
||||
{
|
||||
depth--;
|
||||
if (depth == 0)
|
||||
return source.Substring(start, i - start + 1);
|
||||
}
|
||||
}
|
||||
|
||||
Assert.Fail("Method has no closing brace: " + signature);
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -182,6 +182,77 @@ namespace NT8.Core.Tests.Risk
|
||||
Assert.AreEqual(RiskLevel.Medium, result.RiskLevel);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ValidateOrder_WithCandidateContracts_RejectedWhenSizedRiskExceedsLimit()
|
||||
{
|
||||
// Arrange
|
||||
var manager = CreateManager(
|
||||
weeklyLossLimit: 100000,
|
||||
trailingDrawdownLimit: 50000,
|
||||
maxCrossStrategyExposure: 100000,
|
||||
maxCorrelatedExposure: 100000,
|
||||
tradingTimeWindows: new List<TradingTimeWindow>());
|
||||
|
||||
var intent = TestDataBuilder.CreateValidIntent(symbol: "NQ", stopTicks: 20);
|
||||
var context = TestDataBuilder.CreateTestContext(symbol: "NQ");
|
||||
var config = new RiskConfig(1000.0, 200.0, 5, true);
|
||||
|
||||
// Act
|
||||
var result = manager.ValidateOrder(intent, context, config, 3);
|
||||
|
||||
// Assert
|
||||
Assert.IsFalse(result.Allow);
|
||||
Assert.IsTrue(result.RejectReason.Contains("Trade risk too high"));
|
||||
Assert.AreEqual(3, result.RiskMetrics["contracts"]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ValidateOrder_WithCandidateContracts_AllowsSameIntentAtOneContract()
|
||||
{
|
||||
// Arrange
|
||||
var manager = CreateManager(
|
||||
weeklyLossLimit: 100000,
|
||||
trailingDrawdownLimit: 50000,
|
||||
maxCrossStrategyExposure: 100000,
|
||||
maxCorrelatedExposure: 100000,
|
||||
tradingTimeWindows: new List<TradingTimeWindow>());
|
||||
|
||||
var intent = TestDataBuilder.CreateValidIntent(symbol: "NQ", stopTicks: 20);
|
||||
var context = TestDataBuilder.CreateTestContext(symbol: "NQ");
|
||||
var config = new RiskConfig(1000.0, 200.0, 5, true);
|
||||
|
||||
// Act
|
||||
var result = manager.ValidateOrder(intent, context, config, 1);
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(result.Allow, result.RejectReason);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ValidateOrder_ThreeArgumentOverload_RetainsOneContractBehavior()
|
||||
{
|
||||
// Arrange
|
||||
var manager = CreateManager(
|
||||
weeklyLossLimit: 100000,
|
||||
trailingDrawdownLimit: 50000,
|
||||
maxCrossStrategyExposure: 100000,
|
||||
maxCorrelatedExposure: 100000,
|
||||
tradingTimeWindows: new List<TradingTimeWindow>());
|
||||
|
||||
var intent = TestDataBuilder.CreateValidIntent(symbol: "NQ", stopTicks: 20);
|
||||
var context = TestDataBuilder.CreateTestContext(symbol: "NQ");
|
||||
var config = new RiskConfig(1000.0, 200.0, 5, true);
|
||||
|
||||
// Act
|
||||
var threeArgument = manager.ValidateOrder(intent, context, config);
|
||||
var oneContract = manager.ValidateOrder(intent, context, config, 1);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(oneContract.Allow, threeArgument.Allow);
|
||||
Assert.AreEqual(oneContract.RejectReason, threeArgument.RejectReason);
|
||||
Assert.AreEqual(oneContract.RiskLevel, threeArgument.RiskLevel);
|
||||
}
|
||||
|
||||
private static AdvancedRiskManager CreateManager(
|
||||
double weeklyLossLimit,
|
||||
double trailingDrawdownLimit,
|
||||
|
||||
@@ -99,6 +99,24 @@ namespace NT8.Integration.Tests
|
||||
Assert.AreEqual(16, history[0].TargetTicks);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ExecuteIntent_ValidatesRiskWithSizedContracts()
|
||||
{
|
||||
// Arrange
|
||||
var adapter = new NT8OrderAdapter(new FakeBridge());
|
||||
var risk = new TestRiskManager(true);
|
||||
var sizer = new TestPositionSizer(4);
|
||||
adapter.Initialize(risk, sizer);
|
||||
|
||||
// Act
|
||||
adapter.ExecuteIntent(CreateIntent(), CreateContext(), CreateConfig());
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(4, risk.LastCandidateContracts);
|
||||
Assert.AreEqual(0, risk.ThreeArgumentValidateCalls);
|
||||
Assert.AreEqual(1, risk.FourArgumentValidateCalls);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void GetExecutionHistory_ReturnsCopy_NotMutableInternalReference()
|
||||
{
|
||||
@@ -193,13 +211,27 @@ namespace NT8.Integration.Tests
|
||||
{
|
||||
private readonly bool _allow;
|
||||
|
||||
public int LastCandidateContracts { get; private set; }
|
||||
public int ThreeArgumentValidateCalls { get; private set; }
|
||||
public int FourArgumentValidateCalls { get; private set; }
|
||||
|
||||
public TestRiskManager(bool allow)
|
||||
{
|
||||
_allow = allow;
|
||||
LastCandidateContracts = -1;
|
||||
}
|
||||
|
||||
public RiskDecision ValidateOrder(StrategyIntent intent, StrategyContext context, RiskConfig config)
|
||||
{
|
||||
ThreeArgumentValidateCalls++;
|
||||
return ValidateOrder(intent, context, config, 1);
|
||||
}
|
||||
|
||||
public RiskDecision ValidateOrder(StrategyIntent intent, StrategyContext context, RiskConfig config, int candidateContracts)
|
||||
{
|
||||
FourArgumentValidateCalls++;
|
||||
LastCandidateContracts = candidateContracts;
|
||||
|
||||
return new RiskDecision(
|
||||
_allow,
|
||||
_allow ? null : "Rejected by test risk manager",
|
||||
|
||||
Reference in New Issue
Block a user