{"id":26933,"date":"2024-08-07T13:00:00","date_gmt":"2024-08-07T05:00:00","guid":{"rendered":"https:\/\/www.tejwin.com\/?post_type=insight&#038;p=26933"},"modified":"2024-09-04T15:26:32","modified_gmt":"2024-09-04T07:26:32","slug":"lstm-stock-price-prediction-part-2","status":"publish","type":"insight","link":"https:\/\/www.tejwin.com\/en\/insight\/lstm-stock-price-prediction-part-2\/","title":{"rendered":"Verifying LSTM Stock Price Prediction Effectiveness Using TQuant Lab (Part 2)"},"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-6a00beded5512\" 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-6a00beded5512\"  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-part-2\/#Summary_of_Key_Points\" >Summary of Key Points<\/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-part-2\/#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-part-2\/#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-part-2\/#Applying_the_LSTM_Model_to_Out-of-Sample_Data\" >Applying the LSTM Model to Out-of-Sample Data<\/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-part-2\/#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-part-2\/#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-part-2\/#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-part-2\/#Importing_Prediction_Results_into_the_Pipeline\" >Importing Prediction Results into the Pipeline<\/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-part-2\/#Creating_the_Pipeline_Function\" >Creating the Pipeline Function<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.tejwin.com\/en\/insight\/lstm-stock-price-prediction-part-2\/#Creating_Custom_Factors\" >Creating Custom Factors<\/a><\/li><\/ul><\/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-part-2\/#Creating_the_initialize_Function\" >Creating the initialize Function<\/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-part-2\/#Creating_the_handle_data_Function\" >Creating the handle_data Function<\/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-part-2\/#%E5%BB%BA%E7%AB%8B_analyze_%E5%87%BD%E5%BC%8F\" >\u5efa\u7acb&nbsp;analyze&nbsp;\u51fd\u5f0f<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.tejwin.com\/en\/insight\/lstm-stock-price-prediction-part-2\/#Running_the_LSTM_Stock_Price_Prediction_Strategy\" >Running the LSTM Stock Price Prediction Strategy<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/www.tejwin.com\/en\/insight\/lstm-stock-price-prediction-part-2\/#Performance_Evaluation_Using_Pyfolio\" >Performance Evaluation Using Pyfolio<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/www.tejwin.com\/en\/insight\/lstm-stock-price-prediction-part-2\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/www.tejwin.com\/en\/insight\/lstm-stock-price-prediction-part-2\/#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-18\" href=\"https:\/\/www.tejwin.com\/en\/insight\/lstm-stock-price-prediction-part-2\/#Extended_Reading\" >Extended Reading<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"1139\"><span class=\"ez-toc-section\" id=\"Summary_of_Key_Points\"><\/span>Summary of Key Points<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, technical, and sentiment data for LSTM stock price prediction and backtesting performance.<\/li>\n\n\n\n<li>Reading Recommendation: This article uses RNN architecture for time series forecasting. A basic understanding of time series or deep learning is recommended. For deeper insights into LSTM model construction, refer to \u00a0<a href=\"https:\/\/www.tejwin.com\/en\/insight\/lstm\/\"><strong>[Data Science] LSTM<\/strong><\/a>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"87be\"><span class=\"ez-toc-section\" id=\"Introduction\"><\/span>Introduction<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In the first article\u2014<em><a href=\"https:\/\/www.tejwin.com\/en\/insight\/lstm-stock-price-prediction\/\" class=\"ek-link\">Verifying LSTM Stock Price Prediction Effectiveness Using TQuant Lab (Part 1)<\/a><\/em>\u2014we compared the predicted data with the actual data to conduct an initial evaluation of the performance of two trained models (for stocks 2618 and 8615). The results were promising. For a more detailed analysis, you can click the link above to learn more, as we will not go into further details here due to space constraints.<\/p>\n\n\n\n<p>Therefore, in this second article, we aim to move beyond theoretical discussions and apply the model to out-of-sample data. Based on the prediction results, we will determine entry points and test whether the actual performance aligns with our expectations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"f4e9\"><span class=\"ez-toc-section\" id=\"Editing_Environment_and_Module_Requirements\"><\/span>Editing Environment and Module Requirements<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This article utilizes <strong>Mac OS<\/strong> and <strong>VS Code<\/strong> as the editor.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Applying_the_LSTM_Model_to_Out-of-Sample_Data\"><\/span>Applying the LSTM Model to Out-of-Sample Data<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The in-sample data for our two LSTM stock price prediction models spans from <strong>July 1, 2012<\/strong>, to <strong>July 1, 2021<\/strong>. Therefore, <span style=\"box-sizing: border-box; margin: 0px; padding: 0px;\">to avoid overlap with the training period, the backtesting period will be from\u00a0<strong>January 1, 2021<\/strong>, to\u00a0<strong>June 30, 2024<\/strong><\/span>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Loading_External_Packages\"><\/span>Loading External Packages<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 talib as ta\nfrom talib import abstract\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>Loading Internal Packages<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The <code>ML_stock()<\/code> class is custom-built for pre-processing data. It handles key tasks such as loading the <strong>API_KEY<\/strong>, price-volume data, fundamental data, and technical indicators. Finally, it sets the start and end dates for the backtesting sample. Additionally, we define the <code>model<\/code> variable to load the pre-trained model for use.<\/p>\n\n\n\n<p><em>Note: A friendly reminder to input your own <strong>API_KEY<\/strong> in the <code>config.ini<\/code> file before use to ensure everything works smoothly!<\/em><\/p>\n\n\n\n<p>Next, we retain only the necessary features to complete the LSTM stock price prediction data preprocessing.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full caption-align-center\"><img decoding=\"async\" width=\"1161\" height=\"499\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/\u622a\u5716-2024-08-06-\u4e0a\u53488.55.15.png\" alt=\"LSTM Stock Price Prediction\" class=\"wp-image-26118\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/\u622a\u5716-2024-08-06-\u4e0a\u53488.55.15.png 1161w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u622a\u5716-2024-08-06-\u4e0a\u53488.55.15-300x129.png 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u622a\u5716-2024-08-06-\u4e0a\u53488.55.15-1024x440.png 1024w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u622a\u5716-2024-08-06-\u4e0a\u53488.55.15-150x64.png 150w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u622a\u5716-2024-08-06-\u4e0a\u53488.55.15-768x330.png 768w\" sizes=\"(max-width: 1161px) 100vw, 1161px\" \/><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>Creating Time Series Data<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>As we did during training, we will convert the data into a time series format. For detailed steps, refer to <a href=\"https:\/\/www.tejwin.com\/en\/insight\/lstm-stock-price-prediction\/\" class=\"ek-link\">the previous article<\/a>. Once the conversion is complete, we will use the <code>predict<\/code> function to apply the model for forecasting.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>prediction = model.predict(X)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full caption-align-center\"><img decoding=\"async\" width=\"547\" height=\"449\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/iLoveIMG-output.jpg\" alt=\"LSTM Stock Price Prediction\" class=\"wp-image-26122\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/iLoveIMG-output.jpg 547w, https:\/\/www.tejwin.com\/wp-content\/uploads\/iLoveIMG-output-300x246.jpg 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/iLoveIMG-output-150x123.jpg 150w\" sizes=\"(max-width: 547px) 100vw, 547px\" \/><figcaption class=\"wp-element-caption\">LSTM Stock Price Prediction Results for 2618<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full caption-align-center\"><img loading=\"lazy\" decoding=\"async\" width=\"546\" height=\"449\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/iLoveIMG-output1.jpg\" alt=\"LSTM Stock Price Prediction\" class=\"wp-image-26124\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/iLoveIMG-output1.jpg 546w, https:\/\/www.tejwin.com\/wp-content\/uploads\/iLoveIMG-output1-300x247.jpg 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/iLoveIMG-output1-150x123.jpg 150w\" sizes=\"(max-width: 546px) 100vw, 546px\" \/><figcaption class=\"wp-element-caption\">LSTM Stock Price Prediction Results for 8215<\/figcaption><\/figure>\n\n\n\n<p>We can observe that the LSTM model, trained on data from the past ten years up to 2022, shows a prediction performance that is largely consistent with actual stock prices after 2022. Aside from the model&#8217;s difficulty in fully capturing the magnitude of price fluctuations\u2014something already noted during validation\u2014the overall results are satisfactory.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Importing_Prediction_Results_into_the_Pipeline\"><\/span>Importing Prediction Results into the Pipeline<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The <code>CustomDataset<\/code> allows for the integration of database content into the Pipeline, facilitating future backtesting. In this example, we use it to import the predicted values recorded in the <code>Pred<\/code> column into the Pipeline. Below is an excerpt of the code:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full caption-align-center\"><img loading=\"lazy\" decoding=\"async\" width=\"456\" height=\"576\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/\u622a\u5716-2024-08-06-\u4e0a\u53489.12.00.png\" alt=\"LSTM Stock Price Prediction\" class=\"wp-image-26129\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/\u622a\u5716-2024-08-06-\u4e0a\u53489.12.00.png 456w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u622a\u5716-2024-08-06-\u4e0a\u53489.12.00-238x300.png 238w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u622a\u5716-2024-08-06-\u4e0a\u53489.12.00-119x150.png 119w\" sizes=\"(max-width: 456px) 100vw, 456px\" \/><figcaption class=\"wp-element-caption\">Prediction Results into the Pipeline\u20142618<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creating_the_Pipeline_Function\"><\/span>Creating the Pipeline Function<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Since the LSTM Stock Price Prediction model only predicts the next day&#8217;s closing price, determining the exact entry points, timing, and conditions requires more detailed configuration. To achieve this, we need to design custom factors.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creating_Custom_Factors\"><\/span>Creating Custom Factors<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The <code>CustomFactor<\/code> allows users to design their own custom factors as needed. In this case, we use it to handle:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Daily return<\/strong> relative to the previous trading day (Return)<\/li>\n\n\n\n<li><strong>Average True Range<\/strong> (ATR)<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full caption-align-center\"><img loading=\"lazy\" decoding=\"async\" width=\"1106\" height=\"504\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/\u622a\u5716-2024-08-06-\u4e0a\u53489.20.40.png\" alt=\"LSTM Stock Price Prediction\" class=\"wp-image-26136\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/\u622a\u5716-2024-08-06-\u4e0a\u53489.20.40.png 1106w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u622a\u5716-2024-08-06-\u4e0a\u53489.20.40-300x137.png 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u622a\u5716-2024-08-06-\u4e0a\u53489.20.40-1024x467.png 1024w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u622a\u5716-2024-08-06-\u4e0a\u53489.20.40-150x68.png 150w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u622a\u5716-2024-08-06-\u4e0a\u53489.20.40-768x350.png 768w\" sizes=\"(max-width: 1106px) 100vw, 1106px\" \/><figcaption class=\"wp-element-caption\">Pipeline Output\u20142618<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creating_the_initialize_Function\"><\/span>Creating the <code>initialize<\/code> Function<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The <code>initialize()<\/code> function is used to define the daily trading environment before the start of each trading session. In this case, we configure:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Slippage costs<\/li>\n\n\n\n<li><strong>Transaction fee model<\/strong> for the Taiwan stock market<\/li>\n\n\n\n<li>The <strong>Weighted Return Index (IR0001)<\/strong> as the market benchmark<\/li>\n\n\n\n<li>Integrate the strategy factors designed in the Pipeline into the trading process<\/li>\n\n\n\n<li>Set the <code>context.stop_loss<\/code> variable to record the stop-loss point during backtesting<\/li>\n\n\n\n<li>Set the <code>context.last_price<\/code> variable to track the last buy\/sell price for monitoring stop-losses<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creating_the_handle_data_Function\"><\/span>Creating the <code>handle_data<\/code> Function<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The <code><strong>handle_data()<\/strong><\/code> function is crucial for building the LSTM stock price prediction strategy, as it is called daily during the backtest. Its primary tasks include setting the trading strategy, placing orders, and recording trade information.<\/p>\n\n\n\n<p>For detailed trading rules of this strategy, please refer to <a href=\"https:\/\/github.com\/tejtw\/TEJAPI_Python_Medium_Application\/blob\/main\/LSTM%20%E5%9B%9E%E6%B8%AC\/backtest_2618.ipynb\" class=\"ek-link\" target=\"_blank\" rel=\"noopener\">backtest_2618.ipynb<\/a> \/ <a href=\"https:\/\/github.com\/tejtw\/TEJAPI_Python_Medium_Application\/blob\/main\/LSTM%20%E5%9B%9E%E6%B8%AC\/backtest_8215.ipynb\" class=\"ek-link\" target=\"_blank\" rel=\"noopener\">backtest_8215.ipynb<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        if return_pred &lt; 0 and cash_position &gt;= 0 and returns &lt; 0:\n            order_percent(i , 0.48)\n            buy = True\n            record(\n                **{\n                    f'buy_{sym}':buy\n                }\n            )\n            context.stop_loss = price - atr * 1.25<\/code><\/pre>\n\n\n\n<p>In this example, we use <code>return_pred<\/code> to record predicted stock prices and <code>returns<\/code> for actual price movements. If both trends align positively, we enter a position. Exiting the position is based on a combination of take-profit and trailing stop-loss mechanisms. This strategy only backtests long positions for single-sided entry. Those interested can explore an LSTM stock price prediction strategy that incorporates both long and short positions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%BB%BA%E7%AB%8B_analyze_%E5%87%BD%E5%BC%8F\"><\/span>\u5efa\u7acb&nbsp;<code>analyze<\/code>&nbsp;\u51fd\u5f0f<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The <code><strong>analyze()<\/strong><\/code> function helps us generate custom charts. In this example, we use <code>analyze()<\/code> to observe the differences between predicted and actual stock prices, visualize the entry and exit points of the strategy, and monitor capital utilization.<\/p>\n\n\n\n<p>For detailed trading rules of this strategy, please refer to <a href=\"https:\/\/github.com\/tejtw\/TEJAPI_Python_Medium_Application\/blob\/main\/LSTM%20%E5%9B%9E%E6%B8%AC\/backtest_2618.ipynb\" class=\"ek-link\" target=\"_blank\" rel=\"noopener\">backtest_2618.ipynb<\/a> \/ <a href=\"https:\/\/github.com\/tejtw\/TEJAPI_Python_Medium_Application\/blob\/main\/LSTM%20%E5%9B%9E%E6%B8%AC\/backtest_8215.ipynb\" class=\"ek-link\" target=\"_blank\" rel=\"noopener\">backtest_8215.ipynb<\/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<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Running_the_LSTM_Stock_Price_Prediction_Strategy\"><\/span>Running the LSTM Stock Price Prediction Strategy<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Use the <code><strong>run_algorithm()<\/strong><\/code> function to execute the LSTM stock price prediction strategy configured above. Set the trading period from <code>start_dt<\/code> (2021-01-01) to <code>end_dt<\/code> (2024-06-28), and import the <code>custom_loader<\/code>. The dataset used is <code>tquant<\/code>, with an initial capital of 1 million TWD. The output, <code>results<\/code>, will include the daily performance metrics and detailed transaction records.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full caption-align-center\"><img loading=\"lazy\" decoding=\"async\" width=\"1291\" height=\"886\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/iLoveIMG-output3.jpg\" alt=\"LSTM Stock Price Prediction\" class=\"wp-image-26143\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/iLoveIMG-output3.jpg 1291w, https:\/\/www.tejwin.com\/wp-content\/uploads\/iLoveIMG-output3-300x206.jpg 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/iLoveIMG-output3-1024x703.jpg 1024w, https:\/\/www.tejwin.com\/wp-content\/uploads\/iLoveIMG-output3-150x103.jpg 150w, https:\/\/www.tejwin.com\/wp-content\/uploads\/iLoveIMG-output3-768x527.jpg 768w\" sizes=\"(max-width: 1291px) 100vw, 1291px\" \/><figcaption class=\"wp-element-caption\">Analyze Charts \u2013 2618<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full caption-align-center\"><img loading=\"lazy\" decoding=\"async\" width=\"1291\" height=\"886\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/iLoveIMG-8215.jpg\" alt=\"LSTM Stock Price Prediction\" class=\"wp-image-26145\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/iLoveIMG-8215.jpg 1291w, https:\/\/www.tejwin.com\/wp-content\/uploads\/iLoveIMG-8215-300x206.jpg 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/iLoveIMG-8215-1024x703.jpg 1024w, https:\/\/www.tejwin.com\/wp-content\/uploads\/iLoveIMG-8215-150x103.jpg 150w, https:\/\/www.tejwin.com\/wp-content\/uploads\/iLoveIMG-8215-768x527.jpg 768w\" sizes=\"(max-width: 1291px) 100vw, 1291px\" \/><figcaption class=\"wp-element-caption\">Analyze Charts \u2013 8215<\/figcaption><\/figure>\n\n\n\n<p>The two charts above demonstrate that the strategy achieved strong performance. Specifically, the backtest for stock 2618 consistently outperformed the market throughout the entire period, while stock 8215 initially led the market but was slightly overtaken towards the end.<br><em>Note: In the second small chart, the blue line represents the predicted stock price, the red line shows the actual stock price, red triangles indicate buy signals, and green triangles indicate sell signals.<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Performance_Evaluation_Using_Pyfolio\"><\/span>Performance Evaluation Using Pyfolio<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=\"1288\" height=\"636\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/\u514d\u8cbb\u5f71\u50cf\u5408\u4f75\u5de5\u5177-1.jpg\" alt=\"LSTM Stock Price Prediction\" class=\"wp-image-26154\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/\u514d\u8cbb\u5f71\u50cf\u5408\u4f75\u5de5\u5177-1.jpg 1288w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u514d\u8cbb\u5f71\u50cf\u5408\u4f75\u5de5\u5177-1-300x148.jpg 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u514d\u8cbb\u5f71\u50cf\u5408\u4f75\u5de5\u5177-1-1024x506.jpg 1024w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u514d\u8cbb\u5f71\u50cf\u5408\u4f75\u5de5\u5177-1-150x74.jpg 150w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u514d\u8cbb\u5f71\u50cf\u5408\u4f75\u5de5\u5177-1-768x379.jpg 768w\" sizes=\"(max-width: 1288px) 100vw, 1288px\" \/><figcaption class=\"wp-element-caption\">LSTM Stock Price Prediction Backtest Performance vs. Market \u2013 2618<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full caption-align-center\"><img loading=\"lazy\" decoding=\"async\" width=\"1288\" height=\"632\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/\u514d\u8cbb\u5f71\u50cf\u5408\u4f75\u5de5\u5177.jpg\" alt=\"LSTM Stock Price Prediction\" class=\"wp-image-26152\" style=\"object-fit:cover\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/\u514d\u8cbb\u5f71\u50cf\u5408\u4f75\u5de5\u5177.jpg 1288w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u514d\u8cbb\u5f71\u50cf\u5408\u4f75\u5de5\u5177-300x147.jpg 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u514d\u8cbb\u5f71\u50cf\u5408\u4f75\u5de5\u5177-1024x502.jpg 1024w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u514d\u8cbb\u5f71\u50cf\u5408\u4f75\u5de5\u5177-150x74.jpg 150w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u514d\u8cbb\u5f71\u50cf\u5408\u4f75\u5de5\u5177-768x377.jpg 768w\" sizes=\"(max-width: 1288px) 100vw, 1288px\" \/><figcaption class=\"wp-element-caption\">LSTM Stock Price Prediction Backtest Performance vs. Market \u2013 8215<\/figcaption><\/figure>\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 the backtests for the two stocks above, we can see that the LSTM stock price prediction strategy yielded promising results. Both backtests achieved a Sharpe ratio above 0.5, and the Alpha values were also at a favorable level. Notably, the backtest for stock 2618 was particularly impressive, with an annualized return of 29.6% over 40 months and a cumulative return of nearly 138%.<\/p>\n\n\n\n<p>Looking more closely at the characteristics of the strategy, since it is based on predicting the next day&#8217;s closing price as a target for entry, the return curve closely mirrors the actual stock price trend. In other words, when the stock price rises, the strategy rises, and when the price falls, the strategy follows suit. Therefore, controlling stop-loss and take-profit mechanisms is crucial and demands careful attention.<\/p>\n\n\n\n<p>Additionally, during the strategy development process, we noticed that the rule of &#8220;entering when the predicted return (return_pred) and the actual return (returns) are both positive&#8221; does not universally apply to all models. For some stocks, it may be more profitable to enter when both trends are negative or when there is more divergence between positive and negative trends. Otherwise, the performance of LSTM Stock Price Predictionfluctuations could be significant.<\/p>\n\n\n\n<p>We attribute this issue to the inherent lag in time series models. This lag can result in buying at a peak (since the peak may have occurred the previous day, and the model reflects it only the next day), leading to higher costs, or selling at a low (for the same reason), which can worsen performance. As a result, this strategy requires further adjustment of parameters to find the optimal solution for LSTM Stock Price Prediction.<\/p>\n\n\n\n<p>Investors are welcome to explore this approach, and we will continue to introduce methods for constructing various indicators using the TEJ database and backtesting their performance. For readers interested in various trading backtests, we encourage you to consider purchasing related plans from <a href=\"https:\/\/tquant.tejwin.com\/\" class=\"ek-link\" target=\"_blank\" rel=\"noopener\">TQuant Lab<\/a> to leverage high-quality databases and develop personalized trading strategies.<\/p>\n\n\n\n<p><strong>Important Reminder<\/strong>: This analysis is for reference only and does not constitute any product or investment advice.<\/p>\n\n\n\n<p>We welcome readers interested in various trading strategies to consider purchasing relevant solutions from\u00a0<a href=\"https:\/\/www.tejwin.com\/en\/solution\/quantitative-finance-solution\/\" class=\"ek-link\"><strong><mark style=\"background-color:#ffdf88\" class=\"has-inline-color\">Quantitative Finance Solution<\/mark><\/strong><\/a>. With our high-quality databases, you can construct a trading strategy that suits your needs.<\/p>\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\/financial-data\/\" style=\"border-radius:16px;background:linear-gradient(135deg,rgb(243,224,131) 0%,rgb(102,197,166) 50%,rgb(51,132,181) 100%)\"><strong>Access to Comprehensive Quantitative Data<\/strong><br><strong>Start Building Portfolios That Outperform the Market Today!<\/strong><\/a><\/div>\n<\/div>\n\n\n\n<div style=\"height:22px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/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. 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 and Solutions?<br>Contact Us and Get the Free Trial Today!<\/strong><\/a><\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><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<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 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><strong><a href=\"https:\/\/www.tejwin.com\/en\/insight\/lstm-stock-price-prediction\/\" class=\"ek-link\">Verifying LSTM Stock Price Prediction Effectiveness Using TQuant Lab (Part 1)<\/a><\/strong><\/p>\n\n\n\n<p><strong><a href=\"https:\/\/www.tejwin.com\/en\/insight\/lstm-trading-signal-detection\/\" class=\"ek-link\">LSTM Trading Signal Detection<\/a><\/strong><\/p>\n\n\n\n<p><strong><a href=\"https:\/\/www.tejwin.com\/en\/insight\/lstm\/\" class=\"ek-link\">LSTM<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the first article\u2014Verifying LSTM Stock Price Prediction Effectiveness Using TQuant Lab (Part 1)\u2014we compared the predicted data with the actual data to conduct an initial evaluation of the performance of two trained models (for stocks 2618 and 8615). The results were promising. For a more detailed analysis, you can click the link above to learn more, as we will not go into further details here due to space constraints.<\/p>\n","protected":false},"featured_media":25660,"template":"","tags":[3005,3007,3008],"insight-category":[50,1356],"class_list":["post-26933","insight","type-insight","status-publish","has-post-thumbnail","hentry","tag-tejapi-application","tag-tejapi-data-analysis","tag-tejapi-quant","insight-category-fintech","insight-category-tquant-lab-en"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/26933","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":5,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/26933\/revisions"}],"predecessor-version":[{"id":33712,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/26933\/revisions\/33712"}],"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=26933"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/tags?post=26933"},{"taxonomy":"insight-category","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight-category?post=26933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}