How itxmjr/stock-price-forecast works
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
Components 14
Runtime
- Batch entry point · Sequential composition root invoked with python -m src.main.
src/main.py - Runtime config · Ticker, date range, and output locations.
src/config.py - Dependencies · Python and package constraints.
pyproject.toml - Resolved lockfile
uv.lock
Forecast pipeline
- Market data loader · Owns provider access and CSV persistence.
src/data_loader.py - Yahoo Finance · Accessed through yfinance.
- Feature engineering · Cleans data, creates lags, and performs the temporal split.
src/feature_engineering.py - Train/test split · Feature matrices and targets for historical training and held-out evaluation.
- Modeling and evaluation · Trains regressors and calculates MAE, RMSE, and R².
src/modeling.py - Random Forest
- Linear Regression
- Prediction plotting · Creates comparison and per-model prediction charts.
src/plotting.py
Local artifacts
- Ticker price history · Working historical-price artifact.
data/AAPL.csv - Forecast charts · Comparison, Random Forest, and Linear Regression plots.
Connections 17
- 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