Stock Prediction

Real-Time Stock Prediction with Calibrated Volatility Forecasts

An online learning system that streams market data, forecasts each stock's next move and its likely range, and scores every forecast out-of-sample as it happens. It finds a real edge in volatility, and reports honestly that minute-level direction has none.

+0.41 to +0.75volatility skill against historical volatility
94–95%of outcomes inside the 95% band
≈ 0direction skill, gated, so no false confidence
Every forecastscored out-of-sample, test-then-train

Overview—The system ingests live trades or bars, builds features incrementally, and runs two online models per symbol. One predicts the return, the other the volatility. Each forecast is scored only once its outcome arrives, and only then do the models learn from it, so every reported number is out-of-sample. On 60 days of 5-minute data for five US stocks, the volatility model beats historical volatility on every symbol, and its price bands hold their stated coverage. The return model has no edge, so a skill gate keeps it quiet unless it has recently earned trust.

IThe Problem

Most stock-prediction demos report accuracy from a backtest that quietly saw the future, or from a model tuned on the period it is tested on. At minute horizons, price direction is close to a coin flip, so a model that always answers will often be wrong while sounding certain.

This project asks two separate questions, which way and how far, and scores both against honest baselines. It forecasts direction only when there is evidence of an edge.

IIHow It Works

  • StreamFinnhub trades are aggregated into bars and signed buy or sell by the tick rule. Yahoo Finance polling and a simulator are also supported.
  • FeaturesComputed incrementally and scale-free: lagged returns, RSI and order-flow imbalance; realized volatility over 1, 5 and 20 bars, high–low range, and time of day.
  • ModelsTwo online models learn bar by bar: an adaptive linear model for return, and a HAR-style regression on log volatility, calibrated for QLIKE loss.
  • Skill gateA direction call is shown only while the model has beaten "no change" over its last 300 forecasts. Otherwise the forecast is flat.
  • BandForecast ± q·volatility, with q calibrated from recent errors (conformal), so a 95% band contains about 95% of outcomes.
Pipeline: market data, bars, features, online models, calibration, forecast, then API and dashboard. Forecasts wait in scoring until the outcome arrives h bars later, then the models learn. Market data Finnhub · Yahoo · sim Bars OHLCV · order flow Features returns · HAR vol Online models return · volatility Calibration skill gate · conformal band Forecast return · vol · price band Scoring after h bars wait realized outcome learn FastAPI REST · WebSocket Dashboard live charts · metrics
learned online calibration deterministic stage test-then-train loop
Fig. 1. The forecasting pipeline. A forecast waits in scoring until its outcome arrives h bars later. Only then is it scored and used for learning, so no model ever trains on data it is later tested on.

IIIResults

Skill is 1 − loss ÷ baseline loss, so positive means better than the baseline. Volatility is scored with QLIKE against 20-bar historical volatility, and direction with absolute error against "no change".

Table IOut-of-sample results, 60 days of 5-minute bars

SymbolVol skill, 1 barVol skill, 6 bars95% band coverageDirection skill, raw → gated
AAPL+0.57+0.7595.1%−0.038 → −0.002
MSFT+0.50+0.7295.0%−0.043 → −0.001
SPY+0.41+0.6895.0%−0.017 → −0.002
TSLA+0.46+0.7494.9%−0.022 → −0.005
NVDA+0.50+0.7494.9%−0.032 → −0.001

Band coverage and direction skill are for the 1-bar horizon. At 6 bars, coverage is 94.3–94.8%. On 1-minute bars, volatility skill is lower but still positive in most runs (+0.03 to +0.31).

Volatility is predictable

The model beats historical volatility on every symbol and horizon tested here. Most of the gain comes from intraday seasonality and overnight gaps, which a rolling window misses.

Direction is not

The raw return model loses to "no change" everywhere. The gate keeps it flat about 90% of the time, which shrinks the cost to at most 0.005.

Calibration holds

Bands are sized from the volatility forecast, then calibrated on recent errors, so the stated 95% matches what actually happens.

Negative results, reported

SPY as a cross-asset reference and a bar-based order-flow estimate did not improve direction skill, so they are opt-in or neutral.

IVLive Service

One command runs the pipeline in the background and serves forecasts as they are made.

Live dashboard showing AAPL, MSFT and SPY cards with price charts, shaded 95% forecast bands, flat signals, and metrics: volatility skill +0.03 to +0.16 and band coverage 94.8 to 95.1 percent.
Fig. 2. The live dashboard on Yahoo Finance 1-minute data, resumed from a model trained on the previous four days. The shaded band is plotted at the time it applies to. The signal is flat because the model has not earned a direction call.

REST and WebSocket

Endpoints for forecasts, metrics and status. Each new forecast is pushed over a WebSocket.

Resumable

Learned state is saved atomically and restored on restart. Replayed bars are skipped, so nothing is learned twice.

Real streaming

Finnhub trades become bars about a second after each interval closes. Yahoo bars arrive 3–6 s after each minute.

Tested

30 tests, including checks that the models recover a known signal from synthetic data and stay quiet when there is none.

VTechnology

ModelsOnline HAR and Huber regression in NumPy
DataFinnhub WebSocket, Yahoo Finance
ServiceFastAPI, Uvicorn, WebSockets
Front endHTML, Chart.js
Toolinguv, pytest, ruff

Explore the code

The full source is on GitHub. No API key is needed to start: uv run stockpred serve --source sim runs the live dashboard on synthetic data.