Using Analyzers in Backtrader

Once you have figured out how to write a basic strategy, you then need to be able to quantify whether it is very good. Backtrader has a rich library of analyzers that can provide you metrics from simply tracking wins and losses to more complex Sharpe ratio’s and drawdown analysis.

What are Backtrader Analyzers?

Simply put they are objects that you load into cerebro (the Backtrader engine) that monitor your strategy as it runs. After cerebro has finished running the analyzers can be accessed through strategy objects that are returned by cerebro after running. The analyzer objects themselves have a special method (function) that return a dictionary containing all the statistics that the analyzer is tracking. Sometimes this is a lot of information and other times just one or two statistics. If that does not make sense right now, don’t worry, it will become clearer once you see the code.

There are a whole library of different analyzers in Backtrader. Once you have worked through this script, have a look at the documentation to see which analyzers interest you and tweak the code to include them.

Scope

This post is intended to show how to setup and analyzer and print the results. It follows the Backtrader: First Script post and forms part of the getting started series to Backtrader. To see the rules of the strategy and explanation of the code, take a look at that post.

Background

There a are a couple of backtesting / trading metrics that crop into this post. Here is a glossary:

  • Strike Rate: This is the a percentage that represents the number of times you win vs the total number of trades you placed (win rate / total trades). It can help identify to whether you have an edge in the market. Some people aim for to get the strike rate as high as possible. Usually with lots of small wins. Others are happy with lower strike rates but aim for big wins and small losses.
  • SQN: System Quality Number, this was defined by Dr Van Tharp of the Van Tharp institute. It basically gives your strategy a score. A more academic explanation of SQN from the Van Tharp website is below:

SQN measures the relationship between the mean (expectancy) and the standard deviation of the R-multiple distribution generated by a trading system. It also makes an adjustment for the number of trades involved.

For more information see: http://www.vantharp.com/tharp-concepts/sqn.asp

  • Note: The Backtrader documentation provides a helpful ranking system for SQN:
    • 1.6 – 1.9 Below average
    • 2.0 – 2.4 Average
    • 2.5 – 2.9 Good
    • 3.0 – 5.0 Excellent
    • 5.1 – 6.9 Superb
    • 7.0 – Holy Grail?

The Code

Commentary

Lets start at the setup. Adding an analyzer to the strategy is as easy as calling the addanaylzer() function.

Here we are loading the TradeAnalyzer and giving it a name. The name makes accessing it later much easier.

After cerebro has finished running it will return a list of strategy objects. In our case we only loaded one strategy into cerebro. Even so, the one strategy will still be returned in a list. Therefore we need extract our strategy from the list by using a list index position of [0]. Once we have that, we have our strategy.

Next up we need to access our data…. Analyzers can be accessed from inside the strategy object and have a built in method (function) for returning a dictionary that contains the results. Below we access the analyzer “ta” (which is the name we gave it when loading the analyzer into cerebro) from the firstStrat strategy object and call the “get_analysis()” method.

So now we have a dictionary, we want to get the data of interest out of it and do something with it. In this case, I am just going to take some of the metrics and print them to the terminal. However, you might want to expand this and export the results to a CSV. For this exercise I am going to look at:

  • Total trades still open
  • Total closed trades
  • Total trades won
  • Total trades lost
  • My Strike rate (which I will calculate from the data given)
  • My best winning streak
  • My worst losing streak
  • My profit or loss.
  • The SQN number of the strategy

To do this I have created two special print functions. This is so the code can then be reused (cut + paste) for future scripts. The print function worth commenting further on is:

I fear this function might be overly complex for a beginners post due to the line shown below. I just happen to like the clean output it produces in the terminal. It contains some more advanced python techniques for formatting text. I am talking about the line which looks like this:

This line allows me to print the output evenly spaced without having to install another python module like TextTable. To find out more about data formatting in Python see the docs here:

https://docs.python.org/3/library/string.html#formatspec

An alternative (and a much simpler option) is to use the built-in analyzer print()method. This prints every value inside the analyzer on a separate line. Whether you want to do this or not is a matter of personal preference. I.e whether you want to have some alternative formatting, pick and choose certain statistics of interest or further process the data. An example of how to do this is below.

The result

Fire up the script and you should see something that looks like this:

Terminal output with Analyzer from bactrader

Not a great result trading wise! It looks like our super simple strategy need some refining. Who would have thought it?