Skip to content

Fix ATR calculation for inputs shorter than the window - #369

Open
aerraj wants to merge 1 commit into
bukosabino:masterfrom
aerraj:fix/atr-short-input
Open

aerraj wants to merge 1 commit into
bukosabino:masterfrom
aerraj:fix/atr-short-input

Conversation

@aerraj

@aerraj aerraj commented Aug 27, 2026

Copy link
Copy Markdown

Summary

Fixes #236.

Only initialize the ATR seed when the input contains a complete window. Previously, an empty or short input indexed past the end of the output array and raised IndexError.

This retains the existing zero-valued warm-up convention: a series shorter than the window returns zeros on its original index. It does not change the warm-up policy to NaNs, alter the ATR recurrence, or change results once the window is complete.

Tests

  • Added checks for empty, one-row, and five-row inputs with a six-row window, both fillna settings, and both class/function APIs.
  • Added exact-window and one-extra-row checks to verify seeding and recurrence remain unchanged.
  • Before the fix, all six short-input subcases raised IndexError.
  • python -m unittest test.test_atr_short_input test.unit.volatility: 56 tests passed.
  • Black 23.10.1 (the version in requirements-test.txt) on both changed files and git diff --check: passed.

The full python -m unittest discover run completed 136 tests with two unrelated errors in TestTSIIndicator.test_tsi and test_tsi2: pandas removed the check_less_precise assertion argument. A pristine upstream worktree reproduces the same two errors among its 134 tests. No TSI code or tests were changed.

Environment: Python 3.12, pandas 2.3.3, NumPy 1.26.4. All tests use local data.

Upstream CI status

The CircleCI Python 3.6 job fails during Install dependencies, before lint or tests run: No matching distribution found for numpy==1.21.5. The existing configuration uses Python 3.6 with that NumPy pin. This PR does not modify the CI image or dependency requirements.

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.

average_true_range(..., window=N) raises IndexError in N if greater than length of passed series

1 participant