GAP-032: enforce session daily trade limit
- retain accepted-trade count after position-flat events - reject same-session entries with explicit MaxDailyTrades logging - verify next-session re-entry and update Gate 3 governance Build: clean | Tests: 500 passing | Dual-deployed: yes
This commit is contained in:
@@ -472,6 +472,109 @@ namespace NT8.Core.Tests.Intelligence
|
||||
Assert.IsNull(intent2, "Second trade should be blocked when MaxDailyTrades=1");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SimpleORB_MaxDailyTrades_PositionFlatDoesNotRearmSameSession()
|
||||
{
|
||||
var logger = new RecordingLogger();
|
||||
var strategy = CreateDailyLimitStrategy(logger);
|
||||
DateTime sessionStart = DateTime.Today.AddHours(9.5);
|
||||
StrategyContext context = CreateDailyLimitContext(sessionStart);
|
||||
|
||||
StrategyIntent first = strategy.OnBar(CreateBreakoutBar(sessionStart.AddMinutes(44)), context);
|
||||
Assert.IsNotNull(first, "First signal should fire");
|
||||
first.Metadata["trade_grade"] = "A";
|
||||
var positionFlat = new Dictionary<string, object>();
|
||||
positionFlat.Add("clear_trade_taken", true);
|
||||
strategy.SetParameters(positionFlat);
|
||||
context.CurrentTime = sessionStart.AddMinutes(54);
|
||||
StrategyIntent second = strategy.OnBar(CreateBreakoutBar(sessionStart.AddMinutes(54)), context);
|
||||
|
||||
Assert.AreEqual("A", first.Metadata["trade_grade"].ToString(), "Synthetic first signal must be grade A");
|
||||
Assert.IsNull(second, "Second same-session signal must be rejected after position returns flat");
|
||||
Assert.IsTrue(logger.Contains("MaxDailyTrades reached"), "Rejection must log MaxDailyTrades reached");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SimpleORB_MaxDailyTrades_NextSessionAllowsFirstSignal()
|
||||
{
|
||||
var logger = new RecordingLogger();
|
||||
var strategy = CreateDailyLimitStrategy(logger);
|
||||
DateTime firstSessionStart = DateTime.Today.AddHours(9.5);
|
||||
StrategyContext context = CreateDailyLimitContext(firstSessionStart);
|
||||
|
||||
StrategyIntent first = strategy.OnBar(CreateBreakoutBar(firstSessionStart.AddMinutes(44)), context);
|
||||
Assert.IsNotNull(first, "First session signal should fire");
|
||||
first.Metadata["trade_grade"] = "A";
|
||||
DateTime nextSessionStart = firstSessionStart.AddDays(1);
|
||||
context.Session = new MarketSession(nextSessionStart, nextSessionStart.Date.AddHours(16), true, "RTH");
|
||||
context.CurrentTime = nextSessionStart.AddMinutes(44);
|
||||
StrategyIntent nextDay = strategy.OnBar(CreateBreakoutBar(nextSessionStart.AddMinutes(44)), context);
|
||||
|
||||
Assert.AreEqual("A", first.Metadata["trade_grade"].ToString(), "Synthetic first signal must be grade A");
|
||||
Assert.IsNotNull(nextDay, "First signal after session rollover should fire normally");
|
||||
}
|
||||
|
||||
private static NT8.Strategies.Examples.SimpleORBStrategy CreateDailyLimitStrategy(ILogger logger)
|
||||
{
|
||||
var strategy = new NT8.Strategies.Examples.SimpleORBStrategy(30, 1.0);
|
||||
var config = new StrategyConfig(
|
||||
"SimpleORB",
|
||||
"NQ",
|
||||
new Dictionary<string, object>(),
|
||||
new RiskConfig(1000.0, 200.0, 2, false),
|
||||
new SizingConfig(SizingMethod.FixedDollarRisk, 1, 3, 100.0, null));
|
||||
config.Parameters.Add("TestMode", true);
|
||||
config.Parameters.Add("MaxDailyTrades", 1);
|
||||
config.Parameters.Add("StopTicks", 8);
|
||||
config.Parameters.Add("TargetTicks", 16);
|
||||
config.Parameters.Add("MinTradeGrade", 1);
|
||||
strategy.Initialize(config, null, logger);
|
||||
return strategy;
|
||||
}
|
||||
|
||||
private static StrategyContext CreateDailyLimitContext(DateTime sessionStart)
|
||||
{
|
||||
var session = new MarketSession(sessionStart, sessionStart.Date.AddHours(16), true, "RTH");
|
||||
return new StrategyContext(
|
||||
"NQ",
|
||||
sessionStart.AddMinutes(44),
|
||||
null,
|
||||
new AccountInfo(100000, 100000, 0, 0, DateTime.UtcNow),
|
||||
session,
|
||||
new Dictionary<string, object>());
|
||||
}
|
||||
|
||||
private static BarData CreateBreakoutBar(DateTime time)
|
||||
{
|
||||
return new BarData("NQ", time, 19000, 19060, 18990, 19065, 1500, TimeSpan.FromMinutes(5));
|
||||
}
|
||||
|
||||
private class RecordingLogger : ILogger
|
||||
{
|
||||
private readonly List<string> _messages = new List<string>();
|
||||
|
||||
public bool Contains(string value)
|
||||
{
|
||||
for (int i = 0; i < _messages.Count; i++)
|
||||
{
|
||||
if (_messages[i].IndexOf(value, StringComparison.Ordinal) >= 0)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void LogDebug(string message, params object[] args) { Add(message, args); }
|
||||
public void LogInformation(string message, params object[] args) { Add(message, args); }
|
||||
public void LogWarning(string message, params object[] args) { Add(message, args); }
|
||||
public void LogError(string message, params object[] args) { Add(message, args); }
|
||||
public void LogCritical(string message, params object[] args) { Add(message, args); }
|
||||
|
||||
private void Add(string message, params object[] args)
|
||||
{
|
||||
_messages.Add(args != null && args.Length > 0 ? String.Format(message, args) : message);
|
||||
}
|
||||
}
|
||||
|
||||
private static StrategyIntent CreateIntent()
|
||||
{
|
||||
return new StrategyIntent(
|
||||
|
||||
Reference in New Issue
Block a user