{"id":16114,"date":"2021-07-27T03:31:26","date_gmt":"2021-07-26T19:31:26","guid":{"rendered":"https:\/\/www.tejwin.com\/?post_type=insight&#038;p=16114"},"modified":"2023-08-10T11:30:57","modified_gmt":"2023-08-10T03:30:57","slug":"modular-backtesting-system","status":"publish","type":"insight","link":"https:\/\/tejwin20260323.j.webweb.today\/en\/insight\/modular-backtesting-system\/","title":{"rendered":"Modular Backtesting System"},"content":{"rendered":"\n<p>Use TEJ API to construct our own backtest system.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" src=\"https:\/\/tejwin20260323.j.webweb.today\/wp-content\/uploads\/1_1zZdJl9NOBgocIjH_1K5y7g.jpg\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Photo Creds:\u00a0<a href=\"https:\/\/unsplash.com\/\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"ek-link\">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-6a11531dccc69\" 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-6a11531dccc69\"  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:\/\/tejwin20260323.j.webweb.today\/en\/insight\/modular-backtesting-system\/#Preface\" >Preface<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/tejwin20260323.j.webweb.today\/en\/insight\/modular-backtesting-system\/#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-3\" href=\"https:\/\/tejwin20260323.j.webweb.today\/en\/insight\/modular-backtesting-system\/#The_highlights_of_the_article\" >The highlights of the article<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/tejwin20260323.j.webweb.today\/en\/insight\/modular-backtesting-system\/#Building_System\" >Building System<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/tejwin20260323.j.webweb.today\/en\/insight\/modular-backtesting-system\/#Review_the_performance_of_the_market\" >Review the performance of the market<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/tejwin20260323.j.webweb.today\/en\/insight\/modular-backtesting-system\/#Custom_our_own_portfolio\" >Custom our own portfolio<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/tejwin20260323.j.webweb.today\/en\/insight\/modular-backtesting-system\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/tejwin20260323.j.webweb.today\/en\/insight\/modular-backtesting-system\/#Related_Link\" >Related Link<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"141e\"><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=\"cff7\">Trade Backtest is a more scientific approach to test strategy nowadays. Although the past cannot represent the future. The result of backtest can provide us some information about the strategy. However, if we have to run different strategy, we need to build a new system. This way will waste our much time and it is inefficient. Therefore, we try to modular the system and focus on developing the strategy.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"10c9\"><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=\"fea8\">We use Windows OS and Jupyter Notebook in this article.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import tejapi\nimport pandas as pd\nimport numpy as np\ntejapi.ApiConfig.api_key = 'Your Key'\ntejapi.ApiConfig.ignoretz = True<\/code><\/pre>\n\n\n\n<p id=\"3679\"><em>Note: Remember to replace \u2018Your key\u2019 with the one you applied for. The last line indicates that we ignore the time zone.<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2326\"><span class=\"ez-toc-section\" id=\"The_highlights_of_the_article\"><\/span><strong>The highlights of the article<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Pre-planning of Building System<\/li>\n\n\n\n<li>Modular System<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"05e9\"><span class=\"ez-toc-section\" id=\"Building_System\"><\/span><strong>Building System<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"e893\"><strong>Step 1.&nbsp;<\/strong>Thinking about what tables or variables we might need.<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Stock Pool<\/li>\n\n\n\n<li>Raw Data<\/li>\n\n\n\n<li>Trade Strategy<\/li>\n\n\n\n<li>Trade Signal<\/li>\n\n\n\n<li>Backtesting<\/li>\n\n\n\n<li>Sell Variable<\/li>\n\n\n\n<li>Buy Variable<\/li>\n<\/ul>\n\n\n\n<p id=\"499e\">Consider the columns required for the process.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/tejwin20260323.j.webweb.today\/wp-content\/uploads\/1_1FHBtPUBKG21ejj1EPDmBng.png\" alt=\"\"\/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"6b6e\"><strong>Step 2.\u00a0<\/strong>Creating\u00a0<code><em>class<\/em><\/code>\u00a0name\u00a0<code><em>backtest<\/em><\/code><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>class backtest():<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"e9e7\"><strong>Step 3.\u00a0<\/strong>All of the\u00a0<code><em>def<\/em><\/code>\u00a0is created under this\u00a0<code><em>class<\/em><\/code>.<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>def __init__(self, target_list):\n        # \u8a2d\u5b9a\u521d\u59cb\u503c\n        \n        # \u80a1\u7968\u6c60\n        self.target_list = target_list\n# \u8a0a\u865f\u8868\n        self.signal_data = pd.DataFrame()\n        \n        # \u4ea4\u6613\u8868\n        self.action_data = pd.DataFrame()\n        \n        # \u7372\u5229\u6578\n        self.protfolio_profit = &#91;]\n        \n        # \u6210\u672c\u6578\n        self.protfolio_cost = &#91;]<\/code><\/pre>\n\n\n\n<p>Raw also need to put it into this class.\u00a0<code><em>target_list<\/em><\/code>\u00a0is our stock pool.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u8cc7\u6599\u5eab\nself.data = tejapi.get('TWN\/APRCD',\n               coid = target_list,\n               mdate={'gte':'2020-01-01', 'lte':'2020-12-31'},\n               opts={'columns':&#91;'coid','mdate','close_d', \n               'volume']},\n               chinese_column_name=True,\n               paginate=True).reset_index(drop=True)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"9495\"><strong>Step 4.&nbsp;<\/strong>Making Strategy, here we choose common strategies. We only explain specific point. To see full code, you can click this&nbsp;<a href=\"https:\/\/gist.github.com\/tommy780507\/fde794d165d2d0fa3bc146588bf2de18.js\" rel=\"noreferrer noopener\" target=\"_blank\">link<\/a>.<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Price and Volume Strategy<\/li>\n<\/ul>\n\n\n\n<p id=\"d128\">We will buy the stock if the price and volume break through the highest price and volume in the past 20 days.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def price_volume(self, specific=None):\n        # \u7b56\u7565\u66f4\u65b0\n        self.signal_data = pd.DataFrame()\n        self.action_data = pd.DataFrame()<\/code><\/pre>\n\n\n\n<p id=\"2343\">Generate transaction signals for each stock.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u80a1\u7968\u6c60\u6bcf\u4e00\u6a94\u80a1\u7968\u90fd\u8dd1\u4e00\u6b21\u8a0a\u865f\u7522\u751f\n for i in self.target_list:\n    target_data = self.data&#91;self.data&#91;'\u8b49\u5238\u4ee3\u78bc'] == i]<\/code><\/pre>\n\n\n\n<p>Calculate the highest and lowest price and volume in past 10 and 20 days.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">rolling_max = target_data[['\u6536\u76e4\u50f9(\u5143)', '\u6210\u4ea4\u91cf(\u5343\u80a1)']].rolling(20).max()rolling_min = target_data[['\u6536\u76e4\u50f9(\u5143)', '\u6210\u4ea4\u91cf(\u5343\u80a1)']].rolling(10).min()<\/pre>\n\n\n\n<p>Buy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>stock_data&#91;'\u8cb7\u5165\u8a0a\u865f\u5224\u65b7'] = np.where((stock_data&#91;'\u6536\u76e4\u50f9(\u5143)'] == stock_data&#91;'\u6536\u76e4\u50f9(max)']) &amp; (stock_data&#91;'\u6210\u4ea4\u91cf(\u5343\u80a1)'] == stock_data&#91;'\u6210\u4ea4\u91cf(max)']), -1, 0)<\/code><\/pre>\n\n\n\n<p>Sell<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>stock_data&#91;'\u8ce3\u51fa\u8a0a\u865f\u5224\u65b7'] = np.where((stock_data&#91;'\u6536\u76e4\u50f9(\u5143)'] == stock_data&#91;'\u6536\u76e4\u50f9(min)']) &amp; (stock_data&#91;'\u6210\u4ea4\u91cf(\u5343\u80a1)'] == stock_data&#91;'\u6210\u4ea4\u91cf(min)']), 1, 0)<\/code><\/pre>\n\n\n\n<p>Offset on last day<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>remain_stock = self.signal_data.iloc&#91;:-1,8:10].sum().sum()\nself.signal_data.iloc&#91;-1,9] = 0\nself.signal_data.iloc&#91;-1,8] = 0\nif remain_stock &lt; 0:\n    self.signal_data.iloc&#91;-1,9] = -remain_stock\nelse:\n    self.signal_data.iloc&#91;-1,8] = -remain_stock\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Buy and Hold strategy<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>def buy_hold(self, specific=None):\n        # \u7b56\u7565\u66f4\u65b0\n        self.signal_data = pd.DataFrame()\n        self.action_data = pd.DataFrame()<\/code><\/pre>\n\n\n\n<p>Generate transaction signals for each stock.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u80a1\u7968\u6c60\u6bcf\u4e00\u6a94\u80a1\u7968\u90fd\u8dd1\u4e00\u6b21\u8a0a\u865f\u7522\u751f\n for i in self.target_list:\n    target_data = self.data&#91;self.data&#91;'\u8b49\u5238\u4ee3\u78bc'] == i]<\/code><\/pre>\n\n\n\n<p>Buy on first day and Sell on last day<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>target_data&#91;'\u8cb7\u5165\u8a0a\u865f\u5224\u65b7'] = 0\ntarget_data&#91;'\u8ce3\u51fa\u8a0a\u865f\u5224\u65b7'] = 0\n            \n# \u7b2c\u4e00\u5929\u8cb7\u5165\uff0c\u6700\u5f8c\u4e00\u5929\u8ce3\u51fa\ntarget_data.iloc&#91;0, 4] = -1\ntarget_data.iloc&#91;-1,5] = 1<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 5.\u00a0Processing Backtest<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>def run(self, specific=None):\n        # \u505a\u51fa\u4ea4\u6613\u7d00\u9304\u8868\n        trade_data = pd.DataFrame(index= self.data&#91;'\u5e74\u6708\u65e5'], columns=self.target_list).fillna(0.0)\n        \n        action_data = self.signal_data&#91;(self.signal_data&#91;'\u8cb7\u5165\u8a0a\u865f\u5224\u65b7'] != 0) | (self.signal_data&#91;'\u8ce3\u51fa\u8a0a\u865f\u5224\u65b7'] != 0)]<\/code><\/pre>\n\n\n\n<p>Calculate Profit<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u8a08\u7b97\u500b\u80a1\u7e3d\u7372\u5229\n        self.protfolio_profit.append(sum(action_data&#91;'\u4ea4\u6613'].tolist()))<\/code><\/pre>\n\n\n\n<p>Calculate Cost<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u8a08\u7b97\u500b\u80a1\u7e3d\u6210\u672c\n        self.protfolio_cost.append(-action_data&#91;action_data&#91;'\u8cb7\u5165\u8a0a\u865f\u5224\u65b7'] &lt; 0]&#91;'\u4ea4\u6613'].sum())<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 6.\u00a0Calculate Return on Investment<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>def ROI(self):\n       # \u5831\u916c\u7387\n       return_profit = sum(self.protfolio_profit) \/ \n       sum(self.protfolio_cost)\nreturn return_profit\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"78cf\"><span class=\"ez-toc-section\" id=\"Review_the_performance_of_the_market\"><\/span><strong>Review the performance of the market<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"3873\">We usually compare our strategy to market return. That is because we want beat the market to obtain excess profits.<\/p>\n\n\n\n<p id=\"8a70\"><strong>Step 1.\u00a0<\/strong>Creating\u00a0<code><em>class<\/em><\/code>\u00a0name\u00a0<code><em>market<\/em><\/code><em>, here we use \u2018Y9997\u2019 to represent market index.<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>market = backtest(&#91;'Y9997'])<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 2.\u00a0Chooses the strategy<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>market.buy_hold()<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/tejwin20260323.j.webweb.today\/wp-content\/uploads\/1_1uX3t7ovYvHiZNUgl2Ctldg.png\" alt=\"\"\/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"265e\"><strong>Step 2.\u00a0Backtesting<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>market.run()<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/tejwin20260323.j.webweb.today\/wp-content\/uploads\/1_158SDiOFlZMZnzZwWr2jVdg.png\" alt=\"\"\/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"ef87\"><strong>Step 3.\u00a0Review the performance<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>market.ROI()<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/tejwin20260323.j.webweb.today\/wp-content\/uploads\/1IuBS59LsjoQetMYkzzU85A.png\" alt=\"\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"159a\"><span class=\"ez-toc-section\" id=\"Custom_our_own_portfolio\"><\/span><strong>Custom our own portfolio<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"cbd6\">Now we want to use MSCI to be our portfolio. MSCI is an acronym for Morgan Stanley Capital International. It is an investment research firm that provides stock indexes, portfolio risk and performance analytics. Code of Database is\u00a0<code><a href=\"https:\/\/api.tej.com.tw\/databases.html\" target=\"_blank\" rel=\"noreferrer noopener\"><em>TWN\/AIDXS<\/em><\/a><\/code>\u00a0, and we can find MSCI portfolio in 2020.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>data_MSCI = tejapi.get('TWN\/AIDXS',\n                       coid = 'MSCI',\n                       mdate= '2019-12-31',\n                       opts={'columns':&#91;'coid','mdate','key3']},\n                       chinese_column_name=True)<\/code><\/pre>\n\n\n\n<p>Filter the data and save it to the\u00a0<code><em>list<\/em><\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>MSCI_list = data_MSCI&#91;'\u6210\u4efd\u80a1'].unique().tolist()<\/code><\/pre>\n\n\n\n<p>Delete the Chinese name.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>MSCI_list = &#91;coid&#91;:4] for coid in MSCI_list]<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/tejwin20260323.j.webweb.today\/wp-content\/uploads\/10t2pAMXYeTkbVoOhe5R75w.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"497d\">Due to the effect of module, we do not need to rewrite the process of backtest. Just need to change&nbsp;<code><em>target_list<\/em><\/code>&nbsp;to get the result we want.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"dbc9\"><strong>Step 1.\u00a0Creating\u00a0<code><em>class<\/em><\/code>\u00a0name\u00a0<code><em>msci<\/em><\/code><em>\u00a0,\u00a0<\/em><code><em>target_list<\/em><\/code><em>\u00a0is\u00a0<\/em><code><em>MSCI_list<\/em><\/code><em>.<\/em><\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>msci = backtest(MSCI_list)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 2.\u00a0Chooses the strategy<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>msci.price_volume()<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/tejwin20260323.j.webweb.today\/wp-content\/uploads\/1_1Yc-9PI-anlMK4OoRiRH19A.png\" alt=\"\"\/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"db8b\"><strong>Step 3.\u00a0Backtesting<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>msci.run()<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/tejwin20260323.j.webweb.today\/wp-content\/uploads\/1_1zTqZiqlP815PynGV9jgo8A.png\" alt=\"\"\/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"8765\"><strong>Step 4.\u00a0Review the performance<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>msci.ROI()<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/tejwin20260323.j.webweb.today\/wp-content\/uploads\/1yJq8XVRZ4A1qd_milckn-w.png\" alt=\"\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"e787\"><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=\"d84b\">There will be a strong bull market in Taiwan\u2019s stock market in 2020. It seems like buy and hold is better than price_volume strategy. But the period just only one year, we cannot jump to conclusion that which is better one. Today\u2019s article describes how to build a simple backtest system. Reader can try to adjust the structure. For example, turn rolling days&nbsp;<code><em>10<\/em><\/code><em>&nbsp;\u3001<\/em><code><em>20<\/em><\/code>&nbsp;into adjustable parameters, in order to compare the compact of date change on the rate of return.<\/p>\n\n\n\n<p id=\"5676\">Quantitative Analysis is the trend of investment. Worldwide famous hedge fund adopt this method to make trade strategy. If you have interest on this topic, you can go to our&nbsp;<a href=\"https:\/\/eshop.tej.com.tw\/E-Shop\/Edata_caseIntro\/2\" rel=\"noreferrer noopener\" target=\"_blank\">official website<\/a>. It provides various financial data to help you make better strategy.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"47b9\"><span class=\"ez-toc-section\" id=\"Related_Link\"><\/span><strong>Related Link<\/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>Use TEJ API to construct our own backtest system. Preface Trade Backtest is a more scientific approach to test strategy nowadays. Although the past cannot represent the future. The result of backtest can provide us some information about the strategy. However, if we have to run different strategy, we need to build a new system. [&hellip;]<\/p>\n","protected":false},"featured_media":16127,"template":"","tags":[2371,3005,2428],"insight-category":[690,50],"class_list":["post-16114","insight","type-insight","status-publish","has-post-thumbnail","hentry","tag-python","tag-tejapi-application","tag-2428","insight-category-data-analysis","insight-category-fintech"],"acf":[],"_links":{"self":[{"href":"https:\/\/tejwin20260323.j.webweb.today\/en\/wp-json\/wp\/v2\/insight\/16114","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tejwin20260323.j.webweb.today\/en\/wp-json\/wp\/v2\/insight"}],"about":[{"href":"https:\/\/tejwin20260323.j.webweb.today\/en\/wp-json\/wp\/v2\/types\/insight"}],"version-history":[{"count":0,"href":"https:\/\/tejwin20260323.j.webweb.today\/en\/wp-json\/wp\/v2\/insight\/16114\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tejwin20260323.j.webweb.today\/en\/wp-json\/wp\/v2\/media\/16127"}],"wp:attachment":[{"href":"https:\/\/tejwin20260323.j.webweb.today\/en\/wp-json\/wp\/v2\/media?parent=16114"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tejwin20260323.j.webweb.today\/en\/wp-json\/wp\/v2\/tags?post=16114"},{"taxonomy":"insight-category","embeddable":true,"href":"https:\/\/tejwin20260323.j.webweb.today\/en\/wp-json\/wp\/v2\/insight-category?post=16114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}