A flexible backtesting framework for Python designed for quantitative trading strategy development and testing.
npx @tessl/cli install tessl/pypi-bt@1.1.0A flexible backtesting framework for Python designed for quantitative trading strategy development and testing. bt uses a tree-structured approach with modular algorithmic components (Algos) for building complex trading strategies that are easily testable, reusable, and flexible.
pip install btimport btCommon import patterns:
# Core framework classes
from bt import Strategy, Backtest, Security, Algo, AlgoStack
# Algorithm components
from bt.algos import SelectAll, WeighEqually, Rebalance, RunMonthly
# Backtesting utilities
from bt import runimport bt
import pandas as pd
# Load sample data (bt integrates with ffn for data retrieval)
data = bt.get('SPY,TLT', start='2010-01-01', end='2020-01-01')
# Define a simple monthly rebalancing strategy
strategy = bt.Strategy('EqualWeight',
[bt.algos.RunMonthly(),
bt.algos.SelectAll(),
bt.algos.WeighEqually(),
bt.algos.Rebalance()])
# Create and run backtest
backtest = bt.Backtest(strategy, data)
result = bt.run(backtest)
# Display results
result.display()
result.plot()bt's tree-structured design enables modular strategy construction:
This modular design allows complex strategies to be built from simple, reusable components while maintaining clear separation of concerns between data management, strategy logic, and execution.
Fundamental building blocks for creating strategies and securities within bt's tree structure, including base classes, strategy containers, and security types.
class Node:
def __init__(self, name: str, parent=None, children=None): ...
def setup(self, universe, **kwargs): ...
def update(self, date, data=None, inow=None): ...
class Strategy(StrategyBase):
def __init__(self, name: str, algos=None, children=None, parent=None): ...
def run(self): ...
class Security(SecurityBase):
def __init__(self, name: str, multiplier=1, lazy_add=False): ...Comprehensive library of 50+ algorithmic building blocks for strategy logic, organized into categories including execution control, security selection, weighting, portfolio management, and risk management.
class Algo:
def __init__(self, name=None): ...
def __call__(self, target): ...
class AlgoStack(Algo):
def __init__(self, *algos): ...
# Key algorithm examples
class RunMonthly(RunPeriod): ...
class SelectAll(Algo): ...
class WeighEqually(Algo): ...
class Rebalance(Algo): ...Core backtesting functionality for combining strategies with data, executing backtests, and analyzing results with comprehensive statistics and visualization capabilities.
class Backtest:
def __init__(self, strategy, data, name=None, initial_capital=1000000.0,
commissions=None, integer_positions=True, progress_bar=False,
additional_data=None): ...
def run(self): ...
def run(*backtests): ...
class Result(ffn.GroupStats):
def display_monthly_returns(self, backtest=0): ...
def plot_weights(self, backtest=0, filter=None, figsize=(15, 5), **kwds): ...Advanced security types for specialized use cases including fixed income securities, hedge securities, and coupon-paying instruments with custom valuation and cash flow handling.
class FixedIncomeSecurity(SecurityBase): ...
class HedgeSecurity(SecurityBase): ...
class CouponPayingSecurity(FixedIncomeSecurity): ...
class CouponPayingHedgeSecurity(CouponPayingSecurity): ...
class FixedIncomeStrategy(Strategy): ...bt integrates seamlessly with the ffn financial function library for data retrieval and manipulation:
# Re-exported from ffn
def data(*args, **kwargs): ...
def get(*args, **kwargs): ...
def merge(*args, **kwargs): ...
utils # Module re-exported from ffn__version__: str = "1.1.2"
PAR: float = 100.0 # Par value constant
TOL: float = 1e-16 # Tolerance for floating point comparisons