Fix RSI producing NaN instead of neutral 50 on flat market - #455
Open
cinar wants to merge 1 commit into
Open
Conversation
RSI's `ComputeWithContext` computed RS = averageGain / averageLoss and then RSI = 100 - (100 / (1 + RS)). When a window has zero gains and zero losses (a flat price run, e.g. an untraded bar or synthetic/test data), RS was 0/0 = NaN, so the output RSI was NaN for that bar instead of the conventional neutral value of 50. Replaced the Divide/IncrementBy/MultiplyBy/Pow pipeline with a single helper.OperateWithContext that zips averageGains and averageLosses and special-cases the 0/0 case to 50, otherwise computing the same RS and RSI formula as before. Added TestRsiFlatMarket covering a constant closing-price series and verified the existing CSV-fixture-based TestRsi is unaffected (its fixture has no flat windows). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Xv4stuAb6WuQ8rPZ4cupLp
Codecov Reportβ
All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #455 +/- ##
==========================================
- Coverage 92.48% 91.69% -0.79%
==========================================
Files 229 230 +1
Lines 7302 7355 +53
==========================================
- Hits 6753 6744 -9
- Misses 463 525 +62
Partials 86 86 β View full report in Codecov by Harness. π New features to boost your workflow:
|
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.
Summary
Rsi.ComputeWithContextcomputedRS = averageGain / averageLossand thenRSI = 100 - (100 / (1 + RS)). When a window has zero gains and zero losses (a flat price run β e.g. an untraded bar, or synthetic/test data),averageGainandaverageLossare both0, soRS = 0/0 = NaN, and the resulting RSI for that bar wasNaNinstead of the conventional neutral value of50.DivideWithContextβIncrementBy/MultiplyBy/Powpipeline with a singlehelper.OperateWithContextthat zipsaverageGainsandaverageLossespairwise and special-cases the0/0case to50; the non-flat case computes the identicalRS/RSIformula as before, so normal output is unchanged.momentum/connors_rsi.goand other callers ofmomentum.Rsi/NewRsiβ they only consumeRsi.ComputeWithContext's output channel and don't depend on its internal pipeline shape, so behavior for non-flat windows is unaffected.Streak-based RSI (used byConnorsRsi.StreakRsi) shares the sameRsitype and therefore benefits from the same fix, since a streak can also be flat (i.e.0) with no separate flat-window handling of its own.Test plan
TestRsiFlatMarketinmomentum/rsi_test.go: a constant closing-price series long enough to produce RSI output, asserting every emitted value is exactly50(notNaN).TestRsiis unaffected βmomentum/testdata/rsi.csvhas no flat windows, and the test still passes.go build ./...go vet ./...gofmt -l .(clean for the changed files; unrelated pre-existing formatting diffs exist elsewhere in the repo, outside this change's scope)go test ./...(all packages pass)π€ Generated with Claude Code