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)

FAQs

How to use LSTM for stock price prediction? ›

Predicting Stock Prices with LSTM and GRU: A Step-by-Step Guide
  1. Getting the Data. To get started, we need historical stock price data. ...
  2. Data Visualization. ...
  3. Data Preprocessing. ...
  4. Creating the Training Data. ...
  5. Building the LSTM Model. ...
  6. Training the Model. ...
  7. Making Predictions. ...
  8. Visualizing the Predictions.

How to predict stock market for next day? ›

After-hours trading commonly helps indicate the next day's open. Extended-hours trading in stocks takes place on electronic markets known as ECNs before the financial markets open for the day, as well as after they close. This activity can help investors predict the open market direction.

How accurate is LSTM stock market? ›

Utilizing a Keras LSTM model to forecast stock trends

At the same time, these models don't need to reach high levels of accuracy because even 60% accuracy can deliver solid returns.

How is LSTM used in stock market? ›

LSTMs are employed to analyze time series data, like stock prices, by learning patterns and making predictions. They can process sequential data and provide insights.

How to predict if a stock will go up or down? ›

We want to know if, from the current price levels, a stock will go up or down. The best indicator of this is stock's fair price. When fair price of a stock is below its current price, the stock has good possibility to go up in times to come.

What is the best algorithm for predicting stock prices? ›

In particular, the LSTM algorithm (Long Short- Term Memory) confirms the stability and efficiency in short-term stock price forecasting.

Which stock will be bullish tomorrow? ›

BULLISH STOCKS FOR TOMORROW
Sr.Stock NameSymbol
3Firstsource Solutions LimitedFSL
4Hubtown LimitedHUBTOWN
5Aym Syntex LimitedAYMSYNTEX
6Minda Corporation LimitedMINDACORP
21 more rows

How do I choose stocks for tomorrow? ›

Here are 8 rules that traders should follow.
  1. Choose liquid stocks. ...
  2. Avoid volatile stocks. ...
  3. Invest in correlated stocks. ...
  4. Follow market trends. ...
  5. Use charting tools. ...
  6. Look for transparent companies. ...
  7. Choose stocks with a presence in the derivatives segment. ...
  8. Trade news-sensitive stocks.

What is the most accurate stock market predictor? ›

1. AltIndex – Overall Most Accurate Stock Predictor with Claimed 72% Win Rate. From our research, AltIndex is the most accurate stock predictor to consider today. Unlike other predictor services, AltIndex doesn't rely on manual research or analysis.

How can I improve my LSTM prediction? ›

Another way to optimize your LSTM model is to use hyperparameter optimization, which is a process that involves searching for the best combination of values for the parameters that control the behavior and performance of the model, such as the number of layers, units, epochs, learning rate, or activation function.

Which AI is best for predicting stock prices? ›

10 AI Tools for Stock Trading & Price Predictions
  • Top 10 AI Tools for Stock Trading in 2024.
  • EquBot.
  • Trade Ideas.
  • TrendSpider.
  • Tradier.
  • QuantConnect.
  • Sentient Trader.
  • Awesome Oscillator.
Jun 4, 2024

What is the LSTM trading strategy? ›

The integration of LSTM aims to improve the ability to predict trading signals accurately. In basic trading strategies, instead of using the current day's closing price to generate technical indicators and make trading decisions, an LSTM model is applied to predict the future closing price.

How to use Python in stock market? ›

  1. 3 Basic Steps of Stock Market Analysis in Python. Roman Orac. ...
  2. Get the Stock Data. The easiest way to download the stock's historical data in Python is with yfinance package. ...
  3. Calculate trading indicators. ...
  4. Plot the stock data.

When should LSTM be used? ›

LSTMs are predominantly used to learn, process, and classify sequential data because these networks can learn long-term dependencies between time steps of data. Common LSTM applications include sentiment analysis, language modeling, speech recognition, and video analysis.

What are the advantages of LSTM in stock prediction? ›

LSTM, short for Long Short-term Memory, is a robust algorithm for time series. It can capture historical trend patterns and predict future values with high accuracy.

How to use LSTM for prediction? ›

Time Series Prediction with LSTM
  1. Step 1: Load the Time Series Data.
  2. Step 2: Normalize the Time Series Data.
  3. Step 3: Split the Time Series Data into Training and Testing Sets.
  4. Step 4: Convert the Time Series Data to PyTorch Tensors.
  5. The next step after loading and preprocessing the data is to define the LSTM model.
Mar 25, 2023

Is ARIMA better than LSTM for stock market prediction? ›

The LSTM model provides better results when the data set is large and has fewer Nan values. Whereas, despite providing better accuracy than LSTM, the ARIMA model requires more time in terms of processing and works well when all the attributes of the data set provide legitimate values.

What is the most accurate stock predictor? ›

Capital Economics has been named the most accurate forecaster of major global stock indices in Reuters polls. The 2023 LSEG StarMine Award was given for forecasting accuracy across 11 equities benchmarks and reflects the breadth and depth of our global coverage of macro and markets.

Top Articles
Get Paid to Watch Ads - JumpTask
How Long Do Pending Transactions Take?
Star Wars Mongol Heleer
Ffxiv Shelfeye Reaver
Obor Guide Osrs
Ross Dress For Less Hiring Near Me
America Cuevas Desnuda
Horoscopes and Astrology by Yasmin Boland - Yahoo Lifestyle
10000 Divided By 5
J Prince Steps Over Takeoff
Strange World Showtimes Near Cmx Downtown At The Gardens 16
Prices Way Too High Crossword Clue
Dityship
Wnem Radar
Caresha Please Discount Code
Charmeck Arrest Inquiry
RBT Exam: What to Expect
Craigslist List Albuquerque: Your Ultimate Guide to Buying, Selling, and Finding Everything - First Republic Craigslist
Colorado mayor, police respond to Trump's claims that Venezuelan gang is 'taking over'
Best Nail Salon Rome Ga
Paradise leaked: An analysis of offshore data leaks
St Maries Idaho Craigslist
Jeff Now Phone Number
European Wax Center Toms River Reviews
Cb2 South Coast Plaza
Harbor Freight Tax Exempt Portal
They Cloned Tyrone Showtimes Near Showbiz Cinemas - Kingwood
Tottenham Blog Aggregator
Helpers Needed At Once Bug Fables
Tokioof
Perry Inhofe Mansion
Renfield Showtimes Near Marquee Cinemas - Wakefield 12
L'alternativa - co*cktail Bar On The Pier
No Hard Feelings Showtimes Near Tilton Square Theatre
Eastern New Mexico News Obituaries
3496 W Little League Dr San Bernardino Ca 92407
Bernie Platt, former Cherry Hill mayor and funeral home magnate, has died at 90
Bianca Belair: Age, Husband, Height & More To Know
Flags Half Staff Today Wisconsin
Craigslist Mexicali Cars And Trucks - By Owner
140000 Kilometers To Miles
2132815089
If You're Getting Your Nails Done, You Absolutely Need to Tip—Here's How Much
Tricare Dermatologists Near Me
Quaally.shop
Craigslist Pets Charleston Wv
Freightliner Cascadia Clutch Replacement Cost
Immobiliare di Felice| Appartamento | Appartamento in vendita Porto San
How Did Natalie Earnheart Lose Weight
Unbiased Thrive Cat Food Review In 2024 - Cats.com
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.