When building off a learning sample I am running into an issue where the code is not executing trades until mid 2014. Before adding alternate equities, it was working fine for the time range, but now it does not. Am I missing something?

 

import clr clr.AddReference("System") clr.AddReference("QuantConnect.Algorithm") clr.AddReference("QuantConnect.Indicators") clr.AddReference("QuantConnect.Common") from System import * from QuantConnect import * from QuantConnect.Algorithm import * from QuantConnect.Indicators import * ### <summary> ### In this example we look at the canonical 15/30 day moving average cross. This algorithm ### will go long when the 15 crosses above the 30 and will liquidate when the 15 crosses ### back below the 30. ### </summary> ### <meta name="tag" content="indicators" /> ### <meta name="tag" content="indicator classes" /> ### <meta name="tag" content="moving average cross" /> ### <meta name="tag" content="strategy example" /> class MovingAverageCrossAlgorithm(QCAlgorithm): def Initialize(self): '''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.''' self.SetStartDate(2008, 1, 1) #Set Start Date self.SetEndDate(2015, 1, 1) #Set End Date self.SetCash(100000) #Set Strategy Cash # Find more symbols here: http://quantconnect.com/data self.AddEquity("SPY") self.AddEquity("GOOG") self.AddEquity("AMZN") self.AddEquity("MSFT") self.AddEquity("AAPL") # create a 15 day exponential moving average self.fast = self.EMA("SPY", 15, Resolution.Daily) self.fasta = self.EMA("GOOG", 15, Resolution.Daily) self.fastb = self.EMA("AMZN", 15, Resolution.Daily) self.fastc = self.EMA("MSFT", 15, Resolution.Daily) self.fastd = self.EMA("AAPL", 15, Resolution.Daily) # create a 30 day exponential moving average self.slow = self.EMA("SPY", 30, Resolution.Daily) self.slowa = self.EMA("GOOG", 30, Resolution.Daily) self.slowb = self.EMA("AMZN", 30, Resolution.Daily) self.slowc = self.EMA("MSFT", 30, Resolution.Daily) self.slowd = self.EMA("AAPL", 30, Resolution.Daily) self.previous = None def OnData(self, data): '''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.''' # a couple things to notice in this method: # 1. We never need to 'update' our indicators with the data, the engine takes care of this for us # 2. We can use indicators directly in math expressions # 3. We can easily plot many indicators at the same time # wait for our slow and fast ema to fully initialize if not self.slow.IsReady: return if not self.fast.IsReady: return if not self.slowa.IsReady: return if not self.fasta.IsReady: return if not self.slowb.IsReady: return if not self.fastb.IsReady: return if not self.slowc.IsReady: return if not self.fastc.IsReady: return if not self.slowd.IsReady: return if not self.fastd.IsReady: return # only once per day if self.previous is not None and self.previous.date() == self.Time.date(): return # define a small tolerance on our checks to avoid bouncing tolerance = 0.00015 holdings = self.Portfolio["SPY"].Quantity holdingsa = self.Portfolio["GOOG"].Quantity holdingsb = self.Portfolio["AMZN"].Quantity holdingsc = self.Portfolio["MSFT"].Quantity holdingsd = self.Portfolio["AAPL"].Quantity # we only want to go long if we're currently short or flat if holdings <= 0: # if the fast is greater than the slow, we'll go long if self.fast.Current.Value > self.slow.Current.Value *(1 + tolerance): self.Log("BUY >> {0}".format(self.Securities["SPY"].Price)) self.SetHoldings("SPY", .2) if holdingsa <= 0: if self.fasta.Current.Value > self.slowa.Current.Value *(1 + tolerance): self.Log("BUY >> {0}".format(self.Securities["GOOG"].Price)) self.SetHoldings("GOOG", .2) if holdingsb <= 0: if self.fastb.Current.Value > self.slowb.Current.Value *(1 + tolerance): self.Log("BUY >> {0}".format(self.Securities["AMZN"].Price)) self.SetHoldings("AMZN", .2) if holdingsc <= 0: if self.fastc.Current.Value > self.slowc.Current.Value *(1 + tolerance): self.Log("BUY >> {0}".format(self.Securities["MSFT"].Price)) self.SetHoldings("MSFT", .2) if holdingsd <= 0: if self.fastd.Current.Value > self.slowd.Current.Value *(1 + tolerance): self.Log("BUY >> {0}".format(self.Securities["AAPL"].Price)) self.SetHoldings("AAPL", .2) # we only want to liquidate if we're currently long # if the fast is less than the slow we'll liquidate our long if holdings > 0 and self.fast.Current.Value < self.slow.Current.Value: self.Log("SELL >> {0}".format(self.Securities["SPY"].Price)) self.Liquidate("SPY") if holdingsa > 0 and self.fasta.Current.Value < self.slowb.Current.Value: self.Log("SELL >> {0}".format(self.Securities["GOOG"].Price)) self.Liquidate("GOOG") if holdingsb > 0 and self.fastb.Current.Value < self.slowb.Current.Value: self.Log("SELL >> {0}".format(self.Securities["AMZN"].Price)) self.Liquidate("AMZN") if holdingsc > 0 and self.fastc.Current.Value < self.slowc.Current.Value: self.Log("SELL >> {0}".format(self.Securities["MSFT"].Price)) self.Liquidate("MSFT") if holdingsd > 0 and self.fastd.Current.Value < self.slowd.Current.Value: self.Log("SELL >> {0}".format(self.Securities["AAPL"].Price)) self.Liquidate("AAPL") self.previous = self.Time

 

Author