Hi,
I'd like to structure my algo into two or more files for better clarity and organization.
Individual function calls to the second file work as expected, however I cannot access QCAlgorithm methods such as self.Log because I get a “self not defined” error message if I remove the comment from the line #self.Log("This is tlt") in the second file called tlt.py (see attached backtest).
I guess I need to create a QCAlgorithm class out of my new functions f and g but how to do it escapes me. Any help will be much appreciated.
Cheers,
Andres
Mia Alissi
Based on the information provided, it seems like you're trying to access the
Log
method of theQCAlgorithm
class from another class. TheLog
method is an instance method of theQCAlgorithm
class, so it needs to be called on an instance of the class.In your
B
class, you're trying to callUglyBluePig.Log("This is a test")
, butUglyBluePig
is a class, not an instance of the class. You need to have an instance of theUglyBluePig
class (which is a subclass ofQCAlgorithm
) to call theLog
method.One way to do this is to pass an instance of the
UglyBluePig
class to theB
class when you create an instance ofB
. Here's an example of how you might do this:Then, when you create an instance of
B
, you would pass in an instance ofUglyBluePig
:This way, the
B
class has a reference to an instance of theUglyBluePig
class, and it can call theLog
method on that instance.Please note that this is a simplified example and might not work directly in your algorithm depending on your setup. You might need to adjust it to fit your specific needs.
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.
Andres Arizpe
Hello Mia,
Thank you very much for your quick answer with such a detailed explanation about classes and instances. Your help is greatly appreciated and I will definitely study more on both.
I followed your indications and the error disappeared. However neither self.algo.Log nor self.algo.Debug functions generate any text when called from OnData which makes me believe it is not possible to use another instance of the UglyBluePig class (which is a subclass of QCAlgorithm). I am in the attached backtest that b.call_a method is being executed because of the “2” being returned (see attached backtest).
What I'm ultimately trying to do is combine two algos with quite lengthy OnData methods into one single algorithm in the most structured and object oriented way. Maybe you can suggest the best way to do this since they both need access to al the QCAlgorithm class, OnData method and the same Portfolio object.
Ashutosh
Hi Andres Arizpe
You need to make a small change to get the functionality you are looking for.
Instead of creating a new object for the “UglyBluePig” class, you can pass “self” as an attribute to the object of class “B” to get access to Log and Debug functionalities.
Changed code:
Output:
Backtest Attached.
Let me know if this is helpful!
Best,
Ashutosh
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.
Andres Arizpe
Hi Ashutosh,
Your answer was spot on. I am now able to access not only QCAlgorithm methods from another file but all objects by just placing “self.algo” instead of “self”.
Even though this might not be the best object oriented programming, I believe this is a very useful template for combining two or more algos in order to increase profits or reduce risk and hopefully someone else wll benefit.
I've attached a simple example using the Supertrend (STR) indicator which has a decent predictive power but when combining multiple low correlation assets may create consistent positive alpha.
Cheers,
Andres
Andres Arizpe
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!