Making Stock Predictions with Python

August 1, 2025

Edit 9/24/2025

I'm making a new model for stock predictions utilizing LSTM networks. This post is outdated and does not represent my current abilities.

The equation

While using mathematical formulas as simple as a taylor series won't yield accurate results and cannot account for outside circumstances that can affect the market, it can make short-term predictions for the market's movements.

In the following equation, tt represents the starting day (which would usually be 0), and hh is Δt\Delta t:

f(t+h)=f(t)+f(t)h+f(t)2!h2f(t+h)=f(t)+f'(t)h+\frac{f''(t)}{2!}h^2

In an effort to approximate these derivatives and avoid calculations by hand, the following Python snippet was used to gather all the information. Subsequently, the calculations were then done by hand.

import yfinance 
import numpy
from scipy.interpolate import CubicSpline
ticker = "^DJI"
data = yfinance.download(ticker, period="14d", interval="1d")
prices = data["Close"].values
days = numpy.arange(len(prices))
spline = CubicSpline(days, prices)
t0 = days[-1]
f_t0 = float(spline(t0))
f1_t0 = float(spline.derivative(1)(t0))
f2_t0 = float(spline.derivative(2)(t0))
f3_t0 = float(spline.derivative(3)(t0))
print(f"Day: {t0}")
print(f"Price at t0: ${f_t0:.2f}")
print(f"f'(t0) (velocity): {f1_t0:.2f}")
print(f"f''(t0) (acceleration): {f2_t0:.2f}")

However, another snippet of code can calculate the price altogether.

h = 0.5 #Any number can go here, but how much would you trust the calculation if h is too high?
predicted_price = f_t0 + f1_t0 * h + (f2_t0 / 2) * h**2

print(f"Predicted price delta t: ${predicted_price:.2f}")
print(f"Expected dollar change: ${predicted_price - f_t0:.2f}")

The full code snippet can be found here.

In use

There are many, unignorable issues with such a simple program. It doesn't take external market conditions into effect. In times of market volatility, it can be a poor tool. The main thing to keep in mind, however, is that this should only be taken as an educational exercise.

As of August 1st, 2025, these are the following numbers of the Dow Jones Industrial Average and the predictions this code makes.

Price at t0: $44130.98
f'(t0) (velocity): -495.99
f''(t0) (acceleration): -417.78
Predicted price delta t: $43830.76
Expected dollar change: $-300.22