OPEN-SOURCE SCRIPT

Corwin-Schultz Spread Bands [forexobroker]

291
Corwin-Schultz Spread Bands [forexobroker] estimates the effective bid-ask spread directly from daily high-low data using the Corwin-Schultz (2012) two-bar estimator, then turns the rolling spread into a stress regime and a pair of price bands. The unique angle is using a peer-reviewed microstructure spread formula intended for daily data on any timeframe to flag liquidity stress on the chart and time entries when stress fades into a directional trend.

🔶 ALGORITHM

1. For each pair of adjacent bars compute beta = (ln(H_t / L_t))^2 + (ln(H_{t-1} / L_{t-1}))^2 and gamma = (ln(max(H_t, H_{t-1}) / min(L_t, L_{t-1})))^2.
2. Compute alpha = (sqrt(2 x beta) - sqrt(beta)) / (3 - 2 x sqrt(2)) - sqrt(gamma / (3 - 2 x sqrt(2))).
3. Raw spread = 2 x (exp(alpha) - 1) / (1 + exp(alpha)). Clamp to the [0, 1] range.
4. Smooth with EMA (default 5) to produce a stable percentage spread.
5. Rolling regime stats: 100-bar (default) median and stdev of the smoothed spread; threshold = median + k x stdev (default k = 1.0).
6. In-stress = smoothed spread above threshold. Exiting stress = was above on the prior bar and below now.
7. Trend filter: close versus 20-SMA (default).
8. Visual bands: close +/- (spread / 2) x close x mult.

🔶 SIGNAL LOGIC

- Buy: spread regime just exited stress AND close > trend SMA AND session filter passes AND posState was not long AND cooldown elapsed AND barstate.isconfirmed.
- Sell: spread regime just exited stress AND close < trend SMA AND session filter passes AND posState was not short AND cooldown elapsed AND barstate.isconfirmed.

A flip-prevention posState plus cooldown bars (default 15) avoid stacking signals inside a single regime transition.

🔶 INPUTS

- Spread Estimator: smoothing EMA default 5, regime lookback default 100, stdev multiplier default 1.0.
- Signal Logic: trend SMA length default 20, cooldown bars default 15.
- Filters: optional session window default 0000-2400.
- Visual: spread band multiplier default 1.0, glow, fill, bands, stress bgcolor, neon palette, dashboard.

🔶 ALERTS

CSS Buy, CSS Sell, CSS Any Signal, CSS Enter Stress, CSS Exit Stress, CSS Spread Rising, CSS Spread Falling, CSS Trend Up, CSS Trend Down, CSS Extreme Spread, CSS Webhook JSON.

🔶 LIMITATIONS

- The estimator was designed for daily bars; on very low timeframes the high-low range gets dominated by tick noise and the spread value is noisier than the daily case.
- Spread is computed in price-fraction terms; reporting in basis points assumes that interpretation is meaningful for the instrument.
- The regime threshold depends on a 100-bar lookback, so on fresh history the threshold is unstable until the buffer is full.
- Signals require the regime to flip, so during long sideways regimes with no stress event the indicator can sit idle for many bars.
- The 20-SMA trend filter is intentionally simple and will misclassify direction during tight ranges.

إخلاء المسؤولية

لا يُقصد بالمعلومات والمنشورات أن تكون، أو تشكل، أي نصيحة مالية أو استثمارية أو تجارية أو أنواع أخرى من النصائح أو التوصيات المقدمة أو المعتمدة من TradingView. اقرأ المزيد في شروط الاستخدام.