Skip to main content

backtrader-next

PyPi Release Total downloads Made with Python License

Live Trading and backtesting platform written in Python.

Installation

pip install backtrader-next

History

Package is based on backtrader

Changes:

  • Added new Chart plotting using bn-lightweight-charts-python.
  • Improved testing performance by using the PandasData feed in runonce=True mode.
  • Added performance statistics in both text format (similar to Backtesting.py) and HTML format (similar to Quantstats).
  • Improved support for switching between futures (for testing, etc.).
  • Added new indicators implemented with Numba.
  • Improved performance — now it runs about 4.5× faster than Backtrader in runonce=True mode with PandasData.
  • Detailed results
  • Interactive visualizations

Performance

Performance comparison using the perf_compare benchmark.

Framework Execution Time Relative Speed
Backtrader 42.25 sec Baseline
Backtrader-next 9.40 sec 4.5x faster than Backtrader
Backtesting 2.98 sec 14.2x faster than Backtrader

Here a snippet of a Simple Moving Average CrossOver.

import pandas as pd
import backtrader_next as bt
from backtrader_next.feeds import PandasData


class SimpleSizer(bt.Sizer):
    params = (
        ('percents', 99),
    )

    def _getsizing(self, comminfo, cash, data, isbuy):
        value = self.broker.getvalue()
        price = data.close[0]+comminfo.p.commission
        size = value / price * (self.p.percents / 100)
        return int(size)


class SmaCross(bt.Strategy):
    params = (
        ('MA1', 20),
        ('MA2', 50),
    )

    def __init__(self):
        self.Order = None
        self.ma1 = bt.nind.SMA(self.data.close, period=self.p.MA1)
        self.ma2 = bt.nind.SMA(self.data.close, period=self.p.MA2)


    def notify_order(self, order):
        if order.status in [order.Submitted, order.Accepted]:  # Order is submitted/accepted
            return  # Do nothing until the order is completed

        elif order.status in [order.Canceled]:
            print('Order was Canceled', self.data.datetime.datetime(0))

        elif order.status in [order.Margin]:
            print('Order was Margin ', self.data.datetime.datetime(0))

        elif order.status in [order.Rejected]:
            print('Order was Rejected', self.data.datetime.datetime(0))

        self.Order = None  # Reset order



    def next(self):
        # Use ONLY Long Positions
        if self.crossover(self.ma1, self.ma2):
            pos = self.getposition()
            if pos:
                self.close(size=pos.size)
            self.Order = self.buy()
        elif self.crossover(self.ma2, self.ma1):
            pos = self.getposition()
            if pos:
                self.close(size=pos.size)
            # self.Order = self.sell()

    def crossover(self, ma1, ma2):
        try:
            return ma1[-1] <= ma2[-1] and ma1[0] > ma2[0]
        except IndexError:
            return False



if __name__ == '__main__':
    cerebro = bt.Cerebro()
    cerebro.broker.setcash(1_000_000.0)
    cerebro.broker.set_shortcash(False)
    cerebro.broker.setcommission(commission=0, margin=False)
    cerebro.addsizer(SimpleSizer, percents=90)

    df = pd.read_csv(f"AAPL_1d.csv.zip", sep=";")
    df['Datetime'] = pd.to_datetime(df['Date'].astype(str) , format='%Y-%m-%d')
    df.set_index('Datetime', inplace=True)

    data = PandasData(dataframe=df, timeframe=bt.TimeFrame.Days, compression=1)
    cerebro.adddata(data, name='AAPL')

    cerebro.addstrategy(SmaCross, )

    print(f'Starting Portfolio Value: {cerebro.broker.getvalue():.2f}\n')
    results = cerebro.run()
    print(f'\nFinal Portfolio Value: {cerebro.broker.getvalue():.2f}\n')

    rc = cerebro.statistics
    print(rc)

    # old plot required matplotlib
    # cerebro.old_plot(style='candle')
    
    cerebro.plot(filename="smacross.html")
    cerebro.show_report(filename="smacross_stats.html")
    print("end")

Output log

Starting Portfolio Value: 1000000.00


Final Portfolio Value: 29343500.38

Strategy                             SmaCross
MA1                                        20
MA2                                        50
Start                     2000-01-03 00:00:00
End                       2024-12-31 00:00:00
Duration                   9129 days 00:00:00
Equity Start [$]                    1000000.0
Equity Final [$]              29343500.384917
Equity Peak [$]               30253994.128875
Commissions [$]                           0.0
Cum Return [%]                        2834.35
Return (Ann.) [%]                     14.5018
Volatility (Ann.) [%]                 25.7613
CAGR [%]                                 9.78
Sharpe Ratio                           0.6617
Skew                                   -3.199
Kurtosis                             102.0707
Smart Sharpe Ratio                    -0.8039
Sortino Ratio                          0.9247
VWR Ratio                              4.8799
Calmar Ratio                           0.2017
Recovery factor [%]                    5.9154
Max. Drawdown [%]                    -71.9018
Avg. Drawdown [%]                     -4.7983
Max. Drawdown Duration     1666 days 00:00:00
Avg. Drawdown Duration       59 days 00:00:00
Drawdown Peak             2001-07-25 00:00:00
# Trades                                   66
Win Rate [%]                          56.0606
Best Trade [%]                       104.0816
Worst Trade [%]                      -63.5437
Avg. Trade [%]                         5.5053
Max. Trade Duration         276 days 00:00:00
Avg. Trade Duration          89 days 00:00:00
Profit Factor                          1.1704
Expectancy [%]                         0.0676
SQN                                    2.4064
Kelly Criterion [%]                   39.2016
dtype: object
end

It will create two HTML files and open it in your current browser.

chart1

chart1

chart1

chart1

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

Documentation

The old blog for backtrader:

  • Blog <http://www.backtrader.com/blog>_

Read the full old documentation at:

  • Documentation <http://www.backtrader.com/docu>_

List of built-in Indicators (122)

  • Indicators Reference <http://www.backtrader.com/docu/indautoref.html>_

An example for IB Data Feeds/Trading:

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

From source:

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

Version numbering

X.Y.Z

  • 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

Metadata

Release files for backtrader-next 2.3.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for backtrader-next 2.3.7
File Size Uploaded
backtrader_next-2.3.7.tar.gz 11.1 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for backtrader-next 2.3.7
File Interpreter ABI Platform
backtrader_next-2.3.7-py3-none-any.whl Python 3 none any Details

Total release size: 11.6 MB

Release files / backtrader_next-2.3.7.tar.gz

Download URL backtrader_next-2.3.7.tar.gz
Size 11.1 MB
Tags Source
SHA-256 checksum
How to use checksums
11b00ba378c8d42b656827edfd18dddd49a2a71c0861d9fbdac3ab20413a8745
BLAKE2b-256 checksum
How to use checksums
c8628dcb3273affd85ec966ebc2a8bce63e8152963ea81570aaf1d8e80c949d4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.12

Release files / backtrader_next-2.3.7-py3-none-any.whl

Download URL backtrader_next-2.3.7-py3-none-any.whl
Size 474.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9f6fcba68de798ec1477da72e5641f95e2d3a5d0d554e92cc1f934fdede8aa77
BLAKE2b-256 checksum
How to use checksums
b7155573431cdb07d092f09097154537c1ed86bdab0c5f1b11b87c244432d521
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.12

Release history Release notifications | RSS feed

This release

2.3.7 This release

2 release files

2.3.6

2 release files

2.3.5

2 release files

2.3.4

2 release files

2.3.3

2 release files

2.3.2

2 release files

2.3.1

2 release files

2.3.0

2 release files

2.2.1

2 release files

2.2.0

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release 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