Skip to main content

backtrader

PyPi Version License Travis-ci Build Status Python versions

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 v7)

Tickets

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

For feedback/questions/… use the Community

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.signal_add(bt.SIGNAL_LONG, 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.run()
cerebro.plot()

Including a full featured chart. Give it a try! This is included in the samples as sigsmacross/sigsmacross2.py. Along it is sigsmacross.py which can be parametrized from the command line.

Features:

Live Trading and backtesting platform written in Python.

  • Live Data Feed and Trading with

    • Interactive Brokers (needs IbPy and benefits greatly from an installed pytz)

    • Visual Chart (needs a fork of comtypes until a pull request is integrated in the release and benefits from pytz)

    • Oanda (needs oandapy) (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 pyfolio integration (deprecated)

  • Flexible definition of commission schemes

  • Integrated broker simulation with Market, Close, Limit, Stop, StopLimit, StopTrail, StopTrailLimit*and *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)

Documentation

The blog:

Read the full documentation at:

List of built-in Indicators (122)

Python 2/3 Support

  • Python >= 3.2

  • It also works with pypy and pypy3 (no plotting - matplotlib is not supported under pypy)

Installation

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

From pypi:

  • pip install backtrader

  • pip install backtrader[plotting]

    If matplotlib is not installed and you wish to do some plotting

An example for IB Data Feeds/Trading:

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

    pip install git+https://github.com/blampe/IbPy.git

    or (if git is not available in your system):

    pip install https://github.com/blampe/IbPy/archive/master.zip

For other functionalities like: Visual Chart, Oanda, TA-Lib, check the dependencies in the documentation.

From source:

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

Version numbering

X.Y.Z.I

  • X: Major version number. Should stay stable unless something big is changed like an overhaul to use numpy

  • 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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

backtrader2-1.9.76.123.tar.gz (259.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

backtrader2-1.9.76.123-py3-none-any.whl (419.0 kB view details)

Uploaded Python 3

File details

Details for the file backtrader2-1.9.76.123.tar.gz.

File metadata

  • Download URL: backtrader2-1.9.76.123.tar.gz
  • Upload date:
  • Size: 259.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.6.10

File hashes

Hashes for backtrader2-1.9.76.123.tar.gz
Algorithm Hash digest
SHA256 f9e4fca9543f8a4d96ee7d0480d8db10e66409d51355e924d04595123aef90e4
MD5 9b2a3c58cada31bb2162c44112f18a24
BLAKE2b-256 2bb6a2955a38de01be85732d73ead2ea95cd02d8ad447a98a9b1288bb4772c0a

See more details on using hashes here.

File details

Details for the file backtrader2-1.9.76.123-py3-none-any.whl.

File metadata

  • Download URL: backtrader2-1.9.76.123-py3-none-any.whl
  • Upload date:
  • Size: 419.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.6.10

File hashes

Hashes for backtrader2-1.9.76.123-py3-none-any.whl
Algorithm Hash digest
SHA256 713b1dae0fe2a30537ad1764a492f15c125472c5dd0dbd28fa3f6c2b4105c50b
MD5 c843e2f72ce677a12e6abd3e2a6a5fe5
BLAKE2b-256 eead87720cffc61b0aaee4a48f60c04d501ac0aef9e354226bb28b77bf089072

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.9.76.123 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page