Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Why is handle_data called only after 16:30 ?

Hi guys,

Sorry if this is a trivial question, i didn't find anything related to it in the API docs or the forum.

I am running the most simple backtest to see when the handle_data() is being called, but it seems to skip most of the day and calls it every minute ONLY AFTER 16:30, as opposed to literally every 1m as i would expect based on the API docs. (and the last min that is being called on each day is at 23:00).
Can you confirm why that is? Is it by design like that? Am i missing something?
Also can you confirm what is the timezone reference of the data (UTC?) ? Is it uniform across all assets in Quantopian?

The only thing i can think of is being a timezone offset, but NYSE opens at 09:30 ET and it only has 4 hours difference with GMT/UTC time.

Thanks
Photis

17 responses

I think the timezone is whatever your local browser is. Perhaps you are in Moscow?

Hi Simon

Yeah that seems to make sense (currently based in Athens GMT+3).

However does that mean that if you want to schedule in your strategy the execution for a specific time, the time has to be in relation to the local browser time as opposed to the time of the Exchange the asset trades on?
And more importantly if the same code, with scheduled functions to run at specific times, is run by 2 different people that have a different local browser time will it yield a different backtest?

Thanks

Those times are exchange-local times.

Thanks Simon

Does that mean that the backtesting engine is resolving the time-inconsistency internally?
Because if you want to backtest a strategy where the signal is being produced for example at 10am exchange-time, but the timestamp of the data used in the backtest is based on your browser time then you are obviously not backtesting the right thing, unless the data is actually in the local exchange-time but only displays in the logs with a timezone offset.

Yeah, I'm a little confused by your question, but basically, the logs are being printed by your browser. The backtest itself is running on Quantopian's back end, and those times are all EST.

Just wanted to be absolutely sure as i am not very familiar yet with the Quantopian interface.

Having said that it doesn't look like the data is timestamped in EST but rather in UTC format. (see screenshot)

Hence the question remains: If you want to backtest a strategy that is scheduled to perform operations at 10am NY time, do you put in your code 10:00 or do you put 10+4 = 14:00 UTC when on Daylight saving (Summer time) & 10+5=15:00 UTC during Winter?

Here are the results I get when I run your code:

2016-10-07 09:31  PRINT Equity(24 [AAPL])     222496.5  
Equity(8554 [SPY])    469472.8  
dtype: float64  
2016-10-07 09:32  PRINT Equity(24 [AAPL])     236477.0  
Equity(8554 [SPY])    567329.4  
dtype: float64  
2016-10-07 09:33  PRINT Equity(24 [AAPL])     244683.3  
Equity(8554 [SPY])    638179.6  
dtype: float64  
2016-10-07 09:34  PRINT Equity(24 [AAPL])     248601.4  
Equity(8554 [SPY])    630507.5  
dtype: float64  
2016-10-07 09:35  PRINT Equity(24 [AAPL])     260805.9  
Equity(8554 [SPY])    624699.4  
  1. that is 10. For some reason it's reformatting "10." as "1.". It might be quicker if you just test these things.

Hi Ted,

Can you rerun the attached and see what timestamp you get for the SPY data in relation to the time of your browser?

I am in ET. Here are the results:

2016-10-07 09:31  PRINT                            Equity(24 [AAPL])  Equity(8554 [SPY])  
2016-10-07 13:31:00+00:00           959229.0            934435.0  
2016-10-07 09:32  PRINT                            Equity(24 [AAPL])  Equity(8554 [SPY])  
2016-10-07 13:32:00+00:00           217153.0           1147551.0  
2016-10-07 09:33  PRINT                            Equity(24 [AAPL])  Equity(8554 [SPY])  
2016-10-07 13:33:00+00:00           159739.0            861136.0  
2016-10-07 09:34  PRINT                            Equity(24 [AAPL])  Equity(8554 [SPY])

... skip most of the day, until the close

2016-10-07 15:57  PRINT                            Equity(24 [AAPL])  Equity(8554 [SPY])  
2016-10-07 19:57:00+00:00            89051.0            371664.0  
2016-10-07 15:58  PRINT                            Equity(24 [AAPL])  Equity(8554 [SPY])  
2016-10-07 19:58:00+00:00           126454.0            613276.0  
2016-10-07 15:59  PRINT                            Equity(24 [AAPL])  Equity(8554 [SPY])  
2016-10-07 19:59:00+00:00            92842.0            502498.0  
2016-10-07 16:00  PRINT                            Equity(24 [AAPL])  Equity(8554 [SPY])  
2016-10-07 20:00:00+00:00           499050.0           2508848.0  
2016-10-10 09:31  PRINT                            Equity(24 [AAPL])  Equity(8554 [SPY])  
2016-10-10 13:31:00+00:00          1206017.0            949635.0  
2016-10-10 09:32  PRINT                            Equity(24 [AAPL])  Equity(8554 [SPY])  
2016-10-10 13:32:00+00:00           376144.0            577482.0  
2016-10-10 09:33  PRINT                            Equity(24 [AAPL])  Equity(8554 [SPY])  
2016-10-10 13:33:00+00:00           282627.0            280505.0  
2016-10-10 09:34  PRINT                            Equity(24 [AAPL])  Equity(8554 [SPY])  
2016-10-10 13:34:00+00:00           247877.0            418072.0  
2016-10-10 09:35  PRINT                            Equity(24 [AAPL])  Equity(8554 [SPY])

etc ...  

Got it, thanks a lot Ted. Just wanted to verify that the data is indeed in UTC

Best

Thanks Simon, appreciate your responses

Photi,

Hence the question remains: If you want to backtest a strategy that is scheduled to perform operations at 10am NY time, do you put in your code 10:00 or do you put 10+4 = 14:00 UTC when on Daylight saving (Summer time) & 10+5=15:00 UTC during Winter?

A good way to make sure you are working with proper market timing relative to your time zone is to make use of the datetime library. Copy and paste the code sample below into an algo, run it, and view the print logs. You will be able to tell how market time relates to the time in your browser.

import datetime

def initialize(context):  
    context.security = symbol('AAPL')  
def handle_data(context, data):  
    price = data.current(context.security, 'price')  
    Market_Time = get_datetime('US/Eastern')  
    print Market_Time  
    print price

Now if you want to execute some code at 10:00 AM EST, you can do something like this:

Market_Time = get_datetime('US/Eastern')  
Market_Hour = Market_Time.hour  
Market_Minute = Market_Time.minute

if Market_Hour == 10 and Market_Minute == 0:  
        # Conduct logic for 10AM EST

Hi Frank

Appreciate this.
Thanks

No prob Photi. Happy trading.

You're grappling with some of the challenges we do while building the platform. We work very hard to make the API easy and intuitive, but sometimes it isn't easily resolved.

One of of the nice things about Quantopian's scheduled functions is that they describe events in relation to "market open" and "market close". You don't need to know the time zone, or daylight savings, or hours-offset-from-your-current-local-timezone. You just need to say, "have this happen 10 minutes after market open" or "have this happen 15 minutes before close". It just doesn't matter what the timezone is when you use the API framework.

Disclaimer

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 Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian 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.

@Dann
Yeah it became obvious how people here deal with these issues after Frank's comments.

Many thx