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

context.portfolio.returns -- is it a percent (eg 10% or .1) or is it an absolute dollar amount (eg $10,000 or 10,000)?
I'm using it in an algo and I'm not sure if its a percent or amount

5 responses

Hello Jeffrey,

Looks like the relevant code is here:

https://github.com/quantopian/zipline/blob/master/zipline/transforms/returns.py

I'm not an expert Python programmer, but the result appears to be fractional:

if len(self.closes) == self.window_length:  
            last_close = self.closes[0].price  
            change = event.price - last_close  
            self.returns = change / last_close  

I don't have time now for coding, but you could write a simple algorithm to check the output of context.portfolio.returns against returns that you compute directly within the code.

Grant

Jeff, thanks for the question. The portfolio returns are calculated on a daily basis right after the market close, and they are represented as a percentage of the starting value of the portfolio (i.e. starting cash) at the start of the backtest. We don't format the number as a percentage, so 10% would be 0.1. The calculation is:

def calculate_performance(self):  
        self.ending_value = self.calculate_positions_value()

        total_at_start = self.starting_cash + self.starting_value  
        self.ending_cash = self.starting_cash + self.period_cash_flow  
        total_at_end = self.ending_cash + self.ending_value

        self.pnl = total_at_end - total_at_start  
        if total_at_start != 0:  
            self.returns = self.pnl / total_at_start  
        else:  
            self.returns = 0.0  

These returns include cash, so that dividends will be incorporated into your portfolio returns.
You can see the relevant code for the portfolio returns here: https://github.com/quantopian/zipline/blob/master/zipline/finance/performance.py#L541

Grant, thanks for the answer! The returns transform will give you the returns over the specified period for a single security. The calculations and their units (or lack thereof) mean you can make direct comparisons between the portfolio returns and the security returns.

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.

Hello Fawce,

Thanks for the correction.

I suggest updating your help page to include the details you provide above. Presently (3/17/2013), the relevant sections read:

returns()

The returns of this security since the end of the previous trading
day.


returns

Float: Cumulative percentage returns for the entire portfolio up to
this point.

Grant

I just checked in some updated help, and it will go out in the next push.

I think the existing help doc is correct (if a bit terse). I extended the language to make it more explicit.

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 Dan,

There is also:

returns()
The returns of this security since the end of the previous trading day.

Seems it should also read:

returns() The returns of this security since the end of the previous
trading day. The number is not formatted as a percentage, so a 10%
return is formatted as 0.1.

Might head off some questions/confusion...

Grant