Skip to content

feat: add On-Balance Volume (OBV) (#32) - #209

Merged
jealous merged 1 commit into
jealous:masterfrom
huuan26:feat/add-obv
Oct 26, 2026
Merged

jealous merged 1 commit into
jealous:masterfrom
huuan26:feat/add-obv

Conversation

@huuan26

@huuan26 huuan26 commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Resolves #32.

Summary

This PR adds support for On-Balance Volume (OBV) as a first-class technical indicator in StockDataFrame.

Features

  1. On-Balance Volume (OBV):

    • Exposes df['obv'] as a first-class indicator column.
    • Vectorized computation using NumPy (np.diff, np.sign, np.cumsum):
      • OBV[0] = 0.0
      • OBV[t] = OBV[t-1] + sign(Close[t] - Close[t-1]) * Volume[t]
    • Adds class method StockDataFrame.obv(close, volume).
    • Naturally supports moving averages and crossovers on OBV (e.g. df['obv_10_sma'], df['obv_20_ema'], df['obv_xu_obv_20_ema']).
    • Cleanly handles edge cases (empty series, single-element series, flat price periods).
  2. Handler Registration:

    • Registered as ("obv",): self._get_obv in self.handler for exact membership lookup and zero substring collision.

Verification & Documentation

  • Added unit tests in test.py:
    • test_obv: Verified against 30-day stock data step-by-step benchmark and moving average calculation (obv_5_sma).
    • test_obv_edge_cases: Verified edge cases (empty series, single-element, flat prices, hand-crafted benchmark verification).
  • Updated README.md Summary table, Introduction list, and Volume section with formula and usage examples.
  • Code style: flake8 stockstats.py test.py passed with 0 errors.
  • Test suite: 137/137 tests passed (96% coverage).

@jealous

jealous commented Oct 21, 2026

Copy link
Copy Markdown
Owner

Please help to resolve the conflict.

@huuan26

huuan26 commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

Conflicts fixed! Rebased onto latest master, all tests & lint pass cleanly. Plz lmk if any changes are needed

@jealous
jealous merged commit e993315 into jealous:master Oct 26, 2026
4 checks passed
@jealous

jealous commented Oct 26, 2026

Copy link
Copy Markdown
Owner

Thanks for the contribution.

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