Hi there,

I am testing a simple algorithm using algorithm framework. My backtesting is running and gives out a result but comes up with alerts for various stocks:

CPRT R735QTJ8XC9X: The security does not have an accurate price as it has not yet received a bar of data. Before placing a trade (or using SetHoldings) warm up your algorithm with SetWarmup, or use slice.Contains(symbol) to confirm the Slice object has price before using the data. Data does not necessarily all arrive at the same time so your algorithm should confirm the data is ready before using it. In live trading this can mean you do not have an active subscription to the asset class you're trying to trade. If using custom data make sure you've set the 'Value' property.

 

not sure what's wrong since i m using parameters like:

  • if security.IsTradable
  • if Securities[sec].HasData

 

Also, i m not loading any historical data or using indicators. Find code attached, also backtesting

  1. # region imports
  2. from AlgorithmImports import *
  3. from datetime import timedelta, datetime
  4. from QuantConnect.Data.UniverseSelection import *
  5. from Selection.FundamentalUniverseSelectionModel import FundamentalUniverseSelectionModel
  6. from Execution.ImmediateExecutionModel import ImmediateExecutionModel
  7. from Portfolio.EqualWeightingPortfolioConstructionModel import EqualWeightingPortfolioConstructionModel
  8. # endregion
  9. class Third_Attempt(QCAlgorithm):
  10. def Initialize(self):
  11. self.SetStartDate(2021, 1, 1) # Set Start Date
  12. self.SetEndDate(2022, 1, 1) # Set Start Date
  13. self.SetCash(100000) # Set Strategy Cash
  14. self.AddUniverseSelection(Highperformance())
  15. self.UniverseSettings.Resolution = Resolution.Daily
  16. self.AddAlpha(BuyPerformance())
  17. self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel(lambda time: None))
  18. self.SetExecution(ImmediateExecutionModel())
  19. class Highperformance (FundamentalUniverseSelectionModel):
  20. def __init__(self):
  21. super().__init__( True, None)
  22. self.lastMonth = -1
  23. self.spy = Symbol.Create('SPY', SecurityType.Equity, Market.USA)
  24. def SelectCoarse(self, algorithm, coarse):
  25. #run the algorithm once a month, return Universe.Unchanged in case we are looking at exactly the same month
  26. if algorithm.Time.month == self.lastMonth:
  27. return Universe.Unchanged
  28. self.lastMonth = algorithm.Time.month
  29. sortedByVolume = sorted(coarse, key=lambda x: x.DollarVolume, reverse=True)
  30. filteredByFundamentals = [x.Symbol for x in sortedByVolume if x.HasFundamentalData]
  31. return filteredByFundamentals
  32. def SelectFine(self, algorithm, fine):
  33. sorted_high = sorted([x for x in fine if x.MarketCap > 2e9
  34. and 0.5 > x.OperationRatios.AVG5YrsROIC.FiveYears > 0.20
  35. and 50 > x.ValuationRatios.PERatio > 20
  36. and x.AssetClassification.MorningstarSectorCode != MorningstarSectorCode.FinancialServices
  37. and x.AssetClassification.MorningstarSectorCode != MorningstarSectorCode.Healthcare],
  38. key = lambda x: x.ValuationRatios.PERatio, reverse=True)
  39. fundamental_universe = [x.Symbol for x in sorted_high[:5]] + [self.spy]
  40. algorithm.Debug('Universe Selection:')
  41. algorithm.Debug(str(algorithm.Time))
  42. algorithm.Debug('/n ')
  43. for security in fundamental_universe:
  44. algorithm.Debug(str(security))
  45. #return [x.Symbol for x in sorted_high[:5]] + [self.spy]
  46. return [x.Symbol for x in sorted_high[:5]]
  47. class BuyPerformance(AlphaModel):
  48. def __init__(self):
  49. self.lastMonth = -1
  50. self.newAdds = []
  51. #removals to be removed
  52. self.newRemovals = []
  53. def Update(self, algorithm, data):
  54. insights = []
  55. for added in self.newAdds:
  56. if not algorithm.Securities[added].Invested and algorithm.Securities[added].HasData:
  57. insights.append(Insight(added, timedelta(30), InsightType.Price, InsightDirection.Up))
  58. # to be removed at a later stage
  59. for removed in self.newRemovals:
  60. if removed not in data.Bars:
  61. continue
  62. insights.append(Insight(removed, timedelta(30), InsightType.Price, InsightDirection.Flat))
  63. return insights
  64. def OnSecuritiesChanged(self, algorithm, changes):
  65. #When assets are added to the universe, they will trigger OnSecuritiesChanged() event.
  66. #From there, you can initialize any state or history required for the Alpha Model
  67. for security in changes.AddedSecurities:
  68. #if security.Symbol != self.spy and security.Symbol not in self.newAdds:
  69. if security.Symbol not in self.newAdds and security.IsTradable:
  70. self.newAdds.append(security.Symbol)
  71. for security in changes.RemovedSecurities:
  72. #if security.Symbol != self.spy:
  73. if security.IsTradable:
  74. self.newRemovals.append(security.Symbol)
+ Expand

Author

Jose David Delgado Mosquera

May 2022