5
$\begingroup$

I am trying to learn about pairs trading strategy. I know that we have to long and short cointegrated assests simultaneously. But I still have some confusion in how the strategy works. I wrote the pseudo code for what I think pairs trading strategy is?

x=price data of asset x
y=price data of asset y
if x and y are correlated and cointegrated

 calculate pair ratio(spread) x/y or y/x?
 calculate average of pair ratio(spread)

     if spread > mean
     sell asset ?
     buy asset ?
     else spread < mean
     sell asset ?
     buy asset ?
     close if

else

 find new pair of assets x and y
 go to line 1 with new x and y

close if

Here I am taking pair ratio(x/y or y/x) as the spread? My first question is which pair ratio should I take x/y or y/x?

if I take x/y as spread then what assests should I buy and sell if spread>mean.

If I am wrong in my assesment of pseudo code of pair trading then feel free to correct me.

$\endgroup$

1 Answer 1

11
$\begingroup$

The following link has a good summary of a typical pair trading strategy:

https://www.quantstart.com/articles/Backtesting-An-Intraday-Mean-Reversion-Pairs-Strategy-Between-SPY-And-IWM

It actually has full python code as well. It doesn't include a cointegration check though.

Edit:

if X and Y are cointegrated:
    calculate Beta between X and Y 
    calculate spread as X - Beta * Y
    calculate z-score of spread

    # entering trade (spread is away from mean by two sigmas):
    if z-score > 2:
        sell spread (sell 1000 of X, buy 1000 * Beta of Y)
    if z-score < -2:
        buy spread (buy 1000 of X, sell 1000 * Beta of Y)

    # exiting trade (spread converged close to mean):
    if we're short spread and z-score < 1:
        close the trades
    if we're long spread and z-score > -1:
        close the trades

# repeat above on each new bar, recalculating rolling Beta and spread etc.
$\endgroup$
2
  • 1
    $\begingroup$ It would be very helpful and easy to understand;if you can convert the logic into a pseudo code $\endgroup$
    – Eka
    Commented Jul 13, 2016 at 6:59
  • 2
    $\begingroup$ added pseudo code $\endgroup$
    – KarolisR
    Commented Jul 13, 2016 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.