# itxmjr/stock-price-forecast architecture

> Architecture diagram of the GitHub repository itxmjr/stock-price-forecast, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/itxmjr/stock-price-forecast
- Repository: https://github.com/itxmjr/stock-price-forecast

## Overview

#### Purpose and Entry Point
This is a Python batch forecasting pipeline for next-day stock closing prices. Its executable entry point is `python -m src.main`, implemented in `src/main.py`. The pipeline fetches or refreshes ticker history, builds time-series features, trains two regression models, reports metrics, and writes plots.

#### Configuration Boundary
Central runtime parameters—such as the stock ticker, date range, and output locations—are expected to be defined in `src/config.py`. Configuration separates business parameters from processing logic and drives the data acquisition and output naming behavior.

Paths:
- `src/config.py`
- `src/main.py`

#### Data Acquisition and Persistence
The loader obtains historical market data through the external `yfinance` API and persists the working dataset as CSV. This creates a local artifact boundary: downstream processing operates on tabular historical prices, while the loader owns remote-provider interaction and file storage.

Paths:
- `src/data_loader.py`
- `data/AAPL.csv`

External service/runtime dependency:
- Yahoo Finance via `yfinance`
- Pandas DataFrames/CSV serialization

#### Feature Engineering and Temporal Split
The feature stage cleans missing values and constructs lagged price features suitable for predicting the next closing price. It also produces train/test partitions; for a time-series workflow this is the boundary between historical training observations and later held-out observations used for evaluation.

Paths:
- `src/feature_engineering.py`
- `src/data_loader.py`

Primary flow: market-price DataFrame → cleaned/lagged supervised-learning dataset → feature matrices and target vectors for train/test use.

#### Model Training and Evaluation
`src/modeling.py` encapsulates the ML core: Random Forest regression and Linear Regression. Each model consumes the engineered split, generates test predictions, and is evaluated with MAE, RMSE, and R². Scikit-learn is the primary model/runtime boundary; model objects and predictions are passed onward rather than serving predictions through an API.

Paths:
- `src/modeling.py`
- `src/feature_engineering.py`

#### Visualization and Output Artifacts
The plotting stage converts held-out actual values and model predictions into comparison and per-model charts. Matplotlib is used to generate durable PNG outputs under `assets/`, making plots the principal user-facing result besides terminal metrics.

Paths:
- `src/plotting.py`
- `assets/`

Expected artifacts include model-comparison, Random Forest, and Linear Regression prediction charts.

#### Orchestration and Control Flow
`src/main.py` is the composition root and sequential controller:
1. Read configuration.
2. Download/load and save ticker data.
3. Engineer lagged features and split data.
4. Train/evaluate both regressors.
5. Print metrics.
6. Generate plot artifacts.

The repository is a local, single-process Python application rather than a web service. Dependency and Python-version constraints are declared in `pyproject.toml`; `uv.lock` supports reproducible dependency resolution.

Paths:
- `src/main.py`
- `pyproject.toml`
- `uv.lock`

Main entry points: [`src/main.py`](https://github.com/itxmjr/stock-price-forecast/blob/main/src/main.py) (Batch entry point), [`src/modeling.py`](https://github.com/itxmjr/stock-price-forecast/blob/main/src/modeling.py) (Modeling and evaluation), [`src/data_loader.py`](https://github.com/itxmjr/stock-price-forecast/blob/main/src/data_loader.py) (Market data loader), [`src/feature_engineering.py`](https://github.com/itxmjr/stock-price-forecast/blob/main/src/feature_engineering.py) (Feature engineering)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime"]
  node_main["Batch entry point<br/>Python module<br/>[main.py]"]
  node_config["Runtime config<br/>Python config<br/>[config.py]"]
  node_dependencies["Dependencies<br/>Python project<br/>[pyproject.toml]"]
  node_lockfile["Resolved lockfile<br/>uv lockfile<br/>[uv.lock]"]
end

subgraph group_pipeline["Forecast pipeline"]
  node_loader["Market data loader<br/>Python module<br/>[data_loader.py]"]
  node_yahoo(("Yahoo Finance<br/>External market API"))
  node_features["Feature engineering<br/>Python module"]
  node_split[("Train/test split<br/>Supervised dataset")]
  node_modeling["Modeling and evaluation<br/>Python module<br/>[modeling.py]"]
  node_random_forest["Random Forest<br/>Scikit-learn regressor"]
  node_linear_regression["Linear Regression<br/>Scikit-learn regressor"]
  node_plotting["Prediction plotting<br/>Python module<br/>[plotting.py]"]
end

subgraph group_outputs["Local artifacts"]
  node_prices[("Ticker price history<br/>CSV dataset<br/>[AAPL.csv]")]
  node_charts["Forecast charts<br/>PNG artifacts"]
end

node_dependencies -.->|"defines runtime"| node_main
node_lockfile -.->|"resolves packages"| node_dependencies
node_main -->|"reads"| node_config
node_main -->|"orchestrates"| node_loader
node_config -->|"ticker and dates"| node_loader
node_loader -->|"downloads history"| node_yahoo
node_loader -->|"writes CSV"| node_prices
node_main -->|"orchestrates"| node_features
node_prices -->|"loads price data"| node_features
node_features -->|"lagged temporal split"| node_split
node_main -->|"orchestrates"| node_modeling
node_split -->|"training and test data"| node_modeling
node_modeling -->|"trains and evaluates"| node_random_forest
node_modeling -->|"trains and evaluates"| node_linear_regression
node_main -->|"orchestrates"| node_plotting
node_modeling -->|"actuals, predictions, metrics"| node_plotting
node_plotting -->|"writes PNGs"| node_charts

click node_main "https://github.com/itxmjr/stock-price-forecast/blob/main/src/main.py"
click node_config "https://github.com/itxmjr/stock-price-forecast/blob/main/src/config.py"
click node_dependencies "https://github.com/itxmjr/stock-price-forecast/blob/main/pyproject.toml"
click node_lockfile "https://github.com/itxmjr/stock-price-forecast/blob/main/uv.lock"
click node_loader "https://github.com/itxmjr/stock-price-forecast/blob/main/src/data_loader.py"
click node_prices "https://github.com/itxmjr/stock-price-forecast/blob/main/data/AAPL.csv"
click node_features "https://github.com/itxmjr/stock-price-forecast/blob/main/src/feature_engineering.py"
click node_modeling "https://github.com/itxmjr/stock-price-forecast/blob/main/src/modeling.py"
click node_plotting "https://github.com/itxmjr/stock-price-forecast/blob/main/src/plotting.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_config,node_dependencies,node_lockfile toneBlue
class node_loader,node_yahoo,node_features,node_split,node_modeling,node_random_forest,node_linear_regression,node_plotting toneAmber
class node_prices,node_charts toneMint
```

## Components

### Runtime

- [Batch entry point](https://github.com/itxmjr/stock-price-forecast/blob/main/src/main.py): Python module · Sequential composition root invoked with python -m src.main. · `src/main.py`
- [Runtime config](https://github.com/itxmjr/stock-price-forecast/blob/main/src/config.py): Python config · Ticker, date range, and output locations. · `src/config.py`
- [Dependencies](https://github.com/itxmjr/stock-price-forecast/blob/main/pyproject.toml): Python project · Python and package constraints. · `pyproject.toml`
- [Resolved lockfile](https://github.com/itxmjr/stock-price-forecast/blob/main/uv.lock): uv lockfile · `uv.lock`

### Forecast pipeline

- [Market data loader](https://github.com/itxmjr/stock-price-forecast/blob/main/src/data_loader.py): Python module · Owns provider access and CSV persistence. · `src/data_loader.py`
- **Yahoo Finance**: External market API · Accessed through yfinance.
- [Feature engineering](https://github.com/itxmjr/stock-price-forecast/blob/main/src/feature_engineering.py): Python module · Cleans data, creates lags, and performs the temporal split. · `src/feature_engineering.py`
- **Train/test split**: Supervised dataset · Feature matrices and targets for historical training and held-out evaluation.
- [Modeling and evaluation](https://github.com/itxmjr/stock-price-forecast/blob/main/src/modeling.py): Python module · Trains regressors and calculates MAE, RMSE, and R². · `src/modeling.py`
- **Random Forest**: Scikit-learn regressor
- **Linear Regression**: Scikit-learn regressor
- [Prediction plotting](https://github.com/itxmjr/stock-price-forecast/blob/main/src/plotting.py): Python module · Creates comparison and per-model prediction charts. · `src/plotting.py`

### Local artifacts

- [Ticker price history](https://github.com/itxmjr/stock-price-forecast/blob/main/data/AAPL.csv): CSV dataset · Working historical-price artifact. · `data/AAPL.csv`
- **Forecast charts**: PNG artifacts · Comparison, Random Forest, and Linear Regression plots.

## Connections

- Dependencies → Batch entry point: defines runtime
- Resolved lockfile → Dependencies: resolves packages
- Batch entry point → Runtime config: reads
- Batch entry point → Market data loader: orchestrates
- Runtime config → Market data loader: ticker and dates
- Market data loader → Yahoo Finance: downloads history
- Market data loader → Ticker price history: writes CSV
- Batch entry point → Feature engineering: orchestrates
- Ticker price history → Feature engineering: loads price data
- Feature engineering → Train/test split: lagged temporal split
- Batch entry point → Modeling and evaluation: orchestrates
- Train/test split → Modeling and evaluation: training and test data
- Modeling and evaluation → Random Forest: trains and evaluates
- Modeling and evaluation → Linear Regression: trains and evaluates
- Batch entry point → Prediction plotting: orchestrates
- Modeling and evaluation → Prediction plotting: actuals, predictions, metrics
- Prediction plotting → Forecast charts: writes PNGs
