{"id":16635,"date":"2021-12-21T02:31:31","date_gmt":"2021-12-20T18:31:31","guid":{"rendered":"https:\/\/www.tejwin.com\/?post_type=insight&#038;p=16635"},"modified":"2026-03-02T10:23:45","modified_gmt":"2026-03-02T02:23:45","slug":"rsi-indicator","status":"publish","type":"insight","link":"https:\/\/www.tejwin.com\/en\/insight\/rsi-indicator\/","title":{"rendered":"RSI Indicator"},"content":{"rendered":"\n<p id=\"96c4\">Use common technical indicators to backtesting<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter is-resized caption-align-center\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1AM_rMAQYxF6FhsCMfNwR2A.png\" alt=\"\" style=\"width:832px;height:622px\"\/><figcaption class=\"wp-element-caption\">photo by&nbsp;<a href=\"https:\/\/unsplash.com\/@chrisliverani\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"ek-link\">Chris Liverani<\/a>&nbsp;on&nbsp;<a href=\"https:\/\/unsplash.com\/\" target=\"_blank\" rel=\"noreferrer 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-69f60cc137d21\" 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-69f60cc137d21\"  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\/rsi-indicator\/#Highlights\" >Highlights<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.tejwin.com\/en\/insight\/rsi-indicator\/#Preface\" >Preface<\/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\/rsi-indicator\/#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-4\" href=\"https:\/\/www.tejwin.com\/en\/insight\/rsi-indicator\/#Database_Used\" >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\/rsi-indicator\/#Data_Processing\" >Data Processing<\/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\/rsi-indicator\/#Calculate_the_return\" >Calculate the return<\/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\/rsi-indicator\/#Performance_comparison\" >Performance comparison<\/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\/rsi-indicator\/#Conclusion\" >Conclusion<\/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\/rsi-indicator\/#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-10\" href=\"https:\/\/www.tejwin.com\/en\/insight\/rsi-indicator\/#Extended_Reading\" >Extended Reading<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.tejwin.com\/en\/insight\/rsi-indicator\/#Related_Link\" >Related Link<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"b061\"><span class=\"ez-toc-section\" id=\"Highlights\"><\/span><strong>Highlights<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Difficulty\uff1a\u2605\u2605\u2606\u2606\u2606<\/li>\n\n\n\n<li>Backtesting by RSI indicator and relative performance of this strategy<\/li>\n\n\n\n<li>Advice: This article adopts TEJ database to calculate RSI indicators, create signals and form the return. Before reading this,&nbsp;<a href=\"https:\/\/medium.com\/tej-api-financial-data-anlaysis\/quant-2-technical-analysis-2958435a8922\" class=\"ek-link\" target=\"_blank\" rel=\"noopener\">\u3010Quant(2)\u3011Technical Analysis<\/a>&nbsp;is recommended to explore to better understand the basic procedure of backtesting.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"8e25\"><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=\"2742\">Relative strength index (RSI) is the momentum technical indicator. It is usually used as an oscillator interval to evaluate overbought or oversold condition by measuring recent trend of price movements. Following is the way to calculate this indicator:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Based on daily spread of price, separately calculate average gain or loss in past N days. (Note: Convert the loss to positive value.)<\/li>\n\n\n\n<li>Calculate Relative Strength(RS): Average Gain in past N days \/ Average Loss in past N days.(Note: In the calculation of average gain, loss is counted as 0, vice versa.)<\/li>\n\n\n\n<li>Calculate Relative Strength Index: RS \/ (1 + RS) \u00d7 100<\/li>\n<\/ol>\n\n\n\n<p id=\"1423\">Criterion of RSI:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>RSI &lt; 30 is oversold, RSI &gt; 70 is overbought. (Note: Another conservative interval pair is (20, 80))<\/li>\n\n\n\n<li>RSI Divergence: Trend of RSI is opposite to trend of price, which implies market would become reversal.<\/li>\n\n\n\n<li>Golden Cross: Short-term RSI exceeds long-term RSI upwards, which implies the long position.<\/li>\n\n\n\n<li>Death Cross: Short-term RSI exceeds long-term RSI downwards, which implies the short position.<\/li>\n<\/ol>\n\n\n\n<p id=\"2a75\">RSI\u2019s Deactivation: Gain and loss is separately calculated in RSI. Therefore, if short-term trend of price is almost gain, the reference data of loss would be too few to be considered, which further causes that RSI loses the reference value, vice versa. This situation is specifically frequent during the high or low-level area. We would try to synthesize RSI interval, golden cross and death cross to lower the impact of deactivation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"6ef3\"><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=\"447b\">MacOS and Jupyter Notebook<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#Basic function<br>import pandas as pd<br>import numpy as np<br>import copy#Visualization<br>import plotly.graph_objects as go<br>import plotly.express as px#TEJ<br>import tejapi<br>tejapi.ApiConfig.api_key = 'Your Key'<br>tejapi.ApiConfig.ignoretz = True<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"7835\"><span class=\"ez-toc-section\" id=\"Database_Used\"><\/span><strong>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\/columndoc.html?subId=42\" rel=\"noreferrer noopener\" target=\"_blank\">Unadjusted daily stock price<\/a>: the code of this database is \u2018TWN\/APRCD\u2019, covering listed firms\u2019 stock prices<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"53cc\"><span class=\"ez-toc-section\" id=\"Data_Processing\"><\/span><strong>Data Processing<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"85eb\"><strong>Step 1. Obtain the stock price<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">stock_data = tejapi.get('TWN\/APRCD',<br>           coid= '2324', # Compal Electronics, Inc.<br>           mdate={'gte': '2020-01-01', 'lte':'2021-12-14'},<br>           opts={'columns': ['coid', 'mdate', 'open_d', 'close_d']},<br>           chinese_column_name=True,<br>           paginate=True)<\/pre>\n\n\n\n<p id=\"fb88\">We choose Compal Electronics (2324) as the target. Time interval is ranged from 2020 to today (2021\u201312\u201314). With above circumstances, we would know the fluctuation of stock price of largest notebook OEM under the pandemic situation. On top of that, determine the buy and sell signal with RSI.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1gBsdAqNt50C7fX9ki5aexA.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"1dd6\"><strong>Step 2. Calculate 14-day RSI &amp; 7-day RSI<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Calculate daily spread and categorize gain &amp; loss<br>stock_data['\u9694\u65e5\u5dee\u50f9'] = stock_data['\u6536\u76e4\u50f9(\u5143)'].diff()<br>stock_data['\u4e0a\u6f32'] = stock_data['\u9694\u65e5\u5dee\u50f9'].clip(lower = 0)<br>stock_data['\u4e0b\u8dcc'] = (-1) * stock_data['\u9694\u65e5\u5dee\u50f9'].clip( upper = 0)# 14-day RSI<br>stock_data['14\u65e5\u4e0a\u6f32\u5747\u503c'] = stock_data['\u4e0a\u6f32'].ewm(com = 14, adjust = False).mean()<br>stock_data['14\u65e5\u4e0b\u8dcc\u5747\u503c'] = stock_data['\u4e0b\u8dcc'].ewm(com = 14, adjust = False).mean()<br>stock_data['14\u65e5\u76f8\u5c0d\u5f37\u5f31\u503c'] = stock_data['14\u65e5\u4e0a\u6f32\u5747\u503c'] \/ stock_data['14\u65e5\u4e0b\u8dcc\u5747\u503c']<br>stock_data['14\u65e5\u76f8\u5c0d\u5f37\u5f31\u6307\u6a19'] = stock_data['14\u65e5\u76f8\u5c0d\u5f37\u5f31\u503c'].apply(lambda rs : rs\/(1+rs)*100)# 7-day RSI<br>stock_data['7\u65e5\u4e0a\u6f32\u5747\u503c'] = stock_data['\u4e0a\u6f32'].ewm(com = 7, adjust = False).mean()<br>stock_data['7\u65e5\u4e0b\u8dcc\u5747\u503c'] = stock_data['\u4e0b\u8dcc'].ewm(com = 7, adjust = False).mean()<br>stock_data['7\u65e5\u76f8\u5c0d\u5f37\u5f31\u503c'] = stock_data['7\u65e5\u4e0a\u6f32\u5747\u503c'] \/ stock_data['7\u65e5\u4e0b\u8dcc\u5747\u503c']<br>stock_data['7\u65e5\u76f8\u5c0d\u5f37\u5f31\u6307\u6a19'] = stock_data['7\u65e5\u76f8\u5c0d\u5f37\u5f31\u503c'].apply(lambda rs : rs\/(1+rs)*100)<\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1IHCkYSh846Ul5UDPFJTA0Q.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"55df\"><strong>Step 3. RSI Trending Chart &amp; Interval (30,70)(See the details in source code)<\/strong><\/p>\n\n\n\n<p id=\"503c\">Comparing RSI trending and overbought, oversold interval, we find the Golden Cross and Death Cross. What we should notice is that deactivation remains serious at the high-level area. However, we should sell at the first death cross, since it is impossible to predict market accurately.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/169-z6bWDL97mr8Eyelch3g.gif\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"16cb\"><strong>Step 4. Find Buy and Sell Signal &amp; Visualize Trading Action(See the details in source code)<\/strong><\/p>\n\n\n\n<p id=\"88c2\">Over-sold Condition:&nbsp;<strong>RSI &lt; 30<\/strong>. To boot, consider that 7-day RSI exceeds 14-day RSI upwards so as to lower impact of low-level deactivation.<\/p>\n\n\n\n<p id=\"d0ec\">Over-bought Condition:&nbsp;<strong>RSI &gt; 70<\/strong>. To boot, consider that 7-day RSI exceeds 14-day RSI downwards so as to lower impact of high-level deactivation.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">signal = []trade = 0for i in range(len(stock_data)):<br>    if stock_data.loc[i, '14\u65e5\u76f8\u5c0d\u5f37\u5f31\u6307\u6a19']  &lt;= 30 and stock_data.loc[i-1, '14\u65e5\u76f8\u5c0d\u5f37\u5f31\u6307\u6a19'] &gt; stock_data.loc[i-1, '7\u65e5\u76f8\u5c0d\u5f37\u5f31\u6307\u6a19'] and stock_data.loc[i, '14\u65e5\u76f8\u5c0d\u5f37\u5f31\u6307\u6a19'] &lt;= stock_data.loc[i, '7\u65e5\u76f8\u5c0d\u5f37\u5f31\u6307\u6a19'] and trade == 0:        signal.append(1000)<br>        trade = trade + 1    elif stock_data.loc[i, '14\u65e5\u76f8\u5c0d\u5f37\u5f31\u6307\u6a19'] &gt;= 70 and stock_data.loc[i-1, '14\u65e5\u76f8\u5c0d\u5f37\u5f31\u6307\u6a19'] &lt; stock_data.loc[i-1, '7\u65e5\u76f8\u5c0d\u5f37\u5f31\u6307\u6a19'] and stock_data.loc[i, '14\u65e5\u76f8\u5c0d\u5f37\u5f31\u6307\u6a19'] &gt;= stock_data.loc[i, '7\u65e5\u76f8\u5c0d\u5f37\u5f31\u6307\u6a19'] and trade == 1:        signal.append(-1000)<br>        trade = trade - 1    else:<br>        signal.append(0)stock_data['\u8cb7\u8ce3\u80a1\u6578'] = signal<\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1af3zUJHXxn4lyCuaf0FnKg.png\" alt=\"\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"ae43\"><span class=\"ez-toc-section\" id=\"Calculate_the_return\"><\/span><strong>Calculate the return<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"907b\">In\u00a0<a href=\"https:\/\/medium.com\/tej-api-financial-data-anlaysis\/quant-8-backtesting-by-macd-indicator-f0dc6ceecef2\" target=\"_blank\" aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"ek-link\">\u3010Quant(8)\u3011Backtesting by MACD Indicator<\/a>, we have discussed the details in the calculation of frictions cost and the method to calculate return with initial principal. In this article, we use function to achieve all of it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def target_return(data, principal):<br>    <br>    #\u8a08\u7b97\u6210\u672c<br>    data['\u624b\u7e8c\u8cbb'] = data['\u958b\u76e4\u50f9(\u5143)']* abs(data['\u8cb7\u8ce3\u80a1\u6578'])*0.001425<br>    data['\u624b\u7e8c\u8cbb'] = np.where((data['\u624b\u7e8c\u8cbb']&gt;0)&amp;(data['\u624b\u7e8c\u8cbb'] &lt;20), 20, data['\u624b\u7e8c\u8cbb'])<br>    data['\u8b49\u4ea4\u7a05'] = np.where(data['\u8cb7\u8ce3\u80a1\u6578']&lt;0, data['\u958b\u76e4\u50f9(\u5143)']* abs(data['\u8cb7\u8ce3\u80a1\u6578'])*0.003, 0)<br>    data['\u6469\u64e6\u6210\u672c'] = (data['\u624b\u7e8c\u8cbb'] + data['\u8b49\u4ea4\u7a05']).apply(np.floor)<br>    <br>    #\u8a08\u7b97\u8cc7\u7522\u50f9\u503c<br>    data['\u80a1\u7968\u50f9\u503c'] = data['\u8cb7\u8ce3\u80a1\u6578'].cumsum() * data['\u6536\u76e4\u50f9(\u5143)']<br>    data['\u73fe\u91d1\u50f9\u503c'] = principal - data['\u6469\u64e6\u6210\u672c'] + (data['\u958b\u76e4\u50f9(\u5143)']* -data['\u8cb7\u8ce3\u80a1\u6578']).cumsum() <br>    data['\u8cc7\u7522\u50f9\u503c'] = data['\u80a1\u7968\u50f9\u503c'] + data['\u73fe\u91d1\u50f9\u503c']<br>    <br>    #\u8a08\u7b97\u5831\u916c\u7387<br>    data['\u7576\u65e5\u50f9\u503c\u8b8a\u52d5(%)'] = (data['\u8cc7\u7522\u50f9\u503c']\/data['\u8cc7\u7522\u50f9\u503c'].shift(1) - 1)*100<br>    data['\u7d2f\u8a08\u5831\u916c(%)'] = (data['\u8cc7\u7522\u50f9\u503c']\/principal - 1)*100<br>    <br>    return data<\/pre>\n\n\n\n<p id=\"e497\">We only consider the trading of common stock. There is no margin buy or margin sell situation, so we do not have to calculate premium. With the data containing trading shares and initial principal, we can quickly get the transaction costs and return. In this article, we set&nbsp;<strong>20,000<\/strong>&nbsp;as our principal<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">RSI_return = target_return(data = stock_data, principal = 20000)<\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1BT9ZLGwZi0XoHc6v-YyUDw.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"5065\">Besides, to demonstrate whether RSI strategy performs relatively well, we add buy-and-hold strategy and market performance as our benchmarks.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The return of buy-and-hold<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">bh_data = copy.deepcopy(stock_data)<br>bh_data['\u8cb7\u8ce3\u80a1\u6578'] = 0<br>bh_data.loc[0, '\u8cb7\u8ce3\u80a1\u6578'] = 1000<br>bh_data.loc[len(bh_data)-1, '\u8cb7\u8ce3\u80a1\u6578'] = -1000# Drop the columns useless for buy-and-hold calculation.<br>bh_data = bh_data.drop(['\u9694\u65e5\u5dee\u50f9', '\u4e0a\u6f32', '\u4e0b\u8dcc', '14\u65e5\u4e0a\u6f32\u5747\u503c', '14\u65e5\u4e0b\u8dcc\u5747\u503c', '14\u65e5\u76f8\u5c0d\u5f37\u5f31\u503c','14\u65e5\u76f8\u5c0d\u5f37\u5f31\u6307\u6a19','7\u65e5\u4e0a\u6f32\u5747\u503c', '7\u65e5\u4e0b\u8dcc\u5747\u503c', '7\u65e5\u76f8\u5c0d\u5f37\u5f31\u503c', '7\u65e5\u76f8\u5c0d\u5f37\u5f31\u6307\u6a19'], axis = 1)bh_return = target_return(data=bh_data, principal = 20000)<\/pre>\n\n\n\n<p id=\"8ee9\">Here we also use&nbsp;<code>deepcopy()<\/code>&nbsp;to get stock price, and choose to buy at the beginning and sell at the end during the same period with initial principal of 20000 as well.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1Fyxqv_kjDmSz_EMtZA9DGw.png\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Market Performance<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">market = tejapi.get('TWN\/APRCD',<br>                   coid = 'Y9997',<br>                   mdate = {'gte':'2020-01-01', 'lte':'2021-12-14'},<br>                   opts = {'columns':['coid','mdate',   'close_d','roi']},<br>                   chinese_column_name = True)market['\u7d2f\u8a08\u5831\u916c(%)'] = (market['\u5831\u916c\u7387\uff05'].apply(lambda x:0.01*x +1).cumprod() - 1)*100<\/pre>\n\n\n\n<p id=\"687a\">Obtain return of market return index (Y9997) to stands for market performance and 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\/15mUi2DPoxh6aVPs70ikyvA.png\" alt=\"\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"b95a\"><span class=\"ez-toc-section\" id=\"Performance_comparison\"><\/span><strong>Performance comparison<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"e7d3\"><strong>Step 1. Comparison of cumulative return (See the details in source code)<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1FKuBPOQBAMZopD_U8E7WhA.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"e743\">With the chart of cumulative return, We tell that RSI strategy made us long after the pandemic. As for the short position, RSI made us sell right at the point that electronic-related stocks were shorted, which was caused by the rise of Commodity\u2019s price and market, therefore, focused on traditional stocks. Besides, fortunately, we avoid the impact of abruptly Covid\u2019s intensification in Taiwan. However, due to the standard of signal trigger is strict, we cannot accurately handle every fluctuation period, like the drop during Aug 2020.<\/p>\n\n\n\n<p id=\"a998\"><strong>Step 2. Performance table (See the details in source code)<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1V_ZNHNfHgyCCHJAr8IRJpQ.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"69d9\">Above chart shows that RSI strategy outperforms buy-and-hold on both return and volatility, which means that RSI helps us find adequate long and short point and improve the efficiency of investment. As for the comparison with market, return of RSI does not outperform market return significantly, but the volatility of RSI is relatively stable. Resulting from avoiding pandemic crash in 2020 and local crash in Taiwan in 2021, sharpe ratio of RSI is better than market performance.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5bed\"><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=\"d85d\">With the above content, we believe readers understand the influence of deactivation, especially during high or low-level area. Hence, we add another condition, cross of long-term and short-term RSI, making RSI strategy more steady. Of course, on the other hand, this sacrifices some trading opportunities. The balance between risk and trading opportunities is what investors should evaluate and strike. After all, the application of technical indicator varies from person to person. As a result, if readers are interested in diverse trading backtesting, welcome to purchase the plan offered in&nbsp;<a href=\"https:\/\/eshop.tej.com.tw\/E-Shop\/Edata_intro\" rel=\"noreferrer noopener\" target=\"_blank\">TEJ E-Shop<\/a>. Construct trading strategies fitting you with high quality database.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3ef3\"><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<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/gist.github.com\/tej87681088\/2eaf3710dfdd7f7fbf1089e1d89dbf40#file-tejapi_medium-13-ipynb\" class=\"ek-link\" target=\"_blank\" rel=\"noopener\">Click here to go Github<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"8aed\"><span class=\"ez-toc-section\" id=\"Extended_Reading\"><\/span><strong>Extended Reading<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.tejwin.com\/en\/insight\/modular-backtesting-system\/\" target=\"_blank\" aria-label=\"Modular Backtesting System (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"ek-link\">Modular Backtesting System<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.tejwin.com\/en\/insight\/technical-analysis\/\" class=\"ek-link\">Technical Analysis<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"c325\"><span class=\"ez-toc-section\" id=\"Related_Link\"><\/span>Related Link<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 common technical indicators to backtesting Highlights Preface Relative strength index (RSI) is the momentum technical indicator. It is usually used as an oscillator interval to evaluate overbought or oversold condition by measuring recent trend of price movements. Following is the way to calculate this indicator: Criterion of RSI: RSI\u2019s Deactivation: Gain and loss is [&hellip;]<\/p>\n","protected":false},"featured_media":16637,"template":"","tags":[3063,3582,3008],"insight-category":[690,50],"class_list":["post-16635","insight","type-insight","status-publish","has-post-thumbnail","hentry","tag-backtesting-2","tag-indicator","tag-tejapi-quant","insight-category-data-analysis","insight-category-fintech"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/16635","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":3,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/16635\/revisions"}],"predecessor-version":[{"id":24872,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/16635\/revisions\/24872"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media\/16637"}],"wp:attachment":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media?parent=16635"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/tags?post=16635"},{"taxonomy":"insight-category","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight-category?post=16635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}