Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Scheduled functions vs handle_data

Hi - in a given bar where a function is scheduled using schedule_function, does the scheduled function execute before or after handle_data for that same bar? I assume before but just thought I'd check.

2 responses

@Matt C., That's one of those questions where a simple test can confirm (and imprint) one's speculations...

def initialize(context):  
    symbols("SPY")  
    schedule_function(HandleEntry)

def handle_data(context, data):  
    print("handle_data")

def HandleEntry(context, data):  
    print("HandleEntry")  
'''
Daily  
2011-01-04 PRINT handle_data  
2011-01-04 PRINT HandleEntry  
2011-01-05 PRINT handle_data  
2011-01-05 PRINT HandleEntry  
2011-01-06 PRINT handle_data  
2011-01-06 PRINT HandleEntry  
'''
'''
Minutely  
2011-01-04 PRINT handle_data  
2011-01-04 PRINT HandleEntry  
2011-01-04 PRINT handle_data  
2011-01-04 PRINT handle_data  
2011-01-04 PRINT handle_data  
'''

Notice that in minutely, HandleEntry is called only once, at day's open.

Now what TIME was that? Who knows? Maybe the log statements should include TIME when they emit on log.info or print...

Per https://www.quantopian.com/posts/announcement-new-schedule-function-method-allows-you-to-specify-when-a-function-runs,

The functions are called AFTER handle_data in the order they are
scheduled.

This could be verified by using the system clock, which I believe is accessible with a Python function.