Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to find historical returns?

Hello,

Does anyone know of a more efficient way to calculate a security's annual returns in Quantopian?

Right now, I have the function below run on a daily basis. It simply records the returns of a security everyday and adds it to a running list of daily returns stored in a dictionary keyed to that security. However, I have to wait a year in the backtest (and live trading) in order to determine the security's annual returns!

def returns_recorder(context, data):  
    for security in context.securities:  
        daily_return = data[security].returns()  
        try:  
            context.sec_returns[security] = context.sec_returns[security][-260:]  
        except KeyError:  
            context.sec_returns[security] = []  
        context.sec_returns[security].append(daily_return)  

Surely there's a better way to do this ?

4 responses

Norrin - what you want is the history() command. History() loads up a trailing window of data. You can load a trailing window of 252 days and compare the first and last closing prices in the window to calculate the returns. There are a lot of examples of using history() in the documentation.

Note that this, unfortunately, doesn't include the returns you get from a dividend. We're getting close to shipping a way to compare returns that include dividends as well.

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.

Q's returns don't include dividends? Say it is not so - that would be an egregious oversight. Some say 33% of the weighted return to S&P 500 constituents over a 10 year period ending in 2014 is due to dividends; at least according to this .

Maybe it is time to start due diligence on Q's data infrastructure.

Hi,

The simulation includes dividends. We simulate dividends as events, incrementing cash balances on the pay date if you owned the stock on the ex date. This code is open sourced in zipline.

The history function provides a look back window during simulation. We are currently not smoothing the look back window for dividends.

At the root of the problem with history is a choice I made in the very early days to use split/merger adjusted price&volume data for the simulation. To thoroughly address this problem, we want the events in simulation to be "as traded" values (no adjustments, point in time), and for dividends to be treated as events (so that the algo has to consider re-investing cash payments). However, we want history calls to be fully adjusted (splits/mergers/dividends) so that trailing calculations avoid spurious price jumps. That work is underway, and also open source in zipline.

thanks,
fawce

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.

Hi,

That sounds like a reasonable (?) choice, but I am having a hard time understanding what is so hard about including dividends in the history (returns?) function. Since Q is focused on equities one might imagine having valid returns would be a necessity. What are you going to do with futures and roll gaps when you try to compute continuations?

I see Q-users write "simulation" and "backtest" somewhat interchangeably. I might suggest that the only difference between live trading and a simulation is the pointer into an as-reported data set. This has made my life so much easier for the past 15 years. As for a 'backtest', how 1990s.

Thank you again for the response, still trying to figure this whole Q-thing out.