Back to Community
Python Pairs Trading - Z score and Correlation Coefficient for negative correlation

Hi All, This is more conceptual than technical, as I can insert the logic into the below for loop if I know what I'm looking to do. I have a DataFrame, Df with zscores, correlation coefficients for time series (stocks) data.

The two columns being referenced accordingly are "pairs_zscore" and "ES_CC" (correlation coefficient between the S&P 500 index and US treasury futures). Here's the for loop to determine if I'm going to get long ES (S&P 500 futures) based on the relationship between S&P 500 futures and treasury futures:

for index, row in Df.iterrows():  
    if Df.loc[index, 'pairs_zscore'] < -.5 and Df.loc[index, 'ES_CC'] > .4:  
        Df.loc[index, 'Lead_Lag'] = "Long_ES"  
    elif Df.loc[index, 'pairs_zscore'] < -.5 and Df.loc[index, 'ES_CC'] < -.4:  
        Df.loc[index, 'Lead_Lag'] = "Long_ES"  

So for instance, in the first if statement I'm saying if the z-score is < -.5 and the correlation coefficient is positive (>.4) between S&P 500 (ES) and Treasury futures, then the spread between ES and treasury futures is less than the mean and so I want to get long S&P 500 futures.

For instance, if S&P 500 is at 2300, and treasury futures are priced at 1000, but then treasuries go up to 1100 and S&P 500 stays at 2300, the spread has tightened and z-score will be negative. So I would get long S&P futures expecting them to go up and the spread to widen again back to the mean.

That's straightforward enough, I think the logic in the for loop makes sense for when the two assets correlate positively, but what about when they're negative. As shown above, my logic right now is if the z-score is negative (spread between S&P futures and treasury futures is less than average) and Correlation Coefficient (CC) is negative, then I would anticipate the spread reverting to the mean again.

For instance, if the correlation is negative between the two assets and treasuries move down, it makes sense that S&P 500 should move up (because they move in opposite directions). If the S&P 500 hasn't moved up then the spread is smaller than it should be. So it seems like z-score for negative correlating variables works entirely differently from z-score for positive correlating assets.

So the question is: How would I code the logic for negative correlation and z-scores to figure out when to get long the ES if treasuries have dropped in value?

1 response

So to clarify, I have two assets that correlate negatively. If one goes up, and the other doesn't go down (or go down as much relatively) then I want to short the asset that hasn't gone down. I'm starting to think z-score can't account for negative correlations like this...