VectorBT

0.28.5 · active · verified Thu Apr 16

VectorBT is a Python library for backtesting and analyzing trading strategies at scale. It leverages NumPy and Numba for high-performance vectorized computations, enabling rapid testing of numerous strategy configurations and assets. The library integrates with pandas for data handling and Plotly for interactive visualizations. It is currently at version 0.28.5 and receives active development and maintenance.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to download historical price data, simulate a basic buy-and-hold strategy, and then implement a simple moving average crossover strategy with entry/exit signals using `vectorbt`.

import vectorbt as vbt
import pandas as pd

# Download historical daily price data for Bitcoin (BTC-USD)
# Use a specific date range for reproducibility
price = vbt.YFData.download(
    'BTC-USD',
    start=pd.Timestamp('2020-01-01', tz='UTC'),
    end=pd.Timestamp('2021-01-01', tz='UTC')
).get('Close')

# Simulate a simple buy-and-hold portfolio with $100 initial cash
pf = vbt.Portfolio.from_holding(price, init_cash=100)

# Calculate total profit
profit = pf.total_profit()
print(f"Total profit from buy-and-hold: ${profit:.2f}")

# Calculate a simple moving average crossover strategy
fast_ma = vbt.MA.run(price, 10)
slow_ma = vbt.MA.run(price, 50)

# Generate entry and exit signals
entries = fast_ma.ma_crossed_above(slow_ma)
exits = slow_ma.ma_crossed_above(fast_ma)

# Backtest the MA crossover strategy
pf_ma_crossover = vbt.Portfolio.from_signals(
    price, entries, exits, init_cash=100, fees=0.001
)

# Print strategy statistics
print("\nMA Crossover Strategy Statistics:")
print(pf_ma_crossover.stats())

view raw JSON →