Fast minimalist vector-based backtesting for perpetual futures.
Project description
alphavec
Disclaimer
The content provided in this project is for informational purposes only and does not constitute financial advice. This information should not be construed as professional financial advice, and it is recommended to consult with a qualified financial advisor before making any financial decisions.
No liability is accepted for any losses or damages incurred as a result of acting or refraining from action based on the information provided in this project. Use this information at your own risk.
$$\ $$\
$$ | $$ |
$$$$$$\ $$ | $$$$$$\ $$$$$$$\ $$$$$$\ $$\ $$\ $$$$$$\ $$$$$$$\
\____$$\ $$ |$$ __$$\ $$ __$$\ \____$$\\$$\ $$ |$$ __$$\ $$ _____|
$$$$$$$ |$$ |$$ / $$ |$$ | $$ | $$$$$$$ |\$$\$$ / $$$$$$$$ |$$ /
$$ __$$ |$$ |$$ | $$ |$$ | $$ |$$ __$$ | \$$$ / $$ ____|$$ |
\$$$$$$$ |$$ |$$$$$$$ |$$ | $$ |\$$$$$$$ | \$ / \$$$$$$$\ \$$$$$$$\
\_______|\__|$$ ____/ \__| \__| \_______| \_/ \_______| \_______|
$$ |
$$ |
\__|
Alphavec is a lightning fast, minimalist, cost-aware vectorized backtest engine inspired by the guys at RobotWealth.
The backtest input is the natural output of a typical quant research process - a time series of portfolio weights. You simply provide a dataframe of weights and a dataframe of close prices and order prices, along with some optional cost parameters and the backtest returns a streamlined performance report with insight into the key metrics.
alphavec has first-class support for simulating perptual futures strategies using a small, fast, verifiable simulation core.
Rationale
Alphavec is an antidote to the various bloated and complex backtest frameworks.
To validate ideas all you really need is...
weights * returns.shift(-1)
The goal was to add just enough extra complexity to this basic formula to support sound development of cost-aware systematic trading strategies.
Install
Requires Python >=3.12
pip install alphavec
- From source:
python3 -m venv .venv./.venv/bin/pip install -e .
- For development:
./.venv/bin/pip install -e ".[dev]"
Usage
Considerations
- Simulates cross‑margin with unlimited leverage and borrowing (no liquidations or margin calls).
- Orders execute at
order_pricesplus slippage and fees. - Funding applies per period using signed
funding_ratesand close notional. - NaNs in
order_pricesandclose_pricesimply the asset is not tradable that period; prices are carried forward for valuation. - Positions will always be closed if target weight is zero, regardless of minimum notional filter.
- NaNs in
funding_ratesare treated as 0, and funding is always 0 whenclose_pricesis NaN.
Simulation
simulate() runs a cross‑margin perpetual futures backtest from target portfolio weights.
Inputs:
weights: pandasDataFrameorSerieswith aDatetimeIndex. Values are decimal target weights (1.0 = 100% notional). Positive = long, negative = short. Weights may sum above 1 for leverage.close_prices,order_prices,funding_rates(optional): same shape/index/columns asweights.
Returns:
returns: period returns as a pandasSeries.metrics: key performance metrics as a pandasDataFramewithValueandNotecolumns.
Example:
import pandas as pd
from alphavec import simulate, tearsheet
weights = pd.DataFrame({"BTC": [1, 1, 1]}, index=pd.date_range("2024-01-01", periods=3, freq="1D"))
close_prices = pd.DataFrame({"BTC": [100, 105, 110]}, index=weights.index)
order_prices = close.shift(1).fillna(close.iloc[0])
returns, metrics= simulate(
weights=weights,
close_prices=close_prices,
order_prices=order_prices,
funding_rates=funding_rates,
benchmark_asset="BTC",
order_notional_min=10,
fee_pct=0.00025, # 2.5 bps per trade
slippage_pct=0.001, # 10 bps per trade
init_cash=10_000,
freq_rule="1D",
trading_days_year=365,
risk_free_rate=0.03,
)
html = tearsheet(metrics=metrics, returns=returns, output_path="tearsheet.html")
Tearsheet Example
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file alphavec-0.1.2.tar.gz.
File metadata
- Download URL: alphavec-0.1.2.tar.gz
- Upload date:
- Size: 19.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6ac06c5728f0642a3eb4c834fed338c722a42597e01e26d8a7ad9e637804f203
|
|
| MD5 |
d15cf3627811f3889557cdac4ceb6091
|
|
| BLAKE2b-256 |
269a782c03aa5ba2181e3a5ad764816d34db9cbf100c7cb575fa8454193b0330
|
Provenance
The following attestation bundles were made for alphavec-0.1.2.tar.gz:
Publisher:
python-publish.yml on breaded-xyz/alphavec
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
alphavec-0.1.2.tar.gz -
Subject digest:
6ac06c5728f0642a3eb4c834fed338c722a42597e01e26d8a7ad9e637804f203 - Sigstore transparency entry: 762155008
- Sigstore integration time:
-
Permalink:
breaded-xyz/alphavec@5c7d820875d260c3f7d4c6a3c988c32b0b3a4848 -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/breaded-xyz
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@5c7d820875d260c3f7d4c6a3c988c32b0b3a4848 -
Trigger Event:
release
-
Statement type:
File details
Details for the file alphavec-0.1.2-py3-none-any.whl.
File metadata
- Download URL: alphavec-0.1.2-py3-none-any.whl
- Upload date:
- Size: 15.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
39a35d1b385bbbfb4cb0c57f558b5b58f7666258db92b520ca3e2c69704af3cf
|
|
| MD5 |
3032c84f586035decd8b8fdd993e998b
|
|
| BLAKE2b-256 |
12618d5abc523382e50703ad01766589d412e2f670e1b4cad41d2b653d6ad161
|
Provenance
The following attestation bundles were made for alphavec-0.1.2-py3-none-any.whl:
Publisher:
python-publish.yml on breaded-xyz/alphavec
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
alphavec-0.1.2-py3-none-any.whl -
Subject digest:
39a35d1b385bbbfb4cb0c57f558b5b58f7666258db92b520ca3e2c69704af3cf - Sigstore transparency entry: 762155010
- Sigstore integration time:
-
Permalink:
breaded-xyz/alphavec@5c7d820875d260c3f7d4c6a3c988c32b0b3a4848 -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/breaded-xyz
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@5c7d820875d260c3f7d4c6a3c988c32b0b3a4848 -
Trigger Event:
release
-
Statement type: