Cocalc 02 Kalman Filter Based Pairs Trading Ipynb

Leo Migdal
-
cocalc 02 kalman filter based pairs trading ipynb

Welcome to The QuantConnect Research Page! Refer to this page for documentation https://www.quantconnect.com/docs#Introduction-to-Jupyter Contribute to this template file https://github.com/QuantConnect/Research/tree/master/Notebooks Pairs trading is a market neutral trading strategy and it belongs to statistical arbitrage. The basic idea is to select two stocks which move similarly, sell high priced stock and buy low priced stock where there is a price divergence between the pairs. Before using pairs trading, we need to know the cointegration.

Cointegration is a statistical property of time series (that is a series of random variables) There was an error while loading. Please reload this page. In this notebook we are going look at the concept of building a trading strategy backtest based on mean reverting, co-integrated pairs of assets (Stock and ETFs). So to restate the theory in in terms of US equities, assets that are statistically co-integrated move in a way that means when their prices start to diverge by a certain amount (i.e. the spread between the 2 assets prices increases), we would expect that divergence to eventually revert back to the mean.

In this instance we would look to sell the outperforming stock,and buy the under performing stock under the notion that the under performing stock would eventually recover with the over performing stock and rise... Hence, pairs trading is a market neutral trading strategy enabling traders to profit from virtually any market conditions: Bull Markets, Bear Markets, or Sideways Markets. So in our search for co-integrated assets, economic theory would suggest that we are more likely to find pairs of that are driven by the same factors, or similar business practices. After all, it is logical to consider 2 assets in the same industry to be similar products, to be at the mercy of the same general ups and downs of the volatile market. So what is aKalman Filter? Well this site (http://www.bzarg.com/p/how-a-kalman-filter-works-in-pictures/) explains and states the following:

You can use a Kalman filter in any place where you have uncertain information about some dynamic system, and you can make an educated guess about what the system is going to do next. Even if messy reality comes along and interferes with the clean motion you guessed about, the Kalman filter will often do a very good job of figuring out what actually happened. And it can take advantage of correlations between crazy phenomena that you maybe wouldn’t have thought to exploit! There are a few Python packages out there for Kalman filters, but we're adapting this example and the Kalman filter class code from this article and demonstrating how you can implement similar ideas using... Briefly, a Kalman filter is a state-space model applicable to linear dynamic systems -- systems whose state is time-dependent and state variations are represented linearly. The model is used to estimate unknown states of a variable based on a series of past values.

The procedure is two-fold: a prediction (estimate) is made by the filter of the current state of a variable and the uncertainty of the estimate itself. When new data is available, these estimates are updated. There is a lot of information available about Kalman filters, and the variety of their applications is pretty astounding, but for now, we're going to use a Kalman filter to estimate the hedge ratio... The idea behind the strategy is pretty straightforward: take two equities that are cointegrated and create a long-short portfolio. The premise of this is that the spread between the value of our two positions should be mean-reverting. Anytime the spread deviates from its expected value, one of the assets moved in an unexpected direction and is due to revert back.

When the spread diverges, you can take advantage of this by going long or short on the spread. To illustrate, imagine you have a long position in AAPL worth $2000 and a short position in IBM worth $2000. This gives you a net spread of $0. Since you expected AAPL and IBM to move together, then if the spread increases significantly above $0, you would short the spread in the expectation that it will return to $0, it's natural equilibrium. Similarly, if the value drops significantly below $0, you would long the spread and capture the profits as its value returns to $0. In our application, the Kalman filter will be used to track the hedging ratio between our equities to ensure that the portfolio value is stationary, which means it will continue to exhibit mean-reversion behavior.

Now, we initialize the Kalman Filter, grab our data, and then run the Kalman Filter update process over the data. There was an error while loading. Please reload this page. This post shows how to apply Kalman Filter in pairs trading. It updates the cointegration relationship using Kalman Filter, and then utilize this relationship in a mean-reversion strategy to backtest the pairs trading performance. In previous post we have seen Kalman Filter and its ability to online train a linear regression model.

In last post we have also seen the idea of cointegration and pairs trading. As pointed out at the end of last post, one way to avoid look-ahead bias and gain walk forward analysis is through Bayesian online training mechanism such as Kalman Filter. Today we'll apply this idea to pairs trading. As usual, the backtest codes in this post is located here on Github. The idea is simple. Because we can obtain pairs trading hedge coefifcient through linear regression, and linear regression can be solved by Kalman Filter as in this post, therefore we can link the pairs through Kalman Filter.

In this post we are going to use PyKalman package, so the only thing you need to do is to understand the concept and then express the problem in Bayesian format. Let's inherit the notations from previous post (refer to as Prev). The state variables are still the intercept \(a_k\) and the slope \(b_k\) as in Prev\((2.1)\). But this time let's observe one \(y_t\) a time. So Prev\((2.2)\) is re-written as Pairs trading using the Kalman filter for time-varying parameters, inspired by "Pairs Trading with R" by Daniel Palomar.

People Also Search

Welcome To The QuantConnect Research Page! Refer To This Page

Welcome to The QuantConnect Research Page! Refer to this page for documentation https://www.quantconnect.com/docs#Introduction-to-Jupyter Contribute to this template file https://github.com/QuantConnect/Research/tree/master/Notebooks Pairs trading is a market neutral trading strategy and it belongs to statistical arbitrage. The basic idea is to select two stocks which move similarly, sell high pri...

Cointegration Is A Statistical Property Of Time Series (that Is

Cointegration is a statistical property of time series (that is a series of random variables) There was an error while loading. Please reload this page. In this notebook we are going look at the concept of building a trading strategy backtest based on mean reverting, co-integrated pairs of assets (Stock and ETFs). So to restate the theory in in terms of US equities, assets that are statistically c...

In This Instance We Would Look To Sell The Outperforming

In this instance we would look to sell the outperforming stock,and buy the under performing stock under the notion that the under performing stock would eventually recover with the over performing stock and rise... Hence, pairs trading is a market neutral trading strategy enabling traders to profit from virtually any market conditions: Bull Markets, Bear Markets, or Sideways Markets. So in our sea...

You Can Use A Kalman Filter In Any Place Where

You can use a Kalman filter in any place where you have uncertain information about some dynamic system, and you can make an educated guess about what the system is going to do next. Even if messy reality comes along and interferes with the clean motion you guessed about, the Kalman filter will often do a very good job of figuring out what actually happened. And it can take advantage of correlatio...

The Procedure Is Two-fold: A Prediction (estimate) Is Made By

The procedure is two-fold: a prediction (estimate) is made by the filter of the current state of a variable and the uncertainty of the estimate itself. When new data is available, these estimates are updated. There is a lot of information available about Kalman filters, and the variety of their applications is pretty astounding, but for now, we're going to use a Kalman filter to estimate the hedge...