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

  • Code formatting and whitespace cleanup with ruff/black
  • Removed label field from Leg dataclass
  • Bumped collation size limit from 100MB to 1000MB
  • Dropped Vortex loader test coverage (see #13)

v0.8.0

  • NEW: Added statistical testing skill v1.0-alpha with comprehensive test scripts
    • Includes hypothesis testing for trading strategy validation
    • Supports benchmark comparison, conditional analysis, and temporal validation
    • Added out-of-sample testing capabilities
  • ENHANCED: Simulation module improvements
    • Refactored generators into Time and Sequence engines
    • Added IID lognormal distribution support
    • New generator modes and parameters for more flexible simulation
    • Fixed price stagnation in tick/quote generators
  • Added regression tests for tick generator price movement
  • Updated documentation for simulation module with new examples
  • Code quality improvements with pre-commit hooks

v0.7.0 (BREAKING)

  • BREAKING: Migrated to Pydantic v2.0 compatibility (requires pydantic>=2.0.0)
  • Updated all BaseModel classes to use model_config instead of deprecated Config class
  • Replaced root_validator with model_validator(mode='before')
  • Added default values for Optional fields as required by Pydantic v2
  • Enhanced option chain simulation capabilities
  • Added correlated data simulation features
  • Improved URL pattern matching functionality
  • Code formatting and linting improvements with ruff and black
  • Added load-data skill for efficient data discovery and loading (includes peek_file, efficient_load, collate_data, and normalize_json)

v0.6.0

  • New methods added to TradeBook object
  • mtm - to calculate mtm for open positions
  • clear - to clear the existing entries
  • helper attributes for positions
  • order_fill_price method added to utils to simulate order quantity

v0.5.1

  • Simple bug fixes added

v0.5.0

  • OptionExpiry class added to calculate option payoffs based on expiry

v0.4.0

  • Brokers module deprecation warning added
  • Options module revamped

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.9.0.tar.gz (4.9 MB view details)

Uploaded Source

Built Distribution

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

fastbt-0.9.0-py2.py3-none-any.whl (4.1 MB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: fastbt-0.9.0.tar.gz
  • Upload date:
  • Size: 4.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for fastbt-0.9.0.tar.gz
Algorithm Hash digest
SHA256 6dd3faa624597f30f846314bc9d5fbdd7a14be6222d068dc08f3ea016166b525
MD5 48b3a90230d9a411bc4b1870fa48adc0
BLAKE2b-256 9399204d178952e1d840ee0b069708edd8e17323538f0716f637ca502bb291a6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastbt-0.9.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 4.1 MB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for fastbt-0.9.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c729efaeeb42e11cb0b189982e2eff311d81c380468706b94fc1d4adeed3b4c5
MD5 b778984d526b1890d0394e1a5ad50f5d
BLAKE2b-256 09f72ef37cd11e8460c6d322b8abefd4696ca6018115b1b6569ae220aac30c00

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