Hello,
I am struggling with learning how to warm up an indicator when using universe selection. My test code and the error message it yields are given below. I would just attach the backtest, but apparently I am not able to do that since it results in a runtime error. It seems to dislike the "loc" attribute in line 36 for some reason. Any suggestions would be appreciated because I am at a loss as to how I can fix this.
2010-01-01 00:00:00 : Runtime Error: AttributeError : '0, Culture=neutral, PublicKeyToken=null]]' object has no attribute 'loc' AttributeError : '0, Culture=neutral, PublicKeyToken=null]]' object has no attribute 'loc'
from Execution.ImmediateExecutionModel import ImmediateExecutionModel
from Portfolio.EqualWeightingPortfolioConstructionModel import EqualWeightingPortfolioConstructionModel
from Risk.MaximumDrawdownPercentPerSecurity import MaximumDrawdownPercentPerSecurity
from Selection.QC500UniverseSelectionModel import QC500UniverseSelectionModel
class SimpleRSITestQC500Universe(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2010, 1, 1) # Set Start Date
self.SetEndDate(2010, 2, 28) # Set End Date
self.SetCash(100000) # Set Strategy Cash
self.SetExecution(ImmediateExecutionModel())
self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())
self.SetRiskManagement(MaximumDrawdownPercentPerSecurity(0.05))
symbols = [ Symbol.Create("SPY", SecurityType.Equity, Market.USA)]#, Symbol.Create("GE", SecurityType.Equity, Market.USA), Symbol.Create("BA", SecurityType.Equity, Market.USA) ]
self.SetUniverseSelection(ManualUniverseSelectionModel(symbols))
self.AddAlpha(RsiAlphaModelTest())
class RsiAlphaModelTest(AlphaModel):
def __init__(self, period = 14, resolution = Resolution.Daily):
self.period = period
self.resolution = resolution
self.insightPeriod = Time.Multiply(Extensions.ToTimeSpan(resolution), period)
self.rsiData ={}
resolutionString = Extensions.GetEnumString(resolution, Resolution)
self.Name = '{}({},{})'.format(self.__class__.__name__, period, resolutionString)
def OnSecuritiesChanged(self, algorithm, changes):
addedSymbols = [X.Symbol for X in changes.AddedSecurities]
[algorithm.Debug(str(X) + ' added to universe at ' + str(algorithm.UtcTime)) for X in addedSymbols]
#warmUpData = algorithm.History([X.Symbol for X in changes.AddedSecurities],self.period, self.resolution)
warmUpData = algorithm.History(self.period, self.resolution)
for symbol in addedSymbols:
self.rsiData[symbol] = algorithm.RSI(symbol, self.period, MovingAverageType.Wilders, self.resolution)
for time, row in warmUpData.loc[symbol].iterrows():
self.rsiData[symbol].Update(time, row["close"])
algorithm.Debug("Debug message OnSecuritiesChanged for:" + str(symbol) + " " + str(self.rsiData[symbol]))
#[algorithm.Debug("Time: " + str(X.Time) + " , Close:" + str(X.Bars["GE"].Close)) for X in WarmUpData]
def Update(self, algorithm, data):
insights = []
prior_rsiData = self.rsiData
for symbol in self.rsiData.keys():
#self.rsiData[symbol] = algorithm.RSI(symbol, self.period, MovingAverageType.Wilders, self.resolution)
#if self.rsiData[symbol] <= 34.0:
algorithm.Debug("Debug message for:" + str(symbol) + " " + str(self.rsiData[symbol]))
#prior_rsiData[symbol] <= 34.0 and self.rsiData[symbol] >= prior_rsiData[symbol] + 1.0:
#insights.append(Insight.Price(symbol, self.insightPeriod, InsightDirection.Up))
# elif prior_rsiData[symbol] >= 70 and self.rsiData[symbol] <= prior_rsiData[symbol] - 1:
# #insights.append(Insight.Price(symbol, self.insightPeriod, InsightDirection.Down))
# algorithm.Debug("Insight down for:" + symbol)
return insights
. Many thanks.
Hector Barrio
Hello, I have been struggling with this too, I am no expert but the history calls seems to be constructed as self.History(symbol, bar_count, resolution = null). Your line 33 has just two periods as input, I don´t know if this generates something that is not a dataframe. To solve this I have moved in my algorithms inside your loop starting line 34 as warmUpData = algorithm.History(symbol,self.period, self.resolution).
I have learned a lot from :
https://www.quantconnect.com/forum/discussion/5951/universe-warmup-python/p1
Hector Barrio
I found it, if you call in all securities for history you obtain an array of slice objects according to:
https://www.quantconnect.com/docs/algorithm-reference/historical-data#Historical-Data-All-Securities-History-RequestGarrett Grow
Aahh! I see. I knew that from having read the documentation, but I guess I didn't think of it. Thank you very much, sir.
Garrett Grow
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!