quantwave icon indicating copy to clipboard operation
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

Open lavs9 opened this issue 3 weeks ago • 1 comments

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::new sets direction = 1 before 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:

  1. Emit NaN until the ATR has period bars, with the ATR seeded by the SMA of the first period true ranges (the TA-Lib and Wilder convention). That ties into #55.
  2. Do not assume a direction before data. Add initial_direction=\"down\"|\"up\"|\"from_close\".
    • down makes the first close above the upper band an explicit flip to up. That is the IPO-base breakout, and from memory it is TradingView's ta.supertrend start; please verify.
    • from_close sets the first direction from close vs (H+L)/2 at bar period.
  3. 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.

lavs9 avatar Nov 11 '26 04:11 lavs9

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

  • Startup Seeding & NaN Warmup: Under smoothing="wilder" (and configurable warmup), SuperTrend emits NaN until ATR accumulates period bars (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.
  • Docstrings: Documented startup behavior, initial direction options, and ATR smoothing conventions in q.supertrend.__doc__ and SUPERTREND_METADATA.
  • Regression tests verified in tests/python/test_dogfood_fixes.py::test_gh59_supertrend_initial_direction_down_and_wilder and test_gh59_supertrend_nan_warmup_until_period.

lavs9 avatar Nov 11 '26 05:11 lavs9