Back to Community
Kalman Filter Pairs Trading

# This is my very first algorithm trading strategy #

Recently I have been studying quantitative strategies to be implemented in trading, and this is my very first notebook in quantopian. I decided to post some codes and functions of my algorithm here. Discussion is welcomed, and I do hope experienced professionals can give me some advice on how I should improve it.

The algorithm is based on Kalman Filter and Kelly's Criterion. Potential pairs are selected based ADF test statistics (regression residuals from Orthogonal Distance Regression between 2 stocks) of 1 year before trading start date (choosing the pairs with most negative ADF test statistic). Then train the Kalman Filter for one year before trading and keep updating the state variables to get daily updated hedge ratio. Check the prediction error, and trigger a long/short of spread if certain signal is met (a parameter to be input by user). Close the position if certain signal is met (again, a parameter to be input by user). The size of capital to be invested in each trade is determined by Kelly's Criterion based on trading results of previous x days (x to be input by user).

In the pairTrade2dot0 function, parameters are defined as:

numberOfDays (int): backtesting time period
KellyDays (int): daycount for Kelly's Criterion
multiplier (float): multiplier on bet size
drawDownControl (float): a factor to control bet size with respect to drawdown
rebalancingPeriod (int): rebalancing period
closeSignalx (float): close position signal
openSignalx (float): open long/short signal
closeSignalDown (float): downside control for close signal

Loading notebook preview...