diff --git a/SPRINT_BOARD.md b/SPRINT_BOARD.md index 40daf43..9c8e132 100644 --- a/SPRINT_BOARD.md +++ b/SPRINT_BOARD.md @@ -60,10 +60,10 @@ - Prompt: `TASK_GAP048_REJECT_TAXONOMY_PROMPT.md`. Verification: `verify-build.bat` 516/516, Core Release no-build 427/427, parser syntax checks passed, deploy backup `20260823_142014`. Manual NT8 compile and live/SIM dashboard spot-check still required. - GAP-058 housekeeping implemented 2026-08-23: replaced the tautological reject-line format test with production-path coverage; no production code changed. -**[GAP-049] Telemetry `STATE:` score is direction-incorrect and presented as the trade grade** | Stage 1 | ✅ READY +**[GAP-049] Telemetry `STATE:` score is direction-incorrect and presented as the trade grade** | Stage 1 | ✅ IMPLEMENTED — deployed 2026-08-23 - `GapDirectionAlignmentCalculator` reads `daily.TradeDirection`, which the telemetry path deliberately does not set (DEC-20260818-06). Confirmed: a −523 gap scored "opposes trade" on a `Direction=Sell` setup. - The dashboard's headline Score/Grade is therefore not the trade's. Also degrades the GAP-047 study. -- Prompt: `TASK_GAP049_TELEMETRY_DIRECTION_PROMPT.md`. +- Prompt: `TASK_GAP049_TELEMETRY_DIRECTION_PROMPT.md`. Telemetry now clones `DailyBarContext`, sets clone-only direction/session-open/volume fields from the synthetic telemetry intent path, and adds `orb_range_ticks` metadata for `OrbRangeVsAtr`. Verification: focused tests 30/30, `verify-build.bat` 538/538, Core Release no-build 449/449, deploy backup `20260823_151931`. Manual NT8 compile and live/SIM STATE_DETAIL spot-check still required. P1-E finding: the cited `-523.25` was a telemetry data-alignment defect, not a real overnight gap; NQ SEP26 daily data showed 2026-08-17 close `30096.00` and 2026-08-18 open `30077.00` (`-19.00`). ### Also raised by the review, not yet prompted GAP-051 (`MicroUpgradeThreshold` fires every session) · GAP-052 (two hardcoded tick-value tables) · diff --git a/docs/00-governance/decisions.md b/docs/00-governance/decisions.md index 047cfe6..3746386 100644 --- a/docs/00-governance/decisions.md +++ b/docs/00-governance/decisions.md @@ -13,6 +13,17 @@ Use this file to record durable project decisions that affect implementation pat ## Decision Log +### DEC-20260823-03 +- **ID:** DEC-20260823-03 +- **Date:** 2026-08-23 +- **Status:** implemented +- **Context:** GAP-049 found that `SimpleORBNT8.BuildStateTelemetryIntent()` attached the shared `_strategyConfig.Parameters["daily_bars"]` object directly to synthetic `STATE:` telemetry. `GapDirectionAlignmentCalculator` and `PriorDayCloseStrengthCalculator` read `DailyBarContext.TradeDirection`, but the telemetry path deliberately does not call `SimpleORBStrategy.AttachDailyBarContext()` because that real-trade helper mutates shared daily context fields. Result: short telemetry could retain a stale long direction and log a downward gap as "opposes trade." The same log block also showed `OrbRangeVsAtr="No daily bar context available"` because telemetry had daily context but did not include the separate `orb_range_ticks` metadata key required by that factor. +- **Decision:** Keep GAP-049 observability-only by fixing only the synthetic telemetry path. `SimpleORBNT8.BuildStateTelemetryIntent()` now builds a clone of the `DailyBarContext`, sets clone-only scalar fields from telemetry inputs (`TradeDirection`, `BreakoutBarVolume`, `TodayOpen`, `AvgIntradayBarVolume`), attaches that clone as `daily_bars`, and adds `orb_range_ticks` metadata from the cloned context. No `SIGNAL`, `SUBMIT`, `FILL`, `PNL_UPDATE`, `REJECT`, sizing, risk, order submission, or real trade-decision path was changed. +- **P1-E Finding:** The cited `-523.25` point gap was a data-alignment defect in telemetry, not a real overnight NQ gap. The session log `session_20260818_071117.log` reported different gap values throughout the same RTH session, ranging from roughly `-389.75` to `-555.75`, which cannot represent a fixed overnight open gap. External NQ SEP26 daily data showed 2026-08-17 close `30096.00` and 2026-08-18 open `30077.00`, a `-19.00` daily gap. Root cause: telemetry seeded `DailyBarContext.TodayOpen` from the current primary bar's `Open[0]`; the clone now mirrors the real trade path and prefers `context.CustomData["session_open_price"]`. +- **Evidence:** Added source-level tests proving telemetry uses `CloneDailyBarContextForTelemetry`, attaches the clone rather than the shared daily context, and emits `orb_range_ticks`; added a factor test proving a large down gap with sell direction scores `1.0` and says "aligns with trade," not "opposes trade." Focused GAP-049 tests passed 30/30. `verify-build.bat` passed with 538/538 tests (449 Core, 79 Integration, 10 Performance). `dotnet test tests\NT8.Core.Tests\NT8.Core.Tests.csproj --configuration Release --no-build --verbosity minimal` passed 449/449. `deployment\deploy-to-nt8.bat` completed with backup `20260823_151931` and noted `NinjaTrader.Custom.dll` was not present to invalidate. +- **Related Files:** `src/NT8.Adapters/Strategies/SimpleORBNT8.cs`, `tests/NT8.Core.Tests/Adapters/SimpleORBNT8DefaultsTests.cs`, `tests/NT8.Core.Tests/Intelligence/OrbConfluenceFactorTests.cs`, `TASK_GAP049_TELEMETRY_DIRECTION_PROMPT.md` +- **Action Required:** Manual NT8 startup/compile remains required for NinjaScript source. In the next live/SIM diagnostic session, confirm `STATE_DETAIL` for a sell setup with a down gap reports `GapDirectionAlignment` as aligned and `OrbRangeVsAtr` no longer says no daily context when daily warm-up is satisfied. + ### DEC-20260823-02 - **ID:** DEC-20260823-02 - **Date:** 2026-08-23 diff --git a/src/NT8.Adapters/Strategies/SimpleORBNT8.cs b/src/NT8.Adapters/Strategies/SimpleORBNT8.cs index 54f0d18..99d2a47 100644 --- a/src/NT8.Adapters/Strategies/SimpleORBNT8.cs +++ b/src/NT8.Adapters/Strategies/SimpleORBNT8.cs @@ -231,7 +231,14 @@ namespace NinjaTrader.NinjaScript.Strategies && _strategyConfig.Parameters.ContainsKey("daily_bars") && _strategyConfig.Parameters["daily_bars"] != null) { - metadata.Add("daily_bars", _strategyConfig.Parameters["daily_bars"]); + object dailySource = _strategyConfig.Parameters["daily_bars"]; + if (dailySource is DailyBarContext) + { + DailyBarContext telemetryDaily = CloneDailyBarContextForTelemetry((DailyBarContext)dailySource, side, barData, context); + metadata.Add("daily_bars", telemetryDaily); + if (telemetryDaily.OrbRangeTicks > 0.0) + metadata.Add("orb_range_ticks", telemetryDaily.OrbRangeTicks); + } } int stopTicks = ResolveDynamicStopTicks(StopTicks, metadata, false); @@ -248,6 +255,46 @@ namespace NinjaTrader.NinjaScript.Strategies metadata); } + private static DailyBarContext CloneDailyBarContextForTelemetry(DailyBarContext source, OrderSide side, BarData barData, StrategyContext context) + { + DailyBarContext daily = source; + daily.TradeDirection = side == OrderSide.Buy ? 1 : -1; + + if (barData != null) + { + daily.BreakoutBarVolume = (double)barData.Volume; + daily.TodayOpen = barData.Open; + } + + if (context != null && context.CustomData != null && context.CustomData.ContainsKey("session_open_price")) + { + object sessionOpen = context.CustomData["session_open_price"]; + if (sessionOpen is double) + daily.TodayOpen = (double)sessionOpen; + else if (sessionOpen is float) + daily.TodayOpen = (double)(float)sessionOpen; + else if (sessionOpen is int) + daily.TodayOpen = (double)(int)sessionOpen; + else if (sessionOpen is long) + daily.TodayOpen = (double)(long)sessionOpen; + } + + if (context != null && context.CustomData != null && context.CustomData.ContainsKey("avg_volume")) + { + object avg = context.CustomData["avg_volume"]; + if (avg is double) + daily.AvgIntradayBarVolume = (double)avg; + else if (avg is float) + daily.AvgIntradayBarVolume = (double)(float)avg; + else if (avg is int) + daily.AvgIntradayBarVolume = (double)(int)avg; + else if (avg is long) + daily.AvgIntradayBarVolume = (double)(long)avg; + } + + return daily; + } + protected override string GetStateTelemetryDirectionSource(BarData barData, StrategyContext context) { SdkSimpleORB sdkOrb = _sdkStrategy as SdkSimpleORB; diff --git a/tests/NT8.Core.Tests/Adapters/SimpleORBNT8DefaultsTests.cs b/tests/NT8.Core.Tests/Adapters/SimpleORBNT8DefaultsTests.cs index 758c239..fbf5e26 100644 --- a/tests/NT8.Core.Tests/Adapters/SimpleORBNT8DefaultsTests.cs +++ b/tests/NT8.Core.Tests/Adapters/SimpleORBNT8DefaultsTests.cs @@ -18,6 +18,31 @@ namespace NT8.Core.Tests.Adapters Assert.IsFalse(setDefaults.Contains("EnableShortTrades = false;"), "SetDefaults must not disable short trades by default."); } + [TestMethod] + public void BuildStateTelemetryIntent_DailyContext_UsesDirectionCorrectClone() + { + string source = File.ReadAllText(FindSourceFile()); + string method = ExtractMethodBlock(source, "protected override StrategyIntent BuildStateTelemetryIntent"); + + Assert.IsTrue(method.Contains("CloneDailyBarContextForTelemetry((DailyBarContext)dailySource, side, barData, context)"), "Telemetry must clone daily context and set clone-only fields."); + Assert.IsTrue(method.Contains("metadata.Add(\"daily_bars\", telemetryDaily);"), "Telemetry must attach the cloned daily context."); + Assert.IsTrue(method.Contains("metadata.Add(\"orb_range_ticks\", telemetryDaily.OrbRangeTicks);"), "Telemetry must expose ORB range metadata for OrbRangeVsAtr."); + Assert.IsFalse(method.Contains("metadata.Add(\"daily_bars\", _strategyConfig.Parameters[\"daily_bars\"]);"), "Telemetry must not attach the shared daily context directly."); + } + + [TestMethod] + public void CloneDailyBarContextForTelemetry_UsesTelemetrySideAndSessionOpen() + { + string source = File.ReadAllText(FindSourceFile()); + string method = ExtractMethodBlock(source, "private static DailyBarContext CloneDailyBarContextForTelemetry"); + + Assert.IsTrue(method.Contains("daily.TradeDirection = side == OrderSide.Buy ? 1 : -1;"), "Telemetry clone must derive direction from the synthetic intent side."); + Assert.IsTrue(method.Contains("context.CustomData.ContainsKey(\"session_open_price\")"), "Telemetry clone must prefer session_open_price over current bar open."); + Assert.IsTrue(method.Contains("daily.TodayOpen = (double)sessionOpen;"), "Telemetry clone must apply the session open value."); + Assert.IsTrue(method.Contains("context.CustomData.ContainsKey(\"avg_volume\")"), "Telemetry clone must mirror trade-path avg_volume context."); + Assert.IsTrue(method.Contains("daily.BreakoutBarVolume = (double)barData.Volume;"), "Telemetry clone must use current bar volume for breakout-volume scoring."); + } + private static string FindSourceFile() { DirectoryInfo dir = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory); @@ -45,5 +70,32 @@ namespace NT8.Core.Tests.Adapters return source.Substring(start, end - start); } + + private static string ExtractMethodBlock(string source, string signature) + { + int start = source.IndexOf(signature, StringComparison.Ordinal); + if (start < 0) + Assert.Fail("SimpleORBNT8.cs does not contain method: " + signature); + + int brace = source.IndexOf("{", start, StringComparison.Ordinal); + if (brace < 0) + Assert.Fail("SimpleORBNT8.cs 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("SimpleORBNT8.cs 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 af1afce..edd6743 100644 --- a/tests/NT8.Core.Tests/Intelligence/OrbConfluenceFactorTests.cs +++ b/tests/NT8.Core.Tests/Intelligence/OrbConfluenceFactorTests.cs @@ -124,6 +124,24 @@ namespace NT8.Core.Tests.Intelligence Assert.AreEqual(1.0, result.Score, 0.000001); } + [TestMethod] + public void GapDirection_LargeDownGapForSell_ScoresOneAndAligns() + { + var calc = new GapDirectionAlignmentCalculator(new BasicLogger("test")); + var intent = CreateIntent(); + var daily = CreateDailyContext(new double[] { 8, 8, 8, 8, 8, 8, 8 }); + daily.Closes[daily.Count - 2] = 30096.0; + daily.TodayOpen = 30077.0; + daily.TradeDirection = -1; + intent.Metadata["daily_bars"] = daily; + + var result = calc.Calculate(intent, CreateContext(), CreateBar()); + + Assert.AreEqual(1.0, result.Score, 0.000001); + Assert.IsTrue(result.Reason.Contains("aligns with trade"), result.Reason); + Assert.IsFalse(result.Reason.Contains("opposes trade"), result.Reason); + } + [TestMethod] public void GapDirection_LargeOpposingGap_ScoresVeryLow() {