I tried to code this to execute the Cornerstone Value Strategy found in What Works on Wall Street. I want to just look at the list of equities picked by this strategy first, but this "Runtime Error: One or more errors occurred. Specified cast is not valid." error keeps appearing so I'm not sure what to do. Would appreciate any help provided. Thanks :)

 

 

 

 

 

from numpy import mean
from Alphas.RsiAlphaModel import RsiAlphaModel
from Execution.ImmediateExecutionModel import ImmediateExecutionModel
from Portfolio.EqualWeightingPortfolioConstructionModel import EqualWeightingPortfolioConstructionModel
from Risk.MaximumDrawdownPercentPerSecurity import MaximumDrawdownPercentPerSecurity
from QuantConnect.Data.UniverseSelection import *
from Selection.FundamentalUniverseSelectionModel import FundamentalUniverseSelectionModel

class CSV(QCAlgorithm):

def Initialize(self):
self.SetStartDate(2021, 2, 10) # Set Start Date
self.SetEndDate(2021, 2, 11)
self.SetCash(100000) # Set Strategy Cash
self.UniverseSettings.Resolution = Resolution.Daily
self.AddUniverseSelection(CSVmodel())
self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())
self.SetExecution(ImmediateExecutionModel())

def ViewUniverse(self):
for universe in self.UniverseManager.Values:
if universe is UserDefinedUniverse:
continue
symbols = universe.Members.Keys
symbol_list = []

for symbol in symbols:
symbol_list.append(symbol)

self.Log(symbol_list)

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)

class CSVmodel(FundamentalUniverseSelectionModel):

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

def SelectCoarse(self, algorithm, coarse):
if self.LastMonth == algorithm.Time.month and self.LastYear == algorithm.Time.year:
return Universe.Unchanged
elif self.LastMonth == -1 or (algorithm.Time.month-self.lastMonth) % 3 == 0:
self.lastMonth = algorithm.Time.month
self.lastYear = algorithm.Time.year
return [x for x in coarse if x.HasFundamentalData]
else:
return Universe.Unchanged

def SelectFine(self, algorithm, fine):
MKTCAP_dict={}
OS_dict = {}
CF_dict = {}
S_dict = {}

#exclude utilities
excluded_utilities = [i for i in fine if i.AssetClassification.MorningstarIndustryGroupCode != MorningstarIndustryGroupCode.Utilities]

#filter by mkt_cap
for i in fine:
if i.EarningReports.BasicAverageShares.ThreeMonths != 0 and i.ValuationRatios.PERatio != 0:
MKTCAP_dict[i]=i.EarningReports.BasicAverageShares.ThreeMonths * i.EarningReports.BasicEPS.TwelveMonths *i.ValuationRatios.PERatio
MKTCAP_avg = numpy.mean(MKTCAP_dict.values())
filteredby_MKTCAP = [i for i in excluded_utilities if MKTCAP_dict.get(i)>MKTCAP_avg]

#filter by outstanding shares
for i in fine:
if i.EarningReports.BasicAverageShares.ThreeMonths != 0:
OS_dict[i]=i.EarningReports.BasicAverageShares.ThreeMonths
OS_avg = numpy.mean(OS_dict.values())
filteredby_MKTCAP_OS = [i for i in filteredby_MKTCAP if OS_dict.get(i)>OS_avg]

#filter by cashflow
#the cashflow used to calculate P/CF ratio will be assumed to refer to operating cashflow
for i in fine:
CF_dict[i]=i.FinancialStatements.CashFlowStatement.OperatingCashFlow.TwelveMonths
CF_avg = numpy.mean(CF_dict.values())
filteredby_MKTCAP_OS_CF = [i for i in filteredby_MKTCAP_OS if CF_dict.get(i)>CF_avg]

#filter by Sales
for i in fine:
if i.EarningReports.DilutedAverageShares.ThreeMonths != 0:
S_dict[i] = i.ValuationRatios.SalesPerShare*i.EarningReports.DilutedAverageShares.ThreeMonths
S_avg = numpy.mean(S_dict.values())
filteredby_MKTCAP_OS_CF_S = [i for i in filteredby_MKTCAP_OS_CF if S_dict.get(i)>1.5*S_avg]

#Total yield = shareholder yield = dividend yield + buyback yield
sortedby_TotalYield = sorted(filteredby_MKTCAP_OS_CF_S,key = lambda i: i.ValuationRatios.TotalYield, reverse = True)

universe = sortedby_TotalYield[:25]
self.debug(str(universe))
return universe
#return [f.Symbol for f in universe]