Uploaded by Riccardo Ronco

Hurst Exponent for Algorithmic Trading - Robot Wealth

advertisement
20/12/2022, 20:19
Hurst Exponent for Algorithmic Trading - Robot Wealth
Menu
Hurst Exponent for
Algorithmic Trading
October 31, 2016 by Kris Longmore
Reading Time 3 mins
This is the first post in a two-part series about the Hurst Exponent. Tom and I
worked on this series together and I drew on some of his previously published
work as well as other sources like Quantstart.com.
UPDATE 03/01/16: Please note that the Python code below has been updated with a
more accurate algorithm for calculating Hurst Exponent.
hurst exponent
Mean-reverting time series have long been a fruitful playground for quantitative
traders. In fact, some of the biggest names in quant trading allegedly made their
fortunes exploiting mean reversion of financial time series such as artificially
constructed spreads, which are used in pairs trading. Identifying mean reversion is
therefore of significant interest to algorithmic traders. This is not as simple as it
sounds, in part due to the non-stationary nature of financial data.
https://robotwealth.com/demystifying-the-hurst-exponent-part-1/
1/12
20/12/2022, 20:19
Hurst Exponent for Algorithmic Trading - Robot Wealth
We both think that Ernie Chan’s book “Algorithmic Trading: Winning Strategies and
Their Rationale”, is one of the better introductions to mean reversion available in
the public domain. In the book, Ernie talks about several tools that can be used
when testing if a time series is mean reverting. One is the Augmented DickeyFuller test for mean reversion. Ernie also goes into some detail about the
Johansen test. Both of these have previously been explored on Robot Wealth and
implemented using some simple R code (here and here). Another interesting
aspect of testing for mean reversion is the calculation of the Hurst Exponent.
The idea behind the Hurst Exponent H is that it can supposedly help us determine
whether a time series is a random walk (H ~ 0.5), trending (H > 0.5) or mean
reverting (H < 0.5) for a specific period of time. However, if you’ve ever used Hurst,
you know that it can be a bit bewildering: not only does it often give unexpected
results, but it also returns different results depending on the implementation used
in its calculation. Further, there are a few different methods for calculating Hurst;
we found that these generally agree for a randomly generated time series, but
disagree when we use real data.
So how can Hurst Exponent be of
value to algo traders?
The remainder of this post is devoted to presenting and discussing some Python
code for calculating Hurst. In the next post, we are going to delve more deeply into
the calculation and work out what’s going on. Our ultimate goal is to demystify the
Hurst Exponent and show how to take it beyond some nice theory to something of
practical value to algo traders.
Without further ado, here is the code for calculating the Hurst Exponent in Python.
We determine Hurst by firstly calculating the standard deviation of the difference
between a series and its lagged counterpart. We then repeat this calculation for a
number of lags and plot the result as a function of the number of lags. If we plot
this on a log-log scale, we end up with a straight line, the slope of which provides
an estimate for the Hurst exponent. I found this article which describes this
approach to calculating Hurst, as does this one.
1.
2.
3.
4.
5.
6.
7.
from numpy import *
from pylab import plot, show
# first, create an arbitrary time series, ts
ts = [0]
for i in range(1,100000):
ts.append(ts[i-1]*1.0 + random.randn())
# calculate standard deviation of differenced series using various lags
https://robotwealth.com/demystifying-the-hurst-exponent-part-1/
2/12
20/12/2022, 20:19
8.
9.
10.
11.
12.
13.
14.
15.
Hurst Exponent for Algorithmic Trading - Robot Wealth
lags = range(2, 20)
tau = [sqrt(std(subtract(ts[lag:], ts[:-lag]))) for lag in lags]
# plot on log-log scale
plot(log(lags), log(tau)); show()
# calculate Hurst as slope of log-log plot
m = polyfit(log(lags), log(tau), 1)
hurst = m[0]*2.0
print 'hurst = ',hurst
You can see in the code that we used lags 2 through 20 for calculating H. These
lags are somewhat arbitrary, but based on the best results obtained using
synthetic data with known behaviour. Specifically, we found that if we set the
maximum number of lags too high, the results became quite inaccurate. These
values are the defaults used in some other implementations, such as the standard
Hurst function in MATLAB.
In Demystifying the Hurst Exponent: Part 2, we will look at these lags in more detail
and show how they are actually crucial for calculating Hurst in such a way that is
useful and meaningful. We tweak this part of the calculation to uncover a practical
application of Hurst in developing algo trading systems.
If you have used the Hurst Exponent, or indeed any of the other tests for mean
reversion that we mentioned in this post, please share your experiences in the
comments. Thanks!
Quant trading
cointegration, Hurst Exponent, mean reversion, quantitative trading
How to Create a Trading Algorithm: So You Want to Build Your Own Algo
Trading System?
Demystifying the Hurst Exponent – Part 2
15 thoughts on “Hurst Exponent for Algorithmic
Trading”
Pingback: Quantocracy's Daily Wrap for 10/31/2016 | Quantocracy
https://robotwealth.com/demystifying-the-hurst-exponent-part-1/
3/12
20/12/2022, 20:19
Hurst Exponent for Algorithmic Trading - Robot Wealth
Nick Wienholt
November 2, 2016 at 10:27 am
Hi Kris,
I’ve had a brief look at Kaufman’s Efficiency Ratio. The results weren’t great,
but I didn’t dig into it effectively enough to fully rule it out as a filter for the
mean reversion systems I’m trading.
Happy to write it up with a proper analysis.
Nick
Reply
Kris Longmore
November 5, 2016 at 10:18 pm
Hey Nick
I’d love to see a proper analysis of Kaufman’s Efficiency Ratio! You are
welcome to share it here!
Cheers
Reply
Nick Wienholt
November 9, 2016 at 5:33 pm
Thanks Kris – will do – I’ll write it up and submit. Many thanks
Reply
https://robotwealth.com/demystifying-the-hurst-exponent-part-1/
4/12
20/12/2022, 20:19
Hurst Exponent for Algorithmic Trading - Robot Wealth
David
November 5, 2016 at 6:17 am
Interesting post thanks… I’m currently investigating applying the Hurst
exponent in machine learning to improve my trade selection. Try as I
might, I can’t find a default Hurst Matlab function however!
Reply
Kris Longmore
November 5, 2016 at 10:16 pm
Hi David,
I guess its not really a default function, but try this implementation of the
generalized Hurst exponent:
https://au.mathworks.com/matlabcentral/fileexchange/30076generalized-hurst-exponent?requestedDomain=www.mathworks.com
Reply
David
November 6, 2016 at 7:15 am
Thanks Kris, I’ll give that a try… I also stumbled across this new Matlab
function gives 3 separate (!) estimates of the Hurst exponent too:
https://uk.mathworks.com/help/wavelet/ref/wfbmesti.html
Reply
Kris Longmore
November 6, 2016 at 3:38 pm
https://robotwealth.com/demystifying-the-hurst-exponent-part-1/
5/12
20/12/2022, 20:19
Hurst Exponent for Algorithmic Trading - Robot Wealth
Yes! This is a problem and one that we are going to attempt to get to
the bottom of in the next post. Thanks for sharing that
implementation.
Reply
Pingback: Demystifying the Hurst Exponent – Part 2 – Robot Wealth
Wei
August 8, 2017 at 9:15 pm
Hi Kris,
Thanks for the article, incredibly interesting. I was just wondering if you
could shed some light on where the sqrt of std comes from? I’ve been
through both articles you linked to and find the RS method of calculating
the Hurst exponent (the one originally derived by Hurst for the work on
Nile levels) to be intuitively easier to understand. Looking at your update
note, my guess would be that you were initially using that algorithm for
calculating Hurst, but then moved on to the Generalized Hurst Exponent
calculation that Mike from QuantStart uses on his page?
I’ve been through Mike’s explanation, but I still can’t seem to rationalize
why we are using the square root of the standard deviation in this
algorithm. Any light you could shed on that would be much appreciated.
Thanks again for the great article.
Reply
andy
September 8, 2017 at 6:19 pm
https://robotwealth.com/demystifying-the-hurst-exponent-part-1/
6/12
20/12/2022, 20:19
Hurst Exponent for Algorithmic Trading - Robot Wealth
Hi Kris,
thanks for sharing.
I think we can save the sqrt() step here in : tau = [sqrt(std(subtract(ts[lag:],
ts[:–lag]))) for lag in lags],
and no need to multiply 2 here in : hurst = m[0]*2.0.
So we the computation is simplified to:
…
tau = [std(subtract(ts[lag:], ts[:–lag])) for lag in lags],
…
hurst = m[0]
which saves computation and easier to be understood from the Hurst
exponent definition.
But still thanks a lot for sharing this, learned a lot.
Regards
Andy
Reply
Kris Longmore
September 18, 2017 at 9:32 pm
Nice one! Thanks Andy, that makes a lot of sense.
Reply
rando
October 6, 2019 at 7:35 pm
https://robotwealth.com/demystifying-the-hurst-exponent-part-1/
7/12
20/12/2022, 20:19
Hurst Exponent for Algorithmic Trading - Robot Wealth
Hello,
Noob here, why are you taking the square root of the standard deviation of
the differenced lags? Thought we were just trying to find the standard
deviation?
Is it just so they are on a more condensed scale?
Reply
Curious dude
October 7, 2019 at 1:59 am
Hello,
Why do I get a low Hurst exponent with an only upward moving time
series?
I get a hurst exponent of almost 0 when using the below generated time
series (trends up by about 1).
for i in range(1,100000):
ts.append(i+1+random.uniform(0,1))
Reply
Pingback: Time Series Analysis: Fitting ARIMA/GARCH Predictions Profitable for FX?
Leave a Comment
https://robotwealth.com/demystifying-the-hurst-exponent-part-1/
8/12
20/12/2022, 20:19
Hurst Exponent for Algorithmic Trading - Robot Wealth
Name *
Email *
Website
Save my name, email, and website in this browser for the next time I comment.
POST COMMENT
Subscribe to our Newsletter
We'll send you insights from the trading desk and
updates on quant trading bootcamp.
We will never spam you or share your details.
Your name
Your email
Subscribe
Most Popular Posts
https://robotwealth.com/demystifying-the-hurst-exponent-part-1/
9/12
20/12/2022, 20:19
Hurst Exponent for Algorithmic Trading - Robot Wealth
How to Connect Google Colab to a Local Jupyter Runtime 28.2k views
Machine learning for Trading:
Adventures in Feature Selection 28k views
How to Run Trading Algorithms on Google Cloud Platform in 6 Easy Steps
17.7k views
Deep Learning for Trading Part 1: Can it Work? 15.7k views
Dual Momentum Investing: A Quant’s Review 15.1k views
Hurst Exponent for Algorithmic Trading 15.1k views
Time Series Analysis: Fitting ARIMA/GARCH predictions profitable for FX?
13.4k views
Demystifying the Hurst Exponent – Part 2 13.1k views
Exploring Mean Reversion and Cointegration: Part 2 12.4k views
Exploring mean reversion and cointegration with Zorro and R: part 1 12k
views
Blog Categories
Machine learning (16)
Deep learning (4)
k-means clustering (4)
Keras (4)
Neural networks (6)
Quant trading (94)
Backtesting (31)
Cointegration (6)
Factors (4)
FX (16)
Options (8)
Trading strategies (24)
https://robotwealth.com/demystifying-the-hurst-exponent-part-1/
10/12
20/12/2022, 20:19
Hurst Exponent for Algorithmic Trading - Robot Wealth
RW Insider (71)
Think like a trader (24)
Trading as a business (5)
Tools of the trade (77)
Python (8)
R (50)
Time series modelling (5)
Trading books (6)
Trading infrastructure (8)
Zorro (28)
Tags
activation function
algorithmic trading AUD/NZD augemnted Dickey-Fuller test
backtesting cointegration data Deep learning dplyr ETFs factors forex fx GBM GPU
hedging Hurst Exponent k-means clustering Keras
Machine Learning mean reversion
options pairs trading profiling profvis put option
python quantitative analysis quantitative trading R risk premia
simulation slider
tidyverse time series trading
strategies volatility
Zorro
microbenchmark momentum opinion
selection bias
stationarity TensorFlow
walk forward analysis
Blogroll and useful links
Zorro Trader
Darwinex
Quantocracy
The Financial Hacker
Ernie Chan’s Quantitative Trading
https://robotwealth.com/demystifying-the-hurst-exponent-part-1/
11/12
20/12/2022, 20:19
Hurst Exponent for Algorithmic Trading - Robot Wealth
© 2022 ROBOT WEALTH PTY LTD
PRIVACY POLICY | TERMS OF USE | FAQ | CONTACT@ROBOTWEALTH.COM
https://robotwealth.com/demystifying-the-hurst-exponent-part-1/
12/12
Download