Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
datetime offset

I'm trying to run an event on a specified date, however, get_datetime always gets triggered 3 minutes early as shown in the logs when you run the attached backtest:

2017-12-12 06:37 handle_data:10 INFO getDateTime: 2017-12-12 09:37:00-05:00 fixed time: 2017-12-12 09:40:00-04:56
2017-12-12 06:37 handle_data:12 INFO get_datetime > 9:40am

What's causing the "-05:00" and "-04:56" at the end of get_datetime and my date? I don't see the offset running python on my computer. I could just add 3 minutes to my date, but I'm not sure how reliable the 3 minute offset is. Thanks.

5 responses

It's a sort of 'bug' in pytz. Notice the weird timezone offset in the time. It should be 5:00 hrs but instead its 4:56. You shouldn't generally pass a timezone object to datetime.datetime. (see https://stackoverflow.com/questions/6410971/python-datetime-object-show-wrong-timezone-offset and also the pytz site http://pytz.sourceforge.net/ )

So instead of

   time1 = dt(2017,12,12,9,40, tzinfo=timezone('US/Eastern'))

It works better to set a timezone object and then use the '.localize' method

    tz = timezone('US/Eastern')  
    time1 = tz.localize(dt(2017,12,12,9,40))

The attached backtest shows the desired behavior (I believe).

Good luck.

Good to know.

Meanwhile here's an optional route ...

def initialize(context):  
    schedule_function(something, date_rules.every_day(), time_rules.market_open(minutes=10))

def something(context, data):  
    if str(get_datetime().date()) in ['2017-12-14', '2017-12-18']:  
        log.info(get_datetime())  

Thanks, Dan and Blue. Works perfectly. I guess I'll need to try more google keyword combination searches next time.

@John. Always glad to help.

It’s good to post issues like this here (so no worries about more googling) Others may benefit. It’s what community is all about.

Good luck.

:)     By the way you might already know, but for those who don't, it's useful in searches on Google and others to limit to just quantopian.com by adding the 'site' keyword like this.

https://www.google.com/search?q="specified+date"+site:quantopian.com

Also when results show, , 'Tools' to then specify dates or sort by date.

A couple of the notable things about Quantopian 's built-in search: Specifying backtest or notebooks and/or tags.