fix(strategy): make a flat RSI window neutral instead of oversold - #149
Merged
Conversation
The live interpreter returned 0 for a window with neither gains nor losses. The official RSI_14 definition pins that case to 50, the data pipeline computes 50, and the backtest reads the published series rather than recomputing, so the same strategy was oversold live and neutral in its own backtest. A flat window has no relative strength to divide, so the value is a convention, not a result. Both other implementations chose 50 and wrote down why — a market that did not move is not a market that only rose — and the definition says it must be reproduced verbatim. As an indicator bounded at 0 and 100, 0 also means maximum oversold, which is the wrong reading of a market that did not move at all. The divergence ran in the dangerous direction. With an UP crossing at threshold 30, a previous window of 0 sat below the threshold, so a single later rise crossed it and emitted a candidate; at 50 no crossing exists. Live therefore entered trades the strategy's own backtest never showed, which is exactly the guarantee the official backtest is supposed to provide. The test pins that case: sixteen closes whose previous window is flat and whose current window rises once. It emits a candidate under the old value and none under the pinned one. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The divergence
Live and backtest computed RSI with two different implementations that disagreed on a documented edge case.
data-pipeline(producer)backtest-engine(consumer)trading-engine(live)Backtest reads the published
RSI_14series and never recomputes; live computes in-process from closes. So the same strategy was oversold live and neutral in its own backtest.A flat window has no relative strength to divide, so the value is a convention rather than a result — and both other implementations chose 50 with the reasoning written down. As an indicator bounded at 0 and 100, returning 0 also means maximum oversold, which is the wrong reading of a market that did not move at all.
Why it mattered in the dangerous direction
With an UP crossing at threshold 30:
Live therefore entered trades the strategy's own backtest never showed. That is precisely the guarantee the official backtest exists to provide, so this was a reproducibility break rather than a rounding nit.
Test
Sixteen closes whose previous window is perfectly flat and whose current window rises once — the minimal shape that separates the two values. It emits a candidate under the old value and none under the pinned one.
Note on scope
Only the flat-window convention is changed. A second, much smaller divergence remains: the pipeline quantizes each running average to eight decimal places at every step, while the live interpreter sums gains and losses and divides once. That can differ in the low digits and only matters for a threshold landing exactly on the boundary. Aligning it means reproducing the pipeline's quantization recursion, which is worth its own change rather than being smuggled in here.
Verification
:modules:strategy-runtime:test --tests "*BasicPlanInterpreterTest*"passes locally (JDK 21).Context
Found while auditing the strategy domain for business-logic correctness against catalog
basic-elements:2026-08-08.🤖 Generated with Claude Code