Apparently, history does not return a python DataFrame when combined with a Universe.
This works and returns a python DataFrame with highs, lows and closes for the selected securities:
class algorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2012,1,1)
self.SetEndDate(2012,2,5)
self.SetCash(100000)
self.symbols = ['ABT', 'ACN', 'ACE', 'ADBE']
for symbol in self.symbols:
self.AddEquity(symbol, Resolution.Daily)
self.AddEquity('SPY', Resolution.Daily)
self.Schedule.On(self.DateRules.MonthStart("SPY"),
self.TimeRules.AfterMarketOpen("SPY", 60),
Action(self.Rebalance))
def Rebalance(self):
history = self.History(self.symbols, 20, Resolution.Daily)
self.Debug(history.head())
This throws an error:
class algorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2012,1,1)
self.SetEndDate(2012,3,1)
self.SetCash(100000)
self.AddEquity("SPY", Resolution.Minute)
self.UniverseSettings.Resolution = Resolution.Minute
self.Schedule.On(self.DateRules.EveryDay("SPY"),
self.TimeRules.AfterMarketOpen("SPY", 30),
Action(self.rebalance))
self.universe = []
def CoarseSelectionFunction(self, coarse):
today = self.Time
CoarseWithFundamental = [x for x in coarse if x.HasFundamentalData]
sortedByDollarVolume = sorted(CoarseWithFundamental, key=lambda x: x.DollarVolume, reverse=True)
result = [ x.Symbol for x in sortedByDollarVolume[:self.__numberOfSymbols] ]
self.universe = result
return self.universe
def rebalance(self):
history = self.History(20, Resolution.Daily)
self.Debug(history.head())
How can I change the second snippet such that it returns a DataFrame with the bars for all the assets in the Universe?
Gurumeher Sawhney
Thank you for supplying the code. It turns out that the universe is empty and this is because AddUniverse() is not present in the initialization portion of the code. So the coarse selection function is never called and nothing is ever entered into the universe. The number of symbols was also never initialized so I just set it to 4, which is the amount requested above. The backtest below has those edits, along with a few others that were needed to run correctly. A link to the documentation on using universes is below as well.
Filib Uster
Thanks a lot - no idea how I could have missed that.
Follow up question:
How can I iterate through the University and print out each security contained in it? Oviously, I have saved the universe in a helper array. But can I just use an UniversityManager to do that?
Filib Uster
Another follow up:
Why isn't the following snippet working?
class algorithm(QCAlgorithm): def Initialize(self): self.SetStartDate(2012,1,1) #Set Start Date self.SetEndDate(2012,2,5) #Set End Date self.SetCash(100000) #Set Strategy Cash self.market_window = 200 self.AddEquity('SPY', Resolution.Daily) self.Schedule.On(self.DateRules.MonthStart("SPY"), self.TimeRules.AfterMarketOpen("SPY", 60), Action(self.Rebalance)) def Rebalance(self): history = self.History('SPY', 20, Resolution.Daily) self.Debug(history.head())
How can I get a DataFrame similiar to the once return by the snippet above?
Gurumeher Sawhney
Yes, the UniverseManager can be used to locate the symbols in the defined universe. The code below is a good example of accessing the symbols.
for universe in self.UniverseManager.Values: # User defined universe has symbols from AddSecurity/AddEquity calls if universe is UserDefinedUniverse: continue symbols = universe.Members.Keys
With regards to the code not working see the algorithm below. The symbol was passed to self.History in a list, like the aforementioned code above and the DataFrame was created:
Filib Uster
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!