Skip to content

About

Algo Comparator

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

Β 

History

4 Commits

Folders and files

Repository files navigation

πŸŒ‹ AlphaForge

Python Version FastAPI Docker QuantConnect LEAN License

AlphaForge is an enterprise-grade, local-first algorithmic trading backtesting platform designed to execute and compare quantitative strategies side-by-side using the industrial-strength QuantConnect LEAN engine. It handles the entire lifecycle of a strategyβ€”from raw Python logic to containerized sandbox execution, real-time metrics parsing, and beautiful dual-strategy dashboard comparative analytics.

By utilizing local container execution, AlphaForge bypasses cloud-based paywalls, subscriptions, and restrictive APIs. Your intellectual property (strategy code) and data remain 100% on your machine, executing with real institutional backtesting fidelity.


πŸ” Key Highlights & Features

  • ⚑ Zero-Dependency Interactive Dashboard: An integrated, ultra-fast single-page interface with a responsive Dark-Dimmed theme. Features customized native HTML5 canvas charts for overlaid portfolio equity curves and metric breakdowns without external rendering framework dependencies.
  • πŸ›‘οΈ Hardened Sandbox Isolation: Every strategy runs within its own secure, isolated container built on quantconnect/lean:latest. Resources are strictly limited (--cpus 2.0 --memory 2g --network none) to prevent malicious activity or system resource exhaustion.
  • πŸ”„ Live Pipeline Streaming: Strategy execution states and backtest progress are streamed in real time to the browser via WebSockets over five pipeline stages: HYPOTHESIS βž” DATA_HUB βž” SIGNAL_DETECTION βž” PORTFOLIO_AND_RISK βž” EVALUATION.
  • πŸ“Š Autonomous Alternative Data Hub: Integrated with yfinance to automatically pull, downsample (daily vs. minute), feature-engineer, and cache alternative OHLCV datasets enriched with deterministic momentum, sentiment, and signal strength indicators.
  • 🧬 Heuristic Code Transpilation: Converts high-level, human-readable trading code into enterprise-grade QuantConnect boilerplate using custom AST and Regex-based code mappings.

πŸ—οΈ Architecture Design

AlphaForge is designed with an event-driven, micro-services oriented topology where the FastAPI gateway orchestrates ephemeral container work-loops.

                  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                  β”‚          Client (Browser UI)           β”‚
                  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–²β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜
                    WebSocket  β”‚                      β”‚
                  /ws/compare  β”‚                      β”‚ Submit Strategies
                               β”‚                      β–Ό
                  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                  β”‚            FastAPI Server              β”‚
                  β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
                  β”‚  β€’ Code Transpiler (AST/Regex Engine)  β”‚
                  β”‚  β€’ Data Hub Fetcher & Momentum Engine  β”‚
                  β”‚  β€’ Asynchronous Container Orchestrator β”‚
                  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜
                               β”‚                      β”‚
              Mount Data (Ro)  β”‚                      β”‚ Mount Workspaces (Rw)
                               β–Ό                      β–Ό
                  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                  β”‚     Local Data Hub     β”‚     β”‚ Ephemeral Workspaces   β”‚
                  β”‚  data/custom_sources/  β”‚     β”‚ projects/{strategy_id} β”‚
                  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                               β”‚                              β”‚
                               β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                              β”‚ Spawn Ephemeral Sandbox
                                              β–Ό
                               β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                               β”‚     Docker Engine API        β”‚
                               β”‚   quantconnect/lean:latest   β”‚
                               β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
                               β”‚  - CPU: 2.0  - Memory: 2G    β”‚
                               β”‚  - Network: None             β”‚
                               β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                              β”‚
                                              β”‚ writes results/<id>/output.json
                                              β–Ό
                               β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                               β”‚  Metrics Parser Engine       β”‚
                               β”‚  (Extracts CAGR, Sharpe,     β”‚
                               β”‚   Sortino, Drawdown, Equity) β”‚
                               β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Components Breakdown:

  1. FastAPI Gateway: Exposes async WebSocket and Static routes. Manages concurrent client sessions and schedules execution tasks using asyncio.gather.
  2. Transpiler Engine: Automatically rewrites simple assignments and calls (e.g., positions['SPY'] = 1 or buy()) into a complete subclass of LEAN's QCAlgorithm with zero manual boilerplate.
  3. Local Data Hub: Slices, caches, and indexes historical stock price series. Infers minute-bar resolutions for ranges under 5 weeks and daily-bar resolutions for longer ranges to match LEAN's expectations.
  4. Orchestrator Engine: Interacts with the Docker socket daemon to start/stop the official quantconnect/lean container, monitors progress using live log parsing, and cleans up artifacts upon execution end.
  5. Metrics Parser Engine: Transforms deep, multi-megabyte JSON output structures generated by LEAN's backtester into high-fidelity flat metrics (Sharpe, Drawdown, Win Rate, CAGR, and an equity curve vector).

⏳ The Five-Stage Pipeline Lifecycle

Each strategy submission triggers a robust, asynchronous linear pipeline:

  1. HYPOTHESIS: Performs abstract syntax tree (AST) validation to verify Python code validity. Transpiles user logic, constructs the config.json launch blueprint, and scaffolds the ephemeral workspaces inside ./projects/{strategy_id}/.
  2. DATA_HUB: Verifies if target market data is cached. If absent, downloads historical bars from yfinance, calculates technical features (SentimentScore and SignalStrength based on momentum-derived variables), and writes standard CSV files under data/custom_sources/ as read-only.
  3. SIGNAL_DETECTION: Spawns the Docker sandbox container, maps dependencies, hooks the container standard output stream, initializes indicator warmup loops inside the LEAN engine, and begins step-by-step engine execution.
  4. PORTFOLIO_AND_RISK: Runs the algorithm backtesting loop. Simulates order matching queues, fills, brokerage commissions, margin accounts, tracking constraints, and records continuous performance metrics.
  5. EVALUATION: Stops the container, deletes intermediate project artifacts, parses the LEAN raw results, extracts core statistics, evaluates them against configuration limits (such as maximum drawdowns), and streams the final payload to the client.

πŸ›‘οΈ Sandbox & Security Constraints

To guarantee system stability, AlphaForge enforces absolute sandbox isolation on spawned containers:

  • Resource Caps: Hard limits of --cpus 2.0 and --memory 2g prevent memory exhaustion (OOM crashes) or full-core locks on the host.
  • Zero Network Access: Spawning with --network none guarantees that user strategies cannot transmit intellectual property, secrets, or perform unauthorized internet connectivity.
  • Strict Access Modes: Sourced data caches are mounted as Read-Only (ro) to prevent running strategies from altering historical prices. Only temporary configuration and results output folders are mounted as Read-Write (rw).
  • Docker Optimization: Custom options are written to /etc/docker/daemon.json to optimize container disk performance (overlay2 storage driver) and stream parallel workloads without bottlenecks.

πŸ› οΈ Installation & One-Shot Setup

Follow these streamlined instructions to configure and deploy AlphaForge on your Linux host.

Prerequisites

  • Operating System: Linux (systemd-managed distributions such as Ubuntu, Debian, Arch Linux, or Fedora).
  • Docker Engine: Installed and running (Docker daemon started).
  • Python Runtime: Python 3.10 or 3.11 with pip and virtual environment support (python3-venv).
  • Disk Capacity: Minimum ~13 GB of free disk space is required to store the official quantconnect/lean:latest engine image.
  • And patience

1. Clone & Set Up Directory

git clone https://github.com/Pulkitsheoran/AlphaForge.git
cd AlphaForge

# Create and activate virtual environment
python3 -m venv venv
source venv/bin/activate

# Install backend dependencies
pip install -r requirements.txt

# Initialize configuration
cp .env.example .env

2. Execute Automated Provisioning

Run the automated setup script to set up Docker permissions, fetch the LEAN image, and merge Docker configuration settings:

# Requires sudo access to configure Docker and append group memberships
bash setup.sh

The script will:

  • Enable and start the Docker service.
  • Append your current user to the docker security group.
  • Apply high-performance settings inside /etc/docker/daemon.json.
  • Download and extract the quantconnect/lean:latest base image.

3. Reload Shell Group Membership

Since your user's group configuration has changed, reload your terminal session for Docker group changes to take effect:

newgrp docker

πŸš€ Running the Platform

To start the AlphaForge backend daemon, execute:

./start.sh

This launches the FastAPI server detached inside a new process group. All server events and outputs are automatically written to server.log.

To monitor the backend server log stream:

tail -f server.log

Once running, navigate to:

http://localhost:8000

to access the visual Comparison Dashboard.


✍️ Strategy Authoring Guide

Algorithms in AlphaForge are written as pure decision-making logic blocks. No class boilerplate or indicator definitions are required.

Supported Environment Fields

The following variables are dynamically mapped and available within your strategy runtime:

  • price: The current close price of the active symbol.
  • sentiment: A momentum-derived alternate sentiment score bounded roughly between -1.0 (bearish) and +1.0 (bullish).
  • signal_strength: A technical trend metric representing relative indicators, bounded between 0.0 (no trend) and 1.0 (strong trend).
  • positions['<SYMBOL>']: Set to 1 to allocate full capital (equivalent to SetHoldings) or set to 0 to exit position (equivalent to Liquidate).

High-level Code Translative Mapping

Under the hood, AlphaForge translates your high-level syntax into explicit QuantConnect commands:

User Code Syntax Transpiled LEAN Output
buy() self.SetHoldings(self.custom_symbol, 1.0)
sell() self.Liquidate(self.custom_symbol)
positions['SPY'] = 1 self.SetHoldings(self.custom_symbol, 1.0)
positions['SPY'] = 0 self.Liquidate(self.custom_symbol)
SetHoldings('SPY', 0.5) self.SetHoldings(self.custom_symbol, 0.5)
Liquidate('SPY') self.Liquidate(self.custom_symbol)

Practical Code Examples

1. Sentiment Momentum Strategy

Executes trade positions based on momentum-derived alternative sentiment scores:

if sentiment > 0.80 and signal_strength > 0.4:
    positions['SPY'] = 1
elif sentiment < 0.20 or signal_strength > 0.8:
    positions['SPY'] = 0

2. Mean Reversion Logic

Takes advantage of localized price pullbacks:

if sentiment < -0.60:
    buy()
elif sentiment > 0.60:
    sell()

πŸ“‘ API & WebSocket Contract (/ws/compare)

Integrate custom clients or external bots with the platform using our standard WebSocket schema.

1. Ingestion Request (Client βž” Server)

Submit a payload containing multiple strategy definitions to trigger parallel execution:

{
  "action": "START_COMPARISON",
  "strategies": [
    {
      "id": "momentum_v1",
      "name": "Sentiment Momentum V1",
      "code": "if sentiment > 0.85: buy()",
      "initial_capital": 100000.0,
      "benchmark": "SPY",
      "symbol": "SPY",
      "start_date": "2024-01-01",
      "end_date": "2025-12-31"
    },
    {
      "id": "reversion_v1",
      "name": "Mean Reversion V1",
      "code": "if sentiment < 0.15: buy()\\nelif sentiment > 0.80: sell()",
      "initial_capital": 100000.0,
      "benchmark": "SPY",
      "symbol": "SPY",
      "start_date": "2024-01-01",
      "end_date": "2025-12-31"
    }
  ]
}

2. Streaming Progress State (Server βž” Client)

During the backtest run-loop, states are continuously broadcast to update UI loading gauges:

{
  "strategy_id": "momentum_v1",
  "stage": "PORTFOLIO_AND_RISK",
  "status": "RUNNING",
  "progress_pct": 42.5,
  "message": "Processing slice timestamp: 2024-08-15 11:30:00"
}

3. Final Evaluation Response (Server βž” Client)

Once execution completes, normalized portfolio vectors and statistics are published:

{
  "strategy_id": "momentum_v1",
  "stage": "EVALUATION",
  "status": "SUCCESS",
  "metrics": {
    "initial_capital": 100000.0,
    "final_equity": 124890.50,
    "cagr_pct": 11.75,
    "sharpe_ratio": 1.42,
    "sortino_ratio": 1.68,
    "max_drawdown_pct": 7.35,
    "total_trades": 86,
    "win_rate_pct": 54.65
  },
  "equity_curve": [
    { "timestamp": "2024-01-01T00:00:00Z", "equity": 100000.0 },
    { "timestamp": "2024-01-02T00:00:00Z", "equity": 100150.0 }
  ]
}

πŸ“ File Structure Reference

AlphaForge/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ main.py                  # FastAPI server startup & WebSocket endpoints
β”‚   β”œβ”€β”€ engine/
β”‚   β”‚   β”œβ”€β”€ transpiler.py        # Abstract Code Transpilation & boilerplates
β”‚   β”‚   β”œβ”€β”€ data_hub.py          # Real market-data downloader & indicator feature-engineering
β”‚   β”‚   β”œβ”€β”€ orchestrator.py      # EPHEMERAL Docker workspace launcher & progress tracker
β”‚   β”‚   └── metrics_parser.py    # Raw LEAN backtest JSON parser
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   └── schemas.py           # Pydantic schemas for data-validation
β”‚   └── static/
β”‚       └── index.html           # Single-page client dashboard (Vanilla HTML/CSS/Canvas/JS)
β”œβ”€β”€ config/
β”‚   └── daemon.json              # Optimized Docker Daemon properties
β”œβ”€β”€ data/
β”‚   └── custom_sources/          # Cached historical market data (.csv files)
β”œβ”€β”€ projects/                    # Ephemeral, git-ignored strategy workspace folders
β”œβ”€β”€ results/                     # Ephemeral, git-ignored strategy output folders
β”œβ”€β”€ .env.example                 # Template for custom environment properties
β”œβ”€β”€ pyrightconfig.json           # Python language server typing layout
β”œβ”€β”€ setup.sh                     # System configuration and Docker provisioning shell script
β”œβ”€β”€ SPEC.md                      # Detailed system engineering specification
└── start.sh                     # Daemonized FastAPI server launcher script

βš™οΈ Configuration (.env)

The following parameters can be fine-tuned inside your local .env configuration file:

  • LEAN_PROJECT_NAME: Defines the root strategy project identifier.
  • LEAN_WORKSPACE_DIR: Points to the local workspace folder.
  • RESULTS_ROOT_DIR: Path where backtest evaluation files are temporally structured.
  • SHARPE_DEGRADATION_THRESHOLD: Standard warning threshold when strategy Sharpe ratios dip below target values.
  • MAX_DRAWDOWN_LIMIT: Security policy trigger when absolute drawdown exceeds the acceptable ceiling (e.g. 0.25 for 25%).

⚠️ Platform Limitations

  • Indicator Proxying: Currently, SentimentScore and SignalStrength are generated using deterministic momentum formulas based on raw OHLCV series. They serve as placeholder indicators and should be replaced with live sentiment api feeds for true quantitative validation.
  • Intraday Resolutions: Intraday minute-bar accuracy provided by yfinance is restricted to recent trading periods (typically up to 30 days). Backtests requested for historical ranges beyond this range will automatically fallback to daily resolution bars.
  • Ratios Initialization: Calculating Sharpe and Sortino ratios requires a sufficiently robust transaction frequency and timeline length. Extremely short backtests (e.g., under 15 trading days) may yield mathematical zeros (0.0).

πŸ“„ License

AlphaForge is open-source software licensed under the MIT License. Feel free to adapt, build upon, or integrate this engine into your proprietary trading desks.

About

Algo Comparator

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages