# marketneutral/alphatools architecture

> Architecture diagram of the GitHub repository marketneutral/alphatools, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/marketneutral/alphatools
- Repository: https://github.com/marketneutral/alphatools

## Overview

##### Project Type and Purpose
- This is a Python library (“alphatools”) for quantitative financial research and machine learning. It provides:
- A “bring-your-own-data” Factory for loading arbitrary data sources into Zipline Pipelines
- An Expression-DSL parser & compiler (Lark → AST → Zipline CustomFactor code) for “101 Formulaic Alphas”
- Convenience wrappers around Zipline’s Pipeline API, plus helper modules for fundamentals and industry/sector classification
- Installer scripts, CI configuration, notebooks, and tests demonstrating usage
- It is intended as an extensible, full-stack research environment (data ingestion → factor construction → backtest/analysis).

##### Top-Level Structure & Main Components
- ENTRYPOINT & CLI
- alphatools/__main__.py – module entry, e.g. `alphatools ingest`
- DATA LAYER (alphatools/data)
- factory.py + data_sources.json – Factory pattern loading CSV, SQL, S3, etc. via Blaze/Odo
- built-in datasets (CSV, .npy) for CIK/ticker, sector/industry mappings
- DSL & PARSER (alphatools/expression)
- expression.lark – EBNF grammar for formulaic alphas
- expression.py – Lark-based parser → AST → three-address code generator producing Zipline CustomFactor code
- ALGORITHM UTILITIES (alphatools/algo)
- risk.py, utils.py – common financial computations & helpers
- FUNDAMENTALS MODULE (alphatools/fundamentals)
- make_fundamentals.py → build pipeline factors from in-memory DataFrames (DataFrameLoader)
- INDUSTRY/SECTOR CLASSIFIERS (alphatools/ics)
- SIC/CIK data + ics_scheme.py → Zipline Classifier wrappers (Sector, SubIndustry)
- RESEARCH MODULE (alphatools/research)
- research.py, run_pipeline – wrapper around Zipline’s run_pipeline for interactive notebooks or scripts
- SUPPORTING ARTIFACTS
- notebooks/ – examples integrating all modules
- install_{stable,latest}.sh, requirements_*.txt – environment provisioning scripts for Python 3.5/3.6
- CI (Travis) config & tests/ – ensure environments build and example pipelines run

##### Component Interactions & Data Flow
1. User code or CLI invokes run_pipeline (research module)
2. run_pipeline constructs a Zipline Pipeline using:
– Factory[…] pipeline factors (data ingestion)
– ExpressionAlpha.make_pipeline_factor() (DSL → CustomFactor)
– pre-built fundamentals or ICS factors
– custom Algo utilities as needed
3. Zipline ingest (external process) builds a security master; ICS classifiers are mapped to sids
4. run_pipeline calls Zipline’s engine → fetches pricing data (external bundle), applies factors over time → returns DataFrame
5. User visualizes or analyzes results in notebooks

##### Key Architectural Patterns & Principles
- Layered architecture
- Data Access (Factory, built-in CSV/DB loaders)
- DSL & Code Generation (Expression module)
- Business Logic / Factor Construction (fundamentals, ics, algo)
- Application / Research Interface (research.run_pipeline, notebooks)
- Plugin / Factory pattern for adding new data sources without code changes
- Code‐generation pattern in expression module (AST → three-address code → CustomFactor)
- Modular packaging via sub‐packages for clear separation of concerns
- CI/CD integration (Travis) and environment reproducibility scripts

##### External Dependencies & Services
- Zipline (Pipeline API, ingest engine, pricing bundles)
- Blaze / Datashape / Odo (data URIs → DataFrames)
- Lark (DSL parsing)
- Bottleneck / NumPy / Pandas (vectorized computations)
- Machine‐learning libs: scikit-learn, LightGBM, PyMC3, PyTorch, fastai (via notebook examples)
- Data sources: local CSV, PostgreSQL, S3, Hadoop, etc. (any URI supported by Odo)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Entrypoint & CLI
    CLI["Entrypoint & CLI\nalphatools/__main__.py"]:::module
    click CLI "https://github.com/marketneutral/alphatools/blob/master/alphatools/__main__.py"

    %% Data Layer
    subgraph "Data Layer" 
        direction TB
        Factory["Data Factory\n(alphatools/data/factory.py)"]:::module
        click Factory "https://github.com/marketneutral/alphatools/blob/master/alphatools/data/factory.py"
        DS_JSON["Data Sources Definitions\n(data_sources.json)"]:::module
        click DS_JSON "https://github.com/marketneutral/alphatools/blob/master/alphatools/data/factory/data_sources.json"
        subgraph "Built-in Datasets"
            direction TB
            CSV1["cik_ticker_09152017.csv"]:::data
            click CSV1 "https://github.com/marketneutral/alphatools/blob/master/alphatools/data/cik_ticker_09152017.csv"
            CSV2["dat_file.csv"]:::data
            click CSV2 "https://github.com/marketneutral/alphatools/blob/master/alphatools/data/dat_file.csv"
            PKL["dat_file.pkl"]:::data
            click PKL "https://github.com/marketneutral/alphatools/blob/master/alphatools/data/dat_file.pkl"
            NPY1["industries.npy"]:::data
            click NPY1 "https://github.com/marketneutral/alphatools/blob/master/alphatools/data/industries.npy"
            CSV3["industry_names.csv"]:::data
            click CSV3 "https://github.com/marketneutral/alphatools/blob/master/alphatools/data/industry_names.csv"
            NPY2["sectors.npy"]:::data
            click NPY2 "https://github.com/marketneutral/alphatools/blob/master/alphatools/data/sectors.npy"
            CSV4["sector_names.csv"]:::data
            click CSV4 "https://github.com/marketneutral/alphatools/blob/master/alphatools/data/sector_names.csv"
            CSV5["profiles_20170918.csv"]:::data
            click CSV5 "https://github.com/marketneutral/alphatools/blob/master/alphatools/data/profiles_20170918.csv"
        end
    end

    %% DSL & Parser
    subgraph "DSL & Parser" 
        direction TB
        Grammar["Grammar\nexpression.lark"]:::module
        click Grammar "https://github.com/marketneutral/alphatools/blob/master/alphatools/expression/expression.lark"
        Parser["Parser & CodeGen\nexpression.py"]:::module
        click Parser "https://github.com/marketneutral/alphatools/blob/master/alphatools/expression/expression.py"
        Repro["repro.py"]:::module
        click Repro "https://github.com/marketneutral/alphatools/blob/master/alphatools/expression/repro.py"
        TestExp["test.py"]:::module
        click TestExp "https://github.com/marketneutral/alphatools/blob/master/alphatools/expression/test.py"
    end

    %% Algorithm Utilities
    subgraph "Algorithm Utilities"
        direction TB
        Risk["Risk Computations\nrisk.py"]:::module
        click Risk "https://github.com/marketneutral/alphatools/blob/master/alphatools/algo/risk.py"
        Utils["General Helpers\nutils.py"]:::module
        click Utils "https://github.com/marketneutral/alphatools/blob/master/alphatools/algo/utils.py"
    end

    %% Fundamentals Module
    subgraph "Fundamentals Module"
        direction TB
        FundAPI["Core API\nfundamentals.py"]:::module
        click FundAPI "https://github.com/marketneutral/alphatools/blob/master/alphatools/fundamentals/fundamentals.py"
        Loader["DataFrameLoader\nmake_fundamentals.py"]:::module
        click Loader "https://github.com/marketneutral/alphatools/blob/master/alphatools/fundamentals/make_fundamentals.py"
    end

    %% Industry/Sector Classifiers
    subgraph "Industry/Sector Classifiers"
        direction TB
        ICS["Classifier Wrappers\nics_scheme.py"]:::module
        click ICS "https://github.com/marketneutral/alphatools/blob/master/alphatools/ics/ics_scheme.py"
        subgraph "Built-in ICS Data"
            direction TB
            INPY1["industries.npy"]:::data
            click INPY1 "https://github.com/marketneutral/alphatools/blob/master/alphatools/ics/industries.npy"
            ICSV1["industry_names.csv"]:::data
            click ICSV1 "https://github.com/marketneutral/alphatools/blob/master/alphatools/ics/industry_names.csv"
            NPY3["sectors.npy"]:::data
            click NPY3 "https://github.com/marketneutral/alphatools/blob/master/alphatools/ics/sectors.npy"
            CSV6["sector_names.csv"]:::data
            click CSV6 "https://github.com/marketneutral/alphatools/blob/master/alphatools/ics/sector_names.csv"
            NPY4["sic_major.npy"]:::data
            click NPY4 "https://github.com/marketneutral/alphatools/blob/master/alphatools/ics/sic_major.npy"
            NPY5["sic_specialize.npy"]:::data
            click NPY5 "https://github.com/marketneutral/alphatools/blob/master/alphatools/ics/sic_specialize.npy"
            NPY6["sic_subclass.npy"]:::data
            click NPY6 "https://github.com/marketneutral/alphatools/blob/master/alphatools/ics/sic_subclass.npy"
        end
    end

    %% Research Module
    subgraph "Research Module"
        direction TB
        Research["run_pipeline & Wrappers\nresearch.py"]:::module
        click Research "https://github.com/marketneutral/alphatools/blob/master/alphatools/research/research.py"
    end

    %% External Data Sources
    subgraph "External Data Sources" 
        direction TB
        CSV_EXT["Local CSV"]:::external
        DB["PostgreSQL"]:::external
        S3["AWS S3"]:::external
        Hadoop["Hadoop"]:::external
    end

    %% Zipline Engine
    Zipline["Zipline Engine"]:::engine
    SecurityMaster["Security Master\n(Cylinder)"]:::storage
    PricingBundle["Pricing Bundle\n(Cylinder)"]:::storage

    %% CI and Environment
    subgraph "CI & Environment" 
        direction TB
        InstallL["install_latest.sh"]:::ci
        InstallS["install_stable.sh"]:::ci
        Req1["requirements_latest.txt"]:::ci
        Req2["requirements_stable.txt"]:::ci
        Req3["requirements_blaze_latest.txt"]:::ci
        Req4["requirements_blaze_stable.txt"]:::ci
        Travis[".travis.yml"]:::ci
        CITest["ci/test.py"]:::ci
        TestsDir["tests/"]:::ci
    end

    %% Notebooks
    Notebooks["Notebooks\n(example workflows & ML)"]:::notebook
    click Notebooks "https://github.com/marketneutral/alphatools/tree/master/notebooks/"

    %% Connections
    CLI -->|invokes| Research
    Research -->|uses| Factory
    Research -->|parses DSL| Parser
    Research -->|loads fundamentals| FundAPI
    Research -->|loads ICS factors| ICS
    Factory -->|reads built-in| CSV1 & CSV2 & PKL & NPY1 & CSV3 & NPY2 & CSV4 & CSV5
    Factory -->|reads external| CSV_EXT & DB & S3 & Hadoop
    Parser -->|generates| Research
    FundAPI -->|depends on| Loader
    ICS -->|uses| INPY1 & ICSV1 & NPY3 & CSV6 & NPY4 & NPY5 & NPY6
    Research -->|constructs Pipeline| Zipline
    Zipline -->|ingest| SecurityMaster
    Zipline -->|fetches| PricingBundle
    Zipline -->|runs| Research
    Research -->|outputs| Notebooks
    CITest -->|validates| Research
    Travis -->|runs| CITest
    InstallL & InstallS & Req1 & Req2 & Req3 & Req4 -->|setup| Research

    %% Styles
    classDef module fill:#D0E6FF,stroke:#0366d6,color:#0366d6;
    classDef data fill:#E6FFE6,stroke:#00A000,color:#006000;
    classDef external fill:#E0FFE0,stroke:#00A000,color:#006000,stroke-dasharray: 5 5;
    classDef engine fill:#FFD8A8,stroke:#FF8C00,color:#FF8C00;
    classDef storage fill:#E6FFE6,stroke:#00A000,color:#006000,stroke-dasharray: 2 2;
    classDef ci fill:#FFE6E6,stroke:#D63649,color:#D63649;
    classDef notebook fill:#FFF0B3,stroke:#CC9900,color:#CC9900;
    classDef dashed stroke-dasharray: 5 5;
```
