Stock Price Prediction using Python - AskPython (2024)

Hello there! Today we are going to learn how to predict stock prices of various categories using the Python programming language.

Stock market prediction is the act of trying to determine the future value of company stock or other financial instruments traded on an exchange.

The successful prediction of a stock’s future price could yield a significant profit. In this application, we used the LSTM network to predict the closing stock price using the past 60-day stock price.

For the application, we used the machine learning technique called Long Short Term Memory (LSTM). LSTM is an artificial recurrent neural network (RNN) architecture used in the field of deep learning.

Unlike standard feed-forward neural networks, LSTM has feedback connections. It can not only process single data points (such as images), but also entire sequences of data (such as speech or video).

LSTM is widely used for the problems of sequence prediction and been very effective

Implementation of Stock Price Prediction in Python

1. Importing Modules

First step is to import all the necessary modules in the project.

import numpy as npimport pandas as pdimport matplotlib.pyplot as pltfrom keras.models import Sequentialfrom keras.layers import Dense, LSTMimport mathfrom sklearn.preprocessing import MinMaxScaler

For the project, we will be using basic modules like numpy, pandas, and matplotlib. In addition to this, we will be using some submodules of keras to create and build our model properly.

We would also require the math module for basic calculation and preprocessing module of sklearn to handle the data in a better and simpler way.

2. Loading and Preparation of Data

For the project we will be using the all_stocks_5yrs csv file which includes stock data for 5 years and has seven columns which are listed below.

  1. Date – Format of date is: “yy-mm-dd”
  2. Open – Price of the stock at open market
  3. High – Highest price reached in the day
  4. Low – Lowest price reached in the day
  5. Close – Price of the stock at the close market
  6. Volume – Number of shares traded
  7. Name – The name of the stock ticker
data=pd.read_csv("all_stocks_5yr..csv")data.head()

The head function displays first five rows of the dataset.

Stock Price Prediction using Python - AskPython (1)

3. Understanding the Data

3.1 Getting Unique Stock Names

From the whole dataset, we will first extract all the unique stock ticks name with the help of unique function. In the dataset, we have 444 different stock names.

all_stock_tick_names = data['Name'].unique()print(all_stock_tick_names)
Stock Price Prediction using Python - AskPython (2)

3.2 Extracting Data for a specific stock name

We will try to understand how the stock data works by taking an input of a stock name from the user and collecting all data of that particular stock name.

# 1. Getting a stock namestock_name = input("Enter a Stock Price Name: ")# 2. Extrating all the data having the name same as the stock name enteredall_data = data['Name'] == stock_name# 3. Putting all the rows of specific stock in a variablefinal_data = data[all_data]# 4. Printing first 5 rows of the stock data of a specific stock namefinal_data.head()
Stock Price Prediction using Python - AskPython (3)

3.3 Visualizing the stock data

To visualize the data we will be first plotting the date vs close market prices for the FITB stock for all the data points.

To make the visualization simpler, we would be plotting the same plot but for only the first 60 data points.

# Plotting date vs the close market stock pricefinal_data.plot('date','close',color="red")# Extract only top 60 rows to make the plot a little clearernew_data = final_data.head(60)# Plotting date vs the close market stock pricenew_data.plot('date','close',color="green")plt.show()
Stock Price Prediction using Python - AskPython (4)
Stock Price Prediction using Python - AskPython (5)

4. Creating a new Dataframe and Training data

To make our study easier we will only consider the closing market price and predict the closing market price using Python. The whole train data preparation is shown in the steps below. Comments are added for your reference.

# 1. Filter out the closing market price dataclose_data = final_data.filter(['close'])# 2. Convert the data into array for easy evaluationdataset = close_data.values# 3. Scale/Normalize the data to make all values between 0 and 1scaler = MinMaxScaler(feature_range=(0, 1))scaled_data = scaler.fit_transform(dataset)# 4. Creating training data size : 70% of the datatraining_data_len = math.ceil(len(dataset) *.7)train_data = scaled_data[0:training_data_len , : ]# 5. Separating the data into x and y datax_train_data=[]y_train_data =[]for i in range(60,len(train_data)): x_train_data=list(x_train_data) y_train_data=list(y_train_data) x_train_data.append(train_data[i-60:i,0]) y_train_data.append(train_data[i,0]) # 6. Converting the training x and y values to numpy arrays x_train_data1, y_train_data1 = np.array(x_train_data), np.array(y_train_data) # 7. Reshaping training s and y data to make the calculations easier x_train_data2 = np.reshape(x_train_data1, (x_train_data1.shape[0],x_train_data1.shape[1],1))

Here we create a data set to train the data that contains the closing price of 60 days ( 60 data points) so that we could do the prediction for the 61st closing price.

Now the x_train data set will contain a total of 60 values, the first column will contain from the index of 0 to 59 and the second column from the index of 1 to 60, and so on

The y_train data set will contain the 61st value at its first column located at index 60 and for the second column, it will contain the 62nd value located at index 61 and so on.

Converting both the independent and dependent train data set as x_train_data and y_train_data respectively, into the NumPy arrays so that they can be used to train the LSTM model.

Also, as the LSTM model is expecting the data in 3-dimensional data set, using reshape() function we will reshape the data in the form of 3-dimension.

5. Building LSTM Model

The LSTM model will have two LSTM layers with 50 neurons and two Dense layers, one with 25 neurons and the other with one neuron.

model = Sequential()model.add(LSTM(units=50, return_sequences=True,input_shape=(x_train_data2.shape[1],1)))model.add(LSTM(units=50, return_sequences=False))model.add(Dense(units=25))model.add(Dense(units=1))

6. Compiling the Model

The LSTM model is compiled using the mean squared error (MSE) loss function and the adam optimizer.

model.compile(optimizer='adam', loss='mean_squared_error')model.fit(x_train_data2, y_train_data1, batch_size=1, epochs=1)

Using the fit() function which is another name for train, we are training the data sets. Here, batch_size is the total number of training examples present in the single batch, and epochs are the number of iterations when an entire data set is passed forward and backward through the neural network.

Stock Price Prediction using Python - AskPython (6)

7. Testing the model on testing data

The code below will get all the rows above the training_data_len from the column of the closing price. Then convert the x_test data set into the NumPy arrays so that they can be used to train the LSTM model.

As the LSTM model is expecting the data in 3-dimensional data set, using reshape() function we will reshape the data set in the form of 3-dimension.

Using the predict() function, get the predicted values from the model using the test data. And scaler.inverse_transform() function is undoing the scaling.

# 1. Creating a dataset for testingtest_data = scaled_data[training_data_len - 60: , : ]x_test = []y_test = dataset[training_data_len : , : ]for i in range(60,len(test_data)): x_test.append(test_data[i-60:i,0])# 2. Convert the values into arrays for easier computationx_test = np.array(x_test)x_test = np.reshape(x_test, (x_test.shape[0],x_test.shape[1],1))# 3. Making predictions on the testing datapredictions = model.predict(x_test)predictions = scaler.inverse_transform(predictions)

8. Error Calculation

RMSE is the root mean squared error, which helps to measure the accuracy of the model.

rmse=np.sqrt(np.mean(((predictions- y_test)**2)))print(rmse)

The lower the value, the better the model performs. The 0 value indicates the model’s predicted values match the actual values from the test data set perfectly.

rmse value we received was 0.6505512245089267 which is decent enough.

9. Make Predictions

The final step is to plot and visualize the data. To visualize the data we use these basic functions like title, label, plot as per how we want our graph to look like.

train = data[:training_data_len]valid = data[training_data_len:]valid['Predictions'] = predictionsplt.title('Model')plt.xlabel('Date')plt.ylabel('Close')plt.plot(train['close'])plt.plot(valid[['close', 'Predictions']])plt.legend(['Train', 'Val', 'Predictions'], loc='lower right')plt.show()
Stock Price Prediction using Python - AskPython (7)

10. The Actual vs Predicted Values

Stock Price Prediction using Python - AskPython (8)

Conclusion

Congratulations! Today we learned how to predict stock prices using an LSTM model! And the values for actual (close) and predicted (predictions) prices match quite a lot.

Thank you for reading!

Stock Price Prediction using Python - AskPython (2024)

FAQs

How do you forecast stock prices in Python? ›

Stock Market Prediction Using the Long Short-Term Memory Method
  1. #Importing the Libraries import pandas as PD import NumPy as np %matplotlib inline import matplotlib. ...
  2. #Get the Dataset df=pd.read_csv(“MicrosoftStockData.csv”,na_values=['null'],index_col='Date',parse_dates=True,infer_datetime_format=True) df.head()

How to ask ChatGPT to predict stock price? ›

How to Predict Stock Price Using ChatGPT Code Interpreter?
  1. Understanding the ChatGPT Code Interpreter.
  2. Data Preparation and Exploration.
  3. Building predictive models.
  4. Evaluating Model Performance.
  5. Fine-tuning and Optimization.
  6. Complex Market Dynamics.
  7. Machine Learning Advancements.
  8. Risk Management.
Jan 29, 2024

How to use AI to predict stock price? ›

AI stock prediction software works by evaluating bulk financial data to help you have the most important data insights for stock selection. Machine learning algorithms, NLP, algorithmic strategies, and other such components help create a fine-tuned stock prediction app.

What is the best algorithm for predicting stock prices? ›

The LSTM algorithm has the ability to store historical information and is widely used in stock price prediction (Heaton et al. 2016).

What is the Python app for stock market prediction? ›

Stockastic is an ML-powered stock price prediction app built with Python and Streamlit. It utilizes machine learning models to forecast stock prices and help investors make data-driven decisions.

What is the most accurate stock predictor? ›

AltIndex – We found that AltIndex is the most accurate stock predictor for 2024. Unlike other providers in this space, AltIndex relies on alternative data points, such as social media sentiment and website analytics. It also uses artificial intelligence to convert its findings into risk-averse stock picks.

Can GPT-4 predict stocks? ›

Integration with GPT-4 API

This integration facilitates the model to analyze and predict stock prices and communicate these insights effectively to the users. The GPT-4 API, with its advanced natural language processing capabilities, can interpret complex financial data and present it in a user-friendly way.

How good is ChatGPT at predicting stocks? ›

We find that ChatGPT outperforms traditional sentiment analysis methods. More basic models such as GPT-1, GPT-2, and BERT cannot accurately forecast re- turns, indicating return predictability is an emerging capacity of complex language models. Long-short strategies based on ChatGPT-4 deliver the highest Sharpe ratio.

What is the best model to predict stock price? ›

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

Which AI is best for the stock market? ›

HoopAI, which uses powerful AI technology, is aimed to cater to individual retail investors in the stock market by providing individualized trading ideas based on tastes and needs.

Can open AI predict the stock market? ›

AI-powered systems can analyze news articles, companies' financial reports, and social media conversations in real-time. This sentiment analysis helps investors and financial institutions to gauge market sentiment and make accurate predictions based on this sentiment analysis.

Why can't AI predict stocks? ›

If the data is incomplete, biased, or outdated, the AI algorithm may not be able to accurately predict future market behavior. For example, if an AI algorithm is trained on historical data from a period of economic stability, it may struggle to predict market reactions during times of crisis or volatility.

Is there any software to predict stock prices? ›

CI Markets is an advanced stock prediction software that forecasts future price valuations. It covers over 1,600 assets from multiple global markets. This includes stock constituents from the S&P 500, NASDAQ, FTSE 100, and Nikkei 225.

How to use AI for stock trading? ›

To succeed in AI investing, traders need to have access to a variety of tools. Some essential tools include data analysis software, trading bots, and risk management tools. These tools help traders to identify patterns, automate trading, and manage risk effectively.

Which method is best for stock market prediction? ›

Some of the common indicators that predict stock prices include Moving Averages, Relative Strength Index (RSI), Bollinger Bands, and MACD (Moving Average Convergence Divergence). These indicators help traders and investors gauge trends, momentum, and potential reversal points in stock prices.

How to forecast a stock price? ›

For a beginning investor, an easier task is determining if the stock is trading lower or higher than its peers by looking at the price-to-earnings (P/E) ratio. The P/E ratio is calculated by dividing the current price per share by the most recent 12-month trailing earnings per share.

What are the mathematical methods to predict stock prices? ›

The P/E multiple or price/earnings ratio compares the closing price of the stock with the earnings of the last 12 months. A high value is often a reflection of lofty expectations of stock price and may indicate that the stock is overpriced.

How to get stock market data using Python? ›

You can use pandas_datareader or yfinance module to get the data and then can download or store in a csv file by using pandas. to_csv method. If yfinance is not installed on your computer, then run the below line of code from your Jupyter Notebook to install yfinance.

Top Articles
Latest Posts
Article information

Author: Msgr. Refugio Daniel

Last Updated:

Views: 6270

Rating: 4.3 / 5 (54 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Msgr. Refugio Daniel

Birthday: 1999-09-15

Address: 8416 Beatty Center, Derekfort, VA 72092-0500

Phone: +6838967160603

Job: Mining Executive

Hobby: Woodworking, Knitting, Fishing, Coffee roasting, Kayaking, Horseback riding, Kite flying

Introduction: My name is Msgr. Refugio Daniel, I am a fine, precious, encouraging, calm, glamorous, vivacious, friendly person who loves writing and wants to share my knowledge and understanding with you.