> For the complete documentation index, see [llms.txt](https://market-quant.gitbook.io/home/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://market-quant.gitbook.io/home/api-reference/tools/cointegration/hursthalflifecointegration.md).

# 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()`**

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__()`**

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 is `0.05`.
* **hurst\_threshold** (`float`, optional): Maximum acceptable Hurst exponent value (less than `0.5` for mean-reversion). Default is `0.5`.
* **min\_half\_life** (`int`, optional): Minimum half-life of the spread in days. Default is `1`.
* **max\_half\_life** (`int`, optional): Maximum half-life of the spread in days. Default is `365`.
* **min\_crossings** (`int`, optional): Minimum number of mean crossings per year. Default is `12`.

### **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()`**

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()`**

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()`**

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()`**

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()`**

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()`**

Processes each pair of stocks by:

1. Checking cointegration.
2. Calculating the Hurst exponent.
3. Estimating the half-life of the spread.
4. 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()`**

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()`**

Downloads the stock data and processes all pairs to find eligible pairs.

### **Arguments:**

None

### **Returns:**

None

***

## **Example Usage**

```python
tickers_csv_path = 'tickers.csv'
tickers = load_tickers_from_csv(tickers_csv_path)

# Initialize the analyzer with customizable parameters
analyzer = HurstHalfLifeCointegration(
    tickers=tickers,
    start='2022-01-01',
    end='2024-01-01',
    p_value_threshold=0.05,
    hurst_threshold=0.4,
    min_half_life=5,
    max_half_life=250,
    min_crossings=10
)

# Run the analysis
analyzer.run_analysis()
```

### **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` and `end`) for historical data analysis.
