{"id":26402,"date":"2024-07-31T13:10:55","date_gmt":"2024-07-31T05:10:55","guid":{"rendered":"https:\/\/www.tejwin.com\/?post_type=insight&#038;p=26402"},"modified":"2024-08-16T08:45:06","modified_gmt":"2024-08-16T00:45:06","slug":"lstm-stock-price-prediction","status":"publish","type":"insight","link":"https:\/\/www.tejwin.com\/en\/insight\/lstm-stock-price-prediction\/","title":{"rendered":"Verifying LSTM Stock Price Prediction Effectiveness Using TQuant Lab (Part 1)"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full caption-align-center\"><img fetchpriority=\"high\" decoding=\"async\" width=\"3456\" height=\"2304\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/Metal-Structure-Low-Angle.jpg\" alt=\"LSTM Stock Price Prediction\" class=\"wp-image-25659\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/Metal-Structure-Low-Angle.jpg 3456w, https:\/\/www.tejwin.com\/wp-content\/uploads\/Metal-Structure-Low-Angle-300x200.jpg 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/Metal-Structure-Low-Angle-1024x683.jpg 1024w, https:\/\/www.tejwin.com\/wp-content\/uploads\/Metal-Structure-Low-Angle-150x100.jpg 150w, https:\/\/www.tejwin.com\/wp-content\/uploads\/Metal-Structure-Low-Angle-768x512.jpg 768w, https:\/\/www.tejwin.com\/wp-content\/uploads\/Metal-Structure-Low-Angle-1536x1024.jpg 1536w, https:\/\/www.tejwin.com\/wp-content\/uploads\/Metal-Structure-Low-Angle-2048x1365.jpg 2048w\" sizes=\"(max-width: 3456px) 100vw, 3456px\" \/><figcaption class=\"wp-element-caption\">Photo by&nbsp;<a href=\"https:\/\/unsplash.com\/@alinnnaaaa?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash\" target=\"_blank\" rel=\"noopener\">Alina Grubnyak<\/a>&nbsp;on&nbsp;<a href=\"https:\/\/unsplash.com\/photos\/low-angle-photography-of-metal-structure-ZiQkhI7417A?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash\" target=\"_blank\" rel=\"noopener\">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-69f13409a5958\" 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-69f13409a5958\"  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-stock-price-prediction\/#Key_Highlights\" >Key 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-stock-price-prediction\/#Introduction\" >Introduction<\/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-stock-price-prediction\/#Editing_Environment_and_Module_Requirements\" >Editing Environment and Module Requirements<\/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-stock-price-prediction\/#LSTM_Model_Construction\" >LSTM Model Construction<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.tejwin.com\/en\/insight\/lstm-stock-price-prediction\/#Loading_External_Packages\" >Loading External Packages<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.tejwin.com\/en\/insight\/lstm-stock-price-prediction\/#Loading_Internal_Packages\" >Loading Internal Packages<\/a><\/li><\/ul><\/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-stock-price-prediction\/#Creating_Time_Series_Data\" >Creating Time Series Data<\/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-stock-price-prediction\/#Build_the_LSTM_Stock_Price_Prediction_Model\" >Build the LSTM Stock Price Prediction Model<\/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-stock-price-prediction\/#LSTM_%E8%82%A1%E5%83%B9%E9%A0%90%E6%B8%AC%E7%B5%90%E6%9E%9C%E8%A9%95%E4%BC%B0\" >LSTM \u80a1\u50f9\u9810\u6e2c\u7d50\u679c\u8a55\u4f30<\/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-stock-price-prediction\/#LSTM_Stock_Price_Prediction_%E2%80%93_Feature_Importance_Analysis\" >LSTM Stock Price Prediction \u2013 Feature Importance Analysis<\/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-stock-price-prediction\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.tejwin.com\/en\/insight\/lstm-stock-price-prediction\/#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-13\" href=\"https:\/\/www.tejwin.com\/en\/insight\/lstm-stock-price-prediction\/#Extended_Reading\" >Extended Reading<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"1139\"><span class=\"ez-toc-section\" id=\"Key_Highlights\"><\/span><strong>Key Highlights<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Article Difficulty: \u2605\u2605\u2605\u2605\u2605<\/li>\n\n\n\n<li>Combining fundamental, sentiment, and technical data to perform LSTM stock price prediction and executing backtest performance verification.<\/li>\n\n\n\n<li>Reading Recommendations\uff1a This article uses an RNN architecture for time series prediction. A basic understanding of time series or deep learning is necessary. For a deeper understanding of LSTM model construction, refer to the <a href=\"https:\/\/www.tejwin.com\/en\/insight\/lstm\/\" class=\"ek-link\"><strong>[Data Science] LSTM<\/strong><\/a> resource.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"87be\"><span class=\"ez-toc-section\" id=\"Introduction\"><\/span><strong>Introduction<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"3cb0\">In machine learning, predicting the prices of financial market derivatives has always been popular. Numerous studies have focused on using machine learning to achieve excess returns in the market. However, since the financial market is essentially a collection of human behaviors encompassing many irregular and uncertain factors, ordinary machine learning models like logistic regression, random forests, and extreme gradient boosting seem unable to capture the overly complex market rules effectively. Consequently, with the vigorous development of deep learning, more time series-related models apply to future stock price predictions. This article uses the LSTM time series model for deep learning-based LSTM stock price prediction, utilizing the opening, high, low, and closing prices of the past five days, quarterly ROE, MOM (indicating the magnitude of price trend changes, and the direction of market trends), and RSI indicators to predict the next day&#8217;s closing price.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Editing_Environment_and_Module_Requirements\"><\/span><strong>Editing Environment and Module Requirements<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This article uses Mac OS and VS Code as editors.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"LSTM_Model_Construction\"><\/span><strong>LSTM Model Construction<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Due to the significant impact of large investors and unpredictable market fluctuations on the stock prices of large-cap stocks in Taiwan, making their price movements challenging to predict, this article selects the seventh largest component stock (<strong>2618, Eva Airways Corp.<\/strong>) of <strong>the Taiwan Small and Medium Cap 300 Index<\/strong> (referred to as &#8220;Small and Medium Cap 300 Index&#8221;) for Q2 2024, as the target stock. We also selected a higher market cap stock (<strong>8215, BenQ Materials Corp.<\/strong>) for backtesting as a reference.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Loading_External_Packages\"><\/span><strong>Loading External Packages<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import os\nimport time\nimport tejapi\nimport numpy as np\nimport pandas as pd\n...<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Loading_Internal_Packages\"><\/span><strong>Loading Internal Packages<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><strong><em><code>ML_stock()<\/code><\/em><\/strong> is a custom <em>class<\/em> we created for preprocessing data. It handles loading the <em>API_KEY<\/em>, price-volume data, fundamental data, and technical indicators. Finally, it sets the start and end dates for the model&#8217;s sample period.<br>*Note: To ensure operation, please enter your <em>API_KEY<\/em> in the <strong><em>config.ini<\/em><\/strong> file before using it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ml_stock = ML_stock()\nml_stock.ini()\nstart = '2012-07-01'\nend = '2022-07-01'<\/code><\/pre>\n\n\n\n<p>We have retained only the necessary features for the next steps.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full caption-align-center\"><img decoding=\"async\" width=\"1223\" height=\"662\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/\u622a\u5716-2024-07-26-\u4e0b\u53485.19.38.png\" alt=\"LSTM Stock Price Prediction\" class=\"wp-image-25669\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/\u622a\u5716-2024-07-26-\u4e0b\u53485.19.38.png 1223w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u622a\u5716-2024-07-26-\u4e0b\u53485.19.38-300x162.png 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u622a\u5716-2024-07-26-\u4e0b\u53485.19.38-1024x554.png 1024w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u622a\u5716-2024-07-26-\u4e0b\u53485.19.38-150x81.png 150w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u622a\u5716-2024-07-26-\u4e0b\u53485.19.38-768x416.png 768w\" sizes=\"(max-width: 1223px) 100vw, 1223px\" \/><figcaption class=\"wp-element-caption\">Data Preprocessing<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creating_Time_Series_Data\"><\/span><strong>Creating Time Series Data<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>First, standardize all data and define the training set&#8217;s window_size as 5. This means each data point consists of the current day&#8217;s data and the following five days&#8217; data. Data is iterated through a sliding window approach, so each data point overlaps the previous data point by five days.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"593\" height=\"518\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/image-472.png\" alt=\"LSTM Stock Price Prediction\" class=\"wp-image-25722\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/image-472.png 593w, https:\/\/www.tejwin.com\/wp-content\/uploads\/image-472-300x262.png 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/image-472-150x131.png 150w\" sizes=\"(max-width: 593px) 100vw, 593px\" \/><\/figure>\n\n\n\n<p>From the above diagram, we can see that we created three-dimensional matrices for the dependent variables (such as open, high, low, and close prices) and the independent variable (the next day&#8217;s closing price). The dimensions from left to right represent (the number of data points, number of days, and number of features). After this, we split the data into training, validation, and test sets using an 8:1:1 ratio.<\/p>\n\n\n\n<div style=\"height:25px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button has-custom-width wp-block-button__width-100 has-custom-font-size\" style=\"font-size:22px\"><a class=\"wp-block-button__link has-background wp-element-button\" href=\"https:\/\/www.tejwin.com\/en\/databank-solution\/market-data\/\" style=\"border-radius:16px;background:linear-gradient(135deg,rgb(160,209,216) 0%,rgb(51,145,181) 50%,rgb(50,95,191) 100%)\"><strong>Access to Comprehensive Stock Market Data<\/strong><br><strong>Start Building Models With the Best Quality Today!<\/strong><\/a><\/div>\n<\/div>\n\n\n\n<div style=\"height:25px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Build_the_LSTM_Stock_Price_Prediction_Model\"><\/span><strong>Build the LSTM Stock Price Prediction Model<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This study uses one LSTM layer and three Dense layers for LSTM stock price prediction modeling, with Dropout layers interspersed to prevent overfitting. The final layer is a Dense layer with a single neuron outputting the prediction value. We also define an exponentially decaying learning rate, starting at 0.001, with the learning rate being reduced to 90% of its previous value every 10,000 steps and following a stepwise decrease.<br>We use the Adam Optimizer with the previously defined learning rate settings. The loss function is Mean Squared Error (MSE), and the evaluation metric is Mean Absolute Error (MAE).<br>Finally, we set up an Early Stopping mechanism that monitors <code>val_loss<\/code>. If there is no improvement over 10 epochs, the training will stop to prevent overfitting.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>model = Sequential(&#91;layers.Input((X_train.shape&#91;1], X_train.shape&#91;2])),\n                    layers.LSTM(64),\n                    layers.Dense(32, activation='relu'),\n                    Dropout(0.2),\n                    layers.Dense(32, activation='relu'),\n                    Dropout(0.2),\n                    layers.Dense(1)\n                ])\nlr_schedule = ExponentialDecay(\n    0.001,\n    decay_steps=10000,\n    decay_rate=0.9,\n    staircase=True)\nmodel.compile(optimizer=Adam(learning_rate=lr_schedule), loss='mse', metrics=&#91;'mae'])\nearly_stopping = EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full caption-align-center\"><img loading=\"lazy\" decoding=\"async\" width=\"848\" height=\"654\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/\u622a\u5716-2024-07-30-\u4e0a\u534810.04.28.png\" alt=\"LSTM Stock Price Prediction\" class=\"wp-image-25738\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/\u622a\u5716-2024-07-30-\u4e0a\u534810.04.28.png 848w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u622a\u5716-2024-07-30-\u4e0a\u534810.04.28-300x231.png 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u622a\u5716-2024-07-30-\u4e0a\u534810.04.28-150x116.png 150w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u622a\u5716-2024-07-30-\u4e0a\u534810.04.28-768x592.png 768w\" sizes=\"(max-width: 848px) 100vw, 848px\" \/><figcaption class=\"wp-element-caption\">Model Structure Diagram<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full caption-align-center\"><img loading=\"lazy\" decoding=\"async\" width=\"565\" height=\"413\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/output-1.jpg\" alt=\"LSTM Stock Price Prediction\" class=\"wp-image-25799\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/output-1.jpg 565w, https:\/\/www.tejwin.com\/wp-content\/uploads\/output-1-300x219.jpg 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/output-1-150x110.jpg 150w\" sizes=\"(max-width: 565px) 100vw, 565px\" \/><figcaption class=\"wp-element-caption\">Loss Variation<\/figcaption><\/figure>\n\n\n\n<p>After about 25 epochs, the training loss gradually stops decreasing significantly, indicating that the model converges quickly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"LSTM_%E8%82%A1%E5%83%B9%E9%A0%90%E6%B8%AC%E7%B5%90%E6%9E%9C%E8%A9%95%E4%BC%B0\"><\/span>LSTM \u80a1\u50f9\u9810\u6e2c\u7d50\u679c\u8a55\u4f30<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<figure class=\"wp-block-image size-full caption-align-center\"><img loading=\"lazy\" decoding=\"async\" width=\"559\" height=\"450\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/output5-1.jpg\" alt=\"LSTM Stock Price Prediction\" class=\"wp-image-25821\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/output5-1.jpg 559w, https:\/\/www.tejwin.com\/wp-content\/uploads\/output5-1-300x242.jpg 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/output5-1-150x121.jpg 150w\" sizes=\"(max-width: 559px) 100vw, 559px\" \/><figcaption class=\"wp-element-caption\">Test Set Prediction Comparison \u2013 2618<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large caption-align-center\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"357\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/\/output1-1-1024x357.jpg\" alt=\"LSTM Stock Price Prediction\" class=\"wp-image-25803\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/output1-1-1024x357.jpg 1024w, https:\/\/www.tejwin.com\/wp-content\/uploads\/output1-1-300x105.jpg 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/output1-1-150x52.jpg 150w, https:\/\/www.tejwin.com\/wp-content\/uploads\/output1-1-768x268.jpg 768w, https:\/\/www.tejwin.com\/wp-content\/uploads\/output1-1.jpg 1287w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Prediction vs. Actual Data Comparison \u2013 2618<\/figcaption><\/figure>\n\n\n\n<p>The performance of LSTM stock price prediction on the training and validation sets is quite good, as expected within the sample. However, there is some discrepancy between the predicted and actual prices in the latter part of the out-of-sample test set. While the model captures the overall trend direction, further backtesting is needed to verify its accuracy.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full caption-align-center\"><img loading=\"lazy\" decoding=\"async\" width=\"559\" height=\"450\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/output6-1.jpg\" alt=\"LSTM Stock Price Prediction\" class=\"wp-image-25825\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/output6-1.jpg 559w, https:\/\/www.tejwin.com\/wp-content\/uploads\/output6-1-300x242.jpg 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/output6-1-150x121.jpg 150w\" sizes=\"(max-width: 559px) 100vw, 559px\" \/><figcaption class=\"wp-element-caption\">Test Set Prediction Comparison \u2013 8215<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full caption-align-center\"><img loading=\"lazy\" decoding=\"async\" width=\"1287\" height=\"449\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/output4-2.jpg\" alt=\"LSTM Stock Price Prediction\" class=\"wp-image-25817\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/output4-2.jpg 1287w, https:\/\/www.tejwin.com\/wp-content\/uploads\/output4-2-300x105.jpg 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/output4-2-1024x357.jpg 1024w, https:\/\/www.tejwin.com\/wp-content\/uploads\/output4-2-150x52.jpg 150w, https:\/\/www.tejwin.com\/wp-content\/uploads\/output4-2-768x268.jpg 768w\" sizes=\"(max-width: 1287px) 100vw, 1287px\" \/><figcaption class=\"wp-element-caption\">Prediction vs. Actual Data Comparison \u2013 8215<\/figcaption><\/figure>\n\n\n\n<p>We applied the same method to model 8215 (BenQ Materials) of LSTM stock price prediction and plotted the comparison graph. In the out-of-sample data, the model performs better predicting the next day&#8217;s closing price for 8215. However, the actual effectiveness will be validated in the next article.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"LSTM_Stock_Price_Prediction_%E2%80%93_Feature_Importance_Analysis\"><\/span>LSTM Stock Price Prediction \u2013 Feature Importance Analysis<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<figure class=\"wp-block-image size-full caption-align-center\"><img loading=\"lazy\" decoding=\"async\" width=\"3180\" height=\"855\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/output2-1.jpg\" alt=\"LSTM Stock Price Prediction\" class=\"wp-image-25807\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/output2-1.jpg 3180w, https:\/\/www.tejwin.com\/wp-content\/uploads\/output2-1-300x81.jpg 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/output2-1-1024x275.jpg 1024w, https:\/\/www.tejwin.com\/wp-content\/uploads\/output2-1-150x40.jpg 150w, https:\/\/www.tejwin.com\/wp-content\/uploads\/output2-1-768x206.jpg 768w, https:\/\/www.tejwin.com\/wp-content\/uploads\/output2-1-1536x413.jpg 1536w, https:\/\/www.tejwin.com\/wp-content\/uploads\/output2-1-2048x551.jpg 2048w\" sizes=\"(max-width: 3180px) 100vw, 3180px\" \/><figcaption class=\"wp-element-caption\">Feature Importance Analysis \u2013 2618<\/figcaption><\/figure>\n\n\n\n<p>Daily price changes remain the model&#8217;s primary reference for all features, followed by MOM and RSI indicators. Interestingly, quarterly ROE is not favored by the model, likely due to its less frequent data updates than other features. For a model that predicts daily stock prices, quarterly ROE is not as relevant in LSTM stock price prediction.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full caption-align-center\"><img loading=\"lazy\" decoding=\"async\" width=\"3180\" height=\"855\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/output7-1.jpg\" alt=\"LSTM Stock Price Prediction\" class=\"wp-image-25829\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/output7-1.jpg 3180w, https:\/\/www.tejwin.com\/wp-content\/uploads\/output7-1-300x81.jpg 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/output7-1-1024x275.jpg 1024w, https:\/\/www.tejwin.com\/wp-content\/uploads\/output7-1-150x40.jpg 150w, https:\/\/www.tejwin.com\/wp-content\/uploads\/output7-1-768x206.jpg 768w, https:\/\/www.tejwin.com\/wp-content\/uploads\/output7-1-1536x413.jpg 1536w, https:\/\/www.tejwin.com\/wp-content\/uploads\/output7-1-2048x551.jpg 2048w\" sizes=\"(max-width: 3180px) 100vw, 3180px\" \/><figcaption class=\"wp-element-caption\">Feature Importance Analysis \u2013 8215<\/figcaption><\/figure>\n\n\n\n<p>The observation made with 2618 is also evident in 8215: the model does not effectively utilize quarterly ROE for LSTM stock price prediction.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>model.save(f'lstm_{sample&#91;0]}.keras', include_optimizer = False)<\/code><\/pre>\n\n\n\n<p>Finally, we save the model as a <code><strong><em>.keras<\/em><\/strong><\/code> file to facilitate the next backtesting phase.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In constructing the LSTM stock price prediction, it appears LSTM could perform well in price forecasting based on the charts. However, past research on time series models often shows a certain degree of delay between the model&#8217;s predictions and the actual data. The charts above indicate that the price movements on the first day might only be reflected on the second day. Although the differences are insignificant, this delay could potentially cause issues with timely order execution in backtesting. The next article will provide further analysis of LSTM stock price prediction.<\/p>\n\n\n\n<p>Note: <strong>This analysis is for reference only and does not constitute any product or investment advice.<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Source_Code\"><\/span>Source Code<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><span style=\"text-decoration: underline;\" class=\"ek-underline\"><a href=\"https:\/\/github.com\/tejtw\/TEJAPI_Python_Medium_Application\/tree\/main\/LSTM%20%E5%9B%9E%E6%B8%AC\" class=\"ek-link\" target=\"_blank\" rel=\"noopener\">Click here to visit GitHub<\/a><\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Extended_Reading\"><\/span>Extended Reading<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><a href=\"https:\/\/www.tejwin.com\/en\/?post_type=insight&amp;p=26933\" class=\"ek-link\"><strong>Verifying LSTM Stock Price Prediction Effectiveness Using TQuant Lab (Part 2)<\/strong><\/a><\/p>\n\n\n\n<div style=\"border: 1px black; border-style: solid none; text-align: center; border-color: #296580; padding: 24px; margin-top: 24px; margin-bottom: 24px;\">\n<p style=\"margin: 0px; font-size: 24px; font-weight: bold; line-height: 1.5;\">Start Building Portfolios That Outperform the Market!<\/p>\n<div style=\"margin-top: 32px;\"><strong><a style=\"border: none; border-radius: 4px; background-color: #296580; color: white; font-size: 20px; width: fit-content; text-decoration: none; padding: 12px 30px 12px 30px;\" href=\"https:\/\/www.tejwin.com\/en\/databank-solution\/market-data\/\">TEJ Market Databank<\/a><\/strong><\/div>\n<\/div>\n\n\n\n<p class=\"has-text-align-center\" style=\"font-size:32px\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong><em>&#8220;Taiwan stock market data, TEJ collect it all.&#8221;<\/em><\/strong><\/mark><\/p>\n\n\n\n<p>The characteristics of the Taiwan stock market differ from those of other European and American markets, and <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"><span style=\"text-decoration: underline;\" class=\"ek-underline\">the dynamics of retail investors are worth noting<\/span><\/mark>. Especially in the first quarter of 2024, with the <strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#c05d5d\" class=\"has-inline-color\">Taiwan Stock Exchange reaching a new high of 20,000 points<\/mark><\/strong> due to the rise in TSMC&#8217;s stock price, global institutional investors are paying more attention to the performance of the Taiwan stock market.&nbsp;<\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0978b8\" class=\"has-inline-color\">Taiwan Economical Journal (TEJ)<\/mark><\/strong>, a financial database established in Taiwan for over 30 years, serves local financial institutions and academic institutions, and has long-term cooperation with internationally renowned data providers, providing high-quality financial data for five financial markets in Asia.&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><mark style=\"background-color:#ebc766\" class=\"has-inline-color has-black-color\">Complete Coverage<\/mark><\/strong>: Includes all listed companies on stock markets in Taiwan, China, Hong Kong, Japan, Korea, etc.&nbsp;<\/li>\n\n\n\n<li><strong><mark style=\"background-color:#ebc766\" class=\"has-inline-color\">Comprehensive Analysis of Enterprises<\/mark><\/strong>: Operational aspects, financial aspects, securities market performance, ESG sustainability, etc.&nbsp;<\/li>\n\n\n\n<li><strong><mark style=\"background-color:#ebc766\" class=\"has-inline-color\">High-Quality Database<\/mark><\/strong>: TEJ data is cleaned, checked, enhanced, and integrated to ensure it meets the information needs of financial and market analysis.&nbsp;<\/li>\n<\/ul>\n\n\n\n<p>With TEJ&#8217;s assistance, you can access relevant information about major stock markets in Asia, such as securities market, financials data, enterprise operations, board of directors, sustainability data, etc., providing investors with timely and high-quality content. Additionally, TEJ offers advisory services to help solve problems in theoretical practice and financial management!<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-a89b3969 wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button has-custom-width wp-block-button__width-100 has-custom-font-size\" style=\"font-size:21px\"><a class=\"wp-block-button__link has-background has-text-align-center wp-element-button\" href=\"https:\/\/www.tejwin.com\/en\/contact\/\" style=\"border-radius:16px;background:linear-gradient(135deg,rgb(160,209,216) 0%,rgb(51,145,181) 50%,rgb(50,95,191) 100%)\"><strong>Want to Learn More About Our Databases?<br>Contact Us and Get the Free Trial!<\/strong><\/a><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This article uses the LSTM time series model for deep learning-based LSTM stock price prediction, utilizing the opening, high, low, and closing prices of the past five days, quarterly ROE, MOM (indicating the magnitude of price trend changes, and the direction of market trends), and RSI indicators to predict the next day&#8217;s closing price.<\/p>\n","protected":false},"featured_media":25660,"template":"","tags":[],"insight-category":[690,50,1356],"class_list":["post-26402","insight","type-insight","status-publish","has-post-thumbnail","hentry","insight-category-data-analysis","insight-category-fintech","insight-category-tquant-lab-en"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/26402","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":9,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/26402\/revisions"}],"predecessor-version":[{"id":27057,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/26402\/revisions\/27057"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media\/25660"}],"wp:attachment":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media?parent=26402"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/tags?post=26402"},{"taxonomy":"insight-category","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight-category?post=26402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}