A practical approach to analysing market volatility using ensemble methods. This project helps spot potential market rallies by examining the relationship between realised and implied volatility.
We've built this tool to combine several volatility measures into one robust indicator. By comparing our calculated realised volatility against market-implied volatility, we can spot potential market moves before they happen.
- SPX: The S&P 500 Index (our main market benchmark)
- VIX: The CBOE Volatility Index (the market's "fear gauge")
We fetch this data through Finnhub's API, with built-in rate limiting and local caching to keep things running smoothly.
We use several tried-and-tested volatility estimators:
- Garman-Klass
- Rogers-Satchell
- Parkinson
- Close-to-Close
- Yang-Zhang
Our system uses machine learning to work out the optimal mix of these estimators:
- Linear Regression
- Ridge Regression (for more stable results)
- LASSO (when we need to be selective)
Pop this in your terminal:
pip install -r requirements.txtYou'll need:
- An Alpha Vantage API key (set it as
Finnhub_API_KEYin your environment)
- Live market data fetching (mindful of API limits)
- Smart local caching
- Rolling 21-day calculations
- Automated quality checks
Here's how we calculate realised volatility:
Where:
-
$RV$ is our Realised Volatility -
$a_i$ are our clever ML-derived weights -
$\sigma_i$ are the individual volatility measures -
$k$ is how many measures we're using
- RV = Our calculated Realised Volatility
- IV = Market-implied Volatility (from VIX)
Here's a simple example:
from volatility_system import DataFetcher, VolatilityEstimator
# Set things up
fetcher = DataFetcher()
estimator = VolatilityEstimator()
# Get our market data
spx_data = fetcher.fetch_data("SPX")
vix_data = fetcher.fetch_data("VIX")
# Work out the premium
vol_premium = estimator.calculate_premium(spx_data, vix_data)-
When RV < IV (Negative Premium)
- The market might be too worried
- Often seen near market bottoms
- Could be time to buy
-
When RV > IV (Positive Premium)
- The market might be too complacent
- Usually spotted near market tops
- Worth being cautious
-
Garman-Klass
- Uses the day's full price range
- Sharper than just looking at closes
-
Rogers-Satchell
- Handles trending markets well
- Stays accurate when prices drift
-
Parkinson
- Focuses on price ranges
- Brilliant for choppy markets
We fine-tune our weights by solving:
With these rules:
- All weights must add up to 1
- No negative weights allowed
Found a bug? Got an idea? Open an issue or send us a pull request.