# Pair Trade - Should stock order matter

I am testing a simple pair trading algorithm and I'm having problems if I swap the stocks around. I don't know which stock should be X and which should be Y. When I swap them I get very different results.

I would expect the returns to the be the same when swapped. I may want to long an spread, and if I swap them around would want to short it. Since I swapped, I should still be longing and shorting the same securities.

Here is a tedious example that I worked up in excel:

    X      Y

164.603 155.845
166.226 156.417
168.312 159.387
164.908 155.588
166.571 156.614


I calculate the slope as slope(y1:y5, x1,x5) which gives me a hedge factor of 0.963. Then I create a spread for each value with the formula Y-hedge*X, which gives me this series:

-2.668778868
-3.659738615
-2.698570426
-3.219495888
-3.794975899


The mean of this series is -3.208 and the standard deviation is 0.52417.

Assume X moves to 164 and y moves to 154. Now my spread value is -3.93309. This gives me a Z value of -1.38271. I want to long Y and short X. I want to exit when the zscore returns to near zero. Assume X moves to 163.40 and Y moves to 154.19. My new zscore is 0.08. So I exit the trade with a profit. (my long went up and my short went down).

Now if I swap X and Y the hedge factor changes, and the mean of my spread is vastly different (16.6 vs -3.2).
When X moves to 154 and y moves to 164 (same as above but swapped) the zscore is 2.13. I would want to sort Y and long X, but since I swapped the order I am longing and short the same as before. Now when X moves to 154.19 and Y moves to 163.40 my zscore moves to 48.03. I don't exit - I am waiting for it to return to near zero.

I think I am missing something simple.

• Hi: I wouldn't do what you're doing on raw prices ( they are very non-stationary ) but that's un-related to your question. The reason that "anomaly" occurs is that if one compares the results y = beta_1 * x and x = beta_2 * y, then beta_1 and beta_2 are not inverses of each other. if you want that inverse relationship to hold, then you would need to use total least squares regression which is a pretty different animal. I would just stick with least squares as you're doing but maybe do it on the returns rather than the raw prices. Do it both ways and see which gives better results. – mark leeds Mar 13 at 1:03
• I will give that a shot. I am already using returns to check for stationary. I was using the prices to decide when to enter and exit. – Don Chambers Mar 13 at 14:11
• Okay. Good. To be honest, I'm not sure if that's ( using returns for stationarity and prices for entry and exit ) okay. Are you using Dickey Fuller for statonarity or EG test for cointegrated-ness ? I think it ( using prices to decide ) depends on how you're deciding. Hopefully someone else can chime in here. There are many things on the internet about pairs strategies but each has its own differences and nuances. Good luck and I'm sorry that I can't be more helpful. – mark leeds Mar 13 at 14:59
• Dickey Fuller. At this point I am just concerned with my algorithm. My thought is when the spread reverts to the mean I should profit (if it's stationary). If I have picked a bad pair (not a real pair), then it may not revert to the mean and that's how I lose. Currently, I have thing reverted to I still lose. The zscore and spread revert, but when I look at the prices they have not. – Don Chambers Mar 13 at 16:32
• I'm not sure how to pick my entry/exit points when using returns. There could be a extra large return with one stock that trigger a open, then a large (but not extra large) return with the other stock that triggers the close. – Don Chambers Mar 13 at 16:47