Hello,

I am new to QuantConnect, I am trying to migrate a Quantopian Project. However, I am having difficulty accessing data object from other classes.

I am using the Algorithm Framework. I have a complicated indicator(s) which is in a class that does not extend any other class, let’s called it Class X. The reason for this, is that I want to save a lot of class variables per security and keep them updated. 

So, I create an instance of this Class X per security so the AlphaModel could check the logic by accessing the class variables for each security, and then decide to return insights or not.

The Problem: I can not pass the data Object appropriately, so I can access it in Class X

Please see attached

Any hint is highly appreciated

  1. # region imports
  2. from AlgorithmImports import *
  3. # endregion
  4. class SwimmingBlueTermite(QCAlgorithm):
  5. def Initialize(self):
  6. self.SetStartDate(2020, 1, 1) # Set Start Date
  7. self.SetEndDate(2022,1,1)
  8. self.SetCash(100000) # Set Strategy Cash
  9. self.num_coarse = 500
  10. self.__numberOfSymbolsFine = 4
  11. self.UniverseSettings.Resolution = Resolution.Daily
  12. self.AddUniverse(self.CoarseSelectionFunction, self.FineSelectionFunction)
  13. self.AddAlpha(TestAlphaModel())
  14. # Portfolio construction model
  15. self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())
  16. # Risk model
  17. self.SetRiskManagement(NullRiskManagementModel())
  18. # Execution model
  19. self.SetExecution(ImmediateExecutionModel())
  20. # set a warm-up period to initialize the indicator
  21. self.SetWarmUp(timedelta(60))
  22. # Take 500 stocks worth more than $10, with more than $10M daily
  23. def CoarseSelectionFunction(self, coarse):
  24. sortedByDollarVolume = sorted(coarse, key=lambda x: x.DollarVolume, reverse=True)
  25. filtered = [ x.Symbol for x in sortedByDollarVolume
  26. if x.Price > 10 and x.DollarVolume > 10000000 ]
  27. return filtered[:500]
  28. # sort the data by P/E ratio and take the top 'NumberOfSymbolsFine'
  29. def FineSelectionFunction(self, fine):
  30. # sort descending by P/E ratio
  31. sortedByPeRatio = sorted(fine, key=lambda x: x.ValuationRatios.PERatio, reverse=True)
  32. # take the top entries from our sorted collection
  33. return [ x.Symbol for x in sortedByPeRatio[:self.__numberOfSymbolsFine] ]
  34. def OnSecuritiesChanges(self, changes):
  35. # Remove security
  36. for security in changes.RemovedSecurities:
  37. self.ActiveSecurities.remove(security)
  38. # Add security
  39. for security in changes.AddedSecurities:
  40. if security not in self.ActiveSecurities:
  41. self.ActiveSecurities.append(security)
  42. class TestAlphaModel(AlphaModel):
  43. def __init__(self):
  44. self.InstancePerSecurity = []
  45. self.ListComprehensionCount = 0
  46. def Update(self, algorithm, data):
  47. if (self.ListComprehensionCount == 0):
  48. self.InstancePerSecurity = [ClassX(security, data) for security in algorithm.ActiveSecurities.Keys]
  49. self.ListComprehensionCount += 1
  50. insights = []
  51. for instance in self.InstancePerSecurity:
  52. instance.Run()
  53. class ClassX():
  54. def __init__(self, security, data):
  55. self.security = security
  56. self.data = data
  57. def Run(self):
  58. # Returns Error "Runtime Error: type(s) expected"
  59. if self.data.Bars.ContainsKey[self.security]:
  60. pass
+ Expand

 

 

Author

Adham Al-Harazi

June 2022