Skip to content

vio1ator/backtrader

 
 

Repository files navigation

backtrader

PyPi Version License GitHub Actions 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 Support

  • Python >= 3.9 (tested on 3.9 to 3.12)
  • PyPy is not part of the current test matrix

Installation

backtrader is self-contained with no mandatory dependencies. Optional extras enable plotting and specific data sources.

With uv (recommended):

  • uv pip install backtrader
  • uv pip install "backtrader[plotting]" for matplotlib plotting
  • uv pip install "backtrader[pandas]" for pandas data feeds
  • uv pip install "backtrader[calendars]" for exchange calendars
  • uv pip install "backtrader[talib]" for TA-Lib indicators (requires the TA-Lib native library)

With pip:

  • pip install backtrader
  • pip install "backtrader[plotting]"

Note

Matplotlib >=3.7 is recommended when using 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:

  • uv pip install -e . to install in editable mode for development

Extras overview

  • plotting: matplotlib-based charts
  • pandas: ingest data from pandas DataFrame/Series
  • calendars: exchange calendars via pandas-market-calendars
  • talib: TA-Lib indicator bindings (requires native TA-Lib)

From source:

  • uv pip install -e . to install in editable mode for development

About

Python Backtesting library for trading strategies

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Python 100.0%