I’d like to access history of symbols in my Alpha Model.
I know I can call algorithm.history() in my Alpha Model …
But I realized already call algorithm.history() in my Universe Selection
historySlices = algorithm.History(initialFilter, MAX_BARS_BACK, Resolution.Daily)
So I decided to create a dict property of the QCAlgorithm called histories that I can use to save the gotten history, so I don't have to call algorithm.history() again.
class RedGhost(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2021, 1, 12)
self.SetEndDate(2021, 1, 15)
self.SetCash(100000)
self.AddUniverseSelection(UniverseModule())
self.UniverseSettings.Resolution = Resolution.Daily
self.SetAlpha(RGAlphaModel())
self.histories = {}
I try to save the history of each into this dictionary in my Universe Selection Model like …
algorithm.histories[symbol] = history
But I get the error
Runtime Error: AttributeError : 'QCAlgorithm' object has no attribute 'histories'
I’ve clearly attached a dictionary called histories to the self of QCAlgorithm
I don’t what I am doing wrong here. Any ideas welcome.
Red Ghost
I also tried declaring the dictionary before the calling of the Universe. But same error as above.
Alexandre Catarino
Hi Red Ghost ,
QuantConnect/Lean is a C# engine that supports algorithms written in Python thanks to pythonnet. It entails some limitations. For example, the QCAlgorithm parameter in AlphaModel.Update is not exactly the same object that we have defined on main as it doesn't include the user-defined attributes such as self.histories. In other words, we can only count with attributes of the base class (QCAlgorithm).
We have two alternatives:
1. Use a Global variable;
2. Pass the reference to the object with the constructor:
Let me note that both alternatives violate the separation of concerns principle.
Red Ghost
Hi Alexandre Catarino ,
Thanks for your answer; this demystifies the error.
I just have a few follow-ups to your answer, if you don't mind.
1. Is there a document I can read about the limitations? Because if there are some dealbreakers, I can simply write my algorithms in C#; I just prefer Python.
2. Does what I'm doing (saving the histories) make a significant difference, or I might as well call algorithm.History() in my Alpha? It's getting the history about 100 bars back.
Red Ghost
Anyway I created a module SharedState.py that holds shared state. It's namespaced, so it achieves the same thing I wanted anyway; it's just attached to SharedState instead of QCAlgorithm.
Varad Kabade
Hi Red Ghost,
Is there a document I can read about the limitations? Because if there are some dealbreakers, I can simply write my algorithms in C#; I just prefer Python.
Currently, there is no document on the limitation of Python on QC. There is as such no major limitation except mentioned by Alex in the same thread, and C# provides faster execution of the strategy. We will add the limitation section in the new docs.
Does what I'm doing (saving the histories) make a significant difference, or I might as well call algorithm.History() in my Alpha? It's getting the history about 100 bars back.
If we have a small to medium Universe, saving histories is always better than calling the history method multiple times. Multiple History calls can reduce the speed of the algorithm considerably.
Best,
Varad Kabade
Red Ghost
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!