@BIBANI Iheb is correct. I haven't explicitly stated how to find the beta of Y relative to X but use statsmodels OLS
or some similar method. In calculating the z-scores one needs a series of past spreads to calculate the mean. The beta will vary with time, so one approach is to use a 'rolling' calculation of beta to weight the X prices.
There was a question about how much to allocate to each stock X and Y? One will find several approaches to this if doing an internet search. Often the approach is to have equal size positions so the strategy is 'market neutral'. I don't exactly agree with that (for reasons demonstrated in the example below) but I do want to say there isn't a universal approach. That said, I would allocate with a ratio of -beta of Y relative to X. If one orders $1000 of Y then order -beta x $1000 of stock X. Of course one position will be long and the other short depending upon the sign of the spread.
An example of how this works. We are trying to protect from market movements so the size of the long and short positions should 'balance' each other. Again assume we have stocks X and Y which are priced the same at $100. The difference is they do not move uniformly with the market. Assume stock X generally moves the same as the market. Its beta relative to the market is 1. However, stock Y is more volatile. It sees bigger swings and has a beta, relative to the market, of 1.1. Since X moves the same as the market, the beta of Y relative to X can be taken as the beta to the market. Let's see what happens to a portfolio with a 10% gain in the market. Assume X and Y just move with the market and there is no underlying price reversion.
# note we assume we are shorting X
net_initial_position = pos_Y + pos_X
net_initial_position = $1000 + (-beta * $1000) = $1000 + (-1.1 * $1000) = $1000 - $1100 = -$100
net_final_position_10_pct_mkt_gain = (pos_Y * y_gain) + (pos_X * x_gain)
net_final_position_10_pct_mkt_gain = ($1000 * (1+(.1*1.1))) + (-$1100 * (1+.1) = $1110 - $1210 = -$100
Great. With a big market swing our market neutral strategy left our portfolio untouched with the same initial value of -$100. Now let's see what a 10% market loss does.
# again note we assume we are shorting X
net_initial_position = pos_Y + pos_X
net_initial_position = $1000 + (-beta * $1000) = $1000 + (-1.1 * $1000) = $1000 - $1100 = -$100
net_final_position_10_pct_mkt_loss = (pos_Y * y_gain) + (pos_X * x_gain)
net_final_position_10_pct_mkt_loss = ($1000 * (1-(.1*1.1))) + (-$1100 * (1-(.1))) = ($1000*.89) - ($1100 *.9) = $890 - $990 = -$100
While we probably weren't ecstatic that our market neutral strategy remained flat when the market went up 10%, we should be happy that when the market went down 10% we didn't loose anything.
So, the dollar amount to portion to each position, to remain market neutral, is
X_position = -beta * Y_position.
One way to think of this is, if the beta of Y relative to X is greater than 1, then Y is more volatile than X. Y will have larger price swings. Therefore, to compensate for the larger Y swings, one needs to hold a proportionately larger position of X.
Conversely, if beta is less than 1, then Y is less volatile than X. Y will have smaller price swings. To match the smaller Y swings, one needs to hold a proportionately smaller position of X.
This weighting can be seen implemented in the Lecture 46 pairs trading algo.