I am trying to get History for a symbol. But got below key not exist error
“To prevent the exception, ensure that the 'TCK TJVJKNII45T1' key exist in the collection and/or that collection is not empty.
history = self.History(cf.Symbol in ./cache/algorithm/project/main.py: line 31”
How should I avoid this issue? Check key exist before querying History? If so, how do I do that? It's not documented in history request page.
Thanks!
def CoarseSelection(self, coarse):
by_market_cap = sorted(coarse, key=lambda x: x.DollarVolume, reverse=True)
for cf in by_market_cap[:2000]:
if cf.Symbol not in self.symbols:
self.symbols[cf.Symbol] = Hammer(cf.Symbol)
history = self.History(cf.Symbol, 1, Resolution.Daily).loc[cf.Symbol]
Mia Alissi
Yes, you're correct. Before querying the history, you should check if the key exists in the DataFrame. If the history request returns an empty DataFrame and you try to slice it, it throws an exception.
Here is a safe way to get historical data:
In this function, it first checks if the symbols list is empty. If it is, it logs a message and returns
False, None
. Then it requests the historical data. If the DataFrame is empty (i.e., there is no data for the requested symbols), it logs a message and returnsFalse, None
. Otherwise, it returnsTrue
and the close prices of the symbols.You can use this function in your
CoarseSelection
method like this:This way, you can avoid the exception when the history request returns no data.
For more information, please refer to the History Requests page.
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.
Stanley
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!