Skip to main content

Live Trading and backtesting platform in Python

Project description

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 2–3× slower than Backtesting.py in runonce=True mode with PandasData.
  • Detailed results
  • Interactive visualizations

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.open[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]:  # Canceled, Margin, Rejected
            print('Order was Canceled', self.data.datetime.datetime(0))

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

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

        self.Order = None  # Reset order



    def next_open(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(cheat_on_open=True)
    cerebro.broker.setcash(1_000_000.0)
    cerebro.broker.set_shortcash(False)
    cerebro.broker.set_coo(True)
    cerebro.broker.setcommission(commission=0, margin=False)
    cerebro.addsizer(SimpleSizer, percents=99)

    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: 34851211.71

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 [$]              34851211.709695
Equity Peak [$]               36036465.624429
Commissions [$]                           0.0
Cum Return [%]                      3385.1212
Return (Ann.) [%]                     15.2939
Volatility (Ann.) [%]                 28.2518
CAGR [%]                                 10.3
Sharpe Ratio                           0.6555
Skew                                   -3.586
Kurtosis                             116.5671
Smart Sharpe Ratio                    -0.9693
Sortino Ratio                          0.9118
VWR Ratio                              5.1497
Calmar Ratio                           0.1977
Recovery factor [%]                     5.973
Max. Drawdown [%]                    -77.3588
Avg. Drawdown [%]                      -5.124
Max. Drawdown Duration     1679 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.1697
Expectancy [%]                         0.0735
SQN                                    2.3002
Kelly Criterion [%]                   39.0365
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

Project details


Download files

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

Source Distribution

backtrader_next-2.1.0.tar.gz (6.1 MB view details)

Uploaded Source

Built Distribution

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

backtrader_next-2.1.0-py3-none-any.whl (471.1 kB view details)

Uploaded Python 3

File details

Details for the file backtrader_next-2.1.0.tar.gz.

File metadata

  • Download URL: backtrader_next-2.1.0.tar.gz
  • Upload date:
  • Size: 6.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for backtrader_next-2.1.0.tar.gz
Algorithm Hash digest
SHA256 935b16e15732d6b7fb6e8573e26c51ae53f162c1623a954e43afceea526d2818
MD5 737fabfe564fd4cd93fb9d1250dd1014
BLAKE2b-256 5899a9894e2b78f16588098e44fba0f079e51c6d9c657744ba960e0c8ba6b64a

See more details on using hashes here.

File details

Details for the file backtrader_next-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: backtrader_next-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 471.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for backtrader_next-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c0c9b505b8e213f28f8cae524aa71bd9d9dd31cc7e5fe36b46d075508d64dc5b
MD5 27dff9f72707f5ca919aedc13a497401
BLAKE2b-256 1e0eb7343003f48afd307a6a6afb8577f78fdb6b3965cdbde734378ed7401129

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page