I'm sorry, it seems I wasn't able to attach the code, must be because the backtest resulted in an error so i can't attach the code, but this is it:
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
self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())
self.SetExecution(ImmediateExecutionModel())
class LiquidValueUniverseSelectionModel(FundamentalUniverseSelectionModel):
def __init__(self):
super().__init__(True, None, None)
def SelectCoarse(self,algorithm, coarse):
sortedByDollarVolume = sorted([x for x in coarse if x.HasFundamentalData], \
key=lambda x: x.DollarVolume, reverse=True)
def SelectFine(self, algorithm, fine):
sortedByYields = sorted(fine, key=lambda f: f.ValuationRatios.EarningYield, reverse=True)
self.universe = sortedByYields[:10]
return [f.Symbol for f in self.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)