quantwave icon indicating copy to clipboard operation
quantwave copied to clipboard

SMA/EMA warm-up returns partial-window values, contradicting the Python docs

Open lavs9 opened this issue 3 weeks ago • 2 comments

Found while moving the trade_backtest 9-EMA Bounce backtest onto quantwave 0.9.0 (PyPI wheel, Python 3.12, macOS arm64). Every repro below was rerun and reproduces.

import quantwave as q
q.sma(3, [1., 2, 3, 4])   # -> [1.0, 1.5, 2.0, 3.0]
q.ema(3, [1., 2, 3, 4])   # -> [1.0, 1.5, 2.25, 3.125]

Expected: the docstring says "Period > Length: outputs will be NaN until the warmup is satisfied". TA-Lib's SMA gives [nan, nan, 2, 3].

Why it matters:

  • A partial-window SMA used as a regime filter (for example close > SMA200) is silently true or false on the first 199 bars of every newly listed stock.
  • The EMA first-value seed is documented only in the Rust ATR doc comment, not in the Python docstrings. Downstream consumers such as screener need the exact seed to replicate signals bit for bit.

Ask: either emit NaN for the first period-1 bars, or document the partial-window and first-value-seed behaviour in the Python docstrings. Ideally add a warmup="nan"|"partial" option.

lavs9 avatar Nov 11 '26 03:11 lavs9

Also applies to ALMA: pl.DataFrame({'x':[1.0,2,3,4,5]}).select(pl.col('x').ta.alma(5,0.85,6.0)) gives [1.0, 1.464, 2.277, 3.258, 4.258], which should be NaN for the first 4 values. Impact seen in trade_backtest: an SMA(200) regime gate is 'on' throughout year 0 unless the caller masks the warm-up.

lavs9 avatar Nov 11 '26 04:11 lavs9

Resolved in v0.10.0 (commits 080efe0c and df25c2cf).

  • Added WarmupPolicy::Partial (default) and WarmupPolicy::Nan across moving averages (SMA, EMA, ALMA).
  • Under warmup="nan", the first period - 1 bars emit NaN (seeding with SMA after period bars for EMA).
  • Documented partial-window warmup behavior and seeds across Python docstrings and indicator metadata (CONVENTION_NOTES).
  • Regression tests added and verified in tests/python/test_dogfood_fixes.py::test_gh55_warmup_policy_options.

lavs9 avatar Nov 11 '26 05:11 lavs9