QuantConnect / Lean

Lean Algorithmic Trading Engine by QuantConnect (Python, C#)
https://lean.io
Apache License 2.0
9.41k stars 3.2k forks source link

Adds Hurst Exponent Indicator #3924

Open AlexCatarino opened 4 years ago

AlexCatarino commented 4 years ago

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

ikamanu commented 2 years ago

🙏

LouisSzeto commented 1 year ago

spy_hurst_exponent.csv

LouisSzeto commented 10 months ago

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)