fix: restore missing window sum in CR indicator - #842
Open
NemeZZiZZ wants to merge 1 commit into
Open
Conversation
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.
Problem
The CR indicator's own header documents the formula:
but the implementation never sums over the window — it divides single-bar values:
Since
preMidSubLow === 0wheneverlow ≥ prevMid, every rising bar producescr = 0. The leftoverif (i >= params[0] - 1)delay and the fact thatma1..ma4below are computed overparams[1..4]-bar sums both indicate the window sum was lost.Verified by execution (verbatim logic, Node): on a 500-bar random walk with N=26 the current implementation disagrees with the documented formula on 475 of 500 bars; on a gap-up bar it emits
0where the formula gives221.00.Fix
Two rolling accumulators over
params[0]bars — add the current bar's contribution, subtract the one leaving the window (the same pattern already used forma1Sum..ma4Sumbelow), thencr = highSum / lowSum * 100. The MA thresholds and thei >= params[0] - 1boundary are unchanged, soma1..ma4behave as before, just over correct CR values.Verification
Patched calc vs the naive
O(n·N)formula from the docstring, 500-bar random walk, N=26: max relative diff 0.0, 0/500 mismatches. Gap-up bar:221.00both. DegenerateN=1reproduces the old single-bar ratio exactly. Bar-0 MID semantics (prevData ?? kLineData) unchanged.