I need to calculate the monthly cumulative returns of each stock in the universe. For that i need to calculate the mean of daily returns but i can't find a function to do that.
I need to calculate the monthly cumulative returns of each stock in the universe. For that i need to calculate the mean of daily returns but i can't find a function to do that.
To calculate a mean you can use SimpleMovingAverage()
and for the daily returns you can use DailyReturns()
from quantopian.pipeline.factors import SimpleMovingAverage, DailyReturns
mean_daily_return = SimpleMovingAverage(inputs=[DailyReturns()], window_length=20)
But it would be much simpler to do:
from quantopian.pipeline.factors import Returns
monthly_return = Returns(window_length=20)
Great input here! Shows there are a number of ways to calculate returns.
@Viridian Hawk you're correct that probably the simplest approach is to use the built in Returns
factor. Good example of how to get the average of a factor over a given window by using it as an input to SimpleMovingAverage
. Be careful however with averaging arithmetic returns. One can't really do that. Consider the case averaging +10% and -10%. It's not 0%. Need to use log returns in this situation something like this.
mean_daily_log_return = SimpleMovingAverage(inputs=[DailyReturns().log1p()], window_length=20)
@Mathieu Meynier good observation that one needs to use log returns. As a side note, one can easily convert an arithmetic return factor to log returns by using the log1p()
method. One can just as easily do the opposite and turn a log return factor into arithmetic returns by using the expm1()
method.
Be a little careful with the window length in whichever method one chooses. Because returns are based upon daily differences, there will be n-1 returns over a period of n days. To calculate n daily returns one needs n+1 days.
@ Rishesh Garg hope this gives you some direction. If we maybe all misunderstood your original question please reply. There may have been something else you were trying to accomplish?
Attached is a notebook which summarizes the three approaches (built in Returns, summing average daily returns, and a custom factor). It shows they have identical results for both log and arithmetic returns.
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.