In python, I'm trying to extend RollingWindow to add convenience methods for logic that i use often.
However, the interpreter isn't picking up methods in my subclass. I've attached a small backtest with a simple example showing the issue. The method isn't found and the following error is thrown (the method name is ‘hasAtLeast')
AttributeError : '0, Culture=neutral,
PublicKeyToken=7cec85d7bea7798' object has no attribute 'hasAtLeast'
at OnData
hasAtLeast = self.smPriceWindow.hasAtLeast(3)
===
at Python.Runtime.PyObject.Invoke(PyTuple args in main.py: line 27
Any thoughts?
Varad Kabade
Hi .ekz.,
Thank you for your patience.
Unfortunately not all C# classes like RollingWindow can be extended in a Python algorithm. An alternative way would be to implement a separate class with a RollingWindow as an attribute and implement the necessary functions similar to:
Best,
Varad Kabade
.ekz.
Thanks Varad Kabade , I ended up doing it that way (creating a wrapper) and shared with the community a few days ago in another thread. Here's the current working version, in case you are interested.
One thing i do have a question on is how to pass on the ‘type’ without using a string. I'd like to instantiate my SmartRollingWindow the exact same way I do a regular rolling window. Currently i instantiate like this:
I would like to instantiate like this instead:
Any thoughts?
Implementation below:
Fred Painchaud
Hi .ekz.,
Willing to help… What you want to do is called a Python Generics: https://docs.python.org/3/library/typing.html#generics
It is relatively advanced Python programming. As you can see in the docs, you will need to put your generic type between the () in class SmartRollingWindow(). Then you can reuse it in the class itself. If you've never done generic types in any programming language, this may be difficult. If you've done it, it's very similar.
Remember that the Python runtime (python.exe on Windows) never do static type checking (in other words, it does not verify, for instance in your case, that you are actually adding floats in your [float] window). So your code will be able to add whatever to the rolling window anyway, except that if that whatever is not properly used when you get an item out of the rolling window (like you call a method that does not exist), you'll get runtime errors.
Hope this makes sense to you. If not, you can contact me on discord (I left you a note).
Fred
.ekz.
Brilliant. Thanks Fred Painchaud.
See you on Discord.
.ekz.
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!