Skip to main content

A short description of the alphatest project.

Project description

AlphaTest Logo

AlphaTest: Quantitative Strategy Backtester

PyPI version Python versions License

AlphaTest is a lightweight, vectorized Python framework designed for backtesting quantitative trading strategies. It supports multi-asset portfolios, realistic transaction cost modeling, and benchmarking against both Equity and Cryptocurrency indices.
It is designed to ingest alpha signals (DataFrames of position weights or raw signals) and compute rigorous performance metrics and visualizations.

Key Features

  • Vectorized Performance Analysis: Leverages pandas and numpy for fast, element-wise operations on large datasets.
  • Hybrid Benchmarking: Built-in support for comparing strategies against:
    • Equities: S&P 500 (VOO), Gold (GLD) — (Annualized: 252 days).
    • Crypto: Bitcoin (BTC) — (Annualized: 365 days).
  • Comprehensive Risk Metrics: Automatically computes Sharpe, Sortino, Calmar, Maximum Drawdown, CAGR, Profit Factor, and Turnover.
  • Cost Modeling: Simulates trading fees based on daily turnover to produce net-of-fee returns.
  • Visualization: Generates dual-panel plots showing Log-Scale Cumulative Returns and Rolling Drawdowns.
  • Signal Distribution: Helper methods to analyze the statistical distribution of alpha signals using the Freedman-Diaconis rule for optimal binning.

Dependencies

  • Python 3.x
  • pandas
  • numpy
  • matplotlib

Directory Structure & Data Requirements

The backtest class relies on a specific file system structure to locate your asset data and benchmark data.

File System Layout

project_root/  
│  
├── src/  
│   ├── alphatest.py         \# This package  
│   ├── util.py              \# Must contain 'cross\_sectional' class (imported as cs)  
│  
├── data/                \# Your primary data\_path  
│   ├── returns.csv      \# Required: Asset returns data  
│   ├── benchmarks/          \# Hardcoded benchmark path (relative to data\_path parent)  
│   │      └── returns.csv      \# Must contain columns: 'BTC', 'VOO', 'GLD'
│   └── ...              \# Other feature files (e.g. volume.csv)  
│  

Data Format (.csv)

Input files must contain a time column to be parsed as the index.

time,Asset_A,Asset_B,Asset_C  
2023-01-01,0.01,-0.02,0.005  
2023-01-02,0.005,0.01,-0.01  
...

Usage

1. Initialization

Initialize the backtester by pointing it to your data directory. You can customize risk-free rates, trading fees, and trading frequency.

from alphatest import backtest  
import pandas as pd

# Initialize the engine  
bt = backtest(  
    data_path="./data",  
    annual_days=365,          \# 365 for Crypto, 252 for TradFi  
    risk_free_return=0.03,    \# 3% Risk Free Rate  
    trading_fee=0.00025       \# 2.5 bps taker fee  
)

2. Running a Backtest

Pass a DataFrame of alpha signals to the run method.

  • Input: A DataFrame where columns are assets and index is time.
  • Scaling: By default, run will neutralize and scale your signals (using scale_final=True).
# Load or generate your alpha dataframe  
# Shape: (Time, Assets)  
alpha_df = pd.read_csv("my_signals.csv", index_col=0, parse_dates=True)

# Execute backtest  
metrics = bt.run(alpha_df, scale_final=True)

# 'metrics' is a dictionary containing the results  
print(f"Strategy Sharpe: {metrics['sharpe']:.2f}")

3. Visualizing Alpha Distribution

Check the statistical properties of your raw signal before backtesting.

bt.get_distribution(alpha_df)

Metrics Glossary

The compute_metrics method calculates the following statistics:

Metric Description
CAGR (Annual Return) Geometric annual return compounded over the period.
Annual Volatility Standard deviation of returns * sqrt(annual_ticks).
Sharpe Ratio (CAGR - RiskFree) / Annual Volatility.
Sortino Ratio (CAGR - RiskFree) / Downside Deviation.
Calmar Ratio (CAGR - RiskFree) / abs(Max Drawdown).
Max Drawdown The largest peak-to-trough decline in portfolio value.
Profit Factor Gross Profits / Gross Losses.
Turnover Mean daily portfolio turnover (percentage).

Configuration Options

Parameter Default Description
annual_days 365 Trading days per year (Use 252 for Stocks).
daily_ticks 1 Observations per day (e.g., 24 for hourly).
risk_free_return 0.03 Annualized risk-free rate (decimal).
trading_fee 0.00025 Transaction cost per trade (decimal).
crypto_benchmarks [``BTC"] List of crypto ticker columns in benchmark file.
stock_benchmarks [VOO", GLD"] List of stock ticker columns in benchmark file.

Notes

  • Scaling: The engine assumes the presence of a .util module with a cross_sectional class for signal neutralization/scaling.
  • Returns: The engine uses Geometric Compounding for cumulative returns and Simple Returns for daily input data.

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

alphatest-0.1.1.tar.gz (1.3 MB view details)

Uploaded Source

Built Distribution

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

alphatest-0.1.1-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file alphatest-0.1.1.tar.gz.

File metadata

  • Download URL: alphatest-0.1.1.tar.gz
  • Upload date:
  • Size: 1.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for alphatest-0.1.1.tar.gz
Algorithm Hash digest
SHA256 55bcb6248983cbd94012404f72bf32580e8e457a92ab6fb561e2fd0aff14efb5
MD5 04f8e2ac3e106f936c3d8410e2524b18
BLAKE2b-256 153526e155f75575d87be4e09fe3904b3d78fd0ba4da931338c86855ed5a13b9

See more details on using hashes here.

File details

Details for the file alphatest-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: alphatest-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for alphatest-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7e2b74c6b82de8853acb058286b2c487777ef92177e5177df8d949c377630fe7
MD5 ba7c33d968751ea5048f44ce460dfead
BLAKE2b-256 b29962da5f7f0b8f3ecfce9a84eeb61b63f64fc24d4eba234b4d54bc3c68dc99

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