arbitragelab.other_approaches.kalman_filter
This module implements Kalman filter logic in statistical arbitrage trading. Kalman trading technique is applied to dynamically estimate a hedge ratio between two assets. The output of the Kalman filter can be used either to build a mean-reverting portfolio and use Bollinger-bands strategy on top of that, or use Kalman filter residuals and standard deviation to form trading signals. This implementation is based on the one from the book by E.P Chan: “Algorithmic Trading: Winning Strategies and Their Rationale”,
Additional information can be found in the following sources: Quantitative Research and Trading. Quantopian. QuantStart.
Module Contents
Classes
KalmanFilterStrategy implements a dynamic hedge ratio estimation between two assets using |
- class KalmanFilterStrategy(observation_covariance: float = 0.001, transition_covariance: float = 0.0001)
KalmanFilterStrategy implements a dynamic hedge ratio estimation between two assets using the Kalman filter. Kalman Filter is a state space model that assumes the system state evolves following some hidden and unobservable pattern. The goal of the state space model is to infer information about the states, given the observations, as new information arrives. The strategy has two important values to fit: observation covariance and transition covariance.
There are two ways to fit them: using cross-validation technique or by applying Autocovariance Least Squares (ALS) algorithm. Kalman filter approach generalizes a rolling linear regression estimate.
This class implements the Kalman Filter from the book by E.P Chan: “Algorithmic Trading: Winning Strategies and Their Rationale”,
- update(x: float, y: float)
Update the hedge ratio based on the recent observation of two assets.
By default, y is the observed variable and x is the hidden one. That is the hedge ratio for y is 1 and the hedge ratio for x is estimated by the Kalman filter.
Mean-reverting portfolio series is formed by:
y - self.hedge_ratios * x
One can get spread series from self.spread_series and self.spread_std_series to trade the Bollinger Bands strategy.
- Parameters:
x – (float) X variable value (hidden).
y – (float) Y variable value.
- trading_signals(entry_std_score: float = 3, exit_std_score: float = -3) pandas.DataFrame
Generate trading signals based on existing data.
This method uses recorded forecast errors and standard deviations of forecast errors to generate trading signals, as described in the book by E.P Chan “Algorithmic Trading: Winning Strategies and Their Rationale”.
The logic is to have a long position open from e(t) < -entry_std_score * sqrt(Q(t)) till e(t) >= -exit_std_score * sqrt(Q(t)) And a short position from e(t) > entry_std_score * sqrt(Q(t)) till e(t) <= exit_std_score * sqrt(Q(t))
where e(t) is a forecast error at time t, and sqrt(Q(t)) is the standard deviation of standard errors at time t.
- Parameters:
entry_std_score – (float) Number of st.d. values to enter (long or short) the position.
exit_std_score – (float) Number of st.d. values to exit (long or short) the position.
- Returns:
(pd.DataFrame) Series with forecast errors and target allocation on each observation.