Hi guys. I've been trying to implement an algorithm, but I've been getting this run time exception. I'm really new to QC and would really appreciate some help.
The error it was giving me was: Runtime Error: Object reference not set to an instance of an object.
class FrameworkAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2019, 12, 30)
self.SetEndDate(2020, 12, 30)
self.SetCash(10000)
#1. Set the Universe Selection Model
CustomUniverseSelectionModel = LiquidValueUniverseSelectionModel(self)
self.AddUniverseSelection(
FineFundamentalUniverseSelectionModel(CustomUniverseSelectionModel.SelectCoarse, CustomUniverseSelectionModel.SelectFine)
)
self.UniverseSettings.Resolution = Resolution.Hour
self.AddUniverseSelection(NullUniverseSelectionModel())
#2. Set the Alpha Model
self.SetAlpha(LongShortEYAlphaModel())
#3. Set the NullPortfolioConstructionModel()
self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())
#4. Set the NullRiskManagementModel()
self.SetRiskManagement(NullRiskManagementModel())
#5. Set the NullExecutionModel()
self.SetExecution(ImmediateExecutionModel())
class LongShortEYAlphaModel(AlphaModel):
def __init__(self):
self.lastMonth = -1
def Update(self, algorithm, data):
insights = []
if (self.lastMonth == algorithm.Time.month):
return
self.lastMonth = algorithm.Time.month
for security in algorithm.ActiveSecurities.Values:
direction = 1 if security.Fundamentals.ValuationRatios.EarningYield > 0 else -1
insights.append(Insight.Price(security.Symbol, timedelta(28), direction))
return Insight.Group(insights)
class LiquidValueUniverseSelectionModel():
def __init__(self, algorithm):
self.algorithm = algorithm
self.lastMonth = -1
def SelectCoarse(self, coarse):
#1. If it isn't time to update data, return the previous symbols
if self.lastMonth == self.algorithm.Time.month:
return Universe.Unchanged
#2. Update self.lastMonth with current month to make sure only process once per month
self.lastMonth = self.algorithm.Time.month
#3. Sort symbols by dollar volume and if they have fundamental data, in descending order
sortedByDollarVolume = sorted([x for x in coarse if x.HasFundamentalData],
key=lambda x: x.DollarVolume, reverse=True)
#4. Return the top 100 Symbols by Dollar Volume
return [x.Symbol for x in sortedByDollarVolume[:100]]
def SelectFine(self, fine):
sortedByYields = sorted(fine, key=lambda f: f.ValuationRatios.EarningYield, reverse=True)
self.universe = sortedByYields[:10] + sortedByYields[-10:]
return [f.Symbol for f in self.universe]
Varad Kabade
Hi Keshav Varadarajan,The update method in AlphaModel should return a list of insights, but in the above algorithm, it returns null/None.
instead, we should change it to
Best,Varad kabade
Keshav Varadarajan
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!