OPEN-SOURCE SCRIPT

Deadband Hysteresis Filter [BackQuant]

624
Deadband Hysteresis Filter [BackQuant]

What this is
This tool builds a “debounced” price baseline that ignores small fluctuations and only reacts when price meaningfully departs from its recent path. It uses a deadband to define how much deviation matters and a hysteresis scheme to avoid rapid flip-flops around the decision boundary. The baseline’s slope provides a simple trend cue, used to color candles and to trigger up and down alerts.

Why deadband and hysteresis help
  • They filter micro noise so the baseline does not react to every tiny tick.
  • They stabilize state changes. Hysteresis means the rule to start moving is stricter than the rule to keep holding, which reduces whipsaw.
  • They produce a stepped, readable path that advances during sustained moves and stays flat during chop.


How it works (conceptual)
At each bar the script maintains a running baseline dbhf and compares it to the input price p.
  • Compute a base threshold baseTau using the selected mode (ATR, Percent, Ticks, or Points).
  • Build an enter band tauEnter = baseTau × Enter Mult and an exit band tauExit = baseTau × Exit Mult where typically Exit Mult < Enter Mult.
  • Let diff = p − dbhf.
    If diff > +tauEnter, raise the baseline by response × (diff − tauEnter).
    If diff < −tauEnter, lower the baseline by response × (diff + tauEnter).
    Otherwise, hold the prior value.
  • Trend state is derived from slope: dbhf > dbhf[1] → up trend, dbhf < dbhf[1] → down trend.


Inputs and what they control
Threshold mode
  • ATRbaseTau = ATR(atrLen) × atrMult. Adapts to volatility. Useful when regimes change.
  • PercentbaseTau = |price| × pctThresh%. Scale-free across symbols of different prices.
  • TicksbaseTau = syminfo.mintick × tickThresh. Good for futures where tick size matters.
  • PointsbaseTau = ptsThresh. Fixed distance in price units.

Band multipliers and response
  • Enter Mult — outer band. Price must travel at least this far from the baseline before an update occurs. Larger values reject more noise but increase lag.
  • Exit Mult — inner band for hysteresis. Keep this smaller than Enter Mult to create a hold zone that resists small re-entries.
  • Response — step size when outside the enter band. Higher response tracks faster; lower response is smoother.


UI settings
  • Show Filtered Price — plots the baseline on price.
  • Paint candles — colors bars by the filtered slope using your long/short colors.


How it can be used
  • Trend qualifier — take entries only in the direction of the baseline slope and skip trades against it.
  • Debounced crossovers — use the baseline as a stabilized surrogate for price in moving-average or channel crossover rules.
  • Trailing logic — trail stops a small distance beyond the baseline so small pullbacks do not eject the trade.
  • Session aware filtering — widen Enter Mult or switch to ATR mode for volatile sessions; tighten in quiet sessions.


Parameter interactions and tuning
  • Enter Mult vs Response — both govern sensitivity. If you see too many flips, increase Enter Mult or reduce Response. If turns feel late, do the opposite.
  • Exit Mult — widening the gap between Enter and Exit expands the hold zone and reduces oscillation around the threshold.
  • Mode choice — ATR adapts automatically; Percent keeps behavior consistent across instruments; Ticks or Points are useful when you think in fixed increments.
  • Timeframe coupling — on higher timeframes you can often lower Enter Mult or raise Response because raw noise is already reduced.


Concrete starter recipes
  • General purpose — ATR mode, atrLen=14, atrMult=1.0–1.5, Enter=1.0, Exit=0.5, Response=0.20. Balanced noise rejection and lag.
  • Choppy range filter — ATR mode, increase atrMult to 2.0, keep Response≈0.15. Stronger suppression of micro-moves.
  • Fast intraday — Percent mode, pctThresh=0.1–0.3, Enter=1.0, Exit=0.4–0.6, Response=0.30–0.40. Quicker turns for scalping.
  • Futures ticks — Ticks mode, set tickThresh to a few spreads beyond typical noise; start with Enter=1.0, Exit=0.5, Response=0.25.


Strengths
  • Clear, explainable logic with an explicit noise budget.
  • Multiple threshold modes so the same tool fits equities, futures, and crypto.
  • Built-in hysteresis that reduces flip-flop near the boundary.
  • Slope-based coloring and alerts that make state changes obvious in real time.


Limitations and notes
  • All filters add lag. Larger thresholds and smaller response trade faster reaction for fewer false turns.
  • Fixed Points or Ticks can under- or over-filter when volatility regime shifts. ATR adapts, but will also expand bands during spikes.
  • On extremely choppy symbols, even a well tuned band will step frequently. Widen Enter Mult or reduce Response if needed.
  • This is a chart study. It does not include commissions, slippage, funding, or gap risks.


Alerts
  • DBHF Up Slope — baseline turns from down to up on the latest bar.
  • DBHF Down Slope — baseline turns from up to down on the latest bar.


Implementation details worth knowing
  • Initialization sets the baseline to the first observed price to avoid a cold-start jump.
  • Slope is evaluated bar-to-bar. The up and down alerts check for a change of slope rather than raw price crossings.
  • Candle colors and the baseline plot share the same long/short palette with transparency applied to the line.


Practical workflow
  • Pick a mode that matches how you think about distance. ATR for volatility aware, Percent for scale-free, Ticks or Points for fixed increments.
  • Tune Enter Mult until the number of flips feels appropriate for your timeframe.
  • Set Exit Mult clearly below Enter Mult to create a real hold zone.
  • Adjust Response last to control “how fast” the baseline chases price once it decides to move.


Final thoughts
Deadband plus hysteresis gives you a principled way to “only care when it matters.” With a sensible threshold and response, the filter yields a stable, low-chop trend cue you can use directly for bias or plug into your own entries, exits, and risk rules.

免责声明

这些信息和出版物并不意味着也不构成TradingView提供或认可的金融、投资、交易或其它类型的建议或背书。请在使用条款阅读更多信息。