Skip to main content

Simple trading backtester

Overview

Quantitative approach to trading is done via applying mathematical models to various financial instruments. In order to get money for you strategy, mathematical model beneath it should be sound. And to prove that this model worth money one should do proper backtesting. This project aims to provide easy and straitforward backtesting solution.

Relevance

There are number of python projects for backtesting: backtrader, pyalgotrade, zipline, rqalpha, etc.. When i was trying out them, i was dissatisfied with one or more of the following: event driven, unnecessary complex architecture, no support for trading multiple instruments in convinient way, no proper performance evaluation, etc.. This project solves those issues at cost of not so wide functionality compared to mentioned ones above. Project is designed to be easily build on top of it.

Features

  • Data manipulations are made with pandas.

  • Backtesting operations are vector( no loops, not event driven).

  • Extensive statistical evaluation of strategies.

  • Number of visualizations embedded.

  • Strategy robustness tests.

  • API to work with OHLC data( download, prepare).

  • Clean and straitforward project structure.

  • PEP8 compliant code.

Installation

  • Install via setup.py:

git clone git@github.com:bluella/stbt.git
cd stbt
python setup.py install
  • Install via pip:

pip install stbt
  • Run tests:

pip install pytest
pytest

Usage

import datetime as dt
import pandas as pd
import matplotlib.pyplot as plt
from stbt.simulator import Strategy
from stbt.download_ohlc.cryptocurrency import get_ohlc_cryptocompare
from stbt.operators.technical import skewness

# get trading data from cryptocompare
BTC_TICKER = 'BTC'
ETH_TICKER = 'ETH'
USD_TICKER = 'USD'

END_DATE = dt.datetime(2018, 7, 1, 0, 0, 0)
START_DATE = dt.datetime(2018, 3, 1, 0, 0, 0)

OHLC_BTC = get_ohlc_cryptocompare(BTC_TICKER, USD_TICKER, START_DATE,
                                end_date=END_DATE, interval_key='day')
OHLC_ETH = get_ohlc_cryptocompare(ETH_TICKER, USD_TICKER, START_DATE,
                                end_date=END_DATE, interval_key='day')

# create dfs in format that Strategy requires
closes_df = pd.concat([OHLC_BTC['Close'], OHLC_ETH['Close']],
                      axis=1, keys=['BTC', 'ETH'])

# use imported indicator to create weights
weights_df = skewness(closes_df)

# create strategy
s = Strategy(closes_df, weights_df, cash=100)

# run backtest, robust tests, calculate stats
s.run_all(delay=2, verify_data_integrity=True, instruments_drop=None,
        commissions_const=0, capitalization=False, plot='all')

# check strategy stats
print(s.stats_dict)

# save strategy to futher comparison
s.add_to_pnls_pool()

# plot pool correlation heatmap
heatmap_fig, corr_matrix = s.get_pool_heatmap()

plt.show()

Futher development

  • Improve test coverage.

  • More API for data download.

  • More technical indicators.

  • Portfolio optimization tools.

Releases

See CHANGELOG.

License

This project is licensed under the MIT License - see the LICENSE for details.

Metadata

Release files for stbt 1.0.0

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

Source distribution (sdist)

Source distribution for stbt 1.0.0
File Size Uploaded Provenance Actions
stbt-1.0.0.tar.gz 56.7 kB Without provenance Details for stbt-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for stbt 1.0.0
File Interpreter ABI Platform Actions
stbt-1.0.0-py2.py3-none-any.whl Python 2, Python 3 none any Details for stbt-1.0.0-py2.py3-none-any.whl

Total release size: 76.1 kB

Release files / stbt-1.0.0.tar.gz

Download URL stbt-1.0.0.tar.gz
Size 56.7 kB
Tags Source
SHA-256 checksum
How to use checksums
bc26730406c66dd3fb1c11776307df67dc89dcd048b4ce993bdaa75b3d47f2ea
BLAKE2b-256 checksum
How to use checksums
0f8c8ea38ea1695c1f9e3a4ee78ade49f36786edaba1e32a93a6492f2656b605
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.3.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

Release files / stbt-1.0.0-py2.py3-none-any.whl

Download URL stbt-1.0.0-py2.py3-none-any.whl
Size 19.5 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
6661a9dc013b510de1a17ac7a22cf63d32fd6e1f1fb4b1722c488d7c1c3bfecf
BLAKE2b-256 checksum
How to use checksums
d35e6f5e8e1892d3151f7a22b35b7998b292932e77629fe86168c65985d22ad8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.3.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

Security for stbt 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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