OPEN-SOURCE SCRIPT
Cập nhật

Volatility Regime Clustering

158
# Volatility Regime Clustering (VRC) - Intelligent Volatility Classification

## The Problem

Most traders use the same position size regardless of market conditions. This leads to excessive risk during high volatility periods and missed profits during calm periods. Standard volatility indicators (ATR, Bollinger Bands) show current values but don't automatically classify market regimes.

## The Solution

**Volatility Regime Clustering** uses a machine learning algorithm (k-means clustering) to automatically identify volatility regimes based on historical data. The indicator independently finds boundaries between calm, normal, and nervous markets for each trading instrument.

## How It Works

### Technical Implementation

1. **Volatility Normalization**: ATR is converted to basis points for comparability across instruments with different prices.

2. **K-means Clustering**:
- The indicator analyzes the last N bars (default 200)
- Automatically groups historical ATR values into 2-4 clusters
- Cluster centroids are evenly distributed from minimum to maximum volatility
- The algorithm iteratively refines cluster boundaries every 5 bars

3. **False Signal Protection**:
- Threshold-based smoothing: regime changes only after 3+ consecutive confirmations
- Empty cluster protection with automatic reinitialization
- Warmup period to accumulate sufficient history

4. **Guaranteed Order**:
- Regime 0 always = low volatility
- Regime 1 always = medium volatility
- Regime 2 always = high volatility
- Regime 3 always = extreme volatility

## Visualization

- 🟢 **Green Background**: Low volatility - calm market, full position size recommended
- 🟡 **Yellow Background**: Medium volatility - standard conditions, 75-80% of normal risk recommended
- 🟠 **Orange Background**: High volatility - nervous market, reduce risk to 40-50%
- 🔴 **Red Background**: Extreme volatility - panic/news, minimum risk 25% or stay out

**Info Panel** (top right corner) displays:
- Current volatility regime
- ATR in basis points
- Current cluster center (regime mean value)
- Deviation from cluster center (how close to regime transition)
- Regime stability (% of time in current state)
- Recommended risk multiplier
- Recommended position size in %

## How to Use

### Basic Approach (Risk Management)

1. Add the indicator to any asset chart
2. Wait for warmup period completion (requires minimum 200 bars of history)
3. Monitor background color and recommendations table
4. Adjust position size according to risk multiplier

**Example**:
- You normally risk 2% of capital per trade
- Indicator shows "Risk Multiplier: 0.50x" (orange/red regime)
- Reduce risk to 1% (2% × 0.5 = 1%)

### Advanced Usage

**Entry Filtering**:
- Enter positions only in green/yellow regimes
- Avoid new positions during red background

**Open Position Management**:
- When transitioning to high volatility, move stop-losses further
- In calm regimes, use more aggressive take-profits

**Options Strategies**:
- High volatility → sell options (elevated premium)
- Low volatility → buy options before potential expansion

## Settings

### Main Parameters

- **ATR Length** (14): ATR calculation period. Lower = faster reaction, higher = smoother
- **Clustering Lookback** (200): number of bars for algorithm training. Recommended 150-300
- **Number of Regimes** (3): number of volatility regimes (2-4)
- 2 regimes: simple "calm/nervous"
- 3 regimes: "low/medium/high" (recommended)
- 4 regimes: adds "extreme" regime for rare events
- **Regime Smoothing** (3): number of confirmation bars for regime change. Higher = fewer false switches

### Visual Settings

- **Show Background Color**: enable/disable colored background
- **Show Regime Labels**: display labels on chart when regime changes
- **Show Risk Adjustment Info**: show/hide info panel
- **Info Panel Size**: panel font size (Tiny/Small/Normal/Large)
- **Colors**: customize colors for each regime

## What Makes It Different

Unlike existing volatility indicators:

1. **Automatic Adaptation**: no manual threshold configuration needed for each asset
2. **Machine Learning**: uses k-means clustering instead of fixed percentiles
3. **Self-Learning**: regime boundaries recalculated based on rolling window history
4. **Specific Recommendations**: not just volatility level display, but precise numerical multiplier for position sizing
5. **Noise Protection**: threshold-based smoothing prevents chaotic regime switching
6. **Universality**: ATR normalization allows application across any instruments (forex, stocks, crypto)

## Limitations

- Requires minimum 200 bars of history for correct operation
- On very low timeframes (M1-M5) may be overly sensitive
- Indicator reacts with slight delay due to smoothing (this protects against false signals)

## Recommendations

- **Intraday Trading**: use on H1-H4 with default parameters
- **Swing Trading**: increase Clustering Lookback to 300-500 bars
- **Scalping**: reduce Regime Smoothing to 1-2 and ATR Length to 7-10
- **Combination with Other Indicators**: use VRC as a risk filter on top of your main strategy

## Alerts

The indicator supports three alert types:
- **Regime Change**: any volatility regime change
- **Extreme Volatility**: entry into extreme volatility regime
- **Low Volatility**: entry into low volatility regime

---

**Important**: This indicator is a risk management tool, not a trading system. Use it in combination with a proven entry and exit strategy.
Phát hành các Ghi chú
code update

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.