HurstHalfLifeCointegration()
Analyzes stock pairs based on cointegration, Hurst exponent, half-life, and mean crossings.
The HurstHalfLifeCointegration
class is designed to analyze stock pairs by fetching their historical price data and applying a series of statistical tests. The class uses the Engle-Granger cointegration test, calculates the Hurst exponent, estimates the half-life of the spread between the pairs, and counts the number of mean crossings, based on configurable thresholds.
Class: HurstHalfLifeCointegration()
HurstHalfLifeCointegration()
A tool for identifying mean-reverting pairs of stocks using statistical methods.
Generates a list of eligible pairs based on:
Cointegration test
Hurst exponent (for mean reversion check)
Half-life of the spread
Mean crossings
Supports customizable thresholds for the selection of eligible stock pairs based on user-defined parameters.
Constructor: __init__()
__init__()
Initializes the HurstHalfLifeCointegration
object and configures thresholds for analysis.
Arguments:
tickers (
list
): List of stock tickers to analyze.start (
str
): Start date for fetching historical stock data in the format 'YYYY-MM-DD'.end (
str
): End date for fetching historical stock data in the format 'YYYY-MM-DD'.p_value_threshold (
float
, optional): Maximum acceptable p-value for the cointegration test. Default is0.05
.hurst_threshold (
float
, optional): Maximum acceptable Hurst exponent value (less than0.5
for mean-reversion). Default is0.5
.min_half_life (
int
, optional): Minimum half-life of the spread in days. Default is1
.max_half_life (
int
, optional): Maximum half-life of the spread in days. Default is365
.min_crossings (
int
, optional): Minimum number of mean crossings per year. Default is12
.
Attributes Initialized:
tickers: The list of stock tickers to analyze.
start: The start date for downloading stock data.
end: The end date for downloading stock data.
p_value_threshold: The maximum p-value for the cointegration test.
hurst_threshold: The maximum Hurst exponent value to filter mean-reverting pairs.
min_half_life: The minimum half-life of the spread.
max_half_life: The maximum half-life of the spread.
min_crossings: The minimum number of mean crossings required.
data: Holds the fetched stock data.
Method: download_data()
download_data()
Downloads historical stock data for the specified tickers using the Yahoo Finance API (yfinance
).
Arguments:
None
Returns:
None (Stock data is stored in the self.data
attribute)
Method: calculate_cointegration()
calculate_cointegration()
Performs the Engle-Granger cointegration test on a pair of stocks.
Arguments:
pair (
tuple
): A tuple containing two stock tickers.
Returns:
A tuple of the two tickers and their p-value if the p-value is below the
p_value_threshold
.None
if the p-value is above the threshold or an error occurs.
Method: calculate_hurst()
calculate_hurst()
Calculates the Hurst exponent to determine if a stock pair exhibits mean-reverting behavior.
Arguments:
series (
pd.Series
): A time series of the spread between two stocks.
Returns:
The Hurst exponent (
float
) if the series is valid and meets the criteria.None
if the calculation fails due to invalid data or an error.
Method: calculate_half_life()
calculate_half_life()
Calculates the half-life of mean reversion for a stock pair spread using an Ordinary Least Squares (OLS) regression.
Arguments:
spread (
pd.Series
): The spread between two stock prices.
Returns:
The half-life of the spread (
float
).
Method: calculate_mean_crossings()
calculate_mean_crossings()
Counts how many times the spread crosses its mean, which helps identify whether the pair exhibits frequent mean reversion.
Arguments:
spread (
pd.Series
): The spread between two stock prices.
Returns:
The number of mean crossings (
int
).
Method: process_pair()
process_pair()
Processes each pair of stocks by:
Checking cointegration.
Calculating the Hurst exponent.
Estimating the half-life of the spread.
Counting the mean crossings.
Arguments:
pair (
tuple
): A tuple of two stock tickers.
Returns:
A tuple of the two tickers and their statistical properties (p-value, Hurst exponent, half-life, mean crossings) if they meet all the thresholds.
None
if the pair fails any of the tests or thresholds.
Method: find_eligible_pairs()
find_eligible_pairs()
Applies the process_pair
method to all possible combinations of the stock tickers to find eligible pairs using parallel processing.
Arguments:
None
Returns:
A list of eligible stock pairs that meet all the statistical criteria.
Method: run_analysis()
run_analysis()
Downloads the stock data and processes all pairs to find eligible pairs.
Arguments:
None
Returns:
None
Example Usage
Customization Options
Thresholds for Statistical Tests: Users can set custom thresholds for cointegration p-value, Hurst exponent, half-life of mean reversion, and mean crossings to fine-tune the analysis.
Stock Tickers: The class accepts any combination of tickers supported by Yahoo Finance.
Date Range: Allows specifying a date range (
start
andend
) for historical data analysis.
Last updated