r/PythonProjects2 • • Apr 15 '20

I have created a stock prediction program in python, but it's not always accurate. How would I fix this?

The program is usually able to predict the open and close price within 10 points. Is there any way I can make this margin of error smaller? Right now the most accurate model that works is the SVR RBF model. Any help would be greatly appreciated. Thank you.

Here is the code I have right now:

#Import the libraries
import pandas as pd
import numpy as np
from sklearn.svm import SVR
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
from sklearn.svm import SVR
from sklearn.model_selection import train_test_split

#Load the data
#from google.colab import files # Use to load data on Google Colab
#uploaded = files.upload() # Use to load data on Google Colab
df = pd.read_csv('AAPL.csv')
print(df)

#Create the lists / X and Y data sets
dates = []
prices = []


#Get all of the rows from the Date Column
df_dates = df.loc[:, 'Date']
#Get all of the rows from the Open Column
df_open = df.loc[:, 'Open']

#Create the independent data set X
for date in df_dates:
  dates.append( [int(date.split('-')[2])])
  
#Create the dependent data set 'y'
for open_price in df_open:
  prices.append(float(open_price))

#See what days were recorded
print(dates)

def predict_prices(dates, prices, x):
  
  #Create the 3 Support Vector Regression models
  svr_lin = SVR(kernel='linear', C= 1e3)
  svr_poly= SVR(kernel='poly', C=1e3, degree=2)
  svr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1)
  
  #Train the SVR models 
  svr_lin.fit(dates,prices)
  svr_poly.fit(dates,prices)
  svr_rbf.fit(dates,prices)
      
  #Create the Linear Regression model
  lin_reg = LinearRegression()
  #Train the Linear Regression model
  lin_reg.fit(dates,prices)
  
  #Plot the models on a graph to see which has the best fit
  plt.scatter(dates, prices, color='black', label='Data')
  plt.plot(dates, svr_rbf.predict(dates), color='red', label='SVR RBF')
  plt.plot(dates, svr_poly.predict(dates), color='blue', label='SVR Poly')
  plt.plot(dates, svr_lin.predict(dates), color='green', label='SVR Linear')
  plt.plot(dates, lin_reg.predict(dates), color='orange', label='Linear Reg')
  plt.xlabel('Days')
  plt.ylabel('Price')
  plt.title('Regression')
  plt.legend()
  plt.show()
  
  return svr_rbf.predict(x)[0], svr_lin.predict(x)[0],svr_poly.predict(x)[0],lin_reg.predict(x)[0]


#Predict the price of AAPL on day x
predicted_price = predict_prices(dates, prices, [[36]])
print(predicted_price)
2 Upvotes

5 comments sorted by

4

u/cfeld15 Apr 15 '20

Is this for school...?

0

u/[deleted] Apr 15 '20

[deleted]

2

u/GickRick Apr 16 '20

So in other words It's work for a client you are paid to do ?

1

u/HistoricalPotato1102 Jun 03 '20

How unaccurate this code is? Any RMSE metric you can share?

Also, predicting stock prices is a time series problem you should take a look at that and maybe try a neural network, particularly a LSTM or a RNN.

-8

u/[deleted] Apr 15 '20

Can you guys please help ASAP. I have to get this done soon.