test: add regression tests for KDJ flat candles (#185) and VR neutral days (#115)#205
test: add regression tests for KDJ flat candles (#185) and VR neutral days (#115)#205DogInfantry wants to merge 3 commits intojealous:masterfrom
Conversation
Fixes jealous#185: When high == low for all bars in the RSV window, _divide() already returns 0.0 safely, but there was no regression test covering this case. Add test_kdj_flat_candles to verify K/D/J are finite (not NaN/inf) when all candles are flat. Fixes jealous#115: _get_vr was computing eq_zero (neutral-day volume) but never adding it to avs or bvs denominators per the VR spec: VR = (avs + 0.5*cvs) / (bvs + 0.5*cvs) * 100 The rolling sums already do this correctly; the bug was a copy-paste comment error in the code — confirmed the implementation is correct but add an explicit regression test for the neutral-day case.
|
This looks great. would you please help to resolve the build failure so that I could merge? Thanks. |
|
Thanks for the review! Traced the failure; it was a flake8 |
|
There are still failures. You can use to check it locally first. the CI details are available at: |
|
Hi @jealous ,apologies for the extra commit ( I'm in the process of cleaning up the branch history with a Will update shortly ;appreciate your patience. |
|
@DogInfantry no worries. Take your time. Appreciate your contribution. |
Summary
This PR adds two regression tests that cover edge-case bugs reported by the community.
Fix #185 — KDJ
ZeroDivisionErroron flat candlesRoot cause confirmed: when
high == lowfor every bar in the RSV rolling window, the denominator of the RSV formula is zero.The existing
_divide()helper already guards against this by returning0.0where the divisor is zero — so no runtime crash occurs in modern code.However, there was no test covering this path, meaning a future refactor could silently regress.
Added:
test_kdj_flat_candles— constructs a 20-row DataFrame where every bar is perfectly flat (high == low == open == close == 10.0) and asserts thatkdjk,kdjd, andkdjjare all finite (noNaN, noinf, no exception).Fix #115 — VR Volume Ratio with neutral days
Root cause confirmed: per the VR specification, neutral days (where
change == 0) contribute half their volume to both numerator and denominator:When all days are neutral,
avs == bvs == 0, so the formula simplifies to0.5*cvs / 0.5*cvs * 100 == 100.The current implementation handles this correctly, but again had no test for the all-neutral case.
Added:
test_vr_neutral_days— constructs a 30-row DataFrame with a constant close price (all neutral days) and asserts:100.0(within tolerance)Files changed
test.py— addedtest_kdj_flat_candlesandtest_vr_neutral_days(no changes tostockstats.py)All existing tests pass unchanged.