# tejtw/pyfolio-tej architecture

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

- Interactive diagram: https://gitdiagram.com/tejtw/pyfolio-tej
- Repository: https://github.com/tejtw/pyfolio-tej

## Overview

##### Project type and purpose
- This is an open-source Python library (pyfolio) for performance and risk analysis of financial portfolios.
- It provides “tear sheet” reports—collections of plots and statistics—on backtest results, primarily integrating with the Zipline backtesting engine and using pandas, NumPy, and matplotlib under the hood.

##### File structure
- Top-level:
- setup.py / pyproject.toml / MANIFEST.in – packaging and build metadata
- src/pyfolio – the library code, organized into modules (capacity, perf_attrib, plotting, tears, timeseries, txn, etc.)
- docs/ – Sphinx documentation (source .rst files, notebooks, build scripts)
- tests/ – pytest test cases exercising each module
- CI & tooling: .github/workflows, build_and_deploy_docs.sh, pre-commit, flake8 config.

##### README for additional insights
- Confirms core functionality: generation of performance metrics and plots (“tear sheets”), intended for use with Jupyter notebooks and the Zipline engine.
- Highlights external dependencies (Zipline, Jupyter), user workflow (pip/conda install, import library, run examples in notebooks).

##### Main components and their interactions (
a. Core Library Components (inside pyfolio namespace)
1. Data Input & Wrangling
– timeseries.py, txn.py, pos.py, interesting_periods.py
2. Performance Computation
– perf_attrib.py, capacity.py, round_trips.py
3. Reporting & Visualization
– plotting.py, tears.py, plot_cn_name_map.py, utils.py
4. Compatibility Layer
– ipycompat.py (Jupyter notebook helper), deprecate.py
b. External Dependencies & Integration
– Zipline: supplies backtest results (returns, positions, transactions)
– pandas / NumPy: data structures and numeric routines
– matplotlib / seaborn: plotting engine
– Jupyter notebook: interactive user environment
c. Documentation & Examples
– docs/source/api-reference.rst, notebooks under docs/source/notebooks
– build scripts (docs/deploy.py, Makefile)
d. Testing & CI
– tests/ modules, GitHub Actions workflows (unit tests, packaging, coverage)

##### Relationships & Data Flow
- Data flows into Performance Computation modules → results passed to Reporting modules
- Reporting modules call matplotlib → generate figures
- Jupyter notebook orchestrates calls to pyfolio API
- Documentation build process reads src/pyfolio docstrings and notebooks → Sphinx outputs HTML
- Test runner imports src/pyfolio, executes test suites (tests/test_*.py)

##### Architectural patterns & design principles
- Modular decomposition: clear separation between data ingestion, metric computation, and visualization
- Layered design: library core is independent of presentation (matplotlib wrappers in separate modules)
- Extensibility: utility functions and deprecation module support backward compatibility and future growth
- Convention-over-configuration: modules follow pandas-friendly data structures and naming conventions

##### Capturing essential architectural elements
- Don’t over-complicate; represent each functional area as a separate component
- External integrations are clearly shown and labeled
- Build & deployment (pip install, Sphinx docs generation, CI workflows) as peripheral components to give the full picture

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Dependencies
    subgraph "External Dependencies"
        direction TB
        Zipline(["Zipline"]):::external
        PandasNumPy(["Pandas / NumPy"]):::external
        MatplotlibSeaborn(["Matplotlib / Seaborn"]):::external
        Jupyter(["Jupyter Notebook"]):::external
    end

    %% Core Library
    subgraph "Core Library" 
        direction TB

        subgraph "Data Input & Wrangling"
            direction TB
            Timeseries["pyfolio.timeseries"]:::core
            Txn["pyfolio.txn"]:::core
            Pos["pyfolio.pos"]:::core
            InterestingPeriods["pyfolio.interesting_periods"]:::core
        end

        subgraph "Performance Computation"
            direction TB
            PerfAttrib["pyfolio.perf_attrib"]:::core
            Capacity["pyfolio.capacity"]:::core
            RoundTrips["pyfolio.round_trips"]:::core
        end

        subgraph "Reporting & Visualization"
            direction TB
            Plotting["pyfolio.plotting"]:::core
            Tears["pyfolio.tears"]:::core
            CNMap["pyfolio.plot_cn_name_map"]:::core
            Utils["pyfolio.utils"]:::core
        end

        subgraph "Compatibility Layer"
            direction TB
            IPyCompat["pyfolio.ipycompat"]:::core
            Deprecate["pyfolio.deprecate"]:::core
        end
    end

    %% Documentation & Examples
    subgraph "Documentation & Examples"
        direction TB
        APIRst["api-reference.rst"]:::docs
        Notebooks["notebooks/"]:::docs
        DocMakefile["Makefile"]:::docs
        DeployScript["deploy.py"]:::docs
        DeployShell["build_and_deploy_docs.sh"]:::docs
    end

    %% Testing & CI
    subgraph "Testing & CI"
        direction TB
        TestsFolder["tests/"]:::tests
        CIWorkflow["python-publish.yml"]:::tests
    end

    %% Packaging & Build Metadata
    subgraph "Packaging & Build Metadata"
        direction TB
        SetupPy["setup.py"]:::pkg
        PyProject["pyproject.toml"]:::pkg
        Manifest["MANIFEST.in"]:::pkg
    end

    %% Data Flows
    Zipline -->|"backtest results"| Timeseries
    Zipline -->|"transactions"| Txn
    Timeseries -->|"time series data"| PerfAttrib
    Txn -->|"txn data"| PerfAttrib
    Pos -->|"position data"| PerfAttrib
    PerfAttrib -->|"metrics"| Plotting
    Capacity -->|"capacity metrics"| Tears
    RoundTrips -->|"round trip stats"| Utils
    Plotting -->|"plots"| MatplotlibSeaborn
    Tears -->|"tear sheets"| MatplotlibSeaborn
    Jupyter -->|"user workflow"| Timeseries
    Jupyter -->|"user workflow"| Plotting
    APIRst -->|"source"| DocMakefile
    Notebooks -->|"examples"| APIRst
    DeployScript -->|"build docs"| APIRst
    DocMakefile -->|"run build"| DeployShell
    CIWorkflow -->|"runs tests"| TestsFolder
    TestsFolder -->|"imports src"| Timeseries
    PyProject -->|"metadata"| SetupPy

    %% Click Events
    click Timeseries "https://github.com/tejtw/pyfolio-tej/blob/main/src/pyfolio/timeseries.py"
    click Txn "https://github.com/tejtw/pyfolio-tej/blob/main/src/pyfolio/txn.py"
    click Pos "https://github.com/tejtw/pyfolio-tej/blob/main/src/pyfolio/pos.py"
    click InterestingPeriods "https://github.com/tejtw/pyfolio-tej/blob/main/src/pyfolio/interesting_periods.py"
    click PerfAttrib "https://github.com/tejtw/pyfolio-tej/blob/main/src/pyfolio/perf_attrib.py"
    click Capacity "https://github.com/tejtw/pyfolio-tej/blob/main/src/pyfolio/capacity.py"
    click RoundTrips "https://github.com/tejtw/pyfolio-tej/blob/main/src/pyfolio/round_trips.py"
    click Plotting "https://github.com/tejtw/pyfolio-tej/blob/main/src/pyfolio/plotting.py"
    click Tears "https://github.com/tejtw/pyfolio-tej/blob/main/src/pyfolio/tears.py"
    click CNMap "https://github.com/tejtw/pyfolio-tej/blob/main/src/pyfolio/plot_cn_name_map.py"
    click Utils "https://github.com/tejtw/pyfolio-tej/blob/main/src/pyfolio/utils.py"
    click IPyCompat "https://github.com/tejtw/pyfolio-tej/blob/main/src/pyfolio/ipycompat.py"
    click Deprecate "https://github.com/tejtw/pyfolio-tej/blob/main/src/pyfolio/deprecate.py"
    click APIRst "https://github.com/tejtw/pyfolio-tej/blob/main/docs/source/api-reference.rst"
    click Notebooks "https://github.com/tejtw/pyfolio-tej/tree/main/docs/source/notebooks/"
    click DocMakefile "https://github.com/tejtw/pyfolio-tej/tree/main/docs/Makefile"
    click DeployScript "https://github.com/tejtw/pyfolio-tej/blob/main/docs/deploy.py"
    click DeployShell "https://github.com/tejtw/pyfolio-tej/blob/main/build_and_deploy_docs.sh"
    click TestsFolder "https://github.com/tejtw/pyfolio-tej/tree/main/tests/"
    click CIWorkflow "https://github.com/tejtw/pyfolio-tej/blob/main/.github/workflows/python-publish.yml"
    click SetupPy "https://github.com/tejtw/pyfolio-tej/blob/main/setup.py"
    click PyProject "https://github.com/tejtw/pyfolio-tej/blob/main/pyproject.toml"
    click Manifest "https://github.com/tejtw/pyfolio-tej/blob/main/MANIFEST.in"

    %% Styles
    classDef core fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef external fill:#90EE90,stroke:#333,stroke-width:1px,stroke-dasharray: 5 5
    classDef docs fill:#FFFACD,stroke:#333,stroke-width:1px
    classDef tests fill:#D3D3D3,stroke:#333,stroke-width:1px
    classDef pkg fill:#FFA500,stroke:#333,stroke-width:1px
```
