Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
is there a way to schedule "handle_data" to start 15 minutes after the open?

I have a trading algo and data all under "handle_data". I don't want it to start trading until 15 minutes after the open. I tried doing a: "schedule handle_data" but that doesn't work. Anyone know what the best way to do this is? Thanks!!

6 responses

You can't really 'schedule' the handle_data method (it always runs every minute of trading) but you can do a simple time check to get the time. Something like this should work for you.

def handle_data(context,data):  
    """  
    Called every minute.  
    """  
    current_time = get_datetime('US/Eastern').time()  
    start_time = datetime.time(hour=9, minute=45)  
    if current_time < start_time:  
        return

    # put any code below and it will only execute AFTER the start time

Depending on what you're trying to accomplish, you can also put your logic from handle_data into another function and run that function using scheduled_function()

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.

Josh's suggestion is a good one. More efficient and also potentially more flexible. Maybe something like this.

def initialize(context):  
    """  
    Called once at the start of the algorithm.  
    """  
    # Set the start time and stop time ( both in minutes)  
    # There are typically 390 trading minutes in a day.  
    # Also set the frequency in minutes (in this case every 5 minutes)  
    # Frequency can be set to 1 to mimic the behaviour of handle_data  
    start_time, stop_time, frequency = 15, 390, 5  
    for minutes_offset in range(start_time, stop_time, frequency):  
        schedule_function(  
            my_function,  
            date_rules.every_day(),  
            time_rules.market_open(minutes=minutes_offset),  
            half_days = True)

def my_function(context,data):  
    """  
    Do any logic here. Will be executed each time it's scheduled  
    """  
    log.info(get_datetime('US/Eastern').time())  


this is very helpful - i'm going to give these a go.

i tried to scheduled_function() but i am comparing current minute data to the previous 10 minutes and couldn't find a way to run my function every minuted with scheduled_function().

Thanks.

A condensed version of Dan's in case it might help with the particular way of thinking/learning by some. Since half_days True by default, isn't needed. Here, the 5 is changed to 1, to run every 1 minute instead of every 5 minutes. The 1 can be dropped unless planning to change it. Also 391 is needed to have 390 included in the results from range. market_close() runs one minute before market close (otherwise any orders within the function called would not execute) while market_open(minutes=390) is the route to run something at market close for gathering some sort of information for example.

def initialize(context):  
    for i in range(15, 391, 1):    # start, stop, every n minutes, where n is 1 in this case  
        schedule_function(run_this, date_rules.every_day(), time_rules.market_open(minutes=i))

def run_this(context,data):  
    # your code  

kewl. Thx.