{"id":16915,"date":"2022-05-11T03:32:40","date_gmt":"2022-05-10T19:32:40","guid":{"rendered":"https:\/\/www.tejwin.com\/?post_type=insight&#038;p=16915"},"modified":"2023-08-17T14:31:29","modified_gmt":"2023-08-17T06:31:29","slug":"lstm","status":"publish","type":"insight","link":"https:\/\/www.tejwin.com\/en\/insight\/lstm\/","title":{"rendered":"LSTM"},"content":{"rendered":"\n<p id=\"25a8\">Using deep learning model to predict stock price?<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1wIT8ZcTsDKLSTWgC3-qeDA.jpg\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Photo by&nbsp;<a href=\"https:\/\/unsplash.com\/@markusspiske?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\" rel=\"noreferrer noopener\" target=\"_blank\">Markus Spiske<\/a>&nbsp;on&nbsp;<a href=\"https:\/\/unsplash.com\/s\/photos\/data-science?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\" rel=\"noreferrer noopener\" target=\"_blank\">Unsplash<\/a><\/figcaption><\/figure>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_81 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69f121539ebcb\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"ez-toc-cssicon\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69f121539ebcb\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.tejwin.com\/en\/insight\/lstm\/#Highlights\" >Highlights<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.tejwin.com\/en\/insight\/lstm\/#Preface\" >Preface<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.tejwin.com\/en\/insight\/lstm\/#The_Editing_Environment_and_Modules_Required\" >The Editing Environment and Modules Required<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.tejwin.com\/en\/insight\/lstm\/#Database\" >Database<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.tejwin.com\/en\/insight\/lstm\/#Establish_function\" >Establish function<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.tejwin.com\/en\/insight\/lstm\/#Import_Data\" >Import Data<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.tejwin.com\/en\/insight\/lstm\/#Visualization\" >Visualization<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.tejwin.com\/en\/insight\/lstm\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.tejwin.com\/en\/insight\/lstm\/#Source_Code\" >Source Code<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.tejwin.com\/en\/insight\/lstm\/#See_More\" >See More<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.tejwin.com\/en\/insight\/lstm\/#About_Us\" >About Us<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"9df1\"><span class=\"ez-toc-section\" id=\"Highlights\"><\/span><strong>Highlights<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Difficulty\uff1a\u2605\u2605\u2605\u2606\u2606<\/li>\n\n\n\n<li>Use trading data for stock price forecasting<\/li>\n\n\n\n<li>Advice: This article use RNN framework for time series prediction, which requires basic knowledge of time series or deep learning. You can also refer to&nbsp;<a href=\"https:\/\/medium.com\/tej-api-%E9%87%91%E8%9E%8D%E8%B3%87%E6%96%99%E5%88%86%E6%9E%90\/%E9%87%8F%E5%8C%96%E5%88%86%E6%9E%90-%E4%B8%89-%E9%A0%90%E6%B8%AC%E5%B8%82%E5%A0%B4-bde88352a011\" target=\"_blank\" rel=\"noopener\">[Quantitative Analysis (3)] Predict the market?!<\/a>&nbsp;This article is about using regression models and logistic models to predict stock prices.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3fe5\"><span class=\"ez-toc-section\" id=\"Preface\"><\/span><strong>Preface<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"53c0\">Predicting stock prices has been pursued by people, but the randomness of stock prices not easy to forecast. With the progress of data science, the calculation cost has been greatly reduced. This article use more complex deep learning model for stock prices prediction compare to&nbsp;<a href=\"https:\/\/medium.com\/tej-api-%E9%87%91%E8%9E%8D%E8%B3%87%E6%96%99%E5%88%86%E6%9E%90\/%E9%87%8F%E5%8C%96%E5%88%86%E6%9E%90-%E4%B8%89-%E9%A0%90%E6%B8%AC%E5%B8%82%E5%A0%B4-bde88352a011\" target=\"_blank\" rel=\"noopener\">[Quantitative Analysis (3)] Predict the market?!<\/a>&nbsp;We use the opening price, the highest price, the lowest price, the closing price and the trading volume of the previous ten days to predict the closing price of next day.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"fbe1\"><span class=\"ez-toc-section\" id=\"The_Editing_Environment_and_Modules_Required\"><\/span><strong>The Editing Environment and Modules Required<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"41eb\">This article uses Google colab as the editor<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import numpy as np<br>import pandas as pd<br>import matplotlib.pyplot as pltimport tejapitejapi.ApiConfig.api_key=###yourkey##################import tensorflow as tf<br>from keras.layers.core import Dense, Dropout, Activation<br>from keras.callbacks import EarlyStopping,ModelCheckpoint<br>from keras.models import Sequential<br>from keras.layers import LSTM<br>from sklearn.preprocessing import MinMaxScaler<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"a282\"><span class=\"ez-toc-section\" id=\"Database\"><\/span><strong>Database<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"647e\">Securities trading data sheet: listed company, over-the-counter (OTC) share price(day), the database code is (TWN\/EWPRCD)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"8a30\"><span class=\"ez-toc-section\" id=\"Establish_function\"><\/span><strong>Establish function<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"367e\">Establish RMSE to evaluate the predictive ability of the model, and converting data into the form of the LSTM input as shown in the figure below:<\/p>\n\n\n\n<p id=\"b1bd\">(batch_size, time_steps, seq_len): 1163 groups, 5 days, 5 variables<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1qb05NbveFQ6Pmh1Kfg2dAg.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"69d5\"><strong>Dataset:<\/strong>&nbsp;input training data<\/p>\n\n\n\n<p id=\"c676\"><strong>Target:<\/strong>&nbsp;forecasting data<\/p>\n\n\n\n<p id=\"6288\"><strong>Start_index:&nbsp;<\/strong>It\u2019s always start at 0 because it will group itself.<\/p>\n\n\n\n<p id=\"39c2\"><strong>End_index:&nbsp;<\/strong>destination, set 0<\/p>\n\n\n\n<p id=\"1174\"><strong>History_size:<\/strong>&nbsp;input length, this article chooses 10<\/p>\n\n\n\n<p id=\"5bcd\"><strong>Target_size:&nbsp;<\/strong>prediction length, this article chooses 1<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def root_mean_squared_error(y_true, y_pred):<br>    return np.sqrt(np.mean(np.square(y_pred - y_true)###calculate Rmsedef multivariate_data(dataset, target, start_index, end_index, history_size,<br>                      target_size, single_step=False):<br>  data = []<br>  labels = []<br>  <br>  start_index = start_index + history_size<br>  if end_index is None:<br>    end_index = len(dataset) - target_size<br>  <br>  for i in range(start_index, end_index):<br>    indices = range(i-history_size, i)<br>    data.append(dataset[indices])<br>    <br>    if single_step:<br>      labels.append(target[i+target_size])<br>    else:<br>      labels.append(target[i:i+target_size])<br>  <br>  return np.array(data), np.array(labels)<\/pre>\n\n\n\n<p id=\"6a7f\">The next step is concating the model, dropout is used to prevent overfitting, this article will not explain the principle. We can adjust d between 0 to 1.<\/p>\n\n\n\n<p id=\"e338\">Input length is the input time length, we choose 10 days in the article.<\/p>\n\n\n\n<p id=\"2e5f\">Input_dim is the number of variables; we have 5 variables in this article.<\/p>\n\n\n\n<p id=\"45ef\">Return_sequences: True is maintenance (batch, time_steps, seq_len), connecting the next LSTM set False will become one-dimensional.<\/p>\n\n\n\n<p id=\"c5a9\">Loss is trained by mean_squared_error, Optimizer use Adam.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def build_model(input_length, input_dim):    d=0.3<br>    model= Sequential()<br>    model.add(LSTM(128,input_shape=(input_length, input_dim),return_sequences=True))<br>    <br>    model.add(Dropout(d))<br>    <br>    model.add(LSTM(64,input_shape=(input_length, input_dim),return_sequences=False))<br>    <br>    model.add(Dropout(d))<br>    <br>    model.add(Dense(1,activation='linear'))<br>#linear \/ softmax(\u591a\u5206\u985e) \/ sigmoid(\u4e8c\u5206\u6cd5)<br>    <br>    model.compile(loss='mse',optimizer='adam')<br>    return mode1<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1229\"><span class=\"ez-toc-section\" id=\"Import_Data\"><\/span><strong>Import Data<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">coid='3037'<br>start='2016-01-01'<br>end='2022-5-22'<br>opts={'columns': ['open_d' ,'high_d','low_d','mdate', 'volume','close_d']}<br>tw=tejapi.get('TWN\/EWPRCD',coid=coid,<br>                mdate={'gt':start,'lt':end},<br>                paginate=True,<br>                chinese_column_name=True,<br>                opts=opts<br>                )<br>tw.set_index(\"\u65e5\u671f\",drop=True,inplace=True)<br>tw.sort_index(inplace=True)<\/pre>\n\n\n\n<p id=\"322c\">Set variables and dependent variables, the closing price is forecasting target, the others are variables.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">y =tw[\"\u6536\u76e4\u50f9\"]<br>x =tw<\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1ZbxRbyB4yjvwWpXIVjd4RA.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"27ea\">We normalize the data, which become between 0 and 1, so that the data training speed will faster and easier to converge.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">scaler=MinMaxScaler(feature_range=(0,1))<br>y=scaler.fit_transform(y.to_frame())<br>scaler1=MinMaxScaler(feature_range=(0,1))<br>x=scaler1.fit_transform(x)<\/pre>\n\n\n\n<p id=\"2acd\">Dividing the data into training data, validation data and test data.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">x,y=multivariate_data( x ,y , 0 ,None, 10 , 1 ,single_step=True)<br>split =0.95<br>x_,y_  = x[0:int(split*len(x))] , y[0:int(split*len(x))]<br>x_test ,y_test   = x[int(split*len(x)):] , y[int(split*len(x)):]<br>split= 0.8<br>x_train,y_train  =x_[:int(split*len(x_))] , y_[:int(split*len(x_))]<br>x_vaild,y_vaild  =x_[int(split*len(x_)):] , y_[int(split*len(x_)):]<\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1wVS4ENWycnHkzpXpFO_wuA.png\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1Phow2ebDHZ1B2DGc28JOxQ.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"8c5b\">The models are divided into 4 kinds, one layer is Lstm, another is Dense<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1L_YyNIYJAW8bPS_XCZRrAQ.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\">my_callbacks = [<br>tf.keras.callbacks.EarlyStopping(patience=300, monitor ='val_loss')]<br> <br>######## \u5728\u8a13\u7df4\u7d44\u8a13\u7df4\uff0c\u4f7f\u7528\u9a57\u8b49\u7d44\u9078\u53d6filepath=\"lstm.best.hdf5\"checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, mode='min',save_best_only=True)call_backlist = [my_callbacks,checkpoint]lstm00 = lstm_model0(10,5)historylstm0 = lstm0.fit( x_train, y_train, batch_size=30,shuffle=False , epochs=1000,validation_data=(x_vaild,y_vaild),callbacks=call_backlist)lstm00.summary()<\/pre>\n\n\n\n<p id=\"502c\"><strong>EarlyStopping:<\/strong>&nbsp;training parameter in training data, the lowest selection criteria is validation data. Stop training if there is no improvement after 300 epochs.<\/p>\n\n\n\n<p id=\"90c7\"><strong>Filepath:&nbsp;<\/strong>model storage path<\/p>\n\n\n\n<p id=\"54a6\"><strong>ModelCheckpoint:&nbsp;<\/strong>Choose the lowest val_loss as the final model<\/p>\n\n\n\n<p id=\"f51f\"><strong>Batch:<\/strong>&nbsp;sample of each training<\/p>\n\n\n\n<p id=\"c52a\"><strong>Epochs<\/strong>: frequency of training<\/p>\n\n\n\n<p id=\"cd90\"><strong>Shuffle:&nbsp;<\/strong>True (Randomly disrupt the order), False(not disrupt the order)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"bde8\"><span class=\"ez-toc-section\" id=\"Visualization\"><\/span><strong>Visualization<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">plt.plot(historylstm0.history['loss'])<br>plt.plot(historylstm0.history['val_loss'])<br>plt.title('Model loss')<br>plt.ylabel('Loss')<br>plt.xlabel('Epoch')<br>plt.legend(['Train', 'Test'], loc='upper left')<br>plt.show()<\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/186qlBWCob3z-JgNMGLIGHA.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"a8a5\">It\u2019s found that one layer LSTM is bad, and overfitting.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">lstm0train  = lstm00.predict(x_train)<br>lstm0val = lstm00.predict(x_vaild)<br>lstm0pre = lstm00.predict(x_test)pre = lstm00.predict(x_train)<br>pre1=lstm00.predict(x_vaild)<br>fc=np.concatenate((pre,pre1))<br>yreal=np.concatenate((y_train,y_vaild))<br>plt.figure(facecolor='white')<br>pd.Series(fc.reshape(-1)).plot(color='blue', label='Predict1')<br>pd.Series(yreal.reshape(-1)).plot(color='red', label='Original')<br>plt.legend()<br>plt.show()<\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1dzoEHku8-k1d-72HRX8DDA.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"bfca\">We can find that one layer LSTM model cannot grasp this time series in this picture.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">lstm0pre= scaler.inverse_transform(lstm0pre)#\u5c07\u8cc7\u6599\u8f49\u63db\u56de\u4f86<br>y_test = scaler.inverse_transform(y_test.reshape(-1,1))plt.figure()<br>plt.plot(lstm0pre)<br>plt.plot(y_test)<br>plt.title('pre')<br>plt.ylabel('\u80a1\u50f9')<br>plt.xlabel('day')<br>plt.legend(['pre', 'Test'], loc='upper left')<br>plt.show()<br>root_mean_squared_error(lstm0pre,y_test)<\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1_WY25I5LnhjXiUOeuZUskQ.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"8e7a\">The prediction results are very bad!<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1nkQ1xitCjC3NY8uD75ZNGw.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"e2e3\">We try two layers in the next step.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1kjQYmPiIq0xDx2XfWApHEQ.png\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1cq0fCFDSHO_q1DKf9cYJMg.png\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1ajgEIYXgSmT0NcHj-i730Q.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"41c3\">Using more complex model<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/16MyUt553dOR0BIgDNA7dag.png\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/172WDf9WWdCWhvmkizlMnIA.png\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1XO-BZM2RbrZ3ZcJTxOit4Q.png\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1GSlTD6LDLlbOsoF0AZT-NA.png\" alt=\"\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"d737\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span><strong>Conclusion<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"a8cf\">We can see that although the fitting result is good under stack-LSTM, after actual observation, it is found that the prediction of the model is only predict from yesterday to tomorrow, that is not feasible to use deep learning model to predict the next day closing price. Maybe we can change to predict the return, and add more features or stock pitching strategy to improve forecasting result. We will introduce for you later!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"8f07\"><span class=\"ez-toc-section\" id=\"Source_Code\"><\/span><strong>Source Code<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/gist.github.com\/tej87681088\/3b1c4f8a03b36d76bef456c6e8cb33f9#file-tejapi_medium-14-ipynb\" class=\"ek-link\" target=\"_blank\" rel=\"noopener\">Click here to go Github<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"a0a2\"><span class=\"ez-toc-section\" id=\"See_More\"><\/span><strong>See More<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.tejwin.com\/en\/insight\/svm-model\/\" class=\"ek-link\">SVM Model<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.tejwin.com\/en\/insight\/lasso-regression-model\/\" class=\"ek-link\">Lasso Regression Model<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"c190\"><span class=\"ez-toc-section\" id=\"About_Us\"><\/span><strong>About Us<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/api.tej.com.tw\/index.html\" rel=\"noreferrer noopener\" target=\"_blank\">TEJ API<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/eshop.tej.com.tw\/E-Shop\/Edata_intro\" rel=\"noreferrer noopener\" target=\"_blank\">TEJ E-Shop<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Using deep learning model to predict stock price? Highlights Preface Predicting stock prices has been pursued by people, but the randomness of stock prices not easy to forecast. With the progress of data science, the calculation cost has been greatly reduced. This article use more complex deep learning model for stock prices prediction compare to&nbsp;[Quantitative [&hellip;]<\/p>\n","protected":false},"featured_media":16917,"template":"","tags":[2601,2371,2998,3007],"insight-category":[690,50],"class_list":["post-16915","insight","type-insight","status-publish","has-post-thumbnail","hentry","tag-lstm","tag-python","tag-stock-predict","tag-tejapi-data-analysis","insight-category-data-analysis","insight-category-fintech"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/16915","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight"}],"about":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/types\/insight"}],"version-history":[{"count":0,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/16915\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media\/16917"}],"wp:attachment":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media?parent=16915"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/tags?post=16915"},{"taxonomy":"insight-category","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight-category?post=16915"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}