quantwave icon indicating copy to clipboard operation
quantwave copied to clipboard

Plain API and polars .ta disagree on NaN input (SMA, ATR); polars multi-input output naming and argument order

Open lavs9 opened this issue 3 weeks ago • 1 comments

Found while rebuilding Trend Target Ribbon in trade_backtest on quantwave 0.9.0 (PyPI, Python 3.12). Reproduced.

1. The two APIs give different results for the same input.

import numpy as np, polars as pl, quantwave as q
q.sma(3, np.array([1.0, 2, np.nan, 4, 5, 6]))
# -> [1.0, 1.5, nan, nan, nan, 5.0]     plain: NaN for the window, then recovers

pl.DataFrame({'x': [1.0, 2, float('nan'), 4, 5, 6]}).select(pl.col('x').ta.sma(3))['x'].to_list()
# -> [1.0, 1.5, nan, 2.333, 3.667, 5.0]  polars: skips the NaN and averages the last 3 valid values

ATR in polars also skips the NaN bar and resumes the recursion. screener and trade_backtest may use different layers, and then they cannot agree bit for bit. Both layers should follow one documented NaN policy; see #56.

2. Output naming. pl.col('c').ta.atr(pl.col('h'), pl.col('l'), 5) returns a column named h, not c. trange does the same. In a select that collides with the real h column unless you .alias it.

3. Argument order.

  • quantwave.atr(period, high, low, close) and Expr.ta.atr(high, low, period) take their arguments in different orders.
  • ta.beta makes the receiver the independent (market) series. m.ta.beta(s, n) is the stock's beta, which is the opposite of the natural reading beta(stock, market). Please document it; see #58.

4. Gaps.

  • ta.max and ta.min have empty docstrings and no module-level function.
  • There is no cross-sectional rank (percentile of a value across symbols on one date). percent_rank is a time-series rank. Strategies need a cross-sectional rank for beta and RS filters.

Positive: ALMA matches the Pine formula to 3e-14, ATR matches a Wilder-style recursion exactly, and an indicator pass over 4,226 symbols takes about 8 s.

lavs9 avatar Nov 11 '26 04:11 lavs9

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

  • Plain API & Polars NaN Parity: Removed the skipping filter in Polars expression plugins (sma, ema, atr). Polars .ta and plain streaming indicators now process NaNs identically for bit-for-bit parity across screener and backtest.
  • Output Column Naming: Fixed Polars plugin registrations for atr, natr, and trange to take the receiver series as args[0], ensuring the output column is named after the receiver column (c or close) instead of h.
  • Flexible Argument Order: Plain Python wrappers for atr, ta_atr, atr_wilder accept both (period, high, low, close) and (high, low, close, period). sma, ema, max, min accept both (period, series) and (series, period).
  • Receiver Documentation: Documented m.ta.beta(s, n) receiver convention (m=market, s=stock).
  • Cross-Sectional Universe Ranking: Added cross_sectional_rank on Polars expressions (expr.ta.cross_sectional_rank(group_by=...)) and top-level quantwave.cross_sectional_rank(...) supporting ascending/descending ordering and percentile scaling.
  • Exported ta_max / ta_min: Added module-level q.max and q.min with full docstrings.
  • Regression tests verified in tests/python/test_dogfood_fixes.py.

lavs9 avatar Nov 11 '26 05:11 lavs9