I can't figure out why my algorithm never starts executing any trades. It seems to only calculate the strength close.
class EarningsContinuation(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2021, 9, 14) # Set Start Date
self.SetEndDate(2022, 9, 14)
self.SetCash(100000) # Set Strategy Cash
self.UniverseSettings.Resolution = Resolution.Daily
self.AddUniverse(self.CoarseSelectionFunction, self.FineSelectionFunction)
self.spy = self.AddEquity('SPY',Resolution.Daily).Symbol
self.Schedule.On(self.DateRules.EveryDay('SPY'),self.TimeRules.AfterMarketOpen('SPY'), self.Strong)
self.entryTime = datetime.min
self.openPositions= []
self.strongclose = 0
def CoarseSelectionFunction(self,coarse):
sortedByDollarVolume = sorted(coarse, key=lambda x: x.DollarVolume, reverse=True)
byprice = [x for x in sortedByDollarVolume if x.DollarVolume > 1000000 and x.Price > 5 and x.HasFundamentalData]
byprice = [x.Symbol for x in byprice][:500]
return byprice
def FineSelectionFunction(self,fine):
yesterday = self.Time - timedelta(days=1)
fineUniverse = [x for x in fine if x.EarningReports.FileDate == yesterday
and x.MarketCap > 5e7]
tickerSymbolValuesOnly = [c.Symbol for c in fineUniverse]
return tickerSymbolValuesOnly
def Strong(self):
self.strength_dic = {}
for security in self.ActiveSecurities.Values:
symbol = security.Symbol
historyData = self.History(symbol, 2, Resolution.Daily)
try:
openDayAfterEarnings = historyData['open'][-1]
closeDayAfterEarnings = historyData['close'][-1]
highDayAfterEarnings = historyData['high'][-1]
closeDayBeforeEarnings = historyData['close'][-2]
strongclose = (closeDayAfterEarnings - openDayAfterEarnings)/(highDayAfterEarnings-openDayAfterEarnings)
self.strength_dic[symbol] = strongclose
except:
self.Debug(f"History data unavailable for {symbol.Value}")
continue
def OnSecuritiesChanges(self, changes):
for security in changes.AddedSecurities:
if self.strength_dic[security.Symbol] > 0.88:
self.MarketOrder(security,100)
self.entryTime = Time
self.openPositions.append(security)
else:
continue
def OnData(self, data: Slice):
for security in self.openPositions:
if self.entryTime == (self.entryTime.days + 5) :
self.MarketOrder(security,-100)
self.openPositions.remove(security)
else:
continue
Stelios Sbilis
In General, I am trying to check for gaps ups in a Universe of stocks that have had earnings. Where would be the best place to check for gaps? Most examples I have seen use data from the CoarseSelection Filter to check for gaps. I haven't seen anyone checking for gaps in data from the FineSelection Filter.
Louis Szeto
Hi Stelios
In line 60, it is the first instance that security was added into the universe, thus it is impossible for it to have values in self.strength_dic, since its value is based on self.ActiveSecurities. So, line 61 is never executed, and self.entryTime will always be None, making line 70 never execute as well. You could save an instance of the symbols returned in the fine filter, and call historical data on that instance in Strong method as History call does not need prior subscription of the security:
Best
Louis
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.
Stelios Sbilis
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!