Hello,
I am getting the following error trying to retrive DOW ticker.
Runtime Error: Trying to retrieve an element from a collection using a key that does not exist in that collection throws a KeyError exception. To prevent the exception, ensure that the DOW key exist in the collection and/or that collection is not empty. at OnData in main.py:line 111 KeyError : 'DOW'From 2017/1/1 up to today. Hourly candle.
Karthik Kailash
Have you added DOW to your universe? You should attach a backtest with this type of question so people can see your code
Giuliano Ciccone
Sorry still new in Quantconnect
I got to solve the problem not using .values when accessing the list of filtered securities from a Coarse.
Jared Broad
No problem Giuliano Ciccone we were all new once!
Please post enough information for people to assist though so discussions can be more constructive. You might want to go as far as making a mock-up algorithm that repeats the issue if you don't want to share your code.
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.
Giuliano Ciccone
HiJared Broad thanks!
I am attaching the code with the problem and the solution.
Solution
from sklearn.linear_model import LinearRegression import numpy as np import pandas as pd class CalibratedHorizontalThrustAssembly(QCAlgorithm): def Initialize(self): self.SetTimeZone("America/New_York") self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Margin) self.SetStartDate(2017, 1, 1) # self.SetEndDate(2020, 5, 2) self.SetCash(100000) self.AddUniverse(self.CoarseSelectionFilter) self.UniverseSettings.Resolution = Resolution.Hour def CoarseSelectionFilter(self, coarse): sortedByDollarVolume = sorted(coarse, key=lambda x: x.DollarVolume, reverse=True) filteredByPrice = [x.Symbol for x in sortedByDollarVolume if x.Price > 10 and x.HasFundamentalData == True] self.filteredByPrice = filteredByPrice[:50] return filteredByPrice[:50] def OnData(self, data): portfolio_invested = [x.Symbol.Value for x in self.Portfolio.Values if x.Invested] securities = self.filteredByPrice for x in securities: self.AddEquity(x.Value, Resolution.Hour) self.AddEquity("SPY", Resolution.Hour) # self.Debug(str(data.ContainsKey(x)) + " " + str(x)) market = self.History(["SPY"], 20, Resolution.Daily) market = market.loc[("SPY"), "close"] df_multi = self.History(x, 20, Resolution.Daily) df_single = df_multi.loc[x, "close"] self.Debug(df_single)
Problem
from sklearn.linear_model import LinearRegression import numpy as np import pandas as pd class CalibratedHorizontalThrustAssembly(QCAlgorithm): def Initialize(self): self.SetTimeZone("America/New_York") self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Margin) self.SetStartDate(2017, 1, 1) # self.SetEndDate(2020, 5, 2) self.SetCash(100000) self.AddUniverse(self.CoarseSelectionFilter) self.UniverseSettings.Resolution = Resolution.Hour def CoarseSelectionFilter(self, coarse): sortedByDollarVolume = sorted(coarse, key=lambda x: x.DollarVolume, reverse=True) filteredByPrice = [x.Symbol for x in sortedByDollarVolume if x.Price > 10 and x.HasFundamentalData == True] self.filteredByPrice = filteredByPrice[:50] return filteredByPrice[:50] def OnData(self, data): portfolio_invested = [x.Symbol.Value for x in self.Portfolio.Values if x.Invested] securities = self.filteredByPrice for x in securities: self.AddEquity(x.Value, Resolution.Hour) self.AddEquity("SPY", Resolution.Hour) # self.Debug(str(data.ContainsKey(x)) + " " + str(x)) market = self.History(["SPY"], 20, Resolution.Daily) market = market.loc[("SPY"), "close"] df_multi = self.History(x.Value, 20, Resolution.Daily) df_single = df_multi.loc[x.Value, "close"] self.Debug(df_single)
Shile Wen
Hi Giuliano,
If we use Universe Selection, we should not use AddEquity. Furthermore, we should try to use Symbol objects whenever we can when using Lean. Also, instead of making repeated History calls, we can use a dict of RollingWindows indexed by Symbol to store the data, with just 1 history call at the beginning to initialize the data. I've also made a few stylistic changes. Please see the attached backtest for reference.
Best,
Shile Wen
Giuliano Ciccone
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!