OPEN-SOURCE SCRIPT
CM - Bollinger Bands Mean Reversion [CriptoMatiko]

This indicator tracks **Bollinger Bands mean reversion signals**, replicating the entry and exit logic of a Python backtester that evaluated 81 long + 81 short parameter combinations on ETH/USDT 4H (Bybit Futures Linear, Mar 2025 – Aug 2026).
**The central finding:** the stop loss matters more than the band parameters.
| Setup | Direction | SL | Trades | Win Rate | Return |
|---|---|---|---|---|---|
| Standard (what tutorials teach) | Long | 2% | 110 | 38% | **-22.8%** |
| Optimized (indicator default) | Long | 1% | 110 | 38.2% | **+16.0%** |
| Buy & Hold ETH (same period) | — | — | — | — | -1.2% |
Same indicator. Same band parameters (20, 2.0). Different stop loss.
**Short side (optional):**
BB(20,2) shorts returned -17.4% — avoid with these settings. The optimizer found that shorts require different parameters: BB(10, 2.5) SL=1% TP=3R → +8.9% (Sharpe 7.81, 23 trades). Enable via the "Allow Short" input and adjust the parameters accordingly.
---
**Signal logic:**
- Long signal: bar closes **below** the lower Bollinger Band (no open position)
- Short signal: bar closes **above** the upper Bollinger Band (no open position)
- Entry: executed at the **next bar's open** (no lookahead)
**Exit logic — three mechanisms:**
1. **SL**: price hits SL% below (long) or above (short) the entry price
2. **TP**: price hits TP_ratio × risk from entry
3. **BB_UP / BB_DN**: bar closes beyond the opposite band (mean reversion complete)
**Default parameters (best Sharpe from 81-config long optimization):**
- BB Period: 20 | Std Deviation: 2.0 | Stop Loss: 1.0% | TP Ratio: 2.0R
**What makes this different from other BB indicators:**
- Parameters derived from a grid search (3 periods × 3 std devs × 3 SL% × 3 TP ratios × long/short/both)
- Simulated position tracking: entry marks appear on the next bar's open, matching backtester logic
- Three exit mechanisms with distinct markers (SL ✕ / TP ✓ / BB reversion ▽)
- SL and TP levels displayed as live lines during open position
- Short side included but with honest disclosure: BB(20,2) shorts underperform
- Uses ddof=1 standard deviation (biased=false) to match Python pandas behavior exactly
**Note:** This is an indicator with simulated position tracking, not a strategy. It shows signals and approximate trade timing for visual reference. For exact backtesting and the full optimizer: see GitHub link below.
**Tested on:** ETH/USDT 4H, Bybit Futures Linear. Validate on your own pair before using with real capital. Past performance does not guarantee future results.
**The central finding:** the stop loss matters more than the band parameters.
| Setup | Direction | SL | Trades | Win Rate | Return |
|---|---|---|---|---|---|
| Standard (what tutorials teach) | Long | 2% | 110 | 38% | **-22.8%** |
| Optimized (indicator default) | Long | 1% | 110 | 38.2% | **+16.0%** |
| Buy & Hold ETH (same period) | — | — | — | — | -1.2% |
Same indicator. Same band parameters (20, 2.0). Different stop loss.
**Short side (optional):**
BB(20,2) shorts returned -17.4% — avoid with these settings. The optimizer found that shorts require different parameters: BB(10, 2.5) SL=1% TP=3R → +8.9% (Sharpe 7.81, 23 trades). Enable via the "Allow Short" input and adjust the parameters accordingly.
---
**Signal logic:**
- Long signal: bar closes **below** the lower Bollinger Band (no open position)
- Short signal: bar closes **above** the upper Bollinger Band (no open position)
- Entry: executed at the **next bar's open** (no lookahead)
**Exit logic — three mechanisms:**
1. **SL**: price hits SL% below (long) or above (short) the entry price
2. **TP**: price hits TP_ratio × risk from entry
3. **BB_UP / BB_DN**: bar closes beyond the opposite band (mean reversion complete)
**Default parameters (best Sharpe from 81-config long optimization):**
- BB Period: 20 | Std Deviation: 2.0 | Stop Loss: 1.0% | TP Ratio: 2.0R
**What makes this different from other BB indicators:**
- Parameters derived from a grid search (3 periods × 3 std devs × 3 SL% × 3 TP ratios × long/short/both)
- Simulated position tracking: entry marks appear on the next bar's open, matching backtester logic
- Three exit mechanisms with distinct markers (SL ✕ / TP ✓ / BB reversion ▽)
- SL and TP levels displayed as live lines during open position
- Short side included but with honest disclosure: BB(20,2) shorts underperform
- Uses ddof=1 standard deviation (biased=false) to match Python pandas behavior exactly
**Note:** This is an indicator with simulated position tracking, not a strategy. It shows signals and approximate trade timing for visual reference. For exact backtesting and the full optimizer: see GitHub link below.
**Tested on:** ETH/USDT 4H, Bybit Futures Linear. Validate on your own pair before using with real capital. Past performance does not guarantee future results.
Mã nguồn mở
Theo đúng tinh thần TradingView, tác giả của tập lệnh này đã công bố nó dưới dạng mã nguồn mở, để các nhà giao dịch có thể xem xét và xác minh chức năng. Chúc mừng tác giả! Mặc dù bạn có thể sử dụng miễn phí, hãy nhớ rằng việc công bố lại mã phải tuân theo Nội quy.
Thông báo miễn trừ trách nhiệm
Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.
Mã nguồn mở
Theo đúng tinh thần TradingView, tác giả của tập lệnh này đã công bố nó dưới dạng mã nguồn mở, để các nhà giao dịch có thể xem xét và xác minh chức năng. Chúc mừng tác giả! Mặc dù bạn có thể sử dụng miễn phí, hãy nhớ rằng việc công bố lại mã phải tuân theo Nội quy.
Thông báo miễn trừ trách nhiệm
Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.