{"id":1424,"date":"2024-07-31T13:10:55","date_gmt":"2024-07-31T13:10:55","guid":{"rendered":"https:\/\/www.tejwin.com\/en\/insights\/lstm-stock-price-prediction\/"},"modified":"2024-07-31T13:10:55","modified_gmt":"2024-07-31T13:10:55","slug":"lstm-stock-price-prediction","status":"publish","type":"insight","link":"https:\/\/www.tejwin.com\/en\/insights\/lstm-stock-price-prediction\/","title":{"rendered":"Verifying LSTM Stock Price Prediction Effectiveness Using TQuant Lab (Part 1)"},"content":{"rendered":"<figure class=\"wp-block-image size-full caption-align-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"LSTM Stock Price Prediction\" class=\"wp-image-25659\" height=\"2304\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/Metal-Structure-Low-Angle.jpg\" width=\"3456\"\/><figcaption class=\"wp-element-caption\">Photo by\u00a0<a href=\"https:\/\/unsplash.com\/@alinnnaaaa?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash\" rel=\"noopener\" target=\"_blank\">Alina Grubnyak<\/a>\u00a0on\u00a0<a href=\"https:\/\/unsplash.com\/photos\/low-angle-photography-of-metal-structure-ZiQkhI7417A?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash\" rel=\"noopener\" target=\"_blank\">Unsplash<\/a><\/figcaption><\/figure>\n<h2 class=\"wp-block-heading\" id=\"1139\"><strong>Key Highlights<\/strong><\/h2>\n<ul class=\"wp-block-list\">\n<li>Article Difficulty: \u2605\u2605\u2605\u2605\u2605<\/li>\n<li>Combining fundamental, sentiment, and technical data to perform LSTM stock price prediction and executing backtest performance verification.<\/li>\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 class=\"ek-link\" href=\"\/en\/insight\/lstm\/\"><strong>[Data Science] LSTM<\/strong><\/a> resource.<\/li>\n<\/ul>\n<h2 class=\"wp-block-heading\" id=\"87be\"><strong>Introduction<\/strong><\/h2>\n<p class=\"wp-block-paragraph\" 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\u2019s closing price.<\/p>\n<h2 class=\"wp-block-heading\"><strong>Editing Environment and Module Requirements<\/strong><\/h2>\n<p class=\"wp-block-paragraph\">This article uses Mac OS and VS Code as editors.<\/p>\n<h2 class=\"wp-block-heading\"><strong>LSTM Model Construction<\/strong><\/h2>\n<p class=\"wp-block-paragraph\">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 \u201cSmall and Medium Cap 300 Index\u201d) 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<h3 class=\"wp-block-heading\"><strong>Loading External Packages<\/strong><\/h3>\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<h3 class=\"wp-block-heading\"><strong>Loading Internal Packages<\/strong><\/h3>\n<p class=\"wp-block-paragraph\"><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\u2019s 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<pre class=\"wp-block-code\"><code>ml_stock = ML_stock()\nml_stock.ini()\nstart = '2012-07-01'\nend = '2022-07-01'<\/code><\/pre>\n<p class=\"wp-block-paragraph\">We have retained only the necessary features for the next steps.<\/p>\n<figure class=\"wp-block-image size-full caption-align-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"LSTM Stock Price Prediction\" class=\"wp-image-25669\" height=\"662\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/E688AAE59C96-2024-07-26-E4B88BE58D885.19.38.png\" width=\"1223\"\/><figcaption class=\"wp-element-caption\">Data Preprocessing<\/figcaption><\/figure>\n<h2 class=\"wp-block-heading\"><strong>Creating Time Series Data<\/strong><\/h2>\n<p class=\"wp-block-paragraph\">First, standardize all data and define the training set\u2019s window_size as 5. This means each data point consists of the current day\u2019s data and the following five days\u2019 data. Data is iterated through a sliding window approach, so each data point overlaps the previous data point by five days.<\/p>\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" alt=\"LSTM Stock Price Prediction\" class=\"wp-image-25722\" height=\"518\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/image-472.png\" width=\"593\"\/><\/figure>\n<p class=\"wp-block-paragraph\">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\u2019s 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<div aria-hidden=\"true\" class=\"wp-block-spacer\" style=\"height:25px\"><\/div>\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=\"\/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<div aria-hidden=\"true\" class=\"wp-block-spacer\" style=\"height:25px\"><\/div>\n<h2 class=\"wp-block-heading\"><strong>Build the LSTM Stock Price Prediction Model<\/strong><\/h2>\n<p class=\"wp-block-paragraph\">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<pre class=\"wp-block-code\"><code>model = Sequential([layers.Input((X_train.shape[1], X_train.shape[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=['mae'])\nearly_stopping = EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)<\/code><\/pre>\n<figure class=\"wp-block-image aligncenter size-full caption-align-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"LSTM Stock Price Prediction\" class=\"wp-image-25738\" height=\"654\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/E688AAE59C96-2024-07-30-E4B88AE58D8810.04.28.png\" width=\"848\"\/><figcaption class=\"wp-element-caption\">Model Structure Diagram<\/figcaption><\/figure>\n<figure class=\"wp-block-image size-full caption-align-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"LSTM Stock Price Prediction\" class=\"wp-image-25799\" height=\"413\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/output-1.jpg\" width=\"565\"\/><figcaption class=\"wp-element-caption\">Loss Variation<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">After about 25 epochs, the training loss gradually stops decreasing significantly, indicating that the model converges quickly.<\/p>\n<h2 class=\"wp-block-heading\">LSTM \u80a1\u50f9\u9810\u6e2c\u7d50\u679c\u8a55\u4f30<\/h2>\n<figure class=\"wp-block-image size-full caption-align-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"LSTM Stock Price Prediction\" class=\"wp-image-25821\" height=\"450\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/output5-1.jpg\" width=\"559\"\/><figcaption class=\"wp-element-caption\">Test Set Prediction Comparison \u2013 2618<\/figcaption><\/figure>\n<figure class=\"wp-block-image size-large caption-align-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"LSTM Stock Price Prediction\" class=\"wp-image-25803\" height=\"357\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/output1-1.jpg\" width=\"1024\"\/><figcaption class=\"wp-element-caption\">Prediction vs. Actual Data Comparison \u2013 2618<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">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<figure class=\"wp-block-image size-full caption-align-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"LSTM Stock Price Prediction\" class=\"wp-image-25825\" height=\"450\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/output6-1.jpg\" width=\"559\"\/><figcaption class=\"wp-element-caption\">Test Set Prediction Comparison \u2013 8215<\/figcaption><\/figure>\n<figure class=\"wp-block-image size-full caption-align-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"LSTM Stock Price Prediction\" class=\"wp-image-25817\" height=\"449\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/output4-2.jpg\" width=\"1287\"\/><figcaption class=\"wp-element-caption\">Prediction vs. Actual Data Comparison \u2013 8215<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">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\u2019s closing price for 8215. However, the actual effectiveness will be validated in the next article.<\/p>\n<h2 class=\"wp-block-heading\">LSTM Stock Price Prediction \u2013 Feature Importance Analysis<\/h2>\n<figure class=\"wp-block-image size-full caption-align-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"LSTM Stock Price Prediction\" class=\"wp-image-25807\" height=\"855\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/output2-1.jpg\" width=\"3180\"\/><figcaption class=\"wp-element-caption\">Feature Importance Analysis \u2013 2618<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">Daily price changes remain the model\u2019s 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<figure class=\"wp-block-image size-full caption-align-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"LSTM Stock Price Prediction\" class=\"wp-image-25829\" height=\"855\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/output7-1.jpg\" width=\"3180\"\/><figcaption class=\"wp-element-caption\">Feature Importance Analysis \u2013 8215<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">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<pre class=\"wp-block-code\"><code>model.save(f'lstm_{sample[0]}.keras', include_optimizer = False)<\/code><\/pre>\n<p class=\"wp-block-paragraph\">Finally, we save the model as a <code><strong><em>.keras<\/em><\/strong><\/code> file to facilitate the next backtesting phase.<\/p>\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n<p class=\"wp-block-paragraph\">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\u2019s 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<p class=\"wp-block-paragraph\">Note: <strong>This analysis is for reference only and does not constitute any product or investment advice.<\/strong><\/p>\n<h2 class=\"wp-block-heading\">Source Code<\/h2>\n<p class=\"wp-block-paragraph\"><span class=\"ek-underline\" style=\"text-decoration: underline;\"><a class=\"ek-link\" href=\"https:\/\/github.com\/tejtw\/TEJAPI_Python_Medium_Application\/tree\/main\/LSTM%20%E5%9B%9E%E6%B8%AC\" rel=\"noopener\" target=\"_blank\">Click here to visit GitHub<\/a><\/span><\/p>\n<h2 class=\"wp-block-heading\">Extended Reading<\/h2>\n<p class=\"wp-block-paragraph\"><a class=\"ek-link\" href=\"\/en\/?post_type=insight&amp;p=26933\"><strong>Verifying LSTM Stock Price Prediction Effectiveness Using TQuant Lab (Part 2)<\/strong><\/a><\/p>\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 href=\"\/en\/databank-solution\/market-data\/\" 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;\">TEJ Market Databank<\/a><\/strong><\/div>\n<\/div>\n<p class=\"has-text-align-center wp-block-paragraph\" style=\"font-size:32px\"><mark class=\"has-inline-color has-vivid-cyan-blue-color\" style=\"background-color:rgba(0, 0, 0, 0)\"><strong><em>\u201cTaiwan stock market data, TEJ collect it all.\u201d<\/em><\/strong><\/mark><\/p>\n<p class=\"wp-block-paragraph\">The characteristics of the Taiwan stock market differ from those of other European and American markets, and <mark class=\"has-inline-color has-black-color\" style=\"background-color:rgba(0, 0, 0, 0)\"><span class=\"ek-underline\" style=\"text-decoration: underline;\">the dynamics of retail investors are worth noting<\/span><\/mark>. Especially in the first quarter of 2024, with the <strong><mark class=\"has-inline-color\" style=\"background-color:rgba(0, 0, 0, 0);color:#c05d5d\">Taiwan Stock Exchange reaching a new high of 20,000 points<\/mark><\/strong> due to the rise in TSMC\u2019s stock price, global institutional investors are paying more attention to the performance of the Taiwan stock market.\u00a0<\/p>\n<p class=\"wp-block-paragraph\"><strong><mark class=\"has-inline-color\" style=\"background-color:rgba(0, 0, 0, 0);color:#0978b8\">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.\u00a0<\/p>\n<ul class=\"wp-block-list\">\n<li><strong><mark class=\"has-inline-color has-black-color\" style=\"background-color:#ebc766\">Complete Coverage<\/mark><\/strong>: Includes all listed companies on stock markets in Taiwan, China, Hong Kong, Japan, Korea, etc.\u00a0<\/li>\n<li><strong><mark class=\"has-inline-color\" style=\"background-color:#ebc766\">Comprehensive Analysis of Enterprises<\/mark><\/strong>: Operational aspects, financial aspects, securities market performance, ESG sustainability, etc.\u00a0<\/li>\n<li><strong><mark class=\"has-inline-color\" style=\"background-color:#ebc766\">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.\u00a0<\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\">With TEJ\u2019s 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<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-3e41869c 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=\"\/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\u2019s closing price.<\/p>\n","protected":false},"featured_media":1386,"template":"","tags":[60,70,97],"insight_category":[16],"class_list":["post-1424","insight","type-insight","status-publish","has-post-thumbnail","hentry","tag-quant","tag-tej-api","tag-data-analysis","insight_category-quant-data-science"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/1424","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"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media\/1386"}],"wp:attachment":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media?parent=1424"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/tags?post=1424"},{"taxonomy":"insight_category","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight_category?post=1424"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}