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

I get "HistoryWindowStartsBeforeData" when run the algorithm with 'history function. Any ideas how to fix it? Thanks,

4 responses

Any ideas how to fix it? Thanks!

Hi Maxim,

Generally, running backtests in Research is a process that's error prone, and if possible you should avoid backtesting complex algorithms in Research (Research is great for a lot of things other than backtesting).

The error your running into is due to the fact that algo_obj.run(data.price) uses data as the implicit arguments for the start and end date of the backtest. Since this data only contains pricing data from your start date, any computations which use a lookback window will fail at the beginning of your backtest.

This tutorial notebook I've attached below covers running a backtest in research. If you notice, in handle_data the author checks if a certain number of days has passed, and only starts trading when there is enough data for the lookback window. You could incorporate a similar check in your algorithm which ensures that a sufficient amount of days have passed before the trading start.

After you add this function, you will likely have some more issues with not importing things such as get_open_orders. Furthermore, your algorithm seems to use minute pricing data, so you would need to generate minute data instead of daily data in your get_pricing call.

Is there any particular reason you want to run the backtest in research? Perhaps there exists a more supported feature in research which achieves what you are trying to accomplish.

Hope this helps,
Matt

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.

Thank you for your answer Matt.
As you suggest, i can check if a number of days has passed and only starts trading when there is enough data for the lookback window.
Any suggestions how to delay the execution of the schedule function that can be run only with Initialize and not with handle data? Of course i can use

if get_datetime().time() == '9:31'  

but it will require rewriting lots of code just to use it in research

The problem is initialize is only executed once at the beginning of the trading algorithm.

Your best bet would be to set a context.count variable in initialize, then increment it in one of your daily scheduled functions. All of your functions should check this context.count variable and only run conditionally if the threshold has been passed.