I'm trying to use Quantbook to get equity data. The algorithm works well in backtests, but it's not working in live mode. It throws an exception during algorithm initialization. Has anyone had a similar problem with Quantbook before?
from AlgorithmImports import *
class QuantbookTestingAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetTimeZone(TimeZones.London)
self.AddUniverse(self.SelectSymbols)
self.universe = {}
self.qb = QuantBook()
def OnData(self, data):
pass
def OnSecuritiesChanged(self, changes):
for s in changes.AddedSecurities:
if str(s.Symbol) not in self.universe:
self.universe[str(s.Symbol)] = s
self.qb.AddEquity(str(s.Symbol).split(' ')[0])
for s in changes.RemovedSecurities:
if str(s.Symbol) in self.universe.keys():
self.universe.pop(str(s.Symbol))
self.qb.RemoveSecurity(str(s.Symbol).split(' ')[0])
def SelectSymbols(self, symbols):
selected = [c for c in symbols if c.HasFundamentalData]
sorted_by_dollar_volume = sorted(selected, key=lambda c: c.DollarVolume, reverse=True)
return [ c.Symbol for c in sorted_by_dollar_volume[:50] ]
Mia Alissi
The issue you're experiencing is due to the fact that
QuantBook
is not supported in live trading.QuantBook
is a research environment tool that allows you to access QuantConnect's data sources for research purposes. It is not intended for use in live trading algorithms.In your algorithm, you're trying to use
QuantBook
to add and remove equities. Instead, you should use theAddEquity
andRemoveSecurity
methods of theQCAlgorithm
class. Here's how you can modify your code:This should resolve the issue you're experiencing in live trading. For more information on how to use
AddEquity
andRemoveSecurity
, you can refer to the QuantConnect Documentation.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.
Algo TBKL
Thank you Mia. I want to use the Quantbook because I need some fundamental data (for example, the market cap and valuations ratios). How can I get the fundamental data in live mode, is that possible?
Mia Alissi
Yes, it is possible to get fundamental data without using QuantBook. In live trading, you can access fundamental data through the
FineFundamental
object. This object is available in theFineSelectionFunction
of theAddUniverse
method. Here's an example of how you can select symbols based on their market cap and valuation ratios:In this example, the
FineSelectionFunction
sorts the fine universe by market cap and valuation ratio, and selects the top 50 symbols for each. The selected symbols are then added to the algorithm's universe.For more information on how to use fundamental data in your algorithms, you can refer to the QuantConnect Documentation.
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.
Algo TBKL
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!