logo image of EssayBoard website
x
feature image of Predicting Next Day Stock Price With Chat GPT

Predicting Next Day Stock Price With Chat GPT   

Just another day I converse with Chat GPT!  This time, the topic is about how to predict the stock price 1 day into the future.  Obviously, in reality, I do know this is mission impossible even though Chat GPT might suggest a really good algorithm for this specific task.  Why?  Personally, I believe that historical data can only suggest what might repeat but the probability of such a repeat might not be all that high.  Furthermore, a repeat doesn't mean a projection.  In our case, we want to know the projection of the next day's stock price of a stock ticker.  Even if there is an algorithm that could figure out a pattern of how much a stock price could be increased the next day, could it predict the behavior of the complex market's irrational moves?  This complex market nowadays contains both human irrational behaviors and machine-learning models, and this means the market got way too many variables; trends can be reversed or accelerated at a much faster speed too since we got machines doing the bidding also.

Regardless, I was excited to see what Chat GPT could come up with this request.  Chat GPT was able to suggest a complete script for predicting the next day's stock price.  But I had to rewrite it a bit since it didn't account for how to format the specific stock data I got which relies on yfinance module.  Also, there was an error in the script I'd to converse with Chat GPT for it to correct the mistake.  In the end, the product is below:

import pandas as pd
from sklearn.linear_model import LinearRegression
import numpy as np

import yfinance as yf 

df = yf.download("TSLA", start="2022-01-01", end="2023-01-10")

# create a Linear Regression model
model = LinearRegression()

# fit the model to the training data
x_train = (df.index - df.index[0]).astype("timedelta64[D]").values.reshape(-1, 1)
y_train = df['Adj Close'].values

model.fit(x_train, y_train)

# make a prediction for the next day
x_test = np.array([x_train[-1] + 1]).reshape(-1, 1)
prediction = model.predict(x_test)[0]

print(f'Predicted stock price for the next day: {prediction}')

I also asked Chat GPT:

So, let's assume that this is one awesome prediction model of stock price, but I want it to be more accurate, what steps do I need to take to improve it?

It answered:

There are many different ways to improve the accuracy of a stock price prediction model, but here are a few steps you can take to increase its performance:

  1. Use more data: The more data you have, the better your model will be at capturing patterns in the stock market. You can try using longer historical data, or using more data from different sources.

  2. Use more features: The date index is a simple feature, it may not capture all the variations in the stock prices, you can use more relevant features such as the opening price, the volume, the moving averages, etc. These features can be used to reflect the market trend and company-specific events, or other external factors, like the overall market trend, interest rate, and political events.

  3. Try different models: Linear regression is a simple model, it may not be the best model to use for stock price prediction. you can try other models such as Random Forest, Support Vector Machine, Neural Network models, etc.

  4. Use a combination of models: You can try using an ensemble of multiple models to make a prediction. The idea behind ensemble models is to combine the predictions of multiple models to improve the accuracy.

  5. Tune the model's parameters: Every model has a set of parameters that can be adjusted, you can use techniques such as Grid Search and Random Search to find the best set of parameters for your model and data.

  6. Cross-validation: You need to split your data into training and testing sets, train the model on the training set, and then evaluate its performance on the testing set, this way you can evaluate the model performance on unseen data, and also you can use techniques like k-fold cross-validation to get a more robust evaluation of the model performance.

That's all folks!  Just remember, this is a fun exercise!  Don't take this seriously because the script might be very flawed, and the linear regression model might not be all that great in using in predicting a stock price.  Furthermore, prediction is a very risky game as often prediction doesn't turn out right.  More often than not, the prediction might be way off, and if anyone is too serious about this could get really hurt.  Of course, if you're super smart and got an ensemble of algorithms that work in tandem in a specific machine learning model that you are the creator of, then the more power to you if you're confident in what you've created.

The YouTube video below is about someone else writing a next-day stock price prediction script in Python also.  Check it out if you like.

profile image of Vinh Nguyen

Famous quote by:   
William Shakespeare

“Love all, trust a few, do wrong to none.”

Post Comments