{"id":16070,"date":"2021-07-20T02:33:39","date_gmt":"2021-07-19T18:33:39","guid":{"rendered":"https:\/\/www.tejwin.com\/?post_type=insight&#038;p=16070"},"modified":"2026-03-03T13:25:24","modified_gmt":"2026-03-03T05:25:24","slug":"trading-strategy-by-keeping-an-eye-on-big-players","status":"publish","type":"insight","link":"https:\/\/www.tejwin.com\/en\/insight\/trading-strategy-by-keeping-an-eye-on-big-players\/","title":{"rendered":"Trading Strategy by Keeping an Eye on Big Players"},"content":{"rendered":"\n<p>Chip analysis and backtesting using TEJ API trial database<\/p>\n\n\n\n<figure class=\"wp-block-image caption-align-center\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1_0C8vR-4nCFOk1sBPH.jpg\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Photo Creds:&nbsp;<a href=\"https:\/\/unsplash.com\/photos\/DfjJMVhwH_8\" rel=\"noreferrer noopener\" target=\"_blank\">Usplash<\/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-69d90e7e59914\" 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-69d90e7e59914\"  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\/trading-strategy-by-keeping-an-eye-on-big-players\/#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:\/\/www.tejwin.com\/en\/insight\/trading-strategy-by-keeping-an-eye-on-big-players\/#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:\/\/www.tejwin.com\/en\/insight\/trading-strategy-by-keeping-an-eye-on-big-players\/#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:\/\/www.tejwin.com\/en\/insight\/trading-strategy-by-keeping-an-eye-on-big-players\/#Trial_Database_Used\" >Trial Database Used<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.tejwin.com\/en\/insight\/trading-strategy-by-keeping-an-eye-on-big-players\/#The_Application_of_Chip_Analysis_Indicators\" >The Application of Chip Analysis Indicators<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.tejwin.com\/en\/insight\/trading-strategy-by-keeping-an-eye-on-big-players\/#The_Design_and_Practice_of_Trading_Strategy\" >The Design and Practice of Trading Strategy<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.tejwin.com\/en\/insight\/trading-strategy-by-keeping-an-eye-on-big-players\/#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:\/\/www.tejwin.com\/en\/insight\/trading-strategy-by-keeping-an-eye-on-big-players\/#Related_Link\" >Related Link<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"851a\"><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=\"c23d\">Generally speaking, big players such as institutional investors, insiders or other investors holding large amounts of shares have information advantages over retail investors. Thus, they are more likely to select stocks with potential growth and distance themselves from risky stocks. To decrease this kind of information asymmetry, The Financial Supervisory Committee in Taiwan requires firms and securities firms to release their daily trading data, allowing investors to analyze the trend of stock price by observing big players\u2019 investment. And this is so-called chip analysis.<\/p>\n\n\n\n<p id=\"89d6\">Common indicators in chip analysis include\u00a0<strong>the net buy-and-sell of three primary institutional investors, the days of consecutive buying or selling, trading amounts by institutional investors, stock holding of directors and supervisors, the ratio of short sale and margin purchase<\/strong>. Since the TEJ API database contains a variety of chip indicators, there\u2019s no need to calculate ourselves and we can immediately carry out stock selection and backtesting based on it. This article follows the content of\u00a0<a href=\"https:\/\/medium.com\/tej-api-financial-data-anlaysis\/introduction-5-starting-using-tej-trial-database-223e0cb5c341\" class=\"ek-link\" target=\"_blank\" rel=\"noopener\">\u3010Introduction(5)\u3011Starting Using TEJ Trial Database<\/a>, so we will still obtain data from the<a href=\"https:\/\/api.tej.com.tw\/datatables.html?db=TRAIL&amp;t=%E8%A9%A6%E7%94%A8%E8%B3%87%E6%96%99%E5%BA%AB\" target=\"_blank\" rel=\"noreferrer noopener\">\u00a0trial database<\/a>\u00a0for backtesting. And for those who already applied for access to the trial database, it\u2019s recommended to do the backtesting yourself while reading this article !<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"6932\"><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=\"31d3\">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\nimport matplotlib.pyplot as plt\ntejapi.ApiConfig.api_key = \u201cYour Key\u201d\ntejapi.ApiConfig.ignoretz = True<\/code><\/pre>\n\n\n\n<p id=\"ce10\"><em>Note: Remember to replace \u2018Your Key\u2019 with the one you applied for. The last line indicates the time zone is not shown.<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"bed5\"><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>Understand the indicators of chip analysis<\/li>\n\n\n\n<li>Demonstrate the basic backtesting<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"a738\"><span class=\"ez-toc-section\" id=\"Trial_Database_Used\"><\/span><strong>Trial Database Used<\/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\/columns.html?idCode=TRAIL%2FTAPRCD\" rel=\"noreferrer noopener\" target=\"_blank\">Unadjusted daily stock price<\/a>: The code of this database is \u2018TRAIL\/TAPRCD\u2019, containing data such as stock price, trading volume, P\/E ratio and so on.<\/li>\n\n\n\n<li><a href=\"https:\/\/api.tej.com.tw\/columns.html?idCode=TRAIL%2FTATINST1\" rel=\"noreferrer noopener\" target=\"_blank\">Net buy-and-sell of three primary institutional investors<\/a>: The code of this database is \u2018TRAIL\/TATINST1\u2019, covering big players\u2019 investment, shareholding ratio, turnover rate and so on.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"36f0\"><span class=\"ez-toc-section\" id=\"The_Application_of_Chip_Analysis_Indicators\"><\/span><strong>The Application of Chip Analysis Indicators<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Shareholding ratio of institutional investors: The sum of shareholding ratio of foreign investors, securities investment trust companies and dealers. It can be used to&nbsp;<strong>observe the distribution of each stock.<\/strong><\/li>\n\n\n\n<li>Net Buy-and-sell of institutional investors: The sum of net buy-and-sell of foreign investors, securities investment trust companies and dealers. The positive figure indicates net buy, while negative one means net sell. It can be employed to&nbsp;<strong>trace the movement of big players.<\/strong><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"950f\"><span class=\"ez-toc-section\" id=\"The_Design_and_Practice_of_Trading_Strategy\"><\/span><strong>The Design and Practice of Trading Strategy<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p id=\"a452\">Buy signal: if the institutional investors are&nbsp;<strong>net-buying<\/strong>&nbsp;and&nbsp;<strong>their shareholding ratio is lower than the 5-day moving average,<\/strong>&nbsp;the stock price may begin to rise.<\/p>\n\n\n\n<p id=\"c6ea\">Sell signal: if the institutional investors are<strong>&nbsp;net-selling&nbsp;<\/strong>and<strong>&nbsp;their shareholding ratio exceeds the 5-day moving average,<\/strong>&nbsp;it may indicate the institutional investors may start to dump shares.<\/p>\n\n\n\n<p id=\"2ed3\">Assumption: When the buy signal pops up, we buy the stock at&nbsp;<strong>the next day\u2019s opening price<\/strong>&nbsp;since the net buy-and-sell information is disclosed&nbsp;<strong>after the market closes<\/strong>. During the holding period, we hold&nbsp;<strong>no more than one share<\/strong>&nbsp;and sell it when the sell signal is met. Transaction fee (0.1425%) and transfer tax on stock (0.3%) are considered.<\/p>\n<\/blockquote>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"e1cf\"><strong>Step 1.&nbsp;<\/strong>Obtain data after importing modules<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>evergreen_chip = tejapi.get('TRAIL\/TATINST1',\n                            coid = '2603',\n                            opts = {'columns':&#91;'coid','mdate',\n                                               'ttl_ex','fld024']},\n                            chinese_column_name = True)<\/code><\/pre>\n\n\n\n<p id=\"9f7f\">For starters, we obtain the chip data of Evergreen Marine Corporation (2603). The column codes we select are shareholding share and net buy-and-sell of institutional investors according to&nbsp;<a href=\"https:\/\/api.tej.com.tw\/columns.html?idCode=TRAIL%2FTATINST1\" rel=\"noreferrer noopener\" target=\"_blank\">column description<\/a>&nbsp;of the database.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/0HCE-HGmgcR4Ky2Wr.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>evergreen_price = tejapi.get('TRAIL\/TAPRCD',\n                            coid = '2603',\n                            opts = {'columns':&#91;'mdate','open_d']},\n                            chinese_column_name = True)<\/code><\/pre>\n\n\n\n<p>To calculate the return, we also need stock price data. Here we apply&nbsp;<code><em>shift(-1)<\/em><\/code><em>&nbsp;<\/em>to move the opening price up one row and form a new column called next day\u2019s opening price, in order to match our buying and selling price.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/08mxlbYH9cq_bfjAk.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>market = tejapi.get('TRAIL\/TAPRCD',\n                    coid = 'Y9997',\n                    opts = {'columns':&#91;'mdate', 'roi']},\n                    chinese_column_name = True)<\/code><\/pre>\n\n\n\n<p>We need the return of TAIEX Total Return Index as our benchmark to measure the performance of this strategy. To not confuse with strategy return, we use&nbsp;<code><em>rename()<\/em><\/code>&nbsp;to change the name of the return column into market return.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>market = market.rename(columns = {'\u5831\u916c\u7387\uff05':'\u5e02\u5834\u5831\u916c\u7387%'})<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/04rpxAkHwKlSprWNP.png\" alt=\"\"\/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"1c2d\"><strong>Step 2.&nbsp;<\/strong>Merge the data<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>evergreen = evergreen_chip.merge(evergreen_price, on = '\u5e74\u6708\u65e5')\nevergreen = evergreen.merge(market, on = '\u5e74\u6708\u65e5')<\/code><\/pre>\n\n\n\n<p id=\"5fd8\">In this step, we combine chip, stock price and market return data into one table with the usage of&nbsp;<code><em>merge()<\/em><\/code>&nbsp;and&nbsp;<code><em>on<\/em><\/code>&nbsp;based on their mutual column.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/0SSStm6IMuNyjyUL0.png\" alt=\"\"\/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"18a2\"><strong>Step 3.&nbsp;<\/strong>Create signal identification column<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>evergreen&#91;'\u5408\u8a08\u8cb7\u8ce3\u8d85'] = np.where(evergreen&#91;'\u5408\u8a08\u8cb7\u8ce3\u8d85(\u5343\u80a1)'] &gt;= 0, 1, 0)<\/code><\/pre>\n\n\n\n<p>Here we take advantage of&nbsp;<code><em>np.where()<\/em><\/code><em>&nbsp;<\/em>to create a new column and fill it with1 if the institutional investors are<strong>&nbsp;net-buying<\/strong>, and 0, otherwise.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>evergreen&#91;'\u6301\u80a1\u7387_5\u65e5MA'] = evergreen&#91;'\u5408\u8a08\u6301\u80a1\u7387%'].rolling(5).mean()\nevergreen&#91;'\u5408\u8a08\u6301\u80a1\u8b8a\u5316'] = np.where(evergreen&#91;'\u5408\u8a08\u6301\u80a1\u7387%'] - evergreen&#91;'\u6301\u80a1\u7387_5\u65e5MA'] &gt; 0, 1, 0)<\/code><\/pre>\n\n\n\n<p>To evaluate the change of shareholding ratio, we use the difference between shareholding ratio and its 5-day moving average to decide whether its value is relatively high or low.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>evergreen = evergreen.dropna().reset_index(drop=True)<\/code><\/pre>\n\n\n\n<p id=\"1dd2\">Finally, we utilize&nbsp;<code><em>dropna()<\/em><\/code>&nbsp;to remove NaN values and&nbsp;<code><em>reset_index(drop=True)<\/em><\/code>&nbsp;to reset the index and prevent the original index from forming a new column.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/0Pnqm0wKkZ0EDrH0n.png\" alt=\"\"\/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"87e8\"><strong>Step 4.&nbsp;<\/strong>Add signal column<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Buy<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>evergreen&#91;'\u8a0a\u865f'] = np.where((evergreen&#91;'\u5408\u8a08\u8cb7\u8ce3\u8d85'] == 1)&amp;(evergreen&#91;'\u5408\u8a08\u6301\u80a1\u8b8a\u5316'] == 0), 'Buy', '')<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Sell<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>evergreen&#91;'\u8a0a\u865f'] = np.where((evergreen&#91;'\u5408\u8a08\u8cb7\u8ce3\u8d85'] == 0)&amp;(evergreen&#91;'\u5408\u8a08\u6301\u80a1\u8b8a\u5316'] == 1), 'Sell', evergreen&#91;'\u8a0a\u865f'])<\/code><\/pre>\n\n\n\n<p>Here we newly add the signal column and fill it with \u2018Buy\u2019, \u2018Sell\u2019 or empty string according to the value of the signal identification column. It\u2019s worth noting that when deciding whether or not to assign a \u2018Sell\u2019 value, we have to put the signal column in the third parameter&nbsp;<strong>to not substitute the outcome of the previous assignment of \u2018Buy\u2019 signal<\/strong>. In other words, we keep the original judgement if the selling condition is not satisfied.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>evergreen&#91;'\u8a0a\u865f']&#91;len(evergreen)-1] = 'Sell'<\/code><\/pre>\n\n\n\n<p id=\"2990\"><code><em>len(evergreen)<\/em><\/code>&nbsp;represents the number of data and the index of the last data (12\/30) equals that number minus one. What we are doing is to manually assign a \u2018Sell\u2019 signal to the last day, indicating we\u2019ll close position at the end of 2020.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/0UWYUDqaxT_iwqthR.png\" alt=\"\"\/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"c55a\"><strong>Step 5.&nbsp;<\/strong>Calculate strategy return<\/h4>\n\n\n\n<p id=\"d6f7\">First of all, we create the variable&nbsp;<code><em>hold<\/em><\/code><em>&nbsp;<\/em>and assign 0 by default, meaning we have no stock on hand. Once&nbsp;<strong>there\u2019s a buy signal and we don\u2019t have a stock<\/strong>, we assign 1 to the variable. However, if&nbsp;<strong>we have a stock and meet a sell signal<\/strong>, we reset the value of the variable back to 0.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>hold = 0<\/code><\/pre>\n\n\n\n<p>The value of the variable&nbsp;<code><em>cost<\/em><\/code>&nbsp;is also set 0 by default. If&nbsp;<strong>there\u2019s a buy signal when we hold no stock<\/strong>, we will assign the next day\u2019s opening price as the cost of buying the stock.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cost = 0<\/code><\/pre>\n\n\n\n<p>Then we create the empty list&nbsp;<code><em>Return<\/em><\/code>. When we&nbsp;<strong>meet a sell signal while holding a stock<\/strong>, we use&nbsp;<code><em>np.log()<\/em><\/code>&nbsp;to calculate the holding period return. After taking friction cost into consideration, we adopt&nbsp;<code><em>append()<\/em><\/code>&nbsp;to add the continuous return into the list and add 0&nbsp;<strong>under other circumstances&nbsp;<\/strong>to make sure the length of the list matches the number of the data.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Return = &#91;]<\/code><\/pre>\n\n\n\n<p>Now we use&nbsp;<code><em>for&nbsp;<\/em><\/code>to loop as many times as the number of the data. Among each loop, we apply&nbsp;<code><em>if&nbsp;<\/em><\/code>to examine whether buy or sell signal occurs at the&nbsp;<code><em>i<\/em><\/code>&nbsp;day. If certain conditions are met, we calculate return and alter the value of some variables.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(len(evergreen)):\n    if evergreen&#91;'\u8a0a\u865f']&#91;i] == '':\n        Return.append(0)\n    elif evergreen&#91;'\u8a0a\u865f']&#91;i] == 'Buy':\n        if hold == 0:\n            cost = evergreen&#91;'\u6b21\u65e5\u958b\u76e4\u50f9']&#91;i]\n            hold = 1\n            Return.append(0)\n        else:\n            Return.append(0)\n    elif evergreen&#91;'\u8a0a\u865f']&#91;i] == 'Sell':\n        if hold == 1:\n            Return.append(100*(np.log(evergreen&#91;'\u6b21\u65e5\u958b\u76e4\u50f9']&#91;i]\/cost)- 0.001425*2 - 0.003))\n            hold = 0\n        else:\n            Return.append(0)<\/code><\/pre>\n\n\n\n<p>Finally we take the list&nbsp;<code><em>Return&nbsp;<\/em><\/code>as our new column called chip analysis return.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>evergreen&#91;'\u7c4c\u78bc\u9762\u5831\u916c\u7387(%)'] = Return<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/0sQAqG2lapljNhiKx.png\" alt=\"\"\/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"9e33\"><strong>Step 6.&nbsp;<\/strong>Calculate the cumulative return and visualize the performance<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>evergreen&#91;'\u7c4c\u78bc\u7d2f\u7a4d\u5831\u916c\u7387'] = evergreen&#91;'\u7c4c\u78bc\u9762\u5831\u916c\u7387(%)'].apply(lambda x: 0.01*x+1).cumprod()\nevergreen&#91;'\u5e02\u5834\u7d2f\u7a4d\u5831\u916c\u7387'] = evergreen&#91;'\u5e02\u5834\u5831\u916c\u7387%'].apply(lambda x: 0.01*x+1).cumprod()<\/code><\/pre>\n\n\n\n<p id=\"9dd5\">We firstly use&nbsp;<code><em>apply()<\/em><\/code>&nbsp;to transform the form of return into<strong>&nbsp;the decimal form with principal<\/strong>, then utilize&nbsp;<code><em>cumprod()<\/em><\/code>&nbsp;to calculate the cumulative return.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/0ihSJt-uOAHUDNok5.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>plt.plot(evergreen&#91;'\u5e74\u6708\u65e5'], evergreen&#91;'\u7c4c\u78bc\u7d2f\u7a4d\u5831\u916c\u7387'], label = 'strategy')\nplt.plot(evergreen&#91;'\u5e74\u6708\u65e5'], evergreen&#91;'\u5e02\u5834\u7d2f\u7a4d\u5831\u916c\u7387'], label = 'market')\nplt.legend()\nplt.show()<\/code><\/pre>\n\n\n\n<p id=\"6632\">Lastly, we present the plot with&nbsp;<code><em>plt.plot()<\/em><\/code>&nbsp;and<code><em>&nbsp;plt.show()<\/em><\/code>&nbsp;and show the label by using&nbsp;<code><em>label&nbsp;<\/em><\/code>and&nbsp;<code><em>plt.legend()<\/em><\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/0RIwE7ZYGqozFFV3e.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"a5d3\">Obviously the cumulative return of this strategy is&nbsp;<strong>much better than that of the market<\/strong>&nbsp;during 2020.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"abf8\"><strong>Step 7.&nbsp;<\/strong>Performance table<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>cagr = &#91;100*(evergreen&#91;'\u7c4c\u78bc\u7d2f\u7a4d\u5831\u916c\u7387'].values&#91;-1]**(252\/len(evergreen)) - 1), 100*(evergreen&#91;'\u5e02\u5834\u7d2f\u7a4d\u5831\u916c\u7387'].values&#91;-1]**(252\/len(evergreen)) - 1)]<\/code><\/pre>\n\n\n\n<p id=\"cb5f\">We create the list&nbsp;<code><em>cagr&nbsp;<\/em><\/code>to store the compound annual growth rate of the strategy and market.&nbsp;<code><em>values[-1]<\/em><\/code>&nbsp;means we select the latest cumulative return and annualize it based on 252 trading days a year. Finally we subtract the principal from the annualized return.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>std = &#91;evergreen&#91;'\u7c4c\u78bc\u9762\u5831\u916c\u7387(%)'].std()*(252**0.5),evergreen&#91;'\u5e02\u5834\u5831\u916c\u7387%'].std()*(252**0.5)]<\/code><\/pre>\n\n\n\n<p>The list&nbsp;<code><em>std&nbsp;<\/em><\/code>is used to store the annualized standard deviation, which is the result of daily standard deviation adjusted by trading days a year.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sharpe_ratio = &#91;(cagr&#91;0] - 1)\/std&#91;0],(cagr&#91;1] - 1)\/std&#91;1]]<\/code><\/pre>\n\n\n\n<p>Then we create the list&nbsp;<code><em>sharpe_ratio<\/em><\/code>&nbsp;to store chip analysis strategy and market\u2019s sharpe ratio. Here we assume the&nbsp;<strong>riskless interest rate is 1%<\/strong>. Finally we use&nbsp;<code><em>pd.DataFrame()<\/em><\/code>&nbsp;to form a table<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>result = pd.DataFrame(&#91;cagr,std,sharpe_ratio], columns = &#91;'\u7c4c\u78bc\u9762','\u5e02\u5834'], index = &#91;'\u5e74\u5316\u5831\u916c(%)','\u5e74\u5316\u6a19\u6e96\u5dee(%)','\u590f\u666e\u6bd4\u7387'])<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/0X0cXcLqQyXK0aTQU.png\" alt=\"\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"d0b0\"><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=\"9e4d\">You must have grasped the overall idea of chip analysis indicators and backtesting ! However,&nbsp;<strong>there\u2019s no general strategy that guarantees profit all the time, so you must take this backtesting outcome seriously<\/strong>. If you want to have a longer backtesting period or more indicators, we highly recommend you to consider our&nbsp;<a href=\"https:\/\/eshop.tej.com.tw\/E-Shop\/Edata_caseIntro\/3\" target=\"_blank\" rel=\"noreferrer noopener\">master plan<\/a>&nbsp;to optimize your own strategy !<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"f226\"><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>Chip analysis and backtesting using TEJ API trial database Preface Generally speaking, big players such as institutional investors, insiders or other investors holding large amounts of shares have information advantages over retail investors. Thus, they are more likely to select stocks with potential growth and distance themselves from risky stocks. To decrease this kind of [&hellip;]<\/p>\n","protected":false},"featured_media":6792,"template":"","tags":[3183,3176,3583,3008],"insight-category":[690,50,3509],"class_list":["post-16070","insight","type-insight","status-publish","has-post-thumbnail","hentry","tag-chip-analysis-2","tag-python-2","tag-quantitative-finance","tag-tejapi-quant","insight-category-data-analysis","insight-category-fintech","insight-category-fintech-en"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/16070","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":1,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/16070\/revisions"}],"predecessor-version":[{"id":24859,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/16070\/revisions\/24859"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media\/6792"}],"wp:attachment":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media?parent=16070"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/tags?post=16070"},{"taxonomy":"insight-category","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight-category?post=16070"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}