Python Project - Stock Price Prediction - Machine Learning Project in Python
Posted by Superadmin on August 22 2020 08:29:06

Python Project - Stock Price Prediction - Machine Learning Project in Python

BY  · UPDATED · AUGUST 6, 2020

 

 

Machine learning has significant applications in the stock price prediction. In this machine learning project, we will be talking about predicting the returns on stocks. This is a very complex task and has uncertainties. We will develop this project into two parts:

  1. First, we will learn how to predict stock price using the LSTM neural network.
  2. Then we will build a dashboard using Plotly dash for stock analysis.

Stock Price Prediction project dashboard

Stock Price Prediction Project

Datasets

  1. To build the stock price prediction model, we will use the NSE TATA GLOBAL dataset. This is a dataset of Tata Beverages from Tata Global Beverages Limited, National Stock Exchange of India: Tata Global Dataset
  2. To develop the dashboard for stock analysis we will use another stock dataset with multiple stocks like Apple, Microsoft, Facebook: Stocks Dataset

Source Code

Before proceeding ahead, please download the source code: Stock Price Prediction Project

Stock price prediction using LSTM

 

1. Imports:

  1. import pandas as pd
  2. import numpy as np
  3.  
  4. import matplotlib.pyplot as plt
  5. %matplotlib inline
  6.  
  7. from matplotlib.pylab import rcParams
  8. rcParams['figure.figsize']=20,10 
  9. from keras.models import Sequential
  10. from keras.layers import LSTM,Dropout,Dense
  11.  
  12.  
  13. from sklearn.preprocessing import MinMaxScaler

2. Read the dataset:

  1. df=pd.read_csv("NSE-TATA.csv") 
  2. df.head()

read stock data

 

 

3. Analyze the closing prices from dataframe:

  1. df["Date"]=pd.to_datetime(df.Date,format="%Y-%m-%d") 
  2. df.index=df['Date']
  3. plt.figure(figsize=(16,8))
  4. plt.plot(df["Close"],label='Close Price history')

analyse stock price

 

4. Sort the dataset on date time and filter “Date” and “Close” columns:

  1. data=df.sort_index(ascending=True,axis=0)
  2. new_dataset=pd.DataFrame(index=range(0,len(df)),columns=['Date','Close'])
  3. for i in range(0,len(data)):
  4. new_dataset["Date"][i]=data['Date'][i]
  5. new_dataset["Close"][i]=data["Close"][i]

5. Normalize the new filtered dataset:

  1. scaler=MinMaxScaler(feature_range=(0,1))
  2. final_dataset=new_dataset.values
  3. train_data=final_dataset[0:987,:]
  4. valid_data=final_dataset[987:,:]
  5. new_dataset.index=new_dataset.Date
  6. new_dataset.drop("Date",axis=1,inplace=True)
  7. scaler=MinMaxScaler(feature_range=(0,1))
  8. scaled_data=scaler.fit_transform(final_dataset)
  9. x_train_data,y_train_data=[],[]
  10. for i in range(60,len(train_data)):
  11. x_train_data.append(scaled_data[i-60:i,0])
  12. y_train_data.append(scaled_data[i,0])
  13. x_train_data,y_train_data=np.array(x_train_data),np.array(y_train_data)
  14. x_train_data=np.reshape(x_train_data,(x_train_data.shape[0],x_train_data.shape[1],1))

6. Build and train the LSTM model:

  1. lstm_model=Sequential()
  2. lstm_model.add(LSTM(units=50,return_sequences=True,input_shape=(x_train_data.shape[1],1)))
  3. lstm_model.add(LSTM(units=50))
  4. lstm_model.add(Dense(1))
  5. inputs_data=new_dataset[len(new_dataset)-len(valid_data)-60:].values
  6. inputs_data=inputs_data.reshape(-1,1)
  7. inputs_data=scaler.transform(inputs_data)
  8. lstm_model.compile(loss='mean_squared_error',optimizer='adam')
  9. lstm_model.fit(x_train_data,y_train_data,epochs=1,batch_size=1,verbose=2)

7. Take a sample of a dataset to make stock price predictions using the LSTM model:

  1. X_test=[]
  2. for i in range(60,inputs_data.shape[0]):
  3. X_test.append(inputs_data[i-60:i,0])
  4. X_test=np.array(X_test)
  5. X_test=np.reshape(X_test,(X_test.shape[0],X_test.shape[1],1))
  6. predicted_closing_price=lstm_model.predict(X_test)
  7. predicted_closing_price=scaler.inverse_transform(predicted_closing_price)

8. Save the LSTM model:

  1. lstm_model.save("saved_model.h5")

9. Visualize the predicted stock costs with actual stock costs:

  1. train_data=new_dataset[:987]
  2. valid_data=new_dataset[987:]
  3. valid_data['Predictions']=predicted_closing_price
  4. plt.plot(train_data["Close"])
  5. plt.plot(valid_data[['Close',"Predictions"]])

predict stock cost

You can observe that LSTM has predicted stocks almost similar to actual stocks.

Build the dashboard using Plotly dash

In this section, we will build a dashboard to analyze stocks. Dash is a python framework that provides an abstraction over flask and react.js to build analytical web applications.
Before moving ahead, you need to install dash. Run the below command in the terminal.

  1. pip3 install dash
  2. pip3 install dash-html-components
  3. pip3 install dash-core-components

Now make a new python file stock_app.py and paste the below script:

  1. import dash
  2. import dash_core_components as dcc
  3. import dash_html_components as html
  4. import pandas as pd
  5. import plotly.graph_objs as go
  6. from dash.dependencies import Input, Output
  7. from keras.models import load_model
  8. from sklearn.preprocessing import MinMaxScaler
  9. import numpy as np
  10. app = dash.Dash()
  11. server = app.server
  12. scaler=MinMaxScaler(feature_range=(0,1))
  13. df_nse = pd.read_csv("./NSE-TATA.csv")
  14. df_nse["Date"]=pd.to_datetime(df_nse.Date,format="%Y-%m-%d")
  15. df_nse.index=df_nse['Date']
  16. data=df_nse.sort_index(ascending=True,axis=0)
  17. new_data=pd.DataFrame(index=range(0,len(df_nse)),columns=['Date','Close'])
  18. for i in range(0,len(data)):
  19. new_data["Date"][i]=data['Date'][i]
  20. new_data["Close"][i]=data["Close"][i]
  21. new_data.index=new_data.Date
  22. new_data.drop("Date",axis=1,inplace=True)
  23. dataset=new_data.values
  24. train=dataset[0:987,:]
  25. valid=dataset[987:,:]
  26. scaler=MinMaxScaler(feature_range=(0,1))
  27. scaled_data=scaler.fit_transform(dataset)
  28. x_train,y_train=[],[]
  29. for i in range(60,len(train)):
  30. x_train.append(scaled_data[i-60:i,0])
  31. y_train.append(scaled_data[i,0])
  32. x_train,y_train=np.array(x_train),np.array(y_train)
  33. x_train=np.reshape(x_train,(x_train.shape[0],x_train.shape[1],1))
  34. model=load_model("saved_model.h5")
  35. inputs=new_data[len(new_data)-len(valid)-60:].values
  36. inputs=inputs.reshape(-1,1)
  37. inputs=scaler.transform(inputs)
  38. X_test=[]
  39. for i in range(60,inputs.shape[0]):
  40. X_test.append(inputs[i-60:i,0])
  41. X_test=np.array(X_test)
  42. X_test=np.reshape(X_test,(X_test.shape[0],X_test.shape[1],1))
  43. closing_price=model.predict(X_test)
  44. closing_price=scaler.inverse_transform(closing_price)
  45. train=new_data[:987]
  46. valid=new_data[987:]
  47. valid['Predictions']=closing_price
  48. df= pd.read_csv("./stock_data.csv")
  49. app.layout = html.Div([
  50. html.H1("Stock Price Analysis Dashboard", style={"textAlign": "center"}),
  51. dcc.Tabs(id="tabs", children=[
  52. dcc.Tab(label='NSE-TATAGLOBAL Stock Data',children=[
  53. html.Div([
  54. html.H2("Actual closing price",style={"textAlign": "center"}),
  55. dcc.Graph(
  56. id="Actual Data",
  57. figure={
  58. "data":[
  59. go.Scatter(
  60. x=train.index,
  61. y=valid["Close"],
  62. mode='markers'
  63. )
  64. ],
  65. "layout":go.Layout(
  66. title='scatter plot',
  67. xaxis={'title':'Date'},
  68. yaxis={'title':'Closing Rate'}
  69. )
  70. }
  71. ),
  72. html.H2("LSTM Predicted closing price",style={"textAlign": "center"}),
  73. dcc.Graph(
  74. id="Predicted Data",
  75. figure={
  76. "data":[
  77. go.Scatter(
  78. x=valid.index,
  79. y=valid["Predictions"],
  80. mode='markers'
  81. )
  82. ],
  83. "layout":go.Layout(
  84. title='scatter plot',
  85. xaxis={'title':'Date'},
  86. yaxis={'title':'Closing Rate'}
  87. )
  88. }
  89. )
  90. ])
  91. ]),
  92. dcc.Tab(label='Facebook Stock Data', children=[
  93. html.Div([
  94. html.H1("Facebook Stocks High vs Lows",
  95. style={'textAlign': 'center'}),
  96. dcc.Dropdown(id='my-dropdown',
  97. options=[{'label': 'Tesla', 'value': 'TSLA'},
  98. {'label': 'Apple','value': 'AAPL'},
  99. {'label': 'Facebook', 'value': 'FB'},
  100. {'label': 'Microsoft','value': 'MSFT'}],
  101. multi=True,value=['FB'],
  102. style={"display": "block", "margin-left": "auto",
  103. "margin-right": "auto", "width": "60%"}),
  104. dcc.Graph(id='highlow'),
  105. html.H1("Facebook Market Volume", style={'textAlign': 'center'}),
  106. dcc.Dropdown(id='my-dropdown2',
  107. options=[{'label': 'Tesla', 'value': 'TSLA'},
  108. {'label': 'Apple','value': 'AAPL'},
  109. {'label': 'Facebook', 'value': 'FB'},
  110. {'label': 'Microsoft','value': 'MSFT'}],
  111. multi=True,value=['FB'],
  112. style={"display": "block", "margin-left": "auto",
  113. "margin-right": "auto", "width": "60%"}),
  114. dcc.Graph(id='volume')
  115. ], className="container"),
  116. ])
  117. ])
  118. ])
  119. @app.callback(Output('highlow', 'figure'),
  120. [Input('my-dropdown', 'value')])
  121. def update_graph(selected_dropdown):
  122. dropdown = {"TSLA": "Tesla","AAPL": "Apple","FB": "Facebook","MSFT": "Microsoft",}
  123. trace1 = []
  124. trace2 = []
  125. for stock in selected_dropdown:
  126. trace1.append(
  127. go.Scatter(x=df[df["Stock"] == stock]["Date"],
  128. y=df[df["Stock"] == stock]["High"],
  129. mode='lines', opacity=0.7,
  130. name=f'High {dropdown[stock]}',textposition='bottom center'))
  131. trace2.append(
  132. go.Scatter(x=df[df["Stock"] == stock]["Date"],
  133. y=df[df["Stock"] == stock]["Low"],
  134. mode='lines', opacity=0.6,
  135. name=f'Low {dropdown[stock]}',textposition='bottom center'))
  136. traces = [trace1, trace2]
  137. data = [val for sublist in traces for val in sublist]
  138. figure = {'data': data,
  139. 'layout': go.Layout(colorway=["#5E0DAC", '#FF4F00', '#375CB1',
  140. '#FF7400', '#FFF400', '#FF0056'],
  141. height=600,
  142. title=f"High and Low Prices for {', '.join(str(dropdown[i]) for i in selected_dropdown)} Over Time",
  143. xaxis={"title":"Date",
  144. 'rangeselector': {'buttons': list([{'count': 1, 'label': '1M',
  145. 'step': 'month',
  146. 'stepmode': 'backward'},
  147. {'count': 6, 'label': '6M',
  148. 'step': 'month',
  149. 'stepmode': 'backward'},
  150. {'step': 'all'}])},
  151. 'rangeslider': {'visible': True}, 'type': 'date'},
  152. yaxis={"title":"Price (USD)"})}
  153. return figure
  154. @app.callback(Output('volume', 'figure'),
  155. [Input('my-dropdown2', 'value')])
  156. def update_graph(selected_dropdown_value):
  157. dropdown = {"TSLA": "Tesla","AAPL": "Apple","FB": "Facebook","MSFT": "Microsoft",}
  158. trace1 = []
  159. for stock in selected_dropdown_value:
  160. trace1.append(
  161. go.Scatter(x=df[df["Stock"] == stock]["Date"],
  162. y=df[df["Stock"] == stock]["Volume"],
  163. mode='lines', opacity=0.7,
  164. name=f'Volume {dropdown[stock]}', textposition='bottom center'))
  165. traces = [trace1]
  166. data = [val for sublist in traces for val in sublist]
  167. figure = {'data': data,
  168. 'layout': go.Layout(colorway=["#5E0DAC", '#FF4F00', '#375CB1',
  169. '#FF7400', '#FFF400', '#FF0056'],
  170. height=600,
  171. title=f"Market Volume for {', '.join(str(dropdown[i]) for i in selected_dropdown_value)} Over Time",
  172. xaxis={"title":"Date",
  173. 'rangeselector': {'buttons': list([{'count': 1, 'label': '1M',
  174. 'step': 'month',
  175. 'stepmode': 'backward'},
  176. {'count': 6, 'label': '6M',
  177. 'step': 'month',
  178. 'stepmode': 'backward'},
  179. {'step': 'all'}])},
  180. 'rangeslider': {'visible': True}, 'type': 'date'},
  181. yaxis={"title":"Transactions Volume"})}
  182. return figure
  183. if __name__=='__main__':
  184. app.run_server(debug=True)

Now run this file and open the app in the browser:

  1. python3 stock_app.py

 

Stock Price Prediction project dashboard

Summary

Stock price prediction is a machine learning project for beginners; in this tutorial we learned how to develop a stock cost prediction model and how to build an interactive dashboard for stock analysis. We implemented stock market prediction using the LSTM model. OTOH, Plotly dash python framework for building dashboards.