Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
What Are "Total Returns?"

Greetings,

I just created my first algorithm on Quantopian. (Please bear in mind I have no background in stocks and only a little in programming.) The algorithm is probably the dumbest algorithm you could produce. It literally just checks if today's price is higher than yesterday's. If it is, it buys 50 stocks. Otherwise, it does nothing.

yesterday = 0

def initialize(context):  
    context.aapl = sid(24)

def handle_data(context, data):  
    global yesterday  
    today = data[context.aapl].price  
    if today > yesterday:  
        order(context.aapl, 50)  
    else:  
        #order(context.aapl, -50)  
        pass  
    yesterday = data[context.aapl].price  

When I run a full backtest, it says my total returns are 836%. How can this be? The algorithm only buys stocks. It doesn't sell them. How exactly is "total returns" computed?

Thanks for taking the time to read this n00b question.

Appreciatively,
—Nathan

8 responses

Hi Nathan,
Thanks for your question.

If you buy a stock, and the market price of that stock rises, there is a gain in the value. That gain is an unrealized gain because it is in the stock value, not in cash. When you sell the stock you realize the gain. For our simulation and reporting, we show the sum of your realized and unrealized gains as total return. If you are curious about the method of calculating returns over a period, our code for this is open source as the zipline project. The relevant code is in our PerformancePeriod class, which calculates a total value for a portfolio of stocks plus cash at the beginning of a period, and at the end of a period. Period returns are then (end_value - start value) / start_value, which is done in the calculate_performance method.

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.

Ahh, I see. That makes much more sense. Does that mean a total return of less than 100% would be a loss?

Hello Nathan,

Only a -ve return represents a loss. Also, for you algo to buy all the shares it did you need a starting capital of $12 M which then gives a much more realistic return.

P.

Ok. So my algorithm spent more than my starting capital? What different is happening between using $1m capital and $15m capital? My algorithm's code doesn't take capital into account.

Hello Nathan,

I think the formula for returns is something like:

((Positions Value + Current Cash - Starting Cash) / Starting Cash) *100 %

so with $1 M at the start you have returns of:

((20,260,368 + -10,900,705 - 1,000,000)/1000,000) * 100 = 835.96%

but with $12 M you have returns of:
((20,260,368 + 99,249 - 12,000,000)/12,000,000) * 100 = 69.66%

Something like that, anyway, based on the final 'Positions Value' in a full backtest.

P.

Ahh, yes, looking now I see that I did indeed buy myself into the ground. I'll have to keep in mind that the "Total Returns" value doesn't necessarily reflect debt. Thank you again for the clarification!

Nathan,

Brokers allow you to borrow capital and stock on margin. When you buy more than your starting capital, you are borrowing money from your broker. Your returns, however, are calculated from your original base. So, if you buy 2x as much stock as your starting base, your returns will be 2x the stock returns. If the stock goes up 5%, your returns would be 10%. If the stock falls 5%, your returns would be -10%. Investing with additional borrowed capital is called using leverage, because of the way it amplifies your (positive or negative) returns.

Whenever you see returns that are extreme, you should immediately be skeptical about the algorithm's management of its leverage.

thanks,
fawce

I see. That makes sense. So a very confident investor might go into debt in order to take advantage of an opportunity.