Disambiguate intra-bar moves in max_advance / max_decline#253
Merged
Conversation
`max_advance` and `max_decline` inferred the maximum percentage change solely from highs and lows, implicitly assuming that within any single bar the low was always registered before the high. This caused a single 'down' bar (a wide-range bar that closed lower) to be misread as the maximum advance (low -> high), and, symmetrically, a single 'up' bar to be misread as the maximum decline (high -> low). Disambiguate the intra-bar order from the bar direction: on 'up' bars (close >= open) the low is assumed to precede the high, whilst on 'down' bars (close < open) the high is assumed to precede the low. A bar can therefore only contribute an advance within itself when it is an 'up' bar, and a decline within itself only when it is a 'down' bar. The bar's extremes remain available to pair with the extremes of other bars. Closes #1. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Lv7mb1yrXN6TmEefXjJ6xS
Just revises doc and asserts supposed test conditions.
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.
Closes #1.
Problem
max_advanceandmax_decline(insrc/market_analy/analysis.py) inferred the maximum percentage change purely from each bar's high and low, implicitly assuming that within any single bar the low was always registered before the high.This meant a single down bar (a wide-range bar that closed below its open) could be misread as the maximum advance — the code assumed price travelled
low -> highwithin the bar when, in reality, the high preceded the low. The symmetric problem affectedmax_decline: a single up bar could be misread as the maximum decline (high -> low).Fix
Disambiguate the intra-bar order of the high and low from the bar's direction, as proposed in the issue:
close >= open): the low is assumed to have been registered before the high.close < open): the high is assumed to have been registered before the low.Consequences:
Implementation detail: for
max_advance, the lowest low available to pair with a bar's high is the running min including the current bar on up bars, but excluding it on down bars (cummin().shift(1)).max_declineis the mirror image using the running max of highs.Tests
test_max_advance_and_max_declinecontinues to pass unchanged.test_max_advance_intrabar_disambiguationandtest_max_decline_intrabar_disambiguation, each constructing a wide-range bar in the "wrong" direction and asserting the change is measured across bars rather than within the misleading bar.🤖 Generated with Claude Code. Fully manually reviewed.
https://claude.ai/code/session_01Lv7mb1yrXN6TmEefXjJ6xS
Generated by Claude Code