AlphaFlow
AlphaFlow is a Python-based, event-driven backtesting framework designed for professional-grade trading research and strategy development. Built on a robust pub-sub architecture, AlphaFlow provides a flexible, high-performance environment for quantitative analysts and algorithmic traders.
Vision: Offer a "batteries included" backtesting experience leveraging the simplicity of Python, while also enabling unlimited customization and optimization using an event-driven architecture that can support components written in any language.
Table of Contents
Key Features
-
Event-Driven Core
Uses a publish-subscribe (pub-sub) architecture to simulate market data, order placements, and trade executions in a realistic, decoupled manner. -
Commission Tracking
Built-in commission handling for realistic transaction costs. -
Multi-Asset Support
Initially focused on stocks & ETFs with daily or intraday data, but built to extend to futures, forex, cryptocurrencies, and options in future releases. -
Performance-Oriented
Planned message queue integration which will enable optimization of speed-critical components (like indicator calculations on large datasets). -
Extendable & Modular
- Swap out data sources (CSV, APIs, real-time feeds).
- Plugin-style architecture for custom brokers, strategies, analytics, and risk management.
- Components are planned to be made language agnostic in a future release (v1).
- A solid foundation for live trading integration in a future version (v1).
-
Professional-Grade Analytics
- Built-in performance metrics (Sharpe, Sortino, drawdown, annualized returns).
- Ongoing support for event-based analytics and reporting modules.
Why AlphaFlow?
-
Maintainable & Modern
Many legacy libraries are no longer actively maintained or don’t follow best practices. AlphaFlow focuses on code quality, modular design, and clear APIs. -
Powerful & Future-Proof
By embracing an event-driven architecture, you get fine-grained control over every aspect of your trading simulation. The transition to real-time or live trading is also more natural compared to purely vectorized solutions. -
Commission-Aware Backtesting
Built-in commission tracking ensures realistic transaction costs are accounted for in your strategy performance. -
Performance Upgrades
Future Rust integration will offload compute-heavy tasks, enabling large-scale backtests without major slowdowns or memory bottlenecks. -
Community & Extensibility
Built to be plugin-friendly, allowing the community to add new data feeds, brokers, analytics modules, and advanced features without modifying the core.
High-Level Architecture
1. EventBus (Pub-Sub)
- The heart of AlphaFlow.
- Components (DataFeed, Strategy, Broker, Portfolio, Analytics) subscribe to and publish events.
- Ensures a loose coupling: each module only needs to know how to react to specific event types.
2. DataFeed
- Responsible for providing market data (historical or real-time).
- Publishes MarketDataEvents (price bars, ticks, earnings, news, etc.) to the EventBus.
- Can support multiple timeframes (daily, intraday, tick data in v2).
3. Strategy
- Subscribes to MarketDataEvents from the DataFeed.
- Generates trading signals and publishes
OrderEventsto the Broker. - Can also subscribe to Portfolio updates if needed (to track position sizing, risk limits, etc.).
4. Broker (Execution Engine)
- Subscribes to
OrderEventsfrom the Strategy. - Simulates fills (partial or full) and slippage, calculates commissions, and publishes
FillEvents. - Centralizes order handling logic, making it easy to swap in a real-time broker later.
5. Portfolio
- Subscribes to
FillEventsto track positions, cash balances, and profit/loss. - Optionally publishes portfolio updates (like margin calls, risk alerts) to other modules.
6. Analytics
- Subscribes to relevant events (MarketData, FillEvents, or PortfolioUpdates) to compile performance metrics, visualize PnL curves, or generate custom reports.
- Encourages real-time or post-backtest reporting, ideal for quick iteration.
Getting Started
-
Install AlphaFlow
pip install alphaflow
-
Basic Example
from datetime import datetime from alphaflow import AlphaFlow from alphaflow.brokers import SimpleBroker from alphaflow.data_feeds import PolarsDataFeed from alphaflow.strategies import BuyAndHoldStrategy # 1. Initialize AlphaFlow flow = AlphaFlow() flow.set_cash(100000) flow.set_backtest_start_timestamp(datetime(1990, 2, 10)) flow.set_backtest_end_timestamp(datetime(2025, 1, 5)) # 2. Create DataFeed (e.g., CSV-based daily bars) flow.set_data_feed( PolarsDataFeed( df_or_file_path="historical_data.csv", ) ) # 3. Set Equity Universe flow.add_equity("BND") flow.add_equity("SPY") # 4. Initialize Strategy flow.add_strategy( BuyAndHoldStrategy( symbol="SPY", target_weight=0.9 ) ) flow.add_strategy( BuyAndHoldStrategy( symbol="BND", target_weight=0.1 ) ) # 5. Create Broker flow.set_broker(SimpleBroker()) # 6. Run the backtest flow.run()
-
Monitor Results
- Use built-in analytics or your own custom module to generate metrics and charts.
- Check logs to see partial fills, order details, and event flows.
Contributing
We welcome contributions from the community! To get started:
- Fork the repository and create a new branch.
- Implement or fix a feature.
- Submit a pull request describing your changes.
License
AlphaFlow is released under the MIT License. See LICENSE for details.
Contact & Community
Thank you for choosing AlphaFlow! We’re excited to see what you’ll build.
Metadata
Release files for alphaflow 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| alphaflow-0.3.0.tar.gz | 43.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| alphaflow-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 79.2 kB
Release files / alphaflow-0.3.0.tar.gz
| Download URL | alphaflow-0.3.0.tar.gz |
|---|---|
| Size | 43.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
08fca1c9613677f6a2ffbb6d212ca7010c6826922ea77a02df88b4ac7082228e
|
|
BLAKE2b-256 checksum How to use checksums |
9e4ecd572bb0f3e7b2a89b04aed6067dd05b1b08ecf19d65dd75d24f81da196d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 26, 2025.
Transparency logRelease files / alphaflow-0.3.0-py3-none-any.whl
| Download URL | alphaflow-0.3.0-py3-none-any.whl |
|---|---|
| Size | 35.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8ab68b687ebf804c44353f561f9145aa58adadc24f398de87c6db2218675ecd3
|
|
BLAKE2b-256 checksum How to use checksums |
0d476c8a50da0a5bb26addc3b5616317c9e8f02ba19913140f04001724a7624e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 26, 2025.
Transparency log