Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Pairs trading questions

Hi all,

Could someone please help me with solving the following questions ?

I formed a stock universe where the program seeks cointegrated pairs using python (stocks from SP500). Also there are functions to plot the spread.
So I defined z-score, linear regression and price ratio functions.
Here are GILD/WFC pair, for instance. The slope for our combination is 0.74
1. What exactly positions should I open using the z-score plot at the current moment (when z-score below 0 , around -2)? Should it be opened short WFC vs long 0.74*GILD or long WFC vs short 0.74*GILD ?
2. about fitting the spread to OU process. Here are the formula to compute the speed of mean-reversion .
B - slope = 0.74
So in our example : the speed = -lnB/t = -ln0.74/1/252 = 0.3/0.00396 = 75.75 days for reverting to the equilibrium level , are these arguments correct ?

Thank's in advance.

1 response

Hi Denis, there is a good example: Quantopian Lecture 46 based on an implementation by Ernest Chan in algorithmic trading.

You may find these functions in the example algorithm useful for your purpose:

def hedge_ratio(Y, X, add_const=True):  
def computeHoldingsPct(yShares, xShares, yPrice, xPrice):  

For simplicity, the notional weights for y_target_shares = ±1 and X_target_shares = ∓hedge are for purpose of the lecture, and may not be applicable to the specifics of your actual pairs.

Hope this helps.