Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Emulating a backtest in research notebook - possible in 2020?

This may be a really dumb question, I'm not new to quantopian - but I've not been around for a while, so apologies if I'm missing something obvious.

In the past I've been able to run backtests in the notebook environment in a very similar manner to how it's done in the algorithm environment. However when looking for a refresher on the topic I found a few posts saying this is no longer possible (as of 2018?). I'm guessing that similar functionality has taken the place of what I remember using - can anyone advise?

Ideally I'd like to be able to copy my pipeline, 'before trading starts' and, 'rebalance' functions directly to the notebook (which is easy enough), but then somehow 'run' a backtest and get a set of metrics out. This is all for the goal of optimising values used in my code.

2 responses

I would suggest encapsulating most (or ideally all) of your stock selection logic into a pipeline definition. It sounds like you have done this. The typical workflow would be this:

  • Create a pipeline factor which you intend to trade on. This could be a simple 1 or 0 for hold or not hold, or a number such as RSI.
  • Use Alphalens to analyze the results of this factor. Alphalens runs much faster than a backtest and, moreover, it can analyze a number of groups of factor values at one time. For example, one could find the optimum RSI values at which to buy or sell. Instead of setting up a filter for RSI > max_rsi and then varying max_rsi, one could simply output the RSI value in the factor. In Alphalens, set buckets for 10, 20, ... 80, 90, 100. Aphalens will then show the n-day return for each bucket. Optimize your max_rsi value based on returns for each bucket.
  • Copy the factor to an algo in the IDE. Add simple logic to trade on the signal. Trade every day at open to get results similar to Alphalens.
  • After the backtest has run, use the get_backtest method in a notebook to get the backtest results. All the backtest results are then available to analyze in the notebook environment.

Using the get_backtest method and then the associated AlgorithmResult attributes, one can get the desired backtest metrics. There is info on all the data one can fetch in the docs https://www.quantopian.com/docs/api-reference/research-api-reference#quick-reference

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.

@DanWhitnable

Thanks for the quick response! So I can use Alphalens for backtesting in the research notebooks? The issue I'm having (in part at least) is most of my logic can be kept inside the pipeline, but certain methods can't - eg. only sell if price is above/below X, or can that be done in pipeline? for now I've hard coded that logic using if statements. Maybe there's a way around this?

I'm not too concerned with using get_backtest in the notebook. For the first pass at looking at optimising values I was purely looking at returns % since there is a minimum returns I'm aiming for (which should be easy to get in the notebook backtest) - after that I'll fine tune to handle other attributes!

I'll look into using Alphalens, thanks!