Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
any plan to provide weekly data via history() function?

Hi, there -

some strategy would need to referring weekly data. although, one could calculate weekly value in algo, it will make the backtest consume much more computing time.
It would be nice if those weekly data can be precalculated and accessed via history() function. is this planned? or we shall not hope for this in foreseeable future?

7 responses

You could use the Python datetime library to select a day of the week and then call your history function.

For example, the code below will give you the trailing price every Thursday. I'm using the date.weekday() attribute, which returns the day of the day of the week as an integer starting from Monday as 0 (Thursday == 3).

# Get the current exchange time, in local timezone  
    exchange_time = pd.Timestamp(get_datetime()).tz_convert('US/Eastern')  
    if exchange_time.weekday() ==3: #If today is Thursday, get weekly history  
        weekly_price = history(10, '1d', "price")  
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, Alisa.

this seems not a documented use of history() -- as the first argument shall be bar_count. did I miss anything here
RE: history(bar_count, frequency, field, ffill=True)

ah.. you're right :) I was simultaneously looking at another piece of code and it got jumbled. I edited the code snippet above! It will now store trailing weekly data for 10 weeks.

should that '1d" be changed to '1w'?

History is only supported in '1d' frequency. You are required to run the algorithm in minute mode but will receive the data in daily bars.

Using the check above, it will only accumulate data once per week, effectively making it a weekly frequency. For example, this code will return the last 4 days of daily price data plus today's trailing minutely data.

history(5, '1d', "price")  

If you need help developing your idea, we can collab on an algo!

Thanks, Alisa. will enlist your help via collab after learning a few more basics.

Here's a detailed example that gets you closer to your goal. It doesn't use the history function, but instead uses a deque to accumulate weekly prices. I wasn't sure if you wanted to try this on a single stock or multiple securities, so I included a both set_universe and a single stock call.

This algo will check the price of each stock every Tuesday at 10AM. In general, if you continue to develop this algorithm, I would suggest to check the exchange time during a window such as between 10:00 - 10:30AM. It makes your algorithm robust to handle late launches to market, stocks missing trade data, or system problems to name a few.

Cheers,
Alisa