Back to Community
Research environment returns() method

Is there any convenient method for accessing the returns() method within the research platform? I want to calculate pct chg from the prior day's closing values (which is my understanding of what returns() does within handle_data in backtests). At the moment the best way I can find is messy:

1) get daily bars, shift(1) the close price to get prior daily close
2) get minute bars
3) do a bunch of recasting of the datetime indices to create a date field on both dataframes that have the same type and can be used to merge
4) ffill the prices field of the intraday dataframe
5) merge both dfs, using the appropriate join type and maybe cartesian? to get the prior daily close to appear on every minute-ly bar
6) calculate the pct change intraday
7) figure out some way to adjust for dividends earned between the prior day and current day (which would wreak havoc on the pct_chg calculations)

Messy, and not totally correct. I'm hoping there's an easier and/or more accurate way to go about it?

4 responses

Chad,

Are you inside a zipline algorithm run or do are you just in plain research?

I'm working on a Research Cheat Sheet, where I demonstrate how to do a few common functions from the IDE in Research, so stay tuned for that, I'll put this in there for sure!

For now though there is no built-in returns() function in research.

J

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 for the reply. So, is the hacky algorithm I describe generally the simplest way to do it?

And does dividend data exist within the research environment? I suppose get_fundamentals would have ex-div values which I could use to adjust daily returns within the dataframe?

The price and fundamental data is identical in the IDE and research; they both read from the same databases. Dividends are added to your portfolio as cash, but we don't have dividend event actions. If you have this data, you can import it to research using "local_csv".

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.