Back

Help with code: Datetime? (PY Python)

Hi there, I am trying to figure out how I can print (log) the current date-time when running the code. 

In Quantopian, I used 

now = get_datetime()
print now

I've tried many variations in QuantConnect, such as

self.Time
self.DateTime
self.GetDatetime
#etc

but can't get anything to work.

 

Thank you,

Update Backtest








For logging, please use Log or Debug methods:

self.Debug(str(self.Time)) # prints in Console and log file
self.Log(str(self.Time)) # prints in log file only

 

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.


Thank you Alexandre, 

I think you answered my question, because it looks like my code was wrong elsewhere. so self.Time is the correct code, but I was trying to .Log it without wrapping it with string-str()...

It appears that many of the issues I have faced using QuantConnect are a result of not wrapping a variable when trying to log it.

0

Time is an attribute of QCAlgorithm, it represents the current date-time of the exchange.

The algorithms we create in python inherit attributes from QCAlgorithm which is a C# class. All of its methods are C# and, consequently, we need to pass the exact type. Since Log only accepts string, we need to convert it explicitly, because there is no explicit conversion between, for instance, datetime.datetime (the type of Time) and string. However, you don't need to wrap other variables with str() when there is implicit conversion (float, decimal, etc).

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.


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