Skip to main content

AlphaFlow is a Python-based, event-driven backtesting framework designed for professional-grade trading research and strategy development.

Project description

AlphaFlow

CI Python Versions License: MIT

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

  1. Key Features
  2. Why AlphaFlow?
  3. High-Level Architecture
  4. Getting Started
  5. Contributing
  6. License

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?

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

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

  3. Commission-Aware Backtesting
    Built-in commission tracking ensures realistic transaction costs are accounted for in your strategy performance.

  4. Performance Upgrades
    Future Rust integration will offload compute-heavy tasks, enabling large-scale backtests without major slowdowns or memory bottlenecks.

  5. 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 OrderEvents to the Broker.
  • Can also subscribe to Portfolio updates if needed (to track position sizing, risk limits, etc.).

4. Broker (Execution Engine)

  • Subscribes to OrderEvents from 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 FillEvents to 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

  1. Install AlphaFlow

    pip install alphaflow
    
  2. Basic Example

    from datetime import datetime
    
    import alphaflow as af
    
    # 1. Initialize AlphaFlow
    flow = af.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(
        af.data_feeds.CSVDataFeed(
            file_path="historical_data.csv",
        )
    )
    
    # 3. Set Equity Universe
    flow.add_equity("BND")
    flow.add_equity("SPY")
    
    # 4. Initialize Strategy
    flow.add_strategy(
        af.strategies.BuyAndHoldStrategy(
            symbol="SPY",
            target_weight=0.9
        )
    )
    flow.add_strategy(
        af.strategies.BuyAndHoldStrategy(
            symbol="BND",
            target_weight=0.1
        )
    )
    
    # 5. Create Broker
    flow.set_broker(
        af.brokers.SimpleBroker()
    )
    
    # 6. Run the backtest
    flow.run()
    
  3. 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:

  1. Fork the repository and create a new branch.
  2. Implement or fix a feature.
  3. 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.

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

alphaflow-0.1.1.tar.gz (27.9 kB view details)

Uploaded Source

Built Distribution

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

alphaflow-0.1.1-py3-none-any.whl (23.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: alphaflow-0.1.1.tar.gz
  • Upload date:
  • Size: 27.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for alphaflow-0.1.1.tar.gz
Algorithm Hash digest
SHA256 219af86523f77c0b354d0dc11a840c3a69d708efbe6d83995e21e061b3042c50
MD5 8b648442070287479855b35cd84a05f7
BLAKE2b-256 89327a92dd8ce8eed52fce8574192dd5e5c35608cf374a225ba1bb080dd16118

See more details on using hashes here.

Provenance

The following attestation bundles were made for alphaflow-0.1.1.tar.gz:

Publisher: release.yml on brandonschabell/alphaflow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: alphaflow-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 23.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for alphaflow-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7e950d8b72376dd3dc583b10ee1250eea3778d4580d2932c0bc09c17eb7d032f
MD5 ae5334624acf6f2993cb1ba5ec60b2e1
BLAKE2b-256 445099ca63e2dc357df391f4a7638400711ccaec8973901d40f61a007a20703a

See more details on using hashes here.

Provenance

The following attestation bundles were made for alphaflow-0.1.1-py3-none-any.whl:

Publisher: release.yml on brandonschabell/alphaflow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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