Skip to content

feat: add Weighted Moving Average (WMA) and Hull Moving Average (HMA) (#180, #29) - #208

Merged
jealous merged 1 commit into
jealous:masterfrom
huuan26:feat/wma-hull-ma
Oct 19, 2026
Merged

jealous merged 1 commit into
jealous:masterfrom
huuan26:feat/wma-hull-ma

Conversation

@huuan26

@huuan26 huuan26 commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #180. Resolves #29.

Summary

This PR adds support for Weighted Moving Average (WMA) and Hull Moving Average (HMA) as first-class indicator columns in StockDataFrame.

Features

  1. Weighted Moving Average (WMA):

    • Exposes df['wma'], df['close_10_wma'], df['wma_20'], etc.
    • Default window: 10 on close.
    • Adds class method StockDataFrame.wma(series, window) aliasing the vectorized linear_wma.
  2. Hull Moving Average (HMA):

    • Implements Alan Hull's zero-lag formulation:
      • $W_1 = \text{WMA}(p, \lfloor n/2 \rfloor)$
      • $W_2 = \text{WMA}(p, n)$
      • $\text{Diff} = 2 W_1 - W_2$
      • $\text{HMA} = \text{WMA}(\text{Diff}, \lfloor\sqrt{n}\rfloor)$
    • Exposes df['hma'], df['close_16_hma'], df['hma_9'], etc.
    • Default window: 16 on close.
    • Handles edge cases cleanly ($window = 1$, $window > n$, zeroed warmup period).
  3. Handler Key Normalization:

    • Stores default window entries as 1-tuples in self.handler to ensure exact membership lookup and prevent substring collisions (such as 'wma' matching 'vwma').

Verification & Documentation

  • Added unit tests in test.py:
    • test_wma: Verification against 30-day stock data and default/custom windows.
    • test_hma: Verification against 90-day stock data, warmup handling, and custom windows.
    • test_hma_edge_cases: Edge cases ($window=1$, $window > n$, step-by-step benchmark verification).
  • Updated README.md Summary table and Moving Averages section with formulas and usage examples.
  • Code style: flake8 stockstats.py test.py passed with 0 errors.
  • Test suite: 131/131 tests passed.

@jealous
jealous merged commit 13e7d3f into jealous:master Oct 19, 2026
4 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.

WMA and Hull MA Add weighted moving average

2 participants