How marketneutral/alphatools works
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)