Skip to main content

A simple framework for fast and dirty backtesting

Project description

Introduction

fastbt is a simple and dirty way to do backtests based on end of day data, especially for day trading. The main purpose is to provide a simple framework to weed out bad strategies so that you could test and improve your better strategies further.

It is based on the assumption that you enter into a position based on some pre-defined rules for a defined period and exit either at the end of the period or when stop loss is triggered. See the rationale for this approach and the built-in assumptions. fastbt is rule-based and not event-based.

If your strategy gets you good results, then check them with a full featured backtesting framework such as zipline or backtrader to verify your results. If your strategy fails, then it would most probably fail in other environments.

This is alpha

Most of the modules are stand alone and you could use them as a single file. See embedding for more details

Features

  • Create your strategies in Microsoft Excel
  • Backtest as functions so you can parallelize
  • Try different simulations
  • Run from your own datasource or a database connection.
  • Run backtest based on rules
  • Add any column you want to your datasource as formulas

Installation

fastbt requires python >=3.6 and can be installed via pip

pip install fastbt

Quickstart

Fastbt assumes your data have the following columns (rename them in case of other names)

  • timestamp
  • symbol
  • open
  • high
  • low
  • close
  • volume
from fastbt.rapid import *
backtest(data=data)

would return a dataframe with all the trades.

And if you want to see some metrics

metrics(backtest(data=data))

You now ran a backtest without a strategy! By default, the strategy buys the top 5 stocks with the lowest price at open price on each period and sells them at the close price at the end of the period.

You can either specify the strategy by way of rules (the recommended way) or create your strategy as a function in python and pass it as a parameter

backtest(data=data, strategy=strategy)

If you want to connect to a database, then

from sqlalchemy import create_engine
engine = create_engine('sqlite:///data.db')
backtest(connection=engine, tablename='data')

And to SELL instead of BUY

backtest(data=data, order='S')

Let's implement a simple strategy.

BUY the top 5 stocks with highest last week returns

Assuming we have a weeklyret column,

backtest(data=data, order='B', sort_by='weeklyret', sort_mode=False)

We used sort_mode=False to sort them in descending order.

If you want to test this strategy on a weekly basis, just pass a dataframe with weekly frequency.

See the Introduction notebook in the examples directory for an in depth introduction.

Embedding

Since fastbt is a thin wrapper around existing packages, the following files can be used as standalone without installing the fastbt package

  • datasource
  • utils
  • loaders

Copy these files and just use them in your own modules.

========= History

v0.3.0 (2019-03-15)

  • More helper functions added to utils
  • Tradebook class enhanced
  • A Meta class added for event based simulation

v0.2.0 (2018-12-26)

  • Backtest from different formats added
  • Rolling function added

v0.1.0. (2018-10-13)

  • First release on PyPI

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

fastbt-0.3.0.tar.gz (1.8 MB view details)

Uploaded Source

Built Distribution

fastbt-0.3.0-py2.py3-none-any.whl (177.5 kB view details)

Uploaded Python 2Python 3

File details

Details for the file fastbt-0.3.0.tar.gz.

File metadata

  • Download URL: fastbt-0.3.0.tar.gz
  • Upload date:
  • Size: 1.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.31.1 CPython/3.6.8

File hashes

Hashes for fastbt-0.3.0.tar.gz
Algorithm Hash digest
SHA256 e784713455fb4d5b9aee2e6d83ff38ee59f3aa7aab6b558025ba4edfaa4e9e76
MD5 7fb2130ee80bd3b823a0f633ece6debf
BLAKE2b-256 37b7bbc4f224fd0f2b8f55737f44fa9240f4e2a56efe227fb6359ee2c6463564

See more details on using hashes here.

File details

Details for the file fastbt-0.3.0-py2.py3-none-any.whl.

File metadata

  • Download URL: fastbt-0.3.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 177.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.31.1 CPython/3.6.8

File hashes

Hashes for fastbt-0.3.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6bc6d2eea807c460f8818ae21eb94167281897d1a66e1fc12b783bcb644e4d46
MD5 c874375e0887edf6a068dd9569d28ae4
BLAKE2b-256 92465dee3276d326b6e528b2f01955a3d2fd7051dc3c61409d5809ca38de431a

See more details on using hashes here.

Supported by

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