quantwave icon indicating copy to clipboard operation
quantwave copied to clipboard

0.10.0: plain quantwave.atr and polars .ta.atr are different indicators; polars .ta lacks warmup=; roc/rocp unit mismatch

Open lavs9 opened this issue 3 weeks ago • 1 comments

Found in trade_backtest on quantwave 0.10.0 (PyPI wheel). Reproduced. This breaks the bit-for-bit parity between the plain and polars layers that #60 aimed for, and screener and trade_backtest may use different layers.

import numpy as np, polars as pl, quantwave as q
rng = np.random.default_rng(1)
cc = 100 + np.cumsum(rng.normal(0, 1, 120))
hh = cc + rng.uniform(0, 2, 120)
ll = cc - rng.uniform(0, 2, 120)

a1 = np.array(q.atr(14, hh, ll, cc))   # plain: EMA, alpha 2/15, value from bar 0
a2 = pl.DataFrame({'h': hh, 'l': ll, 'c': cc}).select(
    pl.col('c').ta.atr(pl.col('h'), pl.col('l'), 14))['c'].to_numpy()   # polars: Wilder, alpha 1/14, first value at bar 14
a1[100], a2[100]   # 2.2230 vs 2.1427

pl.DataFrame({'x': [1., 2, 3, 4]}).select(pl.col('x').ta.sma(3, warmup='nan'))
# TypeError: TaNamespace.sma() got an unexpected keyword argument 'warmup'
pl.DataFrame({'x': [1., 2, 3, 4]}).select(pl.col('x').ta.sma(3))['x'].to_list()
# [1.0, 1.5, 2.0, 3.0]   still partial; there is no way to get NaN warm-up in polars

q.roc(1, [1., 2.])[1]   # 100.0 (percent); polars .ta.rocp is a fraction; there is no top-level rocp

Asks:

  1. Make atr the same indicator in both layers, with smoothing= exposed in both. Today the same name means EMA in one layer and Wilder in the other.
  2. Add warmup= to the polars .ta moving averages (sma, ema, alma).
  3. Add a top-level rocp, or document that roc is in percent and rocp is a fraction.
  4. Nice to have: a run or gap reset helper that restarts an indicator after a hole of more than N bars in a series (trade_backtest rule S14), so screener can replicate it.

lavs9 avatar Nov 11 '26 08:11 lavs9

Resolved in v0.10.1 (commit 774fb70d, tag v0.10.1).

  • ATR Plain vs. Polars Parity: Both quantwave.atr and pl.col().ta.atr now default to Wilder's RMA (alpha = 1/period, SMA-seeded after period bars), giving bit-for-bit identical results (2.1427 on the repro). Both layers and Atr streaming class support smoothing='wilder' and smoothing='ema' (2.2230 on the repro).
  • Polars MA Warmup: Added warmup='partial' | 'nan' across pl.col().ta.sma, pl.col().ta.ema, and pl.col().ta.alma.
  • Top-Level ROCP: Added quantwave.rocp and quantwave.Rocp returning the fractional return ratio (p - p_prev) / p_prev. Clarified docs on roc (percent, 100x) vs rocp (fraction, 1x).
  • Gap Reset Helper (Rule S14): Added quantwave.apply_with_gap_reset(indicator_factory, *series_args, max_gap=5) to cleanly reset stateful indicators when gaps exceeding max_gap consecutive NaN/missing bars are encountered.

lavs9 avatar Nov 11 '26 09:11 lavs9