Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Question on comparing strategies

Hi guy

I am a complete newbie on Quantopian and need your help.

I am testing different strategies and want to have the performance of two strategies compared in the backtest. Is this possible?

For example, I want to compare the strategy of buy 10 shares of TSLA every month and hold with a different strategy of buying 5 shares of TSLA and 5 shares of SPY every month and hold. Is it possible to show the backtest performance trace of both strategies in the same chart?

I've thought about using the record function or using set_benchmark function but neither of them seems to work.

Please help.

3 responses

Yes, you could use the record_data() function. You'd likely need a bit of math and store variables in context.
Pseudocode:


initialize():  
  context.portfolio1_return = 1.00  
  context.portfolio2_return = 1.00

  context.tsla_last_px = get_last_price('TSLA')  
  context.spy_last_px = get_last_price('SPY')

  schedule_function(record_data, EVERY_DAY_AT_CLOSE)

record_data():  
  tsla_return = get_last_price('TSLA') / context.tsla_last_px  
  spy_return = get_last_price('SPY') / context.spy_last_px

  context.portfolio1_return *= (tsla_return)  
  context.portfolio2_return *= (0.5*tsla_return + 0.5*spy_return)

  log_data(context.portfolio1_return)  
  log_data(context.portfolio2_return)  

Another approach is to fetch the backtest results in a notebook then compare inside that notebook.

One can retrieve all the data stored in a backtest, including the daily performance, daily portfolio value, and recorded variables, through attributes of the AlgorithmResult object. This is all done in the notebook environment. The first step is to get a reference to each backtest using the get_backtest method and the backtest ID. The backtest ID can be found by opening a backtest and then copying the last string of digit after the last forward slash. Something like this.

# First get the two backtest results  
backtest_id_1 = '5e528e3c9e2bfd435c19cefc'  
backtest_1 = get_backtest(backtest_id_1)

backtest_id_2 = '5e528dc21ff9b7434f987de4'  
backtest_2 = get_backtest(backtest_id_2)

The returned AlgorithmResult object has a number of attributes which fetch different data about the backtest. There is a complete list of these attributes in the documentation (see https://www.quantopian.com/docs/api-reference/research-api-reference#algorithmresult-attributes). The cumulative_performance.ending_portfolio_value attribute will fetch the daily portfolio values. These could then be used to compare our two backtests.

backtest_1_daily_values = backtest_1.cumulative_performance.ending_portfolio_value  
backtest_2_daily_values = backtest_2.cumulative_performance.ending_portfolio_value

To plot these results on the same graph perhaps use the pandas plot method. First get them into a single dataframe, then plot. There are of course a lot of ways to plot but this is convenient.

# First make an empty dataframe  
plot_df = pd.DataFrame()

# Next add the daily values as columns  
plot_df['backtest_1_value'] = backtest_1_daily_values  
plot_df['backtest_2_value'] = backtest_2_daily_values

# Finally, simply plot the dataframe  
plot_df.plot();

Attached is a notebook which includes the above code. Make sure to replace the backtest IDs with your own before running. The notebook will NOT run with the current IDs since backtests are private (ie nobody other than the account holder has access).

Hope that helps.

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.

@Harry Sauers and @Dan Whitnable

Thank you very much for your help. Both are great ways of solving this problem!