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?
Emile Farkouh
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!