Fix Rust RSI moving-average type and max-value regression#4382
Merged
Conversation
The Rust-core RSI ignored the `ma_type` argument (inner gain/loss averages were always built as Exponential) and never advanced `last_value` on zero-loss bars, pinning RSI at `rsi_max` (1.0) even after real down-moves. Both defects diverged from the Cython source of truth; the second is the same bug fixed for Cython in nautechsystems#2703. - Resolve `ma_type` once and pass it into `MovingAverageFactory::create` for both averages. - Advance `last_value` in the zero-loss branch before the early return. - Add regression tests: ma_type distinctness, recovery below max after losses, and a Wilder golden series vs published reference values. Closes #1 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Satisfy the repo's custom pre-commit hooks: replace ', got' phrasing with ', was' in two assert messages, and add the required blank line above the `for` loop in the Wilder golden-series test. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
cjdsellers
approved these changes
Jul 5, 2026
cjdsellers
left a comment
Member
There was a problem hiding this comment.
Thank you for the fix @bebop23 🙏
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.
Pull Request
NautilusTrader prioritizes correctness and reliability, please follow existing patterns for validation and testing.
CONTRIBUTING.mdand followed the established practicesSummary
The Rust-core
RelativeStrengthIndex(crates/indicators/src/momentum/rsi.rs) had two numerical defects that diverged from the Cython source of truth. First, thema_typeargument was ignored — both inner gain/loss averages were always built asExponential, soWilder/Simple/Exponentialproduced identical output. Second,last_valuewas never advanced on zero-loss bars (the assignment sat after the earlyreturn), pinning RSI atrsi_max(1.0) even after real down-moves — the same bug fixed for the Cython indicator in #2703.Related Issues/PRs
Rust port of the Cython fix in #2703 (originally reported for Cython in #2673). The v2 Rust indicator regressed both behaviors relative to the Cython source of truth (#2507).
Type of change
Breaking change details (if applicable)
N/A
Documentation
docs/developer_guide/docs.md)Release notes
RELEASES.mdthat follows the existing conventions (when applicable)Testing
Ensure new or changed logic is covered by tests.
Added three Rust unit tests in
crates/indicators/src/momentum/rsi.rs:test_ma_type_is_plumbed_into_inner_averages—Wilder/Simple/Exponentialnow produce distinct output on a fixed 15-close series.test_recovers_below_max_after_losses— a rise-then-fall series drops belowrsi_maxonce losses arrive (guards the flat-1.0 regression).test_wilder_golden_series— Wilder RSI matches published reference values (0.8935, 0.7269, 0.5586, 0.4192, 0.3489) after each down-move.The existing tests only exercised the default (
Exponential) path with monotonic/constant inputs, which is why both defects slipped the earlier parity pass. Full crate suite green (cargo test -p nautilus-indicators --lib momentum::rsi, 17 passed) and clippy clean.