Hi Community.

QuantConnect's Quant Team has been discussing algorithm design patterns to help members develop their algorithms. We use the SymbolData pattern to hold information (state, indicators, custom methods, etc) from a Symbol/Security in a multi-security algorithm (manual or dynamic universe). It follows the QCAlgorithm.Securities and QCAlgorithm.Portfolio pattern: dictionaries of a user-defined type keyed QuantConnect.Symbol. It's normally used in Alpha Models (e.g. RsiAlphaModel.cs, RsiAlphaModel.py), but it could be used in non-Framework algorithms too.

Create the SymbolData class with the desired variables (state, indicators, etc)
2. The SymbolData class should have at least two methods: Update(Slice), Dispose()
  a. Update will receive a Slice object to update the class variable
  b. Dispose will unsubscribe consolidators
3. The SymbolData class constructor should receive the following arguments: QCAlgorithm, Security
  a. QCAlgorithm to access its methods, e.g. SubscriptionManager, EMA, RSI, etcb. Security to access the Symbol, Mapped (for futures), SymbolProperties, etc...
4. In the main/algorithm class, create a Dictionary of SymbolData keyed by Symbol: SymbolDataBySymbol
  a. Let's key the dict by Symbol, not Security, to follow the QCAlgorithm.Securities and QCAlgorithm.Portfolio pattern.
5. Populate the dictionary in Initialize or OnSecuritiesChanged when a new Security is added to the universe
6. In OnData, iterate the dictionary and pass the Slice object to the SymbolData.Update(Slice) method
7. We may want to keep the trading methods, since we may want to use SetHoldings(List<PortfolioTarget>).
  a. If we have multiple securities:
   - Manage Alpha Signaling/Entry Exit in Symbol Data
  b. If multiple trades per security
   - Manage a Trade Collection in Symbol Data too.

We believe we can make it easier by adding some “sugar” in QuantConnect/Lean. For example, attaching the custom properties to the Security object to eliminate the user-defined class dictionary. We have opened a GitHub issue for the feature: On Security Cache Concept #6809

Please let us know your opinion.

Best regards,
Alex

Author