I think my FineSelectionFunction is pretty simple. First, I filter for securities with more than zero shares and then I filter for net net stocks. For some reason, I get the error:
Runtime Error: Algorithm took longer than 10 minutes on a single time loop. CurrentTimeStepElapsed: 10.0 minutes
Why is this happening? My code is below.
from Execution.ImmediateExecutionModel import ImmediateExecutionModel
from Portfolio.EqualWeightingPortfolioConstructionModel import EqualWeightingPortfolioConstructionModel
class NetNet(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 1, 1) # Set Start Date
self.SetEndDate(2020, 1, 31)
self.SetCash(100000) # Set Strategy Cash
self.SetAlpha(NetNetAlpha())
self.SetExecution(ImmediateExecutionModel())
self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel(lambda time: None))
self.Settings.RebalancePortfolioOnInsightChanges = False
self.Settings.RebalancePortfolioOnSecurityChanges = True
self.SetUniverseSelection(FineFundamentalUniverseSelectionModel(self.CoarseSelectionFunction, self.FineSelectionFunction, None, None))
self.UniverseSettings.Resolution = Resolution.Daily
self.SetSecurityInitializer(lambda x: x.SetDataNormalizationMode(DataNormalizationMode.Raw))
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)
# on 15 Jan, filter for securities with fundamental data
def CoarseSelectionFunction(self, coarse):
if self.Time.month == 1 and self.Time.day == 15:
filtered = [ x.Symbol for x in coarse if x.HasFundamentalData ]
return filtered
else:
return Universe.Unchanged
# on 15 Jan, filter first for securities with shares and then filter a second time for net net stocks
def FineSelectionFunction(self, fine):
# filtered = [ x.Symbol for x in fine if (x.FinancialStatements.BalanceSheet.CurrentAssets.ThreeMonths - x.FinancialStatements.BalanceSheet.TotalLiabilitiesAsReported.ThreeMonths) > 0 ]
filtered = [ x.Symbol for x in fine if x.FinancialStatements.BalanceSheet.OrdinarySharesNumber.ThreeMonths > 0 ]
filtered2 = [ x.Symbol for x in filtered if (x.Price / ((x.FinancialStatements.BalanceSheet.CurrentAssets.ThreeMonths - x.FinancialStatements.BalanceSheet.TotalLiabilitiesAsReported.ThreeMonths) / x.FinancialStatements.BalanceSheet.OrdinarySharesNumber.ThreeMonths)) <= 0.66 ]
return filtered2
class NetNetAlpha(AlphaModel):
def __init__(self):
pass
# self.lastMonth = -1
def OnSecuritiesChanged(self, algorithm, changes):
pass
def Update(self, algorithm, data):
insights = []
if algorithm.Time.month == 1 and algorithm.Time.day == 15:
for security in algorithm.ActiveSecurities.Values:
# price = security.Price
# currentAssets = security.Fundamentals.FinancialStatements.BalanceSheet.CurrentAssets.ThreeMonths
# totalLiabilities = security.Fundamentals.FinancialStatements.BalanceSheet.TotalLiabilitiesAsReported.ThreeMonths
# shares = security.Fundamentals.FinancialStatements.BalanceSheet.OrdinarySharesNumber.ThreeMonths
# if ( price / ( (currentAssets - totalLiabilities) / shares ) <= 0.66 ):
# insights.append(Insight.Price(security.Symbol, timedelta(days = 366), InsightDirection.Up))
insights.append(Insight.Price(security.Symbol, timedelta(days=366), InsightDirection.Up))
return insights
else:
return insights
Arthur Asenheimer
Hi Victor,
I wasn't able to reproduce the issue. Instead, I got other error messages like "Symbol object has no attribute Price." and zero-division errors.
In my observation, backtesting speed can vary widely. So it could help to reduce the number of symbols in coarse selection. You requested fundamental data for all companies (~5,000).
I've added "x.Price > 1 and x.DollarVolume > 1e5" to filter out penny stocks and illiquid equities.
Besides that, I've fixed two small errors regarding "Symbol object has no attribute Price" (--> use fine objects instead) and "zero division error" (--> check x.FinancialStatements.BalanceSheet.CurrentAssets.ThreeMonths - x.FinancialStatements.BalanceSheet.TotalLiabilitiesAsReported.ThreeMonths) > 0 ).
Ryan Riordon
Hi Victor,
I was getting the same error as you when running my backtests last week: Runtime Error: Algorithm took longer than 10 minutes on a single time loop. CurrentTimeStepElapsed: 10.0 minutes
I closed all my internet browser windows and opened up my algo on a fresh page and that seems to have fixed the problem.
Victor Lin
Thank you so much for the help, guys. I think Arthur is right about using fine objects. Before, I was filling up all of my lists with x.Symbol, but I should have been filling my lists with x until I got to the last step, and only then should I have used x.Symbol.
Victor Lin
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!