quantwave
quantwave copied to clipboard
SuperTrend start-up: emits from bar 0 with a 1-bar ATR and assumes an uptrend, which hides the first trend of newly listed stocks
Found in trade_backtest (9-EMA Bounce, quantwave 0.9.0, weekly bars of 1,898 NSE stocks that listed after 2001).
What quantwave does:
SuperTrend::newsetsdirection = 1before any data.- The ATR is an EMA seeded with the first true range, so bar 0 already has a SuperTrend value built from a one-bar ATR.
- A stock that lists and rises is therefore "up" from its first bar. It never shows a down-to-up flip.
Why it is wrong for listings:
- The trend direction at listing is assumed, not measured.
- Bands from a one-bar ATR are noise for roughly the first period bars.
- Strategies that key on a flip to up (a new up-leg) cannot see a listing's first trend, which is often its IPO-base breakout.
- Measured on the weekly SuperTrend(10,3):
quantwave 0.9.0 SMA-seeded ATR, no output before bar 10, first direction from close vs (H+L)/2 Stocks never flipping down in their first 150 weekly bars 116 90 Bounce setups in the first 150 weeks after listing 232 281 (-17% with quantwave) Divergence until about bar 116 for some listings
Ask:
- Emit NaN until the ATR has
periodbars, with the ATR seeded by the SMA of the firstperiodtrue ranges (the TA-Lib and Wilder convention). That ties into #55. - Do not assume a direction before data. Add
initial_direction=\"down\"|\"up\"|\"from_close\".downmakes the first close above the upper band an explicit flip to up. That is the IPO-base breakout, and from memory it is TradingView'sta.supertrendstart; please verify.from_closesets the first direction from close vs (H+L)/2 at barperiod.
- Document the start-up convention in the Python docstring.
q.supertrend.__doc__is None today; see #58.
screener and trade_backtest need the same convention bit for bit, so whichever default is chosen, it must be explicit and versioned.
Resolved in v0.10.0 (commits 080efe0c and df25c2cf).
- Startup Seeding & NaN Warmup: Under
smoothing="wilder"(and configurable warmup), SuperTrend emitsNaNuntil ATR accumulatesperiodbars (seeded via SMA of True Ranges). - Initial Direction Configuration: Added
initial_direction="down" | "up" | "from_close"to both the plain API (q.supertrend) and Polars (df.ta.supertrend/expr.ta.supertrend).- When set to
"down", SuperTrend initializes at upper resistance with direction-1. The first close above the upper band cleanly flips to+1(uptrend), capturing IPO-base breakout setups and restoring the 17% of setups previously lost in early listing windows.
- When set to
- Docstrings: Documented startup behavior, initial direction options, and ATR smoothing conventions in
q.supertrend.__doc__andSUPERTREND_METADATA. - Regression tests verified in
tests/python/test_dogfood_fixes.py::test_gh59_supertrend_initial_direction_down_and_wilderandtest_gh59_supertrend_nan_warmup_until_period.