Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Using buy and hold as benchmark

I'm trying to learn how this tool works and the first thing I thought I'd try was to simply plot a buy and hold portfolio of one stock against the price of the stock itself. In theory, these two series should then be identical.

However, that's not what I'm seeing.

The code I'm using looks like this:

def initialize(context):  
    context.stock = sid(24)  
    context.startup = True  
    set_benchmark(context.stock)

def handle_data(context, data):  
    if context.startup:  
        order_percent(context.stock, 1.0)  
        context.startup = False  

All I'm doing is choosing a stock, setting it as the benchmark and then buying it on the first call to handle_data.

Doing this for a few different stocks and running a backtest for the first week of 2014 gives the following results:

Screenshot

The actual algorithm is always off initially compared to the benchmark. Any idea what I'm doing wrong?

1 response

Hi Quentin,

Thanks for the question, and I admire the test for the platform. You are not doing anything wrong!
There are a few important differences between a benchmark and an algo:

  • The benchmark is "fully invested" on the open of the first bar. In a daily backtest, this can cause a meaningful discrepancy in the benchmark returns and the algo returns.
  • Both the benchmark and the algorithm will receive dividend payments, but the benchmark automatically reinvests them, whereas your algo needs to reinvest the cash.

thanks,
fawce

p.s. I wasn't able to see your screenshot, but you can attach a full backtest to your post to show the effects.

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.