Predicting the Stock Market with LSTM via TensorFlow in Python: Bullish or Bearish Tomorrow? (2024)

Predicting the Stock Market with LSTM via TensorFlow in Python: Bullish or Bearish Tomorrow? (2)

This article will demonstrate how to predict the stock market using an LSTM neural network via TensorFlow in Python, which is a popular method in the financial industry. Historically, many traders on Wall Street used various technical indicators and strategies for technical analysis in trading. However, with advancements in AI and ML such as LSTM neural networks, has emerged as a way to predict the stock market.

The focus is to provide practical implementation and helpful tips for using the technique of price prediction for the next day. However, with a small change in the parameters, you can also make predictions for the next n days. Keep reading…

To understand the underlying theory of LSTM (Long Short Term Memory) which is a powerful variant of RNN (Recurrent Neural Network) I suggest you to check the wiki page here: https://en.wikipedia.org/wiki/Long_short-term_memory

For those that want to jump in the code straight, here is the Github link:

https://github.com/the-tyler/Stock_Price_Prediction_LSTM

Install the libraries that you may not have using the links below.

  1. TensorFlow (https://pypi.org/project/tensorflow/)
  2. Scikit-learn (https://pypi.org/project/scikit-learn/)
  3. Yahoo Finance (https://pypi.org/project/yahoo-finance/)

For re-usability of the codes, I embraced object oriented programming philosophy as much as possible. Therefore, you will see modular functions to perform tasks in the codebase. You can modify the parameters such as stock ticker and batch size to align them with your own preference.

Note: Memory of Jupyter Notebook is incapable to handle TensorFlow. I recommend using Pycharm or any other powerful IDE to run the codes.

This is the first step for most ML projects. We import the Python libraries that are dominantly used in data pre-processing such as pandas and numpy as well as the libraries that are installed above.

# Data Pre-processing & Extraction
import pandas as pd
import numpy as np
from datetime import datetime
from dateutil.relativedelta import relativedelta

from yahoo_fin import stock_info as yf
from sklearn.preprocessing import RobustScaler
from collections import deque

# Modelling
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense, LSTM, Dropout

# Visualization
import matplotlib.pyplot as plt

We will predict the stock price of Roku which is listed on NASDAQ under the symbol ‘ROKU’. First, we pull the daily price data from Yahoo Finance using Extract_Data function. Extract_Data parameters are STOCK, YEAR_BACK and INTERVAL which are explained in details below. Side note, if you are looking for some real-time data extraction straight to the Jupyter Notebook, check this article out: https://medium.com/@onersarpnalcin/real-time-limit-order-book-from-kraken-on-jupyter-notebook-908f11f94a7f

STOCK: The ticker symbol of the stock to be predicted, ‘ROKU’

YEAR_BACK: Number of years of data back from today

INTERVAL: Frequency of the extracted data. 1d: daily, 4h: 4 hourly

STOCK = 'ROKU' # Specify the ticker symbol of the share

YEAR_BACK = 1 # Number of years of data back from today for extraction

INTERVAL = '1d' # The frequency of the extracted data. 1d: daily, 4h: 4 hourly

# Default parameters are seen in the function definition
def Extract_Data(Stock = 'ROKU', year_back = 1, interval = '1d'):

STOCK = Stock
now = datetime.now() # Current date and time
DATE_NOW = now.strftime('%Y-%m-%d') # Extract the today's date as a string
STARTING_DATE = (now - relativedelta(years=year_back)).strftime('%Y-%m-%d') # Extract the starting date as a string

raw_price_df = yf.get_data(STOCK, start_date = STARTING_DATE, end_date = DATE_NOW, interval = interval)

return raw_price_df

Then, we visualize the price movement of the stock by Visualize function.

# Visualize the price data
def Visualize_Data(df, Stock, interval, figsize = (20,9)):

plt.style.use(style='ggplot')
plt.figure(figsize=figsize)
plt.plot(df['close'])
plt.xlabel('Date')
plt.ylabel('Price in $')
plt.legend([f'Price per {Stock} share'])
plt.title(f'Share price of {Stock}')
plt.show()

# Call Extract_Data to pull the data and then call Visualize_Data to plot chart
raw_price_df = Extract_Data(Stock = STOCK, year_back = YEAR_BACK, interval = INTERVAL )
Visualize_Data(raw_price_df, Stock = STOCK, interval = INTERVAL)

You can see the price movement of the ROKU stock for the last 1 year below

Predicting the Stock Market with LSTM via TensorFlow in Python: Bullish or Bearish Tomorrow? (3)

As we plotted the price and built an understanding on its movement, we can proceed to data pre-processing. For this project, we use the closing price to train the LSTM neural network. Thus, we filter in the closing price and the date columns and drop the rest. Because most machine learning algorithms work better with scaled data sets, we use Robust Scaler from scikit-learn to scale the raw price. We create a new column to store the scaled closing price data.

If you want to dig into the difference between Standard Scaler, MinMax Scaler and Robust Scaler, check this short article out: https://medium.com/@onersarpnalcin/standardscaler-vs-minmaxscaler-vs-robustscaler-which-one-to-use-for-your-next-ml-project-ae5b44f571b9

# Filter out the columns that will not be used by LSTM (We will use close price)
raw_price_df = raw_price_df.drop(['open', 'high', 'low', 'adjclose', 'ticker', 'volume'], axis=1)

# Create 'date' column using the index
raw_price_df['date'] = raw_price_df.index

# Scale the raw price data on a new column
scaler = RobustScaler()
raw_price_df['scaled_close'] = scaler.fit_transform(np.expand_dims(raw_price_df['close'].values, axis=1))

We continue with data pre-processing with Prepare_Data function. This function is designed to prepare the training data format for LSTM neural network given the dataframe and the number of days to predict. In more details, Prepare_Data divides the price data into small sequences to create training set and a target value. It uses the last m days price as X_train and m+1 as the Y (target value) to build a sequence. It iterates over the whole data set, including the last sequence and stores them for model training and prediction.

# 'Prepare_Data' function puts the data set correct form for LSTM
def Prepare_Data(dataframe, days):

df = dataframe.copy()
df['future'] = df['scaled_close'].shift(-days)
last_sequence = np.array(df[['scaled_close']].tail(days))
df.dropna(inplace=True)

sequence_data = []
sequences = deque(maxlen=NUMBER_of_STEPS_BACK)

for entry, target in zip(df[['scaled_close','date']].values, df['future'].values):
sequences.append(entry)
if len(sequences) == NUMBER_of_STEPS_BACK:
sequence_data.append([np.array(sequences), target])

last_sequence = list([s[:1] for s in sequences]) + list(last_sequence)
last_sequence = np.array(last_sequence).astype(np.float32)

# build X and Y training set
X, Y = [], []
for seq, target in sequence_data:
X.append(seq)
Y.append(target)

# convert X and Y to numpy arrays for compatibility
X = np.array(X)
Y = np.array(Y)

return last_sequence, X, Y

Predicting the Stock Market with LSTM via TensorFlow in Python: Bullish or Bearish Tomorrow? (4)

LSTM is notorious for the number of hyperparameters it has. We first go over the the parameters/hyperparameters and explain what they do. Then, we configure them. Finally, we train the LSTM neural network with the set values.

Configuration of the LSTM

The following dictionary walks you over the parameters and hyperparameters that we use to build LSTM neural network.

NUMBER_of_STEPS_BACK: The depth of the neural network. In other words, the number of days in x_train the model will be trained for

PREDICTION_STEPS: An array that defines the number of days to be predicted. For instance, when it is equal to [1], it is set to predict the price of next day. If it is [1, 2], then it will predict the price of tomorrow and the day after tomorrow.

BATCH_SIZE: Number of training samples that will be passed to the network in one epoch

DROP_OUT: Probability to exclude the input and recurrent connections to improve performance by regularization

UNITS: Number of neurons connected to the layer

EPOCHS: Number of times that the learning algorithm will work through the entire training set

LOSS: Methodology to measure the inaccuracy

OPTIMIZER: Optimizer used to iterate to better states

Below, you can see the set values for the parameters/hyperparameters. You can customize these values and try boosting your performance.

# Before we run the LSTM neural network, we have to make configurations on some hyperparameters

# Number of days back that the model will be trained for
NUMBER_of_STEPS_BACK = 30

# Number of days that the model will predict. To predict the next three days, modify it as follows: [1,2,3]
PREDICTION_STEPS = [1]

# Number of training samples that will be passed to the network in one epoch
BATCH_SIZE = 16

# Probability to exclude the input and recurrent connections to improve performance by regularization (25%)
DROPOUT = 0.25

# Number of neurons connected to the layer
UNITS = 60

# Number of times that the learning algorithm will work through the entire training set
EPOCHS = 10

# Methodology to measure the inaccuracy
LOSS='mean_squared_error'

# Optimizer used to iterate to better states
OPTIMIZER='adam'

Training of the LSTM

Train_Model function trains an LSTM neural network that has 5 layers in total. The model consists of 2 LSTM layers, 2 Dropout layers and 1 Dense layer. The ‘x_train’ parameter is the array of data prepared for the model to train on, and ‘y_train’ is the target column that is the target price during training. The model has 60 (UNITS) neurons on the LSTM layers, dropout layers with 25% (DROP_OUT) probability of neurons dropped. The Dense layer has 1 neuron that is the result of the last step of the model. The model is trained for 10 (EPOCHS) epochs with a batch size of 16 (BATCH_SIZE). For compilation of the model, mean squared error (LOSS) is used as the loss metric and the famous adam (OPTIMIZER) optimizer.

# To train the 5 layer LSTM model with set hyperparameters, 'Train_Model' function is implemented
def Train_Model(x_train, y_train, NUMBER_of_STEPS_BACK, BATCH_SIZE, UNITS, EPOCHS, DROPOUT, OPTIMIZER, LOSS):

model = Sequential()

model.add(LSTM(UNITS, return_sequences=True, input_shape=(NUMBER_of_STEPS_BACK, 1)))
model.add(Dropout(DROPOUT))
model.add(LSTM(UNITS, return_sequences=False))
model.add(Dropout(DROPOUT))
model.add(Dense(1)) # Makes sure that for each day, there is only one prediction

model.compile(loss=LOSS, optimizer=OPTIMIZER)

model.fit(x_train, y_train, epochs=EPOCHS, batch_size=BATCH_SIZE, verbose=1)

model.summary()

return model

Predicting the Stock Market with LSTM via TensorFlow in Python: Bullish or Bearish Tomorrow? (5)

When the training of the model is over, we make the prediction, check the loss function output to evaluate the performance, inverse scale to get the raw price value and print the final result.

# Make Prediction
predictions = []

for step in PREDICTION_STEPS:
last_sequence, x_train, y_train = Prepare_Data(raw_price_df, step)
x_train = x_train[:, :, :1].astype(np.float32)

model = Train_Model(x_train, y_train, NUMBER_of_STEPS_BACK, BATCH_SIZE, UNITS, EPOCHS, DROPOUT, OPTIMIZER, LOSS)

last_sequence = last_sequence[-NUMBER_of_STEPS_BACK:]
last_sequence = np.expand_dims(last_sequence, axis=0)
prediction = model.predict(last_sequence)
predicted_price = scaler.inverse_transform(prediction)[0][0]

predictions.append(round(float(predicted_price), 2))

The final epoch’s loss function shows the final result of the performance. For this project, it is below that 0.0292 which means there is 2.92% inaccuracy in the prediction. This result may change even for the same data set due to the random nature of the adam optimizer.

To print the tomorrow’s price prediction by the model, you run this final code and you are finished!

if len(predictions) > 0:
predictions_list = [str(d)+'$' for d in predictions]
predictions_str = ', '.join(predictions_list)
message = f'{STOCK} share price prediction for the next day(s) {predictions_str}'
print(message)

In this article, we built a stock price prediction model using LSTM via Tensorflow in Python. If you like it, do not forget to hit the clap below.

Disclaimer: Statements are for educational purposes only. Nothing contained herein is intended to be or to be constructed as financial advice.

Predicting the Stock Market with LSTM via TensorFlow in Python: Bullish or Bearish Tomorrow? (2024)
Top Articles
Latest Posts
Article information

Author: Delena Feil

Last Updated:

Views: 5884

Rating: 4.4 / 5 (65 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Delena Feil

Birthday: 1998-08-29

Address: 747 Lubowitz Run, Sidmouth, HI 90646-5543

Phone: +99513241752844

Job: Design Supervisor

Hobby: Digital arts, Lacemaking, Air sports, Running, Scouting, Shooting, Puzzles

Introduction: My name is Delena Feil, I am a clean, splendid, calm, fancy, jolly, bright, faithful person who loves writing and wants to share my knowledge and understanding with you.