7.9 KiB
TASK: GAP-077 — Strategy Analyzer backtests take zero trades: live catch-up guard blocks every historical intent
Staged: 2026-10-24 — DEC-20261024-01
Sprint: Stage 0 — restore trust in the artifact
Owner: Kilocode
Deploy: COMMIT ONLY — do NOT run deploy-to-nt8.bat. The operator deploys once, after P0-A
(orphan sweep) and the other Stage 0 commits have landed. See NEXT_PROMPT.md.
Constraints: C# 5.0 / .NET Framework 4.8 — no string interpolation, no ?., no => bodies,
no async/await, no pattern matching. NT8 API must be verified against
https://developer.ninjatrader.com/docs/desktop — do not infer signatures.
THE DEFECT (evidence, not hypothesis)
session_20261019_155354.log — a Strategy Analyzer backtest, MNQ, Jan–Oct 2026:
Mode : LIVE/SIM <- wrong; this is a backtest
Account : Backtest <- Account is NOT null in Strategy Analyzer
REJECT: reason=HistoricalReplayCatchup detail=Blocked catch-up replay order Time=2026-01-02 10:14:00
REJECT SUMMARY: reason=HistoricalReplayCatchup count=117
117 intents generated, 117 blocked, zero SUBMIT, zero FILL. 15,594 of the log's 15,613 lines are
repeated REJECT SUMMARY lines.
Cause — NT8StrategyBase.ProcessStrategyIntent() (~:2069 deployed):
if (State == State.Historical && Account != null) // intended: live catch-up replay only
DEC-20260818-08 assumed Account == null during genuine Strategy Analyzer runs. That is false —
Strategy Analyzer assigns the Backtest account. Every backtest since 2026-08-18 has therefore
blocked every intent. DEC-20260818-08's own Action Required ("run a Strategy Analyzer backtest …
confirm trade count and P&L are unchanged") was never executed.
SAME FALSE ASSUMPTION ELSEWHERE — ALL IN SCOPE
ShouldAllowDailyTradeCountIncrement()(~:2000): inState.HistoricalreturnsAccount == null→ alwaysfalsein a backtest. Fixing only the entry guard would let backtests take unlimited trades per day, silently diverging from liveMaxDailyTradesrules. Must be fixed in the same commit.- Daily-trade-count persistence wiring (~
:595,if (_sdkStrategy != null && Account != null)): confirm whichStatebranch this runs in. A backtest must never read or writedaily_trade_count_Backtest_*.json. If it can, gate it on the new predicate. TrailingDrawdownMonitorregistration (~:1797,InitializeTrailingDrawdownState~:1834): a backtest registers an account literally namedBacktestwith the singleton monitor. Gate on the new predicate unless reading shows it is provably inert.- Session header
Modelabel (WriteSessionHeader, ~:1620): computed fromState == State.Historical, but the header is written fromState.DataLoaded, so it always readsLIVE/SIM. Use the new predicate. LogRejectRateLimited()(~:1528): drops every non-Realtime reason exceptHistoricalReplayCatchup, so a backtest can never recordGradeFilter,OrbRangeNotEstablished,RegimeVeto,MaxDailyTradesetc. In a backtest, count all reasons (soREJECT SUMMARYis meaningful) and write each reason's firstREJECT:detail line once. Live/SIM historical catch-up behaviour must stay exactly as it is today.MaybeWriteRejectSummarySnapshot()(~:1563): in a backtest, write at most one summary per trading date (plus the existing end-of-session summary). Live/SIM cadence unchanged.
READ-FIRST
src/NT8.Adapters/Strategies/NT8StrategyBase.cs— every occurrence ofAccount != null,Account == null,State.Historical,_realtimeBarSeen,LogRejectRateLimited,MaybeWriteRejectSummarySnapshot,WriteSessionHeader. Enumerate them all in your implementation note with a keep/change verdict for each.tests/NT8.Core.Tests/Adapters/NT8StrategyBaseHistoricalGuardTests.cs— the second test asserts the literalif (State == State.Historical && Account != null). It enshrines the bug and must be rewritten, not deleted.docs/00-governance/decisions.md—DEC-20260818-08(the original guard),DEC-20261024-01(this task).- NT8 docs:
IsInStrategyAnalyzer. The documented definition is "run from a Strategy Analyzer chart". Confirm from the docs (and, if available, localNinjaTrader.Core.xmlmetadata) that it istrueduring the backtest execution itself, not only for the Analyzer's chart display. Report what you found; do not assume.
THE FIX
Add one private predicate to NT8StrategyBase and route every item above through it:
/// <summary>
/// True when this instance is executing inside Strategy Analyzer (backtest/optimization),
/// as opposed to a live/SIM account replaying catch-up history. GAP-077.
/// </summary>
private bool IsBacktestRun()
{
if (IsInStrategyAnalyzer)
return true;
if (Account != null && string.Equals(Account.Name, "Backtest", StringComparison.OrdinalIgnoreCase))
return true;
return false;
}
- The
Account.Name == "Backtest"clause is the evidence-backed half (the Oct 19 header proves the name). Keep it even ifIsInStrategyAnalyzerchecks out. If the docs show a better-specified API, propose it in the note; do not swap it in unasked. - Catch-up guard becomes
if (State == State.Historical && !IsBacktestRun()). ShouldAllowDailyTradeCountIncrement()historical branch becomesreturn IsBacktestRun();.- Header:
Mode : BACKTESTwhenIsBacktestRun(), elseLIVE/SIM.
DO NOT
- Do not change any live/SIM behaviour. A live account in
State.Historicalmust still be blocked withHistoricalReplayCatchupbefore portfolio risk, sizing and submission, exactly as today. - Do not touch
SimpleORBStrategy.cs, scoring, risk limits, sizing, or the replay-burst guard. - Do not delete the orphan
bin\Custom\Strategies\SimpleORBStrategy.cs— that is operator task P0-A. - Do not deploy.
REQUIRED TESTS (every one must fail against the current source)
- Rewrite
ProcessStrategyIntent_BlocksLiveAccountHistoricalOrdersBeforeRiskAndSubmissionto assert the guard readsState == State.Historical && !IsBacktestRun()and still precedes portfolio risk andSubmitOrderToNT8. - New:
IsBacktestRun()exists and contains both theIsInStrategyAnalyzerclause and the"Backtest"account-name clause. - New:
ShouldAllowDailyTradeCountIncrement()'s historical branch referencesIsBacktestRun()and does not containAccount == null. - New: no remaining
State == State.Historical && Account != nullanywhere inNT8StrategyBase.cs(regression guard against the original defect). - New:
WriteSessionHeader()'sModeline referencesIsBacktestRun().
Source-text tests in the established NT8StrategyBaseHistoricalGuardTests pattern are acceptable —
this class cannot be instantiated outside NT8.
VERIFICATION
verify-build.batgreen; report total test count.dotnet test tests\NT8.Core.Tests\NT8.Core.Tests.csproj --configuration Release --no-build.- Commit:
[GAP-077]: backtest historical guard + backtest-aware reject logging.
OPERATOR VALIDATION (after P0-A + deploy + full recompile — not part of this task)
- Strategy Analyzer, same settings as 2026-10-19 (MNQ, Jan–Oct 2026, ≥60 trading-day warm-up):
header reads
Mode : BACKTEST;SUBMIT/FILLpresent; trade count > 0; max one trade per trading date;REJECT SUMMARYshows real strategy reasons (GradeFilter,OrbRangeNotEstablished, …); nodaily_trade_count_Backtest_*.jsoncreated. - Live/SIM late enable on the SIM account:
REJECT: reason=HistoricalReplayCatchupstill appears during catch-up; zero historicalSUBMIT.
GOVERNANCE
Track as GAP-077 in SPRINT_BOARD.md (HIGH). Update DEC-20261024-01 status to implemented
with commit SHA and test counts. Note that GAP-076 (does Strategy Analyzer bind the orphan or the
DLL?) is answered empirically by operator validation step 1 once P0-A has landed.