Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Probabilistic Sharpe Ratio
0%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
0.901
Tracking Error
0.205
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
Lowest Capacity Asset
# region imports
from AlgorithmImports import *
# endregion

class DancingYellowGreenZebra(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2022, 3, 17)  # Set Start Date
        self.SetCash(100000)  # Set Strategy Cash

        # add SPY so that we can use it in the schedule rule below
        self.SPY = self.AddEquity('SPY', Resolution.Minute).Symbol

        # build a universe using the CoarseFilter and FineFilter functions defined below
        self.AddUniverse(self.CoarseFilter, self.FineFilter)

        #Add event to run every day
        self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.AfterMarketOpen("SPY", 1), self.AfterMarketOpen)
        
        #lis of tickers
        tickerSymbolValuesOnly = []
        print1 = 1


   

    def CoarseFilter(self, universe):
        # filter universe, ensure DollarVolume is above a certain threshold
        # also filter by assets that have fundamental data
        universe = [asset for asset in universe if asset.DollarVolume > 1000000 and asset.Price > 10 and asset.HasFundamentalData]
        
        # sort universe by highest dollar volume
        sortedByDollarVolume = sorted(universe, key=lambda asset: asset.DollarVolume, reverse=True)
        
        # only select the first 500
        topSortedByDollarVolume = sortedByDollarVolume[:5]

        # we must return a list of the symbol objects only
        symbolObjects = [asset.Symbol for asset in topSortedByDollarVolume]

        # this line is not necessary, but we will use it for debugging to see a list of ticker symbols
        tickerSymbolValuesOnly = [symbol.Value for symbol in symbolObjects]
       
        return symbolObjects

    def FineFilter(self, coarseUniverse):
        yesterday = self.Time - timedelta(days=1)

        fineUniverse = [asset.Symbol for asset in coarseUniverse if asset.EarningReports.FileDate == yesterday and asset.MarketCap > 1e9]

        tickerSymbolValuesOnly = [symbol.Value for symbol in fineUniverse]

        return fineUniverse

    def AfterMarketOpen(self):
        for security in self.ActiveSecurities.Values:
            symbol = security.Symbol
            
            if symbol == self.SPY:
                continue

            historyData = self.History(symbol, 2, Resolution.Daily)

            try:
                openDayAfterEarnings = historyData['open'][-1]
                closeDayAfterEarnings = historyData['close'][-1]
                highDayAfterEarnings = historyData['high'][-1]
                closeDayBeforeEarnings = historyData['close'][-2]

            except:
                self.Debug(f"History data unavailable for {symbol.Value}")
                continue
            
            self.Debug(f"{symbol.Value} in list")

            priceGap = openDayAfterEarnings - closeDayBeforeEarnings
            percentGap = priceGap / closeDayBeforeEarnings
            closeStrength = (closeDayAfterEarnings - openDayAfterEarnings) / (highDayAfterEarnings - openDayAfterEarnings)

            if percentGap > 0.05:
                self.Debug(f"{symbol.Value} gapped up by {percentGap} - {closeDayBeforeEarnings} {openDayAfterEarnings}")

                if closeDayAfterEarnings > closeDayBeforeEarnings and closeStrength > 0.5:
                    self.Debug(f"{symbol.Value} closed strong!")
                else:
                    self.Debug(f"{symbol.Value} faded after earnings")