Back

Covariance Matrix in Python

Hello everyone,

I am new to QC and was hoping to get some help. Trying to adapt an algorithm to QC, I am trying to create a covariance matrix but am having a hard time with the QC History Dataframes.

Here is my attempt:

hist = self.History(self.stocks, self.lookback+1, Resolution.Daily).unstack(level=0).close
hist = hist[symbols].pct_change()[1:]
covar_matrix = hist.cov

Using this code, I receive the following error while attempting to call covar_matrix.shape[0] (to determine the number of symbols being analyzed):

QuantConnect.Scheduling.ScheduledEventException: In Scheduled Event 'SPY: MonthEnd: SPY: 15 min before MarketClose', ---> System.Exception: AttributeError : 'function' object has no attribute 'shape' ---> Python.Runtime.PythonException: AttributeError : 'function' object has no attribute 'shape'
at Python.Runtime.PyObject.Invoke (Python.Runtime.PyObject[] args) [0x00035] in <945e8bed1d5b4491a3fcef472d4dd5dc>:0
at QuantConnect.Scheduling.ScheduleManager+<>c__DisplayClass15_0.b__0 (System.String name, System.DateTime time) [0x00006] in <08e06e9d6cbe44de803fb98b16360adf>:0
at QuantConnect.Scheduling.ScheduledEvent.OnEventFired (System.DateTime triggerTime) [0x00029] in <08e06e9d6cbe44de803fb98b16360adf>:0
--- End of inner exception stack trace ---
--- End of inner exception stack trace ---

 

Can you help me solve this error and correct the format of the covariance matrix?

Any assistance is greatly appreciated. Thank you!

Update Backtest







0

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.


Is there anyone able to assist with this?

0

I believe I may have found a solution...

import numpy as np
import pandas as pd

class CovarianceMatrixTest(QCAlgorithm):

def Initialize(self):
self.SetStartDate(2019,9,1) # Set Start Date
self.SetCash(10000) # Set Strategy Cash

self.tickers = [
"SPY",
"QQQ",
"TLT"
]

for symbol in self.tickers:
self.AddEquity(symbol, Resolution.Minute)

self.history = self.History(self.tickers, 22, Resolution.Daily)
self.history['log_ret'] = np.log(1 + self.history.close.pct_change())
self.Log(self.history['log_ret'].unstack(level=0).dropna())
cov_matrix = np.cov(self.history['log_ret'].unstack(level=0).dropna())
self.Log(cov_matrix)

Please let me know if you have any feedback or recommendations to improve this code...

Thanks!

0

I'm thinking there must be an error in my code... The shape of the covariance matrix appears off...

0

I believe I have found a solution - posting it in case it can be helpful:

import numpy as np
import pandas as pd

class CovarianceMatrixTest(QCAlgorithm):

def Initialize(self):
self.SetStartDate(2019,9,1) # Set Start Date
self.SetCash(10000) # Set Strategy Cash

self.tickers = [
"SPY",
"QQQ",
"TLT"
]

for symbol in self.tickers:
self.AddEquity(symbol, Resolution.Minute)

self.history = self.History(self.tickers, 22, Resolution.Daily)
self.history['log_ret'] = np.log(1 + self.history.close.pct_change())
self.Log(self.history['log_ret'].unstack(level=0).dropna().cov())

Please let me know if you have any feedback or recommendations to improve this code...

0

Hi Brad,

I am glad you solve the problem. Yes, the code works. If you just need log return, you can try the code below. If you need the open, high, low, close price for further usage, then your code is fine.
 

import numpy as np
import pandas as pd

class CovarianceMatrixTest(QCAlgorithm):

def Initialize(self):
self.SetStartDate(2019,9,1) # Set Start Date
self.SetCash(10000) # Set Strategy Cash

self.tickers = [
"SPY",
"QQQ",
"TLT"
]

for symbol in self.tickers:
self.AddEquity(symbol, Resolution.Minute)

returns = np.log(self.History(self.tickers, 22, Resolution.Daily).\
close.unstack(level=0).pct_change()+1).dropna()
self.Log(returns.cov())

Hope this helps!

1

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.


Thank you!

0

Update Backtest





0

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.


Loading...

This discussion is closed