# predictivelabs/finespresso-modelling architecture

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

- Interactive diagram: https://gitdiagram.com/predictivelabs/finespresso-modelling
- Repository: https://github.com/predictivelabs/finespresso-modelling

## Overview

##### Project Type and Purpose
- This is a full‐stack data science / machine learning platform for predicting the impact of financial news on stock prices. It ingests data from multiple external sources (news publishers, financial APIs), processes and cleans the data, engineers features, trains classification and regression models, backtests trading strategies, and generates performance reports.

##### Main Components
a. External Data Sources
- News feeds and press releases via custom publisher scripts (tasks/publisher/*)
- Historical price data via Polygon API (playground/polygon_*.py) and Yahoo Finance (tasks/features_engineering/fetch_yfinance_data.py)
- Relational database (PostgreSQL, MySQL, etc.) referenced by DATABASE_URL (.env)
b. Raw Data Storage
- data/ directory for CSV/PKL inputs: raw news, instruments, price moves, lineage, quality metrics
c. Data Processing Pipelines
- Data Cleaning (tasks/data_cleaning/): cleaning, validation, quality metrics, versioning
- Feature Engineering (tasks/features_engineering/): text & time features, company features, TF-IDF vectorization
- SQL Utilities (sql/): schema creation, DDL extraction, queries for ingestion
d. ML Training & Prediction
- Classification & regression training scripts (tasks/ai/train_classifier*.py, train_regression*.py)
- Prediction scripts (tasks/ai/predict.py)
- Utility modules for OpenAI, spaCy, model persistence (utils/ai/*, models/*.joblib)
e. Backtesting & Reporting
- Backtester (tasks/backtesting/backtester.py)
- Reports output (reports/): model performance, backtest trades, feature importance
f. Orchestration & Ad‐hoc
- Playground scripts (playground/*.py) for data download, backfill, sector examples
- Jupyter notebooks (notebooks/) for exploration and prototyping

##### Component Interactions & Data Flow
- External Data Sources → Raw Data Storage
- Raw Data Storage → Data Cleaning Pipeline
- Clean Data → Feature Engineering Pipeline
- Engineered Features → Model Training
- Trained Models + New Data → Prediction Service
- Predictions & Models → Backtesting
- SQL Layer ↔ Database

##### Architectural Patterns & Design Principles
- Modular pipelines by function (data cleaning, feature engineering, AI, backtesting)
- Separation of concerns: tasks/ for batch pipelines, utils/ for shared code, playground/ for ad-hoc scripts
- Data versioning & lineage tracking (data/lineage, tasks/data_cleaning/data_versioning.py)
- File-based persistence for intermediate artifacts and final models
- Configurable via YAML (config/backtest_config.yaml) and .env

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Sources Layer
    subgraph "External Sources"
        direction TB
        Publishers["News Publishers"]:::external
        PolygonScripts["Polygon API Scripts"]:::external
        PolygonData["Polygon Data"]:::datastore
        YahooFinance["Yahoo Finance Fetch"]:::external
    end

    %% Raw Data Storage
    subgraph "Raw Data Storage"
        direction TB
        RawData["data/"]:::datastore
    end

    %% Data Processing Pipelines
    subgraph "Data Cleaning Service"
        direction TB
        Cleaning["Data Cleaning"]:::processing
    end

    subgraph "Feature Engineering Service"
        direction TB
        Engineering["Feature Engineering"]:::processing
    end

    subgraph "SQL Utilities"
        direction TB
        SQLUtils["SQL Utilities"]:::processing
    end

    %% ML Training & Prediction
    subgraph "ML Training & Prediction"
        direction TB
        Training["Train Models"]:::processing
        Prediction["Predict"]:::processing
    end

    %% Backtesting Module
    subgraph "Backtesting Module"
        direction TB
        Backtesting["Backtester"]:::processing
    end

    %% Data Stores & Outputs
    subgraph "Model Store"
        direction TB
        ModelStore["models/*.joblib"]:::datastore
    end

    subgraph "Reports"
        direction TB
        Reports["reports/"]:::datastore
    end

    %% Ad-hoc & Orchestration
    subgraph "Playground & Notebooks"
        direction TB
        Playground["playground/"]:::adhoc
        Notebooks["notebooks/"]:::adhoc
    end

    %% Database
    Database["Relational Database"]:::datastore

    %% Data Flows
    Publishers -->|"raw CSVs"| RawData
    PolygonScripts -->|"raw CSVs"| RawData
    PolygonData --> RawData
    YahooFinance -->|"raw CSVs"| RawData

    RawData -->|"raw CSVs"| Cleaning
    Cleaning -->|"cleaned CSVs"| RawData

    Cleaning -->|"cleaned CSVs"| Engineering
    Engineering -->|"feature CSVs"| RawData

    Engineering -->|"feature CSVs"| Training
    Training -->|".joblib models"| ModelStore

    ModelStore -->|".joblib models"| Prediction
    RawData -->|"new data"| Prediction
    Prediction -->|"predictions"| Reports

    Prediction -->|".joblib models + predictions"| Backtesting
    Backtesting -->|"trade logs"| Reports

    SQLUtils <-->|"queries / DDL"| Database
    Cleaning <-->|"schema / ingestion"| Database
    Engineering <-->|"schema / ingestion"| Database
    Playground <-->|"DB utilities"| Database

    Playground --> RawData
    Notebooks --> RawData

    %% Click Events
    click Publishers "https://github.com/predictivelabs/finespresso-modelling/tree/main/tasks/publisher/"
    click PolygonScripts "https://github.com/predictivelabs/finespresso-modelling/tree/main/playground/"
    click PolygonData "https://github.com/predictivelabs/finespresso-modelling/tree/main/data/polygon/"
    click YahooFinance "https://github.com/predictivelabs/finespresso-modelling/blob/main/tasks/features_engineering/fetch_yfinance_data.py"
    click RawData "https://github.com/predictivelabs/finespresso-modelling/tree/main/data/"
    click Cleaning "https://github.com/predictivelabs/finespresso-modelling/tree/main/tasks/data_cleaning/"
    click Engineering "https://github.com/predictivelabs/finespresso-modelling/tree/main/tasks/features_engineering/"
    click SQLUtils "https://github.com/predictivelabs/finespresso-modelling/tree/main/sql/"
    click Training "https://github.com/predictivelabs/finespresso-modelling/tree/main/tasks/ai/"
    click Prediction "https://github.com/predictivelabs/finespresso-modelling/tree/main/tasks/ai/"
    click Backtesting "https://github.com/predictivelabs/finespresso-modelling/blob/main/tasks/backtesting/backtester.py"
    click Database "https://github.com/predictivelabs/finespresso-modelling/tree/main/utils/db/"
    click ModelStore "https://github.com/predictivelabs/finespresso-modelling/tree/main/models/"
    click Reports "https://github.com/predictivelabs/finespresso-modelling/tree/main/reports/"
    click Playground "https://github.com/predictivelabs/finespresso-modelling/tree/main/playground/"
    click Notebooks "https://github.com/predictivelabs/finespresso-modelling/tree/main/notebooks/"

    %% Styles
    classDef external fill:#FFA500,stroke:#333,stroke-width:1px
    classDef datastore fill:#268BD2,stroke:#333,stroke-width:1px
    classDef processing fill:#859900,stroke:#333,stroke-width:1px
    classDef adhoc fill:#839496,stroke:#333,stroke-width:1px
```
