# What is the pseudo code for a pairs trading strategy?

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?

sell asset ?
sell 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.

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

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

if z-score > 2:
sell spread (sell 1000 of X, buy 1000 * Beta of Y)
if z-score < -2: