I cloned and am trying to run this example code but it throws an error “name Fundamental not defined”.  Is there a module I need to import?

  1. from AlgorithmImports import *
  2. class MorningStarDataAlgorithm(QCAlgorithm):
  3. def Initialize(self) -> None:
  4. self.SetStartDate(2021, 1, 1)
  5. self.SetEndDate(2021, 7, 1)
  6. self.SetCash(100000)
  7. # Requesting data
  8. self.AddUniverse(self.FundamentalSelectionFunction)
  9. self.num_coarse_symbols = 100
  10. self.num_fine_symbols = 10
  11. self.UniverseSettings.Resolution = Resolution.Daily
  12. def FundamentalSelectionFunction(self, fundamental: List[Fundamental]) -> List[Symbol]:
  13. selected = [f for f in fundamental if f.HasFundamentalData and f.Price > 1]
  14. sorted_by_dollar_volume = sorted(selected, key=lambda f: f.DollarVolume, reverse=True)[:self.num_coarse_symbols]
  15. sorted_by_pe_ratio = sorted(sorted_by_dollar_volume, key=lambda f: f.ValuationRatios.PERatio, reverse=False)[:self.num_fine_symbols]
  16. return [ f.Symbol for f in sorted_by_pe_ratio ]
  17. def OnData(self, slice: Slice) -> None:
  18. # if we have no changes, do nothing
  19. if self._changes is None: return
  20. # liquidate removed securities
  21. for security in self._changes.RemovedSecurities:
  22. if security.Invested:
  23. self.Liquidate(security.Symbol)
  24. # we want 1/N allocation in each security in our universe
  25. for security in self._changes.AddedSecurities:
  26. self.SetHoldings(security.Symbol, 1 / self.num_fine_symbols)
  27. self._changes = None
  28. def OnSecuritiesChanged(self, changes: SecurityChanges) -> None:
  29. self._changes = changes
  30. for security in changes.AddedSecurities:
  31. # Historical data
  32. history = self.History(security.Symbol, 7, Resolution.Daily)
  33. self.Debug(f"We got {len(history)} from our history request for {security.Symbol}")
+ Expand

Author

Levi Freedman

October 2023