or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

algorithm-components.mdbacktesting-engine.mdcore-framework.mdindex.mdspecialized-securities.md
tile.json

tessl/pypi-bt

A flexible backtesting framework for Python designed for quantitative trading strategy development and testing.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/bt@1.1.x

To install, run

npx @tessl/cli install tessl/pypi-bt@1.1.0

index.mddocs/

bt

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

Package Information

  • Package Name: bt
  • Package Type: pypi
  • Language: Python
  • Installation: pip install bt
  • Documentation: http://pmorissette.github.io/bt

Core Imports

import bt

Common 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 run

Basic Usage

import 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()

Architecture

bt's tree-structured design enables modular strategy construction:

  • Node: Base building block for the tree structure, providing price tracking and hierarchy management
  • Strategy: Tree node that defines strategy logic through algorithmic stacks
  • Security: Tree node representing individual securities with position tracking
  • Algo: Individual algorithmic building blocks that can be combined into stacks
  • AlgoStack: Container for multiple algorithms executed sequentially
  • Backtest: Combines strategy with data to produce results

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.

Capabilities

Core Framework Classes

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): ...

Core Framework

Algorithm Components

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): ...

Algorithm Components

Backtesting Engine

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): ...

Backtesting Engine

Specialized Securities

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): ...

Specialized Securities

Data Integration

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

Constants

__version__: str = "1.1.2"
PAR: float = 100.0  # Par value constant
TOL: float = 1e-16  # Tolerance for floating point comparisons