arbitragelab.cointegration_approach.johansen
This module implements the Johansen cointegration approach.
Module Contents
Classes
The class implements the construction of a mean-reverting portfolio using eigenvectors from |
- class JohansenPortfolio
Bases:
arbitragelab.cointegration_approach.base.CointegratedPortfolio
The class implements the construction of a mean-reverting portfolio using eigenvectors from the Johansen cointegration test. It also checks Johansen (eigenvalue and trace statistic) tests for the presence of cointegration for a given set of assets.
- __slots__ = ()
- fit(price_data: pandas.DataFrame, dependent_variable: str = None, det_order: int = 0, n_lags: int = 1)
Finds cointegration vectors from the Johansen test used to form a mean-reverting portfolio.
Note: Johansen test yields several linear combinations that may yield mean-reverting portfolios. The function stores all of them in decreasing order of eigenvalue meaning that the first linear combination forms the most mean-reverting portfolio which is used in trading. However, researchers may use other stored cointegration vectors to check other portfolios.
This function will calculate and set johansen_trace_statistic and johansen_eigen_statistic only if the number of variables in the input dataframe is <=12. Otherwise it will generate a warning.
A more detailed description of this method can be found on p. 54-58 of “Algorithmic Trading: Winning Strategies and Their Rationale” by Ernie Chan.
This function is a wrapper around the coint_johansen function from the statsmodels.tsa module. Detailed descriptions of this function are available in the statsmodels documentation.
- Parameters:
price_data – (pd.DataFrame) Price data with columns containing asset prices.
dependent_variable – (str) Column name which represents the dependent variable (y). By default, the first column is used as a dependent variable.
det_order – (int) -1 for no deterministic term in Johansen test, 0 - for constant term, 1 - for linear trend.
n_lags – (int) Number of lags used in the Johansen test. The practitioners use 1 as the default base value.
- construct_mean_reverting_portfolio(price_data: pandas.DataFrame, cointegration_vector: pandas.Series = None) pandas.Series
When cointegration vector was formed, this function is used to multiply asset prices by cointegration vector to form mean-reverting portfolio which is analyzed for possible trade signals.
- Parameters:
price_data – (pd.DataFrame) Price data with columns containing asset prices.
cointegration_vector – (pd.Series) Cointegration vector used to form a mean-reverting portfolio. If None, a cointegration vector with maximum eigenvalue from fit() method is used.
- Returns:
(pd.Series) Cointegrated portfolio dollar value.
- get_scaled_cointegration_vector(cointegration_vector: pandas.Series = None) pandas.Series
This function returns the scaled values of the cointegration vector in terms of how many units of other cointegrated assets should be bought if we buy one unit of one asset.
- Parameters:
cointegration_vector – (pd.Series) Cointegration vector used to form a mean-reverting portfolio. If None, a cointegration vector with maximum eigenvalue from fit() method is used.
- Returns:
(pd.Series) The scaled cointegration vector values.