arbitragelab.cointegration_approach.minimum_profit
This module optimizes the upper and lower bounds for mean-reversion cointegration pair trading.
Module Contents
Classes
This is a class that optimizes the upper and lower bounds for mean-reversion cointegration |
- class MinimumProfit
This is a class that optimizes the upper and lower bounds for mean-reversion cointegration pair trading.
The model assumes the cointegration error follows an AR(1) process and utilizes mean first-passage time to determine the optimal levels to initiate trades. The trade will be closed when the cointegration error reverts to its mean.
The implementation is based on the method described by Lin, Y.-X., McCrae, M., and Gulati, C. in “Loss protection in pairs trading through minimum profit bounds: a cointegration approach”
- set_train_dataset(price_df: pandas.DataFrame)
Provide price series for model to calculate the cointegration coefficient and beta.
- Parameters:
price_df – (pd.DataFrame) Price series dataframe which contains both series.
- fit(sig_level: str = '95%', use_johansen: bool = False) Tuple[float, pandas.Series, float, numpy.array]
Find the cointegration coefficient, beta, and the AR(1) coefficient for cointegration error.
Note
Cointegration of the price series is crucial to the success of the optimization. In order for the strategy to work successfully, the prices of the asset pairs should at least be cointegrated at a 90% level.
- Parameters:
sig_level – (str) Cointegration test significance level. Possible options are “90%”, “95%”, and “99%”.
use_johansen – (bool) If True, use Johansen to calculate beta; if False, use Engle-Granger.
- Returns:
(float, pd.Series, float, np.array) Cointegration coefficient, beta; Cointegration error, epsilon_t; AR(1) coefficient; AR(1) fit residual on cointegration error.
- optimize(ar_coeff: float, epsilon_t: pandas.Series, ar_resid: numpy.array, horizon: int, granularity: float = 0.01) Tuple[float, Ellipsis]
Optimize the upper bound following the optimization procedure in the paper.
Note
The Nyström method used to estimate the inter-trade interval is computationally intensive and could take a considerable amount of time to yield the final result, especially when cointegration error has a large standard deviation.
- Parameters:
ar_coeff – (float) AR(1) coefficient of the cointegrated spread.
epsilon_t – (pd.Series) Cointegration error.
ar_resid – (np.array) AR(1) fit residual on cointegration error.
horizon – (int) Test trading period.
granularity – (float) Integration discretization interval, default to 0.01.
- Returns:
(float, float, float, float, float) Optimal upper bound; optimal trade duration; optimal inter-trades interval; optimal minimum trade profit; optimal number of trades.
- static get_optimal_levels(upper_bound: float, minimum_profit: float, beta: float, epsilon_t: numpy.array) Tuple[pandas.DataFrame, numpy.array, numpy.array]
Generate the optimal trading levels to use in a strategy.
- Parameters:
upper_bound – (float) Optimized upper bound based on mean passage time optimization.
minimum_profit – (float) Optimized minimum profit based on mean passage time optimization.
beta – (float) Fitted cointegration coefficient, beta.
epsilon_t – (np.array) Cointegration error obtained from training set.
- Returns:
(np.array, np.array) Number of shares to trade for each leg in the cointegration pair; exact values of cointegration error for initiating and closing trades.
- static construct_spread(price_series: pandas.DataFrame, beta: float) pandas.Series
Constructs spread using Johansen/Engle-Granger beta coefficient.
- Spread is simply calculated as:
Spread = Asset_A_Price + beta * Asset_B_Price
- Parameters:
price_series – (pd.DataFrame) Dataframe with prices for two assets in a spread.
beta – (float) Fitted cointegration coefficient, beta.
- Returns:
(pd.Series) Resulting spread series.