Lean
Lean copied to clipboard
Adds Hurst Exponent Indicator
Expected Behavior
Hurst Exponent Indicator is part of Lean indicator collection.
Actual Behavior
Hurst Exponent Indicator is not part of Lean indicator collection.
Potential Solution
Implement Hurst Exponent Indicator. Community suggestion: https://www.quantconnect.com/forum/discussion/1695/hurst-exponent-indicator/p1 Note: Indicator implementation must include unit tests with third party data.
Checklist
- [x] I have completely filled out this template
- [x] I have confirmed that this issue exists on the current
masterbranch - [x] I have confirmed that this is not a duplicate issue by searching issues
- [x] I have provided detailed steps to reproduce the issue
🙏
spy_hurst_exponent.csv columns: "SPY close" "hurst exponent"
script:
import talib
import pandas as pd
history = pd.read_csv("https://github.com/QuantConnect/Lean/raw/master/Data/equity/usa/daily/spy.zip",
index_col=0, names=["open", "high", "low", "close", "volume"])
close = history.close
# Source: https://towardsdatascience.com/introduction-to-the-hurst-exponent-with-code-in-python-4da0414ca52e
def get_hurst_exponent(time_series):
lags = range(2, 20)
tau = [np.std(np.subtract(time_series.shift(-lag), time_series)) for lag in lags]
reg = np.polyfit(np.log(lags), np.log(tau), 1)
return reg[0]
# we use a rolling window of past 252 data points
hurst_exponent = close * 0
for i in range(252, len(close)):
hurst_exponent[i] = get_hurst_exponent(close.iloc[i-252:i])
hurst_exponent = hurst_exponent[hurst_exponent != 0].to_frame()
hurst_exponent["spy close"] = close
hurst_exponent = hurst_exponent.iloc[:, [1, 0]].dropna()
hurst_exponent.to_csv("spy_hurst_exponent.csv", header=False)