Overall Statistics
class UncoupledResistanceEngine(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2018, 12, 1)  # Set Start Date
        self.SetEndDate(2018, 12, 20)
        self.SetCash(100000)  # Set Strategy Cash
        # self.AddEquity("SPY", Resolution.Minute)
        self.UniverseSettings.Resolution = Resolution.Daily
        self.AddUniverseSelection(FineFundamentalUniverseSelectionModel(self.CoarseSelectionFunction, self.FineSelectionFunction, None, None))
        self.update = False # flag
        self.rw = {} # stores a dictionary mapping from symbol to its past 8 years EPS record
        
        # warm up for 2 years
        self.SetWarmUp(timedelta(days=30))
        self.Log(self.Time)


    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
        '''

        # if not self.Portfolio.Invested:
        #    self.SetHoldings("SPY", 1)

    # sort the data by daily dollar volume and take the top 'NumberOfSymbols'
    def CoarseSelectionFunction(self, coarse):
        # enable update, wait for the start of next quarter
        #if not self.Time.month%3 == 0:
        #    self.update = True
            
        #if not (self.update and self.Time.month%3==0): return []
    
        
        # pass all tickers with fundamental data to fine selection
        self.filtered_coarse = [x.Symbol for x in coarse if (x.HasFundamentalData)]
        self.Log(self.Time.strftime("%Y-%m-%d %H:%M:%S") + " COARSE")
        return self.filtered_coarse
    
    # return symbols that has had positive EPS for previous 8 quarters
    def FineSelectionFunction(self, fine):
        #if not (self.update and self.Time.month%3==0): return []
        
        selected = []
        for f in fine:
            if not f.Symbol in self.rw.keys():
                self.rw[f.Symbol] = RollingWindow[int](8)
            if f.EarningReports.BasicEPS.ThreeMonths > 0:
                self.rw[f.Symbol].Add(1)
            else:
                self.rw[f.Symbol].Add(0)
            if self.rw[f.Symbol].IsReady and sum(self.rw[f.Symbol]) == 8:
                selected.append(f.Symbol)
        self.Log([x.Value for x in selected])
        
        # disable update until next month
        self.update = False
        self.Log(self.Time.strftime("%Y-%m-%d %H:%M:%S") + " FINE")
        return selected