Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
before_trading_start - does it time-out?

Quantopian folks,

Does the function before_trading_start have a time-out? If so, how long? And if the time-out is reached, is there any way to exit the function gracefully? Or will the algo crash?

If there is no time-out, what happens in live trading when the market opens and data starts rolling in? Does the first call to handle_data get delayed until before_trading_starts completes (i.e. minute bars are skipped)?

Grant

9 responses

Belated response here: yes, there is a time out. It is 60 seconds. The algo would crash.

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.

Thanks Josh,

The help page says:

Called daily prior to the open of market.

Is this meant to imply that before_trading_start will run just prior to the open? For example, would it be called at 9:29 am, and run until exactly 9:30 am?

Also, how do you trigger the call? Is it synchronized with your Nanex Nxcore feed? Or is it based on the scheduled market open and a wall/system clock?

Grant

In backtesting, this isn't a timed event per se. It isn't simulated at a specific time currently.

In the work we're doing to bring this to live trading, we have this scheduled to occur at 8:45.

And just to emphasize, fundamentals does not yet work with live trading (for those reading along). We are actively working on adding this. We'll first roll it out to paper trading via Quantopian. Then we'll add live trading with fundamentals through IB.

Thanks Josh,

So, I gather that in backtesting, before_trading_start will run for up to 60 seconds just prior to the first call to handle_data for the day, correct?

Also, why not allow a longer time-out period (say, 6 hours or more), in case someone wants to really churn over data on a daily basis? Why 60 seconds? I can't say I have a specific use case, but aren't your servers just twiddling their thumbs overnight?

Grant

Grant! they don't twiddle their thumbs: they work for me! As I'm in Australia I have lovely fast backtests when you guys are sleeping ;)

@Peter, Ha.

@Grant, I should have been more specific. 8:45 AM. I suspect 60 seconds was an initial choice since initialize() has the same timeout. There's a case to be made that the processing time here in before_trading_start() could be expanded significantly to cover quant use cases where they do a lot of heavy-weight, pre-market processing and analysis. This is something we've discussed internally. But the initial use case we focused upon for before_trading_start() is to handle screening of companies for inclusion in the update_universe() call. 60 seconds seems sufficient.

Hi Josh,

Unless something has changed, there is no 8:45 am in backtesting, right? Or is there a 'clock' now, running along with the data?

I'll have to play around with before_trading_start when I get the chance, because something is not making sense. If the backtester is 'event-driven' then what event triggers before_trading_start?

Grant

Hi Josh,

Unless something has changed, there is no 8:45 am in backtesting, right? Or is there a 'clock' now, running along with the data?

I'll have to play around with before_trading_start when I get the chance, because something is not making sense. If the backtester is 'event-driven' then what event triggers before_trading_start?

[Edit] Sorry, I just realized that for 8:45 am, you were referring to live trading. I gather that when backtesting, before_trading_starts runs after the last call of handle_data of the day (and after any scheduled functions for that day).

Grant

And what about "after_trading_end()", which could be used to email MOC positions to the account owner, or used to setup EOD style metrics for use in posting to a CSV file for Fetcher use on the next day's start. And what's with 08:45? Odd time that. Why not:

after_trading_end() -- 16:05:00 EST
before_trading_start() -- 9:25:00 EST

which seems more rational.

And if a long duration processing step is needed: "on_midnight_roll()" could be added.