Back

How to use Log/Debug/Error/Quit etc .. in any Python class?

Hi guys,

As the title suggests, in a Python class that does not inherit from QCAlgorithm base class, how should I do debugging?

Thanks!

Update Backtest







0

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.


I understand that the Debug and Log methods are defined in QCAlgorithm class, therefore I tried the following:

class somerandomclass:
    def __init__(self):
        self.qca = QCAlgorithm()
        self.Log("testing")

which does not work. Then I tried making 'somerandomclass' a inheried class:

class somerandomclass(QCAlgorithm):
    def __init__(self):
        self.Log("testing")

that does not work either (I tried to create a custom indicator and register it by following the tutorial). It produced error:

SystemError : <bound method 'RegisterIndicator'> returned a result with an error set

Any advice would be appreciated!

0

Hi Thomas,

You could use the QC method in other classes like this 

class A(object):
def __init__(self, algo):
self.algo = algo

def log_time(self):
self.algo.Log(str(self.algo.Time))

When you call the method in the class

A(self).log_time()
0

Hi  Jing Wu ,

Thanks for the reply. 

In your example, when I construct the object of class A in a top level file (i.e. main.py), what should I substitute the 'algo'  in the constructor with? Should I do the following?

obj_A = A(QCAlgorithm)

Thanks for the help!

0

You need to pass the "self" in basic QCAlgorithm() class to create the instance of the class. Please see this example

0


Jing Wu , I see. Thanks a lot!

0

Hi All,

I was trying to find some answers on how to do something like self.Log or self.Debug in AlphaModel classes.

This relatively old post is the closest thing I could find but still I could not get my problem solved.

Anybody had similar problem with solution?

Thanks in advance

0

Hey - most functions (Update() and OnSecuritiesChanged()) in the AlphaModel class have the containing algorithm passed in via the algorithm parameter. Just use algorithm.Log or algorithm.Debug to do the debugging.

0

Ohhh my Gosh, that's so simple and working.

Thanks so much

 

0

Update Backtest





0

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.


Loading...

This discussion is closed