OPEN-SOURCE SCRIPT
更新済

RC Tools - CUSUM Drift Detector

319
────────────────────────────────────────────────────────────────────

█ OVERVIEW

Most trend tools measure a single bar or a moving-average deviation, so a slow, persistent drift that never produces one dramatic bar can slip under their radar. This tool applies CUSUM — Cumulative Sum Control Chart, a classical statistical process control technique — to detect exactly that: a sustained departure from "no drift" that accumulates over many small moves rather than one large one.

█ WHAT IT DOES

Tracks two running sums of log returns — one for upward drift, one for downward — and flags a directional regime once the accumulated drift breaches a volatility-scaled threshold. Colours the chart background Bullish or Bearish Drift accordingly, plots the two accumulating sums against their threshold lines in a dedicated pane, and shows a table with the current state, how long price has been in it, and historical base rates (average forward return and win rate) for each state.

█ THE THEORY BEHIND IT

CUSUM was developed by E.S. Page in 1954 for detecting a persistent shift in a manufacturing process mean — the same statistical question as "has this market started drifting in a new direction," just applied to price instead of a factory line. The key property that separates it from a moving average or a single-bar threshold: CUSUM accumulates. A string of small, consistent moves in one direction builds up and eventually crosses the detection threshold, even if no individual bar looks remarkable. Pure noise — moves that cancel out — never accumulates at all, because a small "allowance" is subtracted from every observation before it's added to the running sum.

Critically, the reference point CUSUM measures departure from is fixed at zero, not a rolling average of the same series. A rolling mean would chase the trend and cancel out the very drift being measured — anchoring at a fixed "no drift" baseline is what makes the classical test work.

█ HOW IT IS CALCULATED

1. Compute log returns of the selected source.
2. Estimate sigma — the rolling standard deviation of those log returns over the Window Length — as the local noise scale.
3. Two running sums accumulate each bar:
S+ = max(0, S+ prev + (log return − k)) — accumulates upward drift
S− = min(0, S− prev + (log return + k)) — accumulates downward drift
where k (the Drift Allowance) is a small multiple of sigma, subtracted out so ordinary noise never builds a signal.
4. When S+ crosses above the threshold h (a larger multiple of sigma), a sustained upward drift is declared, the background flips Bullish, and S+ resets to zero to begin monitoring fresh. S− works symmetrically for downward drift.
5. Between detections, the classification holds — this is deliberate persistence, not a bug: CUSUM is built to answer "has the regime changed," not to flicker every bar.

Classification occurs ONLY on confirmed bar close — the plotted sums, the background colour and the table all update together, so nothing here can disagree mid-bar or flip back and forth as the current bar forms.

█ SETTINGS & CONFIGURATION

• Source (default close)
• Window Length (default 14) — rolling window for the sigma (noise-scale) estimate
• Decision Threshold h (default 2.0σ) — how much accumulated drift is required before a regime shift is declared
• Drift Allowance k (default 0.2σ) — moves smaller than this are subtracted out and never accumulate
• Forward Return Window (default 20 bars) — the horizon used for the base-rate table
• Table position and colours are fully configurable; the main-chart background painting can be toggled off if you only want the CUSUM pane

█ HOW TO USE IT

Use it as a slow-drift filter alongside faster tools, not as a standalone entry trigger. Because CUSUM only flips after drift has genuinely accumulated, it tends to confirm a regime later than a fast oscillator but with fewer false starts in choppy conditions — the trade-off is lag for reliability. Check the base-rate table's sample count before treating any single state as meaningfully predictive.

Works on any asset and timeframe with sufficient history for the Window Length.

█ LIMITATIONS

• CUSUM detects a PERSISTENT departure from zero drift, not a magnitude or overbought/oversold level. Any use of it as a precise reversal forecast is a misuse.
• The fixed zero reference is directional-agnostic to any trend that existed before the current accumulation window began — it only measures drift accumulated since the last reset.
• h and k are both expressed in sigma multiples; a poorly-fit Window Length will misclassify ordinary volatility as drift, or vice versa.
• Resets on trigger mean the tool can flip again quickly after one large accumulation event, then need to rebuild before flipping a second time.
• Historical base-rate stats need a meaningful sample count (check N) before being trusted, especially in a low-frequency-flip regime or on a short history.
• This script does NOT repaint. All classification updates on confirmed bar close only.

█ DISCLAIMER

For educational and informational purposes only. Nothing here is financial advice. Past behaviour of any drift state does not indicate future results. Trade at your own risk.
リリースノート
Removed Performance Table

免責事項

これらの情報および投稿は、TradingViewが提供または承認する金融、投資、取引、またはその他の種類の助言もしくは推奨であることを意図したものではなく、またこれらに該当するものでもありません。詳細は利用規約をご覧ください。