Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Scheduler overide beyond 1 month?

Is it possible to have the scheduler run beyond 1 month periods? I'm trying to run an annual rebalance.

Thanks,
Michael

6 responses

Hi Michael, I think the best you can do is to schedule a function to execute every month, and check the current month via get_datetime().month. This off the top of my head, so this might not be the precise form of the code.

Sunil

You can setup a schedule_function to run at any custom frequency. Take a look at the example here (in the Scheduling Multiple Functions section): https://www.quantopian.com/help#ide-schedulefunction

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.

Alisa, I can see how you can schedule multiple functions to get more frequent execution, but if I'm not mistaken Michael was asking about less frequent execution, specifically once an year?

I went digging around the zipline code, and found this:

https://github.com/quantopian/zipline/blob/master/zipline/utils/events.py#L447

If you really want, it looks like you ought to be able to implement your own scheduling class. I haven't tried it, don't really see any need for this, so I don't know if it will actually work.

Sunil

In the specific example in the help doc, you can make a schedule_function run every 30 minutes. Similar to how you can manipulate the time interval, you can also manipulate the date interval to run at a custom frequency. Using this technique, you can run a function every quarter, every year, or at any other period:

def initialize(context):  
  # For every minute available (max is 6 hours and 30 minutes)  
  total_minutes = 6*60 + 30

  for i in range(total_minutes):  
    # Every 30 minutes run schedule  
    if i % 30 == 0:  
      # This will start at 9:31AM and will run every 30 minutes  
      schedule_function(  
      myfunc,  
        date_rules.every_day(),  
        time_rules.market_open(minutes=i),  
        True  
      )  
def myfunc(context,data):  
  pass  
def handle_data(context,data):  
  pass  

Do you have an example of how to run this annually or quarterly? I'm having a hard time taking this example and modifying it for this purpose.

Brian

Hi Brian,

The simplest approach is to put a test in your scheduled function to check if it should run. For example, something like this:

def stock_selection_by_calendar(context, data):  
    today = get_datetime()  
    return today.month == 1  

This function is called by my scheduled function that I want to run every year to check if the month is January. You can use other types of calculations to check for start of quarter, etc. Hope that helps.

Sunil