Skip to main content

Simple trading backtester

Project description


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 oportunities.


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, bugs, 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.


  • Data manipulations are made with pandas
  • Backtesting operations are vector(no loops, no event driven)
  • Extensive statistical evaluation of strategies
  • Number of visualizations embedded
  • Clean and straitforward project structure
  • Strategy robustness tests
  • OHLC data checking and preparation tools


  • Install via
git clone
cd stbt
python install
  • Install via pip:
pip install stbt
  • Run tests:
pip install pytest


import datetime as dt
import pandas as pd
import matplotlib.pyplot as plt
from Simple_trading_backtest.simulator import Strategy
import Simple_trading_backtest.helpers as hlp

# creating fake trading data

# prepare datetime index
some_date = dt.datetime(2017, 1, 1)
days = pd.date_range(some_date, some_date + dt.timedelta(30), freq='D')

# initialize close prices
data_values = list(range(1, 32))
closes_df = pd.DataFrame({'Date': days, 'inst1': data_values})
closes_df.set_index('Date', inplace=True)

# initialize weights
weights_values = [1 for i in range(31)]
weights_df = pd.DataFrame({'Date': days, 'inst1': weights_values})
weights_df.set_index('Date', inplace=True)

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

# check strategy stats

# save strategy to futher comparison

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

Futher development

  • Improve test coverage
  • API for data download
  • Technical indicators support
  • Portfolio optimization tools




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

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

stbt-0.0.7.tar.gz (47.7 kB view hashes)

Uploaded source

Built Distribution

stbt-0.0.7-py2.py3-none-any.whl (24.4 kB view hashes)

Uploaded py2 py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page