Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Date TIME - Unable to subtract

I am doing this :

a = datetime().date()

after 10 days I am checking

if (a- datetime().date()) > 12:

But it is throwing up runtime error telling me that "Cannot compare datetime.delta with int"...

Any one have any idea how to work around this??

1 response

For backtesting, instead of using date(), best to use the current bar Timestamp with get_datetime('US/Eastern') or to increment a variable each day before opening bell.

This example code from Python Standard Library Tour...

from datetime import date
now = date.today()
now
datetime.date(2003, 12, 2)
now.strftime("%m-%d-%y. %d %b %Y is a %A on the %d day of %B.")
'12-02-03. 02 Dec 2003 is a Tuesday on the 02 day of December.'

birthday = date(1964, 7, 31)
age = now - birthday
age.days
14368

There is also an example of incrementing a daily count variable in example code "Algorithm Framework For Robinhood Users" at end of the "Sample Algorithms" section in the Quantopian Help documentation.