How to pick the right strategy with the Hurst exponent
How to pick the right strategy with the Hurst exponent
How to pick the right strategy with the Hurst exponent
In today’s newsletter, I’m going to show you how to use the Hurst exponent to pick the right type of strategy for the market.
The Hurst exponent is a measure of the long-term memory of a time series. It quantifies the tendency of a time series to revert to its mean or cluster in one direction. The Hurst exponent shows if a time series behaves in a random, trending, or mean-reverting way. It captures the speed autocorrelation decrease as the lag increases.
And it’s great for trading.
Traders use it to pick the right trading strategy for the current market conditions. For example, you do not want to trade a trend strategy if the Hurst exponent shows mean reverting market behavior.
Use the Hurst exponent for strategy selection
The Hurst exponent ranges between 0 and 1.
If the Hurst exponent is below 0.5, the market is mean reverting. Reversal strategies win in these markets.
If the Hurst exponent of 0.5 means the market is random. In this case, a trading strategy that relies on the market direction will lose money.
If the Hurst exponent is above 0.5 the market is trending. Markets with a high Hurst exponent are perfect for trend-following strategies.
Unfortunately, most traders will trade the same strategy through all markets. Use the Hurst exponent to pick the right strategy for the market.
By the end of this newsletter you will be able to:
- Get stock price data
- Calculate the Hurst exponent
- Determine the type of market
Here's how to do it in Python, step by step.
Step 1: Get stock price data
Start with the imports. I use the OpenBB SDK for data.
1import pandas as pd
2import numpy as np
3
4from openbb_terminal.sdk import openbb
Download 20 years of data and plot it.
1df = openbb.stocks.load("^GSPC", start_date="2000-01-01", end_date="2019-12-31")["Adj Close"]
2
3df.plot(title="S&P 500")
The trending and mean reverting periods are obvious when you inspect them visually. Though you can’t visually interpret every chart at every time frame.
But you can use the Hurst exponent.
Step 2: Calculate the Hurst exponent
There are a few ways to calculate the Hurst exponent. You can estimate the rate of diffusion based on the variance of log prices in three lines of Python code.
1def get_hurst_exponent(ts, max_lag=20):
2 lags = range(2, max_lag)
3 tau = [np.std(np.subtract(ts[lag:], ts[:-lag])) for lag in lags]
4
5 return np.polyfit(np.log(lags), np.log(tau), 1)[0]
The Hurst exponent uses lags to measure the long-term memory of the time series. For each lag in the range, calculate the standard deviation of the differenced series. Then calculate the slope of the log lags versus the standard deviations. You can do this by returning the first value from NumPy’s polyfit
function which fits a first-degree polynomial function.
Step 3: Determine the type of market
Take a look at how the lag parameter impacts the Hurst exponent.
1for lag in [20, 100, 250, 500, 1000]:
2 hurst_exp = get_hurst_exponent(df.values, lag)
3 print(f"{lag} lags: {hurst_exp:.4f}")
This will print the Hurst exponent at different lags. Over the entire time series, the S&P 500 is close to random. In the shorter lags, there is evidence of mean reversion.
Now, zoom in on a specific period in time.
1shorter_series = df.loc["2005":"2007"].values
2for lag in [20, 100, 250, 500]:
3 hurst_exp = get_hurst_exponent(shorter_series, lag)
4 print(f"{lag} lags: {hurst_exp:.4f}")
Just as the Great Financial Crisis hit, the S&P 500 was mean reverting. In fact, with 500 lags, the Hurst exponent is close to 0.13.
In today’s newsletter, I showed you how to use the Hurst exponent to pick the right strategy for the market. Now you can get stock price data, calculate the Hurst exponent, and determine the type of market to help select the right trading strategy.