Plain API and polars .ta disagree on NaN input (SMA, ATR); polars multi-input output naming and argument order
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)andExpr.ta.atr(high, low, period)take their arguments in different orders.ta.betamakes the receiver the independent (market) series.m.ta.beta(s, n)is the stock's beta, which is the opposite of the natural readingbeta(stock, market). Please document it; see #58.
4. Gaps.
ta.maxandta.minhave empty docstrings and no module-level function.- There is no cross-sectional rank (percentile of a value across symbols on one date).
percent_rankis 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.
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.taand 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, andtrangeto take the receiver series asargs[0], ensuring the output column is named after the receiver column (corclose) instead ofh. - Flexible Argument Order: Plain Python wrappers for
atr,ta_atr,atr_wilderaccept both(period, high, low, close)and(high, low, close, period).sma,ema,max,minaccept 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_rankon Polars expressions (expr.ta.cross_sectional_rank(group_by=...)) and top-levelquantwave.cross_sectional_rank(...)supporting ascending/descending ordering and percentile scaling. - Exported
ta_max/ta_min: Added module-levelq.maxandq.minwith full docstrings. - Regression tests verified in
tests/python/test_dogfood_fixes.py.