Skip to content

fix(grid): use Wilder ATR for adaptive grid bounds (inline, no new dependency) - #387

Merged
brokermr810 merged 2 commits into
OpenByteInc:mainfrom
Mattbusel:use-fin-primitives
Nov 16, 2026
Merged

brokermr810 merged 2 commits into
OpenByteInc:mainfrom
Mattbusel:use-fin-primitives

Conversation

@Mattbusel

@Mattbusel Mattbusel commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

The adaptive grid sized its bounds from a simple moving average of true range, not Wilder's ATR, so the bounds reacted differently from the ATR traders see on their charts. This PR implements Wilder ATR inline in grid_runtime.py, with no new dependency.

What changes

  • _compute_atr seeds with the mean of the first period true ranges (the first TR is high - low), then applies Wilder's RMA, atr = (atr*(period-1) + tr)/period, over the full frame. There is no window truncation.
  • Inputs are validated explicitly. Missing high/low/close columns log a warning and return 0.0. Bars with missing or non-finite values are skipped, so the previous close comes from the last valid bar.
  • Fewer than period valid bars returns 0.0, as before, so the existing range/min-width fallback takes over.
  • No new dependencies. requirements.txt and requirements.lock are unchanged from the base.

Tests (tests/test_grid_runtime.py, 14 tests)

  • Exact agreement with a hand-written Wilder reference, and a check that the result differs from a simple average of the last period TRs.
  • period - 1, exactly period, and a long history (period*10 + 37) that uses the full frame.
  • NaN, inf, None and non-numeric bars, missing columns (logged), and empty input.
  • The range fallback when ATR is unavailable, the min-width fallback when bars have no high/low, and an unchanged fixed-bound grid (adaptiveBounds: false).

All grid-related test files: 92 passed. ruff check app scripts tests is clean, bandit reports nothing on the module, and both commits are signed off for DCO.

Rollback: revert the PR. There are no data or config changes.

@codecov

codecov Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

_compute_atr averaged true range with rolling(period, min_periods=1).mean(),
an SMA that also returned a value after a single bar. Compute Wilder's
RMA-smoothed ATR with fin-primitives instead, return 0.0 until period bars
exist so the existing range fallback applies, and keep the pandas path when
the package is unavailable.

Signed-off-by: Matthew Charles Busel <mattbusel@gmail.com>
@brokermr810

Copy link
Copy Markdown
Collaborator

Thanks for identifying the SMA-vs-Wilder ATR issue and for the clear disclosure about maintaining fin-primitives. The correction is useful, but we would like this PR revised before merge in the following direction:

  1. Please implement Wilder ATR/RMA directly in grid_runtime.py instead of adding a new production dependency for this single calculation. Remove the fin-primitives entries from both requirements files.
  2. Keep behavior deterministic across every deployment environment. There should not be a fin-primitives present -> Wilder ATR / package absent -> old SMA split.
  3. Avoid swallowing every exception silently. Validate the required OHLC columns and finite numeric inputs explicitly, and let the existing adaptive-range fallback handle an unavailable ATR in a predictable way. If an unexpected calculation failure is intentionally converted to 0.0, it should be observable rather than silently hidden.
  4. Use the standard Wilder seed (mean of the first period true ranges) followed by the recursive RMA update. Please do not use an unverified period * 10 truncation unless its error bound is documented and tested; using the full frame supplied to this helper is preferable here.
  5. Preserve the warmup behavior: fewer than period valid bars should return 0.0, allowing the existing range/min-width fallback to take over.
  6. Expand regression coverage for:
    • period - 1, exactly period, and long histories greater than period * 10;
    • NaN/Inf or missing OHLC values;
    • exact agreement with a hand-written Wilder reference;
    • confirmation that fixed-bound grids and non-adaptive behavior remain unchanged;
    • the fallback/error paths currently reported as uncovered by Codecov.

Please also run the complete repository CI/DCO/security checks after updating the branch. Once those changes are in place, we can review the revised implementation again.

Seed with the mean of the first period true ranges, then Wilder's RMA over
the full frame. Validate OHLC columns, skip non-finite bars, log when ATR is
unavailable, and keep the 0.0 warmup so the range/min-width fallback applies.
Removes fin-primitives from requirements.txt and requirements.lock.

Signed-off-by: Matthew Charles Busel <mattbusel@gmail.com>
@Mattbusel

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed review. Revised in 94eee13:

  1. Wilder ATR is now implemented directly in grid_runtime.py, and fin-primitives is removed from requirements.txt and requirements.lock.
  2. There's one code path now, with no optional import and no SMA fallback.
  3. No blanket except. Missing high/low/close columns log a warning and return 0.0. Bars with missing or non-finite values (NaN, inf, non-numeric) are skipped explicitly, so the previous close always comes from the last valid bar.
  4. The standard seed: the mean of the first period true ranges (the first TR is high - low), then atr = (atr*(period-1) + tr)/period over the full frame. No period*10 truncation.
  5. Fewer than period valid bars returns 0.0, so the existing range/min-width fallback takes over.
  6. New tests: period-1 and exactly period bars, a long history (period*10+37) that matches the full-frame reference and differs from a truncated window, NaN/inf/None/string bars, missing columns (logged), empty input, exact agreement with the hand-written Wilder reference, the range fallback when ATR is 0, the min-width fallback when bars have no high/low (the previously uncovered branch, bounds 96/104), and an unchanged fixed-bound grid (adaptiveBounds: false).

pytest on all grid-related test files: 92 passed. ruff check app scripts tests is clean, and bandit reports nothing on the module. Both commits are signed off for DCO.

@sonarqubecloud

Copy link
Copy Markdown

@brokermr810

Copy link
Copy Markdown
Collaborator

Thanks for the quick and thorough revision. I reviewed 94eee13 against the requested changes, and the implementation now follows the direction we wanted:

  • fin-primitives and the requirements changes are fully removed;
  • Wilder ATR is implemented inline with the standard seed and full-frame RMA recursion;
  • warmup and range/min-width fallback behavior are deterministic;
  • missing columns and invalid rows are handled explicitly without a blanket exception;
  • the long-history, invalid-data, fallback, and fixed-bound regression cases are covered.

I also ran the updated tests/test_grid_runtime.py locally against the latest commit: 14 passed. I did not find a blocking logic issue in the revised diff.

One remaining documentation item: please update the PR title/body details where necessary, especially the current description that still says the implementation uses fin-primitives, the optional-import fallback, and the old test counts. The final PR description should describe the inline Wilder implementation and the latest test evidence.

The latest head currently only shows the SonarCloud checks. Once the full repository CI/DCO/security/coverage checks have run successfully and the PR description is current, this should be ready for final approval and merge.

@Mattbusel Mattbusel changed the title fix(grid): use Wilder ATR for adaptive grid bounds fix(grid): use Wilder ATR for adaptive grid bounds (inline, no new dependency) Nov 16, 2026
@Mattbusel

Copy link
Copy Markdown
Contributor Author

Thanks for checking it locally. I've updated the title and description to cover the inline Wilder implementation and the latest test results. The Backend CI, Security CI, DCO and REUSE runs on 94eee13 show action_required, so they look to be waiting on maintainer approval for workflows from a fork. They should start once approved.

@brokermr810
brokermr810 merged commit 79caf19 into OpenByteInc:main Nov 16, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants