As the title states, I'm running an algorithm where part of it is calculating the correlation between the SPY and TLT.

Below is the code I'm using for just the correlation component.

 

from scipy.stats.stats import pearsonr
import pandas as pd

class CalmFluorescentPinkAntelope(QCAlgorithm):
    
    def Initialize(self):
        self.SetStartDate(2020, 8, 9)
        self.SetCash(100000)
        self.AddEquity("SPY", Resolution.Daily)
        self.AddEquity("TLT", Resolution.Daily)
        self.SetWarmUp(50)
        
        
    def OnData(self, data):
        
        my_corr = self.find_corr(data)
        
    def find_corr(self, data):
        
        spy_close = self.History(self.Symbol("SPY"), 22, Resolution.Daily).loc["SPY"]["close"][:-1]
        tlt_close = self.History(self.Symbol("TLT"), 22, Resolution.Daily).loc["TLT"]["close"][:-1]
        
        try:
            self.Debug(pearsonr(spy_close, tlt_close)[0])
        except:
            self.Debug("spy " + str(len(spy_close)))
            self.Debug("tlt " + str(len(tlt_close)))
        
        return pearsonr(spy_close, tlt_close)[0]

What ends up happening is I get the “x and y must have the same length” error. This only happens at certain points in time, such as on the 11th of August of 2020. I checked the length of the arrays using the debug in the code, and found that on that day, the length of the arrays is indeed different, where the SPY data would have a length of 21, while the TLT data would have a length of 20.

The warm up runs without issue. Am I doing something wrong?