Back to Community
schedule_function running handle_data every day despite specifying each month?

I have the following schedule function in the initialize function:

schedule_function(handle_data,  date_rules.month_start(), time_rules.market_close())  

Within the handle_data function I output some results to an empty DataFrame, with a new row added for each new day handle_data is called. The DataFrame has 2 columns, the date and a mean of some prices:

  close_price_mean = buy_list.mean()  
   today = zipline.api.get_datetime().date()  
   df.loc[today] = pd.Series({'Average portfolio perf':close_price_mean}  

When I run the backtest, I am surprised to find that there is a new row for every day of the backtest, rather than only at the start of each month, as the schedule function defines with date_rules.month_start() . Does this means that handle_data is actually getting called each day in the background?

The actual backtest results appear to only form portfolios at the start of each month, as you would expect, but it seems that somehow the handle_data function gets called every day as the DataFrame gets data for each day of the backtest?

I would be very grateful if anyone could help me understand what is happening here.

2 responses

The function handle_data is a built in function which automagically gets called every minute. One doesn't need to schedule it. It just happens. It's generally good practice to NOT name a function handle_data and schedule it too. If one does, it will run every minute, and run an extra time when it is scheduled. That is what's causing the behavior you are seeing.

The solution is to simply rename your function something other than handle_data.

That should give you the expected results.

good luck.

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 very much Dan, that's done the trick.