OPEN-SOURCE SCRIPT
Corwin-Schultz Spread Bands [forexobroker]

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.
🔶 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的精神,该脚本的作者将其开源,以便交易者可以查看和验证其功能。向作者致敬!您可以免费使用该脚本,但请记住,重新发布代码须遵守我们的网站规则。
🔵Gain access to our powerful tools : forexobroker.com/tv
🔵Join our free telegram for updates : t.me/dominicwalsfxhtrader
All scripts & content provided by Forexobroker are for informational & educational purposes only.
🔵Join our free telegram for updates : t.me/dominicwalsfxhtrader
All scripts & content provided by Forexobroker are for informational & educational purposes only.
免责声明
这些信息和出版物并非旨在提供,也不构成TradingView提供或认可的任何形式的财务、投资、交易或其他类型的建议或推荐。请阅读使用条款了解更多信息。
开源脚本
秉承TradingView的精神,该脚本的作者将其开源,以便交易者可以查看和验证其功能。向作者致敬!您可以免费使用该脚本,但请记住,重新发布代码须遵守我们的网站规则。
🔵Gain access to our powerful tools : forexobroker.com/tv
🔵Join our free telegram for updates : t.me/dominicwalsfxhtrader
All scripts & content provided by Forexobroker are for informational & educational purposes only.
🔵Join our free telegram for updates : t.me/dominicwalsfxhtrader
All scripts & content provided by Forexobroker are for informational & educational purposes only.
免责声明
这些信息和出版物并非旨在提供,也不构成TradingView提供或认可的任何形式的财务、投资、交易或其他类型的建议或推荐。请阅读使用条款了解更多信息。