Skip to content

Disambiguate intra-bar moves in max_advance / max_decline#253

Merged
maread99 merged 2 commits into
mainfrom
claude/dreamy-mendel-nxf0ya
Jun 19, 2026
Merged

Disambiguate intra-bar moves in max_advance / max_decline#253
maread99 merged 2 commits into
mainfrom
claude/dreamy-mendel-nxf0ya

Conversation

@maread99

@maread99 maread99 commented Jun 19, 2026

Copy link
Copy Markdown
Owner

Closes #1.

Problem

max_advance and max_decline (in src/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 -> high within the bar when, in reality, the high preceded the low. The symmetric problem affected max_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:

  • Up bar (close >= open): the low is assumed to have been registered before the high.
  • Down bar (close < open): the high is assumed to have been registered before the low.

Consequences:

  • A bar can only represent an advance within itself when it is an up bar, and a decline within itself only when it is a down bar.
  • A bar's low/high is still available to pair with the extremes of other bars (inter-bar moves are unaffected — the low of an earlier bar always precedes the high of a later bar).

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_decline is the mirror image using the running max of highs.

Tests

  • Existing test_max_advance_and_max_decline continues to pass unchanged.
  • Added test_max_advance_intrabar_disambiguation and test_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

claude and others added 2 commits June 19, 2026 12:37
`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.
@maread99 maread99 added the fix Bug fix label Jun 19, 2026
@maread99 maread99 merged commit 4ce917b into main Jun 19, 2026
8 checks passed
@maread99 maread99 deleted the claude/dreamy-mendel-nxf0ya branch June 19, 2026 14:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

max_chg bug

2 participants