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

Regarding this code (especially unclear about the time_function portion):
schedule_function(ma_crossover_handling, date_rules.every_day(), time_rules.market_open(hours=1))

I know that it means to execute the function everyday , 1 hour after the market opens but does it mean that trades are executed within that hour only? Or throughout the day?

Also, the documentation only states time_rules.market_open and time_rules.market_close as possible options but when I am coding this in the IDE, the autocomplete gives me many more options like time_rules.every_minute. I am wondering why they are not documented?

1 response

The schedule_function method schedules a user defined function to run at a particular time based upon the parameters. The trade execution logic is completely separate and independent. Open orders are monitored every minute of the trading day (behind the scenes) and executed per the slippage model. So yes, trades are executed throughout the day. Scheduling a function only specifies when the user logic is executed.

The schedule_function method takes the following parameters

    def schedule_function(self,  
                          func,  
                          date_rule=None,  
                          time_rule=None,  
                          half_days=True,  
                          calendar=None):  

Just like most of the Quantopian code, this code is open source and can be found on GitHub but the Quantopian documentation is probably easier to read and a better resource.

The only date rules that are currently defined are

  • date_rules.every_day()
  • date_rules.month_start(days_offset=0)
  • date_rules.month_end(days_offset=0)
  • date_rules.week_start(days_offset=0)
  • date_rules.week_end(days_offset=0)

The only time rules currently defined are

  • time_rules.market_close(offset=None, hours=None, minutes=None)
  • time_rules.market_open(offset=None, hours=None, minutes=None)

Click on the links above to go to the documentation.

A every_minute option does pop up in the IDE when using the autocomplete feature. This is a carryover from when Quantopian could be scheduled 'daily' and currently does the same as market_open. If one wants to 'schedule' a function to execute every minute then simply place ones code in the pre-defined handle_data function. This automatically executes every minute.

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.