Skip to content
Tutorials Center
My learning

Topics

Lesson 103 of 111 ยท Projects

Project: Stock and Crypto Price Prediction

Predict stock or crypto direction on a time series: why price R2 is a trap, walk-forward validation, look-ahead leakage and a backtest with trading costs.

"Predict the price of a stock, Bitcoin or Dogecoin with machine learning" is probably the most popular beginner project, and the one where beginners most often fool themselves. A model that "predicts the price with 97% accuracy" is almost always an illusion. This project walks through the whole task on a time series, and shows precisely where the illusions come from, so that you can build such a model without believing in it more than it deserves.

This lesson is about modeling and evaluation techniques. It is not investment advice. Markets are noisy, and nothing here is a trading strategy.

We use a synthetic price series that behaves like a real daily market: a small upward drift, volatility that comes in clusters (calm spells and turbulent spells), a faint momentum effect and a daily volume that rises when prices move a lot. The same code works on real data from any source: replace make_prices() with a table that has a close and a volume column and a date index. Real data from stocks (for example a large software company), Bitcoin or Dogecoin behaves the same way at the daily level, with even more volatility for the coins.

You will:

  1. see why predicting the price level is a trap and predict returns instead,
  2. build technical features (lagged returns, momentum, moving-average gap, volatility, RSI) and evaluate with walk-forward validation,
  3. reproduce the classic look-ahead leak and run a backtest with trading costs.

Related lessons: time series with machine learning, stationarity and exponential smoothing, cross-validation for special data and leakage.

Step 1: The price-level trap

Regress tomorrow's price on today's price, and you get a spectacular Rยฒ. But a rule that needs no learning at all ("tomorrow's price equals today's price") scores the same, because prices change by only a small percentage from one day to the next. The Rยฒ is high because the level is easy to predict, not because the model understands anything. The thing worth predicting is the change: the return, today / yesterday - 1.

Python Step 1: price level versus returns

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The linear model predicts tomorrow's price with an Rยฒ of 0.971, and the "no-learning" rule gets 0.971 as well: the model has added nothing. When we switch to the quantity that matters, predicting tomorrow's return from today's, the Rยฒ is 0.004, close to zero. The returns also show the two facts that shape every modeling decision in finance: they are nearly uncorrelated with their own past (lag-1 autocorrelation of 0.06), but their size is highly predictable (the autocorrelation of absolute returns is 0.25), because volatility clusters. So predicting how much the price will move is much easier than predicting which way. The daily direction is nearly a coin flip: here 48.7% of days are up.

Step 2: Features and walk-forward validation

We turn the question into classification: will tomorrow's close be higher than today's? The features use only information available at the close of day t:

  • the last few daily returns (lags 1, 2, 3 and 5),
  • 10-day momentum and the gap between the price and its 20-day moving average,
  • 10-day volatility and the volume relative to its 20-day average,
  • the relative strength index (RSI), a classic indicator based on the ratio of recent gains to recent losses.

For validation we use TimeSeriesSplit: each fold trains on the past and tests on the next 250 trading days (about a year). Random K-fold would let the model train on days after the ones it is tested on. We compare with the baseline "always predict up".

Python Step 2: four models, six walk-forward windows

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The baseline hovers around 50% (the share of up days changes by window, from 47% to 53%). The best of our models, the logistic regression, reaches 52.6% on average, a small edge that comes from the faint momentum in the generated data. The random forest and the SVM, flexible enough to fit noise, do no better than the baseline. This is what a real, honest result usually looks like in daily price prediction: an edge of one or two percentage points at best, and not stable from window to window (look at how the logistic regression ranges between 50.0% and 54.4%). A model for which you see 60% or more should make you suspicious, not happy.

Step 3: Leakage and a backtest with costs

Two experiments show how an impressive result can arise from a mistake.

Look-ahead leakage. Pandas can compute a "centered" moving average (rolling(5, center=True)), which averages the two days before and the two days after each date. Using it as a feature is a common bug: it quietly hands the model tomorrow's price. With the same logistic regression and the same walk-forward evaluation, accuracy jumps from about 51% to about 87%, and the result is worthless in practice, because tomorrow's price will not be known when you need the prediction. Shuffled K-fold on clean features gives a different kind of nonsense (here 49.8%): the folds mix past and future, and the scores no longer describe how the model would perform going forward.

The backtest. An accuracy number is not a profit number. We turn the model's out-of-sample predictions into a simple strategy: hold the asset on days when the model predicts "up", hold cash otherwise, and charge 0.10% for each change of position (fees and the spread between buying and selling prices). We compare with simply buying and holding.

Python Step 3: look-ahead leakage and a backtest with trading costs

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In this run the model trades 448 times in 1,500 days and is invested only 23% of the time. Without costs it turns 1.00 into 1.43, against 2.01 for buy-and-hold: lower, because it sits in cash on most days of a market that drifts upward. With costs the strategy ends at 0.91, below its starting value, and its Sharpe ratio (return per unit of risk) falls to about zero. Costs eat a small, noisy edge completely. These are not lucky or unlucky numbers: on real markets the combination of weak signals, turnover and costs is exactly why very few such models survive in practice.

What to do with this

  • Start with the right target. Predict returns, direction, or volatility (the most predictable of the three), never the raw price level.
  • Always compare with simple baselines: "always up", "same as yesterday", buy-and-hold.
  • Use walk-forward validation and keep the final test period untouched until the end (cross-validation for special data).
  • Audit every feature for look-ahead: can I compute it with only data up to the close of day t? Beware of centered windows, scalers fitted on the whole series, and features that use the same day's high and low when predicting that day's close.
  • Include costs, slippage and position sizing in any backtest, and test on a period the model never saw.
  • Be skeptical of tutorials that show a smooth prediction curve hugging the real price: those curves predict the level, and they look good for the reasons in Step 1.
  • More advanced tools (read-only here): recurrent networks such as LSTMs, forecasting libraries such as Prophet or ARIMA/SARIMA models (time series), and gradient boosting on lagged features. They share all the traps above, and they need more data and more discipline, not less.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A model predicts tomorrow's closing price from today's with R2 = 0.97. Why is this not impressive?

    The price level is dominated by its own recent value. Compare with the no-learning baseline and predict returns instead.
  2. 2. Why is TimeSeriesSplit preferred over shuffled K-fold for price data?

    Shuffled folds mix past and future days, so the model is tested on periods it effectively already saw.
  3. 3. Which feature creates look-ahead leakage when predicting tomorrow's direction?

    A centered window uses future values. A feature must be computable from information up to the prediction time.
  4. 4. In the backtest, why did the strategy lose money after costs even though accuracy was above 50%?

    A small advantage in direction does not translate into profit when each trade costs money and the winning and losing days differ in size.

Summary

  • Predicting the price level looks accurate but adds nothing over "same as yesterday"; predict returns, direction or volatility.
  • Returns have almost no autocorrelation, but their size clusters, so volatility is much easier to forecast than direction.
  • Use walk-forward validation, simple baselines and a final untouched test period.
  • Audit every feature for look-ahead: a centered window or a scaler fitted on the whole series can turn 51% into 87%.
  • A backtest must include trading costs; small, noisy edges usually disappear once costs are paid.

Next: a project about advertising instead of markets: Click-Through Rate Prediction.