Skip to content

brunocucco/stock-forecasting

Repository files navigation

Stock Forecasting Using Automated Parameter-Optimization for Prophet Models and Time-Aware Bellman-Ford Strategy Planning

Overview

This project forecasts stock prices using a combination of:

  • Globally-optimized Prophet models and parameters by minimizing a custom multi-metric objective function.

  • Time-aware Bellman-Ford algorithm to optimize multi-step investment strategies dynamically across different stocks.

  • XGBoost as an alternative engine for tabular modeling. (under development)

  • Margin analysis and profit range estimation.

It supports automated model optimization, backtesting (train test split), plotting of forecast paths, strategy visualization, and margin viability analysis.


Remarkable Results

The results and statistics provided below were obtained with no human intervation. The only input to the code is the stock ticker list.

  • 1-month stock forecasting: Achieved mean R² = 0.92(median: 0.95) and MAPE = 9.86%(median:7.64%) over 50+ tickers using globally optimized models.
  • 6-months stock forecasting: Achieved mean R² = 0.90(median: 0.94) and MAPE = 11.57%(median:8.51%) over 50+ tickers using globally optimized models.
  • 1-year stock forecasting: Achieved mean R² = 0.93(median: 0.95) and MAPE = 11.80%(median:8.33%) over 50+ tickers using globally optimized models.
  • 5-year stock forecasting: Achieved mean R² = 0.92(median: 0.95) and MAPE = 11.45%(median:8.66%) over 50+ tickers using globally optimized models.

Features

  • ✅ Meta-optimized time series models. Works in parallel.
  • ✅ Fast data loading with local caching
  • ✅ Dynamic Bellman-Ford investment strategy planner
  • ✅ Visualization tools
  • ✅ Margin borrowing analysis with profitability check
  • ✅ Easy restart and resume of stock forecasting
  • ✅ Compatible with large-scale multi-ticker analysis

Installation

  • Clone the repository:

git clone https://gitlab.com/bcucco/stock-forecasting.git

cd stock-forecasting

  • Install dependencies:

pip install -r requirements.txt


Quick Start

  • Initialize the forecaster:
from forecaster import StockForecaster

tickers = ["AAPL", "MSFT", "NVDA", "AMZN"]
stock = StockForecaster(tickers, forecast_horizon="1y")
  • Download and cache stock data:

stock.data(api="yfinance", interval="1mo")

  • Train models using global optimization:

stock.model(engine="prophet", metaopt=True, cores=10, maxiter=100)

  • Run forecasts:

stock.run()

  • Generate best investment path using time-aware Bellman-Ford:

stock.bellmanford(forecast_horizon=["60d", "120d", "240d", "365d"], invest=10000.00)

  • Plot investment path:

stock.plot_bellmanford()

  • Assess margin borrowing:

stock.assess_margin()


Outputs

  • results_forecast.csv: Forecasted prices, R², and MAPE per ticker.
  • bellmanford_results.csv: Top dynamic investment strategies.
  • bellmanford_margin_analysis.csv: Margin viability report.
  • Plots:
    Forecast curves: forecast_plots/
    Strategy graphs: plot_graph.png
    Path-wise Bellman-Ford forecasts: bellmanford_plot_*.png

Project structure

.
├── forecaster.py                   # Main forecasting code
├── README.md                       # This file
├── requirements.txt                # Contains dependencies to be installed
├── data_cache/                     # (Generated) Cached historical stock data
├── forecast_plots/                 # (Generated) Forecast plots
├── results_forecast.csv            # (Generated) Forecast results per-ticker
├── bellmanford_results.csv         # (Generated) Top strategy paths
├── bellmanford_margin_analysis.csv # (Generated) Margin assessment
├── LICENSE                         # Code's License Information

License

This project is protected under the terms impose on its license. See the LICENSE file for more details.

License

Author

Code developed by B. Cucco.


About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages