diff --git a/src/NT8.Adapters/Strategies/NT8StrategyBase.cs b/src/NT8.Adapters/Strategies/NT8StrategyBase.cs index 1c8eaa8..8459d7d 100644 --- a/src/NT8.Adapters/Strategies/NT8StrategyBase.cs +++ b/src/NT8.Adapters/Strategies/NT8StrategyBase.cs @@ -852,6 +852,7 @@ namespace NinjaTrader.NinjaScript.Strategies if (_logger != null) _logger.LogError("OnBarUpdate failed: {0}", ex.Message); + FileLog(string.Format("SDK ONBARUPDATE FAILED: {0}: {1}", ex.GetType().Name, ex.Message)); Print(string.Format("[SDK ERROR] OnBarUpdate: {0}", ex.Message)); Log(string.Format("[SDK ERROR] {0}", ex.ToString()), NinjaTrader.Cbi.LogLevel.Error); } diff --git a/tests/NT8.Core.Tests/Adapters/NT8StrategyBaseOnBarUpdateVisibilityTests.cs b/tests/NT8.Core.Tests/Adapters/NT8StrategyBaseOnBarUpdateVisibilityTests.cs new file mode 100644 index 0000000..b355667 --- /dev/null +++ b/tests/NT8.Core.Tests/Adapters/NT8StrategyBaseOnBarUpdateVisibilityTests.cs @@ -0,0 +1,65 @@ +using System; +using System.IO; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace NT8.Core.Tests.Adapters +{ + [TestClass] + public class NT8StrategyBaseOnBarUpdateVisibilityTests + { + [TestMethod] + public void OnBarUpdate_CatchBlockWritesExceptionToFileLog() + { + string source = File.ReadAllText(FindStrategyBaseSourceFile()); + string method = ExtractMethodBlock(source, "protected override void OnBarUpdate"); + int catchIndex = method.IndexOf("catch (Exception ex)", StringComparison.Ordinal); + int fileLogIndex = method.IndexOf("FileLog(string.Format(\"SDK ONBARUPDATE FAILED: {0}: {1}\", ex.GetType().Name, ex.Message));", StringComparison.Ordinal); + int printIndex = method.IndexOf("Print(string.Format(\"[SDK ERROR] OnBarUpdate: {0}\", ex.Message));", StringComparison.Ordinal); + + Assert.IsTrue(catchIndex >= 0, "OnBarUpdate must keep its exception guard."); + Assert.IsTrue(fileLogIndex > catchIndex, "OnBarUpdate catch must write the exception to FileLog."); + Assert.IsTrue(printIndex > fileLogIndex, "FileLog visibility must be additive and preserve the existing Print path after it."); + } + + private static string FindStrategyBaseSourceFile() + { + 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; + } + } +} diff --git a/tests/NT8.Core.Tests/Intelligence/OrbConfluenceFactorTests.cs b/tests/NT8.Core.Tests/Intelligence/OrbConfluenceFactorTests.cs index 88e534d..6627411 100644 --- a/tests/NT8.Core.Tests/Intelligence/OrbConfluenceFactorTests.cs +++ b/tests/NT8.Core.Tests/Intelligence/OrbConfluenceFactorTests.cs @@ -358,8 +358,8 @@ namespace NT8.Core.Tests.Intelligence config.Parameters["MinTradeGrade"] = 1; strategy.Initialize(config, null, new BasicLogger("test")); - var sessionStart = DateTime.Today.AddHours(9.5); - var session = new MarketSession(sessionStart, DateTime.Today.AddHours(16), true, "RTH"); + var sessionStart = new DateTime(2026, 9, 3, 9, 30, 0); + var session = new MarketSession(sessionStart, sessionStart.Date.AddHours(16), true, "RTH"); var context = new StrategyContext("NQ", sessionStart.AddMinutes(31), null, new AccountInfo(100000, 100000, 0, 0, DateTime.UtcNow), session, new Dictionary()); @@ -398,8 +398,8 @@ namespace NT8.Core.Tests.Intelligence config.Parameters["MinTradeGrade"] = 1; strategy.Initialize(config, null, new BasicLogger("test")); - var sessionStart = DateTime.Today.AddHours(9.5); - var session = new MarketSession(sessionStart, DateTime.Today.AddHours(16), true, "RTH"); + var sessionStart = new DateTime(2026, 9, 3, 9, 30, 0); + var session = new MarketSession(sessionStart, sessionStart.Date.AddHours(16), true, "RTH"); var context = new StrategyContext("NQ", sessionStart, null, new AccountInfo(100000, 100000, 0, 0, DateTime.UtcNow), session, new Dictionary()); @@ -445,8 +445,8 @@ namespace NT8.Core.Tests.Intelligence config.Parameters["MinTradeGrade"] = 1; strategy.Initialize(config, null, new BasicLogger("test")); - var sessionStart = DateTime.Today.AddHours(9.5); - var session = new MarketSession(sessionStart, DateTime.Today.AddHours(16), true, "RTH"); + var sessionStart = new DateTime(2026, 9, 3, 9, 30, 0); + var session = new MarketSession(sessionStart, sessionStart.Date.AddHours(16), true, "RTH"); var context = new StrategyContext("NQ", sessionStart, null, new AccountInfo(100000, 100000, 0, 0, DateTime.UtcNow), session, new Dictionary()); @@ -496,8 +496,8 @@ namespace NT8.Core.Tests.Intelligence config.Parameters["MinTradeGrade"] = 1; strategy.Initialize(config, null, new BasicLogger("test")); - var sessionStart = DateTime.Today.AddHours(9.5); - var session = new MarketSession(sessionStart, DateTime.Today.AddHours(16), true, "RTH"); + var sessionStart = new DateTime(2026, 9, 3, 9, 30, 0); + var session = new MarketSession(sessionStart, sessionStart.Date.AddHours(16), true, "RTH"); var context = new StrategyContext("NQ", sessionStart, null, new AccountInfo(100000, 100000, 0, 0, DateTime.UtcNow), session, new Dictionary()); @@ -546,8 +546,8 @@ namespace NT8.Core.Tests.Intelligence config.Parameters["MinTradeGrade"] = 1; strategy.Initialize(config, null, new BasicLogger("test")); - var sessionStart = DateTime.Today.AddHours(9.5); - var session = new MarketSession(sessionStart, DateTime.Today.AddHours(16), true, "RTH"); + var sessionStart = new DateTime(2026, 9, 3, 9, 30, 0); + var session = new MarketSession(sessionStart, sessionStart.Date.AddHours(16), true, "RTH"); var context = new StrategyContext("NQ", sessionStart, null, new AccountInfo(100000, 100000, 0, 0, DateTime.UtcNow), session, new Dictionary()); @@ -573,7 +573,7 @@ namespace NT8.Core.Tests.Intelligence { var logger = new RecordingLogger(); var strategy = CreateDailyLimitStrategy(logger); - DateTime sessionStart = DateTime.Today.AddHours(9.5); + DateTime sessionStart = new DateTime(2026, 9, 3, 9, 30, 0); StrategyContext context = CreateDailyLimitContext(sessionStart); StrategyIntent first = strategy.OnBar(CreateBreakoutBar(sessionStart.AddMinutes(44)), context); @@ -595,7 +595,7 @@ namespace NT8.Core.Tests.Intelligence { var logger = new RecordingLogger(); var strategy = CreateDailyLimitStrategy(logger); - DateTime firstSessionStart = DateTime.Today.AddHours(9.5); + DateTime firstSessionStart = new DateTime(2026, 9, 3, 9, 30, 0); StrategyContext context = CreateDailyLimitContext(firstSessionStart); StrategyIntent first = strategy.OnBar(CreateBreakoutBar(firstSessionStart.AddMinutes(44)), context); @@ -615,7 +615,7 @@ namespace NT8.Core.Tests.Intelligence { var logger = new RecordingLogger(); var strategy = CreateDailyLimitStrategy(logger); - DateTime sessionStart = DateTime.Today.AddHours(9.5); + DateTime sessionStart = new DateTime(2026, 9, 3, 9, 30, 0); StrategyContext context = CreateDailyLimitContext(sessionStart); context.CustomData.Add("AllowDailyTradeCountIncrement", false); @@ -630,7 +630,7 @@ namespace NT8.Core.Tests.Intelligence { var logger = new RecordingLogger(); var strategy = CreateDailyLimitStrategy(logger); - DateTime sessionStart = DateTime.Today.AddHours(9.5); + DateTime sessionStart = new DateTime(2026, 9, 3, 9, 30, 0); StrategyContext context = CreateDailyLimitContext(sessionStart); context.CustomData.Add("AllowDailyTradeCountIncrement", true); @@ -645,7 +645,7 @@ namespace NT8.Core.Tests.Intelligence { var logger = new RecordingLogger(); var strategy = CreateDailyLimitStrategy(logger); - DateTime sessionStart = DateTime.Today.AddHours(9.5); + DateTime sessionStart = new DateTime(2026, 9, 3, 9, 30, 0); StrategyContext context = CreateDailyLimitContext(sessionStart); StrategyIntent first = strategy.OnBar(CreateBreakoutBar(sessionStart.AddMinutes(44)), context); diff --git a/tests/NT8.Core.Tests/Strategies/SimpleORBHydrationTests.cs b/tests/NT8.Core.Tests/Strategies/SimpleORBHydrationTests.cs index e8775b9..6596130 100644 --- a/tests/NT8.Core.Tests/Strategies/SimpleORBHydrationTests.cs +++ b/tests/NT8.Core.Tests/Strategies/SimpleORBHydrationTests.cs @@ -14,7 +14,7 @@ namespace NT8.Core.Tests.Strategies public void OnBar_FullOpeningWindowReplay_HydratesOpeningRangeBeforeRealtimeBreakout() { SimpleORBStrategy strategy = CreateStrategy(); - DateTime sessionStart = DateTime.Today.AddHours(9.5); + DateTime sessionStart = new DateTime(2026, 9, 3, 9, 30, 0); StrategyContext context = CreateContext(sessionStart); StrategyIntent first = strategy.OnBar(CreateBar(sessionStart.AddMinutes(5), 19000.0, 19040.0, 18990.0, 19020.0), context); @@ -38,7 +38,7 @@ namespace NT8.Core.Tests.Strategies public void OnBar_PartialOpeningWindowReplay_HydratesFromDeliveredBarsWithoutNewProductionLogic() { SimpleORBStrategy strategy = CreateStrategy(); - DateTime sessionStart = DateTime.Today.AddHours(9.5); + DateTime sessionStart = new DateTime(2026, 9, 3, 9, 30, 0); StrategyContext context = CreateContext(sessionStart); StrategyIntent firstDelivered = strategy.OnBar(CreateBar(sessionStart.AddMinutes(18), 19020.0, 19055.0, 19005.0, 19035.0), context); diff --git a/tests/NT8.Core.Tests/Strategies/SimpleORBRejectionLoggingTests.cs b/tests/NT8.Core.Tests/Strategies/SimpleORBRejectionLoggingTests.cs index cf3eb02..1d04651 100644 --- a/tests/NT8.Core.Tests/Strategies/SimpleORBRejectionLoggingTests.cs +++ b/tests/NT8.Core.Tests/Strategies/SimpleORBRejectionLoggingTests.cs @@ -16,7 +16,7 @@ namespace NT8.Core.Tests.Strategies public void OnBar_BelowThresholdGrade_RecordsGradeFilterRejectLineWithScore() { SimpleORBStrategy strategy = CreateStrategy((int)TradeGrade.APlus); - DateTime sessionStart = DateTime.Today.AddHours(9.5); + DateTime sessionStart = new DateTime(2026, 9, 3, 9, 30, 0); StrategyContext context = CreateContext(sessionStart); BarData bar = CreateBreakoutBar(sessionStart); @@ -37,7 +37,7 @@ namespace NT8.Core.Tests.Strategies public void OnBar_RegimeVeto_RecordsRegimeVetoRejectLine() { SimpleORBStrategy strategy = CreateStrategy((int)TradeGrade.F); - DateTime sessionStart = DateTime.Today.AddHours(9.5); + DateTime sessionStart = new DateTime(2026, 9, 3, 9, 30, 0); StrategyContext context = CreateContext(sessionStart); InvokeRecordSignalRejection(strategy, context, "RegimeVeto", "Regime veto before score; Side=Buy AVWAPSlope=-1.0000", sessionStart.AddMinutes(5)); @@ -55,7 +55,7 @@ namespace NT8.Core.Tests.Strategies public void RejectLineFormat_DiffersOnlyByReasonToken() { SimpleORBStrategy strategy = CreateStrategy((int)TradeGrade.F); - DateTime sessionStart = DateTime.Today.AddHours(9.5); + DateTime sessionStart = new DateTime(2026, 9, 3, 9, 30, 0); DateTime barTime = sessionStart.AddMinutes(5); string detail = "Regime veto before score; Side=Buy AVWAPSlope=-1.0000"; @@ -80,7 +80,7 @@ namespace NT8.Core.Tests.Strategies public void OnBar_RthNoBreakout_RecordsOrbRangeNotEstablishedBeforeAndAfterOrbClose() { SimpleORBStrategy strategy = CreateStrategy((int)TradeGrade.F, false); - DateTime sessionStart = DateTime.Today.AddHours(9.5); + DateTime sessionStart = new DateTime(2026, 9, 3, 9, 30, 0); StrategyContext buildingContext = CreateContext(sessionStart); BarData buildingBar = new BarData("NQ", sessionStart.AddMinutes(5), 19000, 19010, 18990, 19000, 1500, TimeSpan.FromMinutes(5));