top of page

Stock price prediction

  • Writer: Chaitanya Singh
    Chaitanya Singh
  • Jul 25
  • 2 min read
ree

Data Import and Preparation

We started by loading Microsoft’s daily closing prices, converting the date strings into real datetime objects, and setting them as our time index. This let us immediately visualize the historic price curve and spot long‑term trends and key inflection points.

Creating a Windowed Dataset

Next, we transformed the series into overlapping windows of three consecutive days to predict the following day’s price. By sliding this window forward day by day, we ensured each training example had exactly three prior values without needing to hard‑code any specific start date.

Train, Validation, and Test Splits

We preserved chronology by assigning the first 80 % of windows to our training set, the next 10 % to validation, and the final 10 % to testing. This forward‑looking split guarantees that at no point did our model see future data during training.

LSTM Model Design and Training

We built an LSTM‑based neural network that takes those three‑day sequences as input and learns temporal dependencies. Its output flows through two fully connected layers before producing a single prediction for the next day’s closing price. We trained the network to minimize mean squared error, using the validation set to monitor for overfitting and determine when to stop training.

Predictions and Performance

After training, we plotted our model’s forecasts alongside the actual prices for the training, validation, and test periods. This side‑by‑side comparison showed how well the LSTM tracked short‑term movements. We then computed the mean absolute percentage error on the test set to quantify our typical forecasting deviation.

What’s Next

To push performance further, we can extend our input window beyond three days so the model captures longer‑term patterns. We can also incorporate additional features—such as trading volume or technical indicators like moving averages—and introduce dropout or other regularization techniques to guard against overfitting. Finally, adopting a walk‑forward validation approach—continuously retraining as new data arrives—will give us a more realistic assessment of how our model would perform in live trading.


ree

Comments


LinkedIn

  • LinkedIn

© 2025 by Chaitanya Singh

bottom of page