Back to Community
Calculating average daily return from pipeline VS prices function

I calculated then compared this year's average daily return of AAPL between Feb 15th and Nov 29th (tried 28th as well), using the data from USEquityPricing.close and prices function. I'm getting 0.001365 from the first and 0.00124489294233 from the latter. That's a pretty big difference. I assume this has something to do with prices being adjusted, but I'm not quite sure.

Then I manually printed the USEquityPricing.close data for stocks for which the calculation returned NaN. Closing price data for Ticker AAME, for instance, is missing in pipelne during the said period whereas prices function outputs valid data (very few dates appear to be marked NaN). I see that there are enough shares outstanding (20.46M). Could somebody kindly provide an explanation?

Loading notebook preview...
Notebook previews are currently unavailable.
1 response

Hi John,

One thing to remember is that pipelines use values up to the previous day when carrying out calculations. For example, running a pipeline on 11/29/2017 that calculates the YTD returns of a stock will calculate the returns between 01/01/2017 and 11/28/2017.

On the other hand, the prices function returns the pricing data for the date requested. Using the example from your notebook, calculating the YTD returns on 11/29/2017 will result on calculating returns between 01/01/2017 and 11/29/2017.

To make the outputs match, you will need to adjust the start/end dates you input to prices to be 1 day previous to the date on which you run the pipeline. Attached you will find a copy of your notebook where I have made this change, you will notice the outputs from both methods match.

Regarding AAME's missing values, it appears to be missing pricing data on some days but not for the entire year. The problem is that the sum function does not handle NaN values properly, a single NaN value will cause the result to be NaN as well. You should use numpy.nansum instead.

I hope this helps.

Loading notebook preview...
Notebook previews are currently unavailable.
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.