by mementum

mementum / backtrader

Python Backtesting library for trading strategies

4.8K Stars 1.5K Forks Last release: Not found GNU General Public License v3.0 2.4K Commits 139 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:


.. image:: :alt: PyPi Version :scale: 100% :target:

.. .. image:: :alt: PyPi Monthly Donwloads :scale: 100% :target:

.. image:: :alt: License :scale: 100% :target: .. image:: :alt: Travis-ci Build Status :scale: 100% :target: .. image:: :alt: Python versions :scale: 100% :target:

Yahoo API Note:

[2018-11-16] After some testing it would seem that data downloads can be again relied upon over the web interface (or API



The ticket system is (was, actually) more often than not abused to ask for advice about samples.

For feedback/questions/... use the


Here a snippet of a Simple Moving Average CrossOver. It can be done in several different ways. Use the docs (and examples) Luke! ::

from datetime import datetime import backtrader as bt

class SmaCross(bt.SignalStrategy): def init(self): sma1, sma2 = bt.ind.SMA(period=10), bt.ind.SMA(period=30) crossover = bt.ind.CrossOver(sma1, sma2) self.signaladd(bt.SIGNALLONG, crossover)

cerebro = bt.Cerebro() cerebro.addstrategy(SmaCross)

data0 = bt.feeds.YahooFinanceData(dataname='MSFT', fromdate=datetime(2011, 1, 1), todate=datetime(2012, 12, 31)) cerebro.adddata(data0) cerebro.plot()

Including a full featured chart. Give it a try! This is included in the samples as

. Along it is
which can be parametrized from the command line.


Live Trading and backtesting platform written in Python.

  • Live Data Feed and Trading with

    • Interactive Brokers (needs
      and benefits greatly from an installed
    • Visual Chart (needs a fork of
      until a pull request is integrated in the release and benefits from
    • Oanda (needs
      ) (REST API Only - v20 did not support streaming when implemented)
  • Data feeds from csv/files, online sources or from pandas and blaze

  • Filters for datas, like breaking a daily bar into chunks to simulate intraday or working with Renko bricks

  • Multiple data feeds and multiple strategies supported

  • Multiple timeframes at once

  • Integrated Resampling and Replaying

  • Step by Step backtesting or at once (except in the evaluation of the Strategy)

  • Integrated battery of indicators

  • TA-Lib indicator support (needs python ta-lib / check the docs)

  • Easy development of custom indicators

  • Analyzers (for example: TimeReturn, Sharpe Ratio, SQN) and

    integration (deprecated)
  • Flexible definition of commission schemes

  • Integrated broker simulation with Market, Close, Limit, Stop, StopLimit, StopTrail, StopTrailLimitand OCO orders, bracket order, slippage, volume filling strategies and continuous cash adjustmet for future-like instruments

  • Sizers for automated staking

  • Cheat-on-Close and Cheat-on-Open modes

  • Schedulers

  • Trading Calendars

  • Plotting (requires matplotlib)


The blog:

  • Blog 

Read the full documentation at:

  • Documentation 

List of built-in Indicators (122)

  • Indicators Reference 

Python 2/3 Support

  • Python >=

  • It also works with

    (no plotting -
    is not supported under pypy)


is self-contained with no external dependencies (except if you want to plot)

From pypi:

  • pip install backtrader
  • pip install backtrader[plotting]


    is not installed and you wish to do some plotting

.. note:: The minimum matplotlib version is


An example for IB Data Feeds/Trading:

  • IbPy
    doesn't seem to be in PyPi. Do either::

    pip install git+

    or (if

    is not available in your system)::

    pip install

For other functionalities like:

Visual Chart
, check the dependencies in the documentation.

From source:

  • Place the backtrader directory found in the sources inside your project

Version numbering


  • X: Major version number. Should stay stable unless something big is changed like an overhaul to use
  • Y: Minor version number. To be changed upon adding a complete new feature or (god forbids) an incompatible API change.
  • Z: Revision version number. To be changed for documentation updates, small changes, small bug fixes
  • I: Number of Indicators already built into the platform

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.