I've read through everything I can on the forums but I'm still having difficulty getting my universe to warm up using the History() method. In the attached algorithm, what I believe is happening is:
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Indicators")
AddReference("QuantConnect.Common")
AddReference("QuantConnect.Algorithm.Framework")
from System import *
from QuantConnect import *
from QuantConnect.Data import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
from System.Collections.Generic import List
class PublicHelp(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2017,1,1) #Set Start Date
self.SetEndDate(datetime.now().date() - timedelta(1)) #Set End Date
#self.SetEndDate(2013,1,1) #Set End Date
self.SetCash(150000) #Set Strategy Cash
self.UniverseSettings.Resolution = Resolution.Hour
self.averages = { };
self.AddEquity("SPY", Resolution.Hour)
self.AddUniverse(self.CoarseSelectionFunction)
self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.At(9,31), self.BuyFunc)
I'm initializing and importing most of the basic stuff. I've set my Universe resolution to Hours, so the CoarseSelectionFunction should be run each hour, yeah?
So going forward, I have my coarse filter which filters down by volume and uses the EMACross tutorial code that you can find in the Universe selection portion of the Documentation.
#Universe Filter
# sort the data by volume and price, apply the moving average crossver, and take the top 24 sorted results based on breakout magnitude
def CoarseSelectionFunction(self, coarse):
filtered = [ x for x in coarse if (x.DollarVolume > 50000000) ]
# We are going to use a dictionary to refer the object that will keep the moving averages
for cf in filtered:
if cf.Symbol not in self.averages:
self.averages[cf.Symbol] = SymbolData(cf.Symbol)
# Updates the SymbolData object with current EOD price
avg = self.averages[cf.Symbol]
history = self.History(cf.Symbol, 16)
avg.WarmUpIndicators(history.iloc[cf.Symbol])
avg.update(cf.EndTime, cf.AdjustedPrice)
# Filter the values of the dict: we only want up-trending securities
values = list(filter(lambda x: x.is_uptrend, self.averages.values()))
# Sorts the values of the dict: we want those with greater difference between the moving averages
values.sort(key=lambda x: x.scale, reverse=True)
for x in values[:200]:
self.Log('symbol: ' + str(x.symbol.Value) + ' scale: ' + str(x.scale))
# we need to return only the symbol objects
return [ x.symbol for x in values[:200] ]
# this event fires whenever we have changes to our universe
def OnSecuritiesChanged(self, changes):
self.changes = changes
# liquidate removed securities
for security in changes.RemovedSecurities:
if security.Invested:
self.Liquidate(security.Symbol)
#EMA Crossover Class
class SymbolData(object):
def __init__(self, symbol):
self.symbol = symbol
self.fast = ExponentialMovingAverage(50)
self.slow = ExponentialMovingAverage(200)
self.is_uptrend = False
self.scale = None
def update(self, time, value):
if self.fast.Update(time, value) and self.slow.Update(time, value):
fast = self.fast.Current.Value
slow = self.slow.Current.Value
self.is_uptrend = (fast / slow) > 1.00
if self.is_uptrend:
self.scale = (fast - slow) / ((fast + slow) / 2.0)
def WarmUpIndicators(self, history):
for tuple in history.itertuples():
self.fast.Update(tuple.index, tuple.close)
self.slow.Update(tuple.index, tuple.close)
Here I'm running into my main problem. I don't know where to/how to apply the history loop to warmup each indicator as its added to the universe. In this example I'm getting the error:
Runtime Error: TypeError : object is not callable
I've even attempted to constantly keep the indicators updated in the OnData method like so:
at CoarseSelectionFunction in main.py:line 20
TypeError : object is not callable (Open Stacktrace)#OnData
def OnData(self, data):
'''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.'''
'''Arguments:
data: Slice object keyed by symbol containing the stock data'''
#Constantly update the universe moving averages and if a slice does not contain any data for the security, remove it from the universe
if self.IsMarketOpen("SPY"):
if bool(self.averages):
for x in self.Securities.Values:
if data.ContainsKey(x.Symbol):
if data[x.Symbol] is None:
continue
avg = self.averages[x.Symbol]
avg.update(data[x.Symbol].EndTime, data[x.Symbol].Open)
else:
self.RemoveSecurity(x.Symbol)
However, this isn't working. Perhaps I don't understand entirely, but I thought each hour a slice of data of be pumped through the indicators stored in the averages dictionary and so after 200 hours the indicators would be warmed up. I've tried this on a slow indicator of only 5 hours assuming it would be ready by the final hour, but this isn't the case.
Could someone with a deeper understanding explain my errors please? I've attached the project, for what its worth. Thank you in advance.