Backtrader, a Python backtesting and trading framework

After looking at zipline, another backtesting framework, I thought it would make sense to take a look at some other options in the open source community for backtesting and trading. The next framework to investigate is backtrader, an open source project that aims to provide tooling for backtesting and live trading algorithmic strategies. I’ll use the topics in my post on open source software to consider this project, along with a quick overview of how it works.

Project Activity

The GitHub repo for backtrader shows recent activity, with the last release about eight months old. Development has been steady since mid 2015. There’s over 3,000 stars on the project, 7 open/117 closed pull requests with 35 contributors at time of writing. This project appears to be mostly run by one developer, but obviously has a number of contributors.

Open Issues

Backtrader is not using GitHub for issue tracking, the author asks users to use the Community. There isn’t a history of issues or a list of open issues that I could find there, but if users have questions or discover a bug it appears that the community and author are responsive.

Documentation

The Backtrader documentation is quite good, with a solid overview of the framework, its features, and how to use it in the getting started guide. The API documentation includes details about each part of the platform with clear examples. There are also a number of blog posts linked from the framework web site that give insight into how people are using the framework in research papers and real trading.

Getting Started

Backtrader says it supports through Python 3.7 at time of writing on GitHub, and I can see build failures for Python 3.8, so I will try the getting started guide using 3.7. I’ll do this using pyenv and a virtualenv, although if you have any supported version installed you can just create a virtualenv and skip this step. (One nitpick is that the installation docs list up to 3.5, so at time of writing they are not up to date)

$ pyenv virtualenv 3.7.5 backtrader
$ pyenv activate backtrader
# I want to enable plotting, so I specify that here, but could omit
$ pip install backtrader[plotting] jupyter

Installation succeeded with no issues in my environment. However, when running the sample code I had a failure in the Yahoo data download due to a missing requirement for the requests module. A quick pip install requests and a restart fixed that, and the example script ran with no problems. This was a very smooth initial start.

Ecosystem

Backtrader provides quite a bit of functionality out of the box, including a number of indicators, as well as ta-lib integration. For data, it supports a number of Data Feed parsers and works with Yahoo data out of the box. The documentation shows how to import your own data using CSV files, and includes information about handling future rolls. Most vendors can supply CSV or the conversion can be made, so this should allow for data from just about any vendor. There are the usual order types (Market, Limit, Stop, StopLimit), as well as some more advanced order types. Slippage and commissions are handled as well. For live trading, Interactive Brokers, Oanda v1, and Visual Chart are all supported. This is one big advantage over zipline.

Code Quality

The code looks to be of high quality. There’s good test coverage, and comments are helpful. For example, the indicators are documented with links to a descriptions somewhere on the web, so reading the code is a great place for someone to learn. Checkin comments are concise but clear.

The only issue I saw looking over GitHub was that Python 3.8 was setup in Travis but failing. The issue is with the removal of time.clock() in Python 3.8. This may be a good pull request for someone who wants to contribute and requires Python 3.8.

Project Structure

Backtrader appears to be run by one developer with input from a number of users. It appears to me that at time of writing the developer has remove the issue tracker in GitHub, though I see references to GitHub issues in the documentation. I don’t see a contributions document or developer guidelines. My guess is that too many issues were being opened by users that were not bugs. For a project of this complexity that attracts non-developer users, this is probably pretty common. Since the community seems to be responsive though, this may not be a huge issue, especially if the software is stable currently and has no major known issues. I personally would prefer to have an issue tracker that would tell me where things stand, since users typically use the issue tracker for both feature requests and bugs, and no software has every feature or is bug free.

Corporate Backing

Unlike zipline, backtrader is not sponsored by a corporation, so this is a bit of a risk if the main developer gives up on the project or moves on. The good thing about this project is that there are no strange incentives where features are not included (like live trading or data integrations) because the company backing the software only provides that with paid versions of the software.

License

Backtrader is released under the GNU General Public License v3.0.

Conclusion

Backtrader looks like a very good option for anyone looking for a backtesting framework in Python, especially for trades in Equities, Futures, or Crypto using daily or minute bars. The project appears to be very stable and in fairly wide use. There is excellent documentation and plenty of examples, and an active community. One major advantage to backtrader over zipline is the support for live trading.

Don't miss any articles!

If you like this article, give me your email and I'll send you my latest articles along with other helpful links and tips with a focus on Python, pandas, and related tools.

Invalid email address
I promise not to spam you, and you can unsubscribe at any time.

Have anything to say about this topic?