Overall Statistics
from datetime import timedelta
from QuantConnect.Data.UniverseSelection import *
from Selection.FundamentalUniverseSelectionModel import FundamentalUniverseSelectionModel

class NadionUncoupledPrism(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2019, 1, 1)
        self.SetEndDate(2020,1 ,1)
        self.SetCash(100000)
        self.AddUniverseSelection(LiquidValueUniverseSelectionModel())
        self.UniverseSettings.Resolution = Resolution.Daily

class LiquidValueUniverseSelectionModel(FundamentalUniverseSelectionModel):

    def __init__(self):
        super().__init__(True, None, None)
        self.lastMonth = -1

    def SelectCoarse(self,algorithm, coarse):
        
        if self.lastMonth == algorithm.Time.month:
            return Universe.Unchanged
        self.lastMonth = algorithm.Time.month
        
        sortedByDollarVolume = sorted([x for x in coarse if x.HasFundamentalData], \
        key=lambda x: x.DollarVolume, reverse=True)
        return [x.Symbol for x in sortedByDollarVolume[:100]]
        
    def SelectFine(self, algorithm, fine):
        #sortedByYields = sorted(fine, key=lambda f: f.ValuationRatios.EarningYield, reverse=True)
        filteredByYields = [f.Symbol for f in fine if f.ValuationRatios.EarningYield > 7]
        universe = filteredByYields[:10]
        return [f.Symbol for f in universe]
        
    def OnSecuritiesChanged(self, changes):
        
        self.changes = changes
        
        for security in self.changes.RemovedSecurities:
            if security.Invested:
                self.Liquidate(security.Symbol)
                
        for security in self.changes.AddedSecurities:
            if not security.Invested:
                self.SetHoldings(security.Symbol, .10)