{"id":15447,"date":"2023-01-09T09:30:00","date_gmt":"2023-01-09T01:30:00","guid":{"rendered":"https:\/\/www.tejwin.com\/?post_type=insight&#038;p=15447"},"modified":"2026-02-25T11:25:10","modified_gmt":"2026-02-25T03:25:10","slug":"seeking-alpha","status":"publish","type":"insight","link":"https:\/\/tejwin20260323.j.webweb.today\/en\/insight\/seeking-alpha\/","title":{"rendered":"Seeking Alpha"},"content":{"rendered":"\n<p id=\"f426\">The alpha obtained from the Fama&amp;French three-factor model is used to construct a long-short strategy and backtest the performance against the market return.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/tejwin20260323.j.webweb.today\/wp-content\/uploads\/1jH_NLrLmHYxxoYjvnpN7IQ.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Photo by\u00a0<a href=\"https:\/\/unsplash.com\/@ishant_mishra54?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\" target=\"_blank\" rel=\"noreferrer noopener\">Ishant Mishra<\/a>\u00a0on\u00a0<a href=\"https:\/\/unsplash.com\/s\/photos\/stock?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\" 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-6a10d690f2715\" 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-6a10d690f2715\"  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\/seeking-alpha\/#Highlights%EF%BC%9A\" >Highlights\uff1a<\/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\/seeking-alpha\/#Advice%EF%BC%9A\" >Advice\uff1a<\/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\/seeking-alpha\/#Preface%EF%BC%9A\" >Preface\uff1a<\/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\/seeking-alpha\/#The_Editing_Environment_and_Module_Required\" >The Editing Environment and Module Required<\/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\/seeking-alpha\/#Database\" >Database<\/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\/seeking-alpha\/#Data_import\" >Data import<\/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\/seeking-alpha\/#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\/seeking-alpha\/#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-9\" href=\"https:\/\/tejwin20260323.j.webweb.today\/en\/insight\/seeking-alpha\/#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-10\" href=\"https:\/\/tejwin20260323.j.webweb.today\/en\/insight\/seeking-alpha\/#Related_Link\" >Related Link<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"0828\"><span class=\"ez-toc-section\" id=\"Highlights%EF%BC%9A\"><\/span><strong>Highlights\uff1a<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"1407\">Difficulty\uff1a<em>\u2605\u2605\u2606\u2606\u2606<\/em><\/p>\n\n\n\n<p id=\"fef9\">The Fama&amp;French three-factor model is used to calculate the alpha of Taiwan-listed stocks, and the top 20% of stocks with the highest alpha and the bottom 20% of stocks with the lowest alpha are obtained to make a long-short strategy portfolio and evaluate the performance.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"b5ca\"><span class=\"ez-toc-section\" id=\"Advice%EF%BC%9A\"><\/span>Advice\uff1a<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"e0ce\">This article will start with the establishment of the three-factor model to calculate the SMB and HML. Therefore, it is necessary to have a preliminary understanding of the Capital Asset Pricing Model (CAPM) in investment and to understand the concept of alpha and beta, which will be more helpful to read the article.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"d263\"><span class=\"ez-toc-section\" id=\"Preface%EF%BC%9A\"><\/span><strong>Preface\uff1a<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"e687\">The Capital Asset Pricing Model (CAPM) occupies an important position in Modern Portfolio theory and is the basis of modern financial market price theory, and many scholars have continued to extend on this basis and created various factor models, and there is even a factor zoo. For example, Fama &amp; French\u2019s three-factor model used in this paper is to add a size premium and a B\/M ratio premium to the CAPM, which only considers the market factor, in order to expect the return of the portfolio or stock to be explained by these three factors.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"6d18\"><span class=\"ez-toc-section\" id=\"The_Editing_Environment_and_Module_Required\"><\/span><strong>The Editing Environment and Module Required<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"8131\">This article uses Mac OS as system and jupyter as editor.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import pandas as pd<br>import numpy as np<br>import tejapi<br>import statsmodels.api as sm<br>import matplotlib.pyplot as plt<br>import matplotlib.transforms as transforms<br>plt.rcParams['font.sans-serif'] = ['Arial Unicode MS'] # \u89e3\u6c7aMAC\u96fb\u8166 plot\u4e2d\u6587\u554f\u984c<br>plt.rcParams['axes.unicode_minus'] = False<br>tejapi.ApiConfig.api_key =\"Your Key\"<br>tejapi.ApiConfig.ignoretz = True<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"6294\"><span class=\"ez-toc-section\" id=\"Database\"><\/span><strong>Database<\/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=\"4669\">Listed company adjusted stock price(daily)-ex-right and dividends adjustment (TWN\/APRCD1)<br>Securities Property Information(TWN\/ANPRCSTD)<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"7235\"><span class=\"ez-toc-section\" id=\"Data_import\"><\/span><strong>Data import<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"10c1\">Data was obtained for the period from January 2014 to June 2021 for listed stock codes, closing prices, return, market capitalization, and price-to-book ratio.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">data=tejapi.get('TWN\/ANPRCSTD' ,chinese_column_name=True )<br>select=data[\"\u4e0a\u5e02\u5225\"].unique()<br>select=select[1:3]<br>condition =(data[\"\u4e0a\u5e02\u5225\"].isin(select)) &amp; ( data[\"\u8b49\u5238\u7a2e\u985e\u540d\u7a31\"]==\"\u666e\u901a\u80a1\" )<br>data=data[condition]<br>twid=data[\"\u8b49\u5238\u78bc\"].to_list()  #\u53d6\u5f97\u4e0a\u5e02\u6ac3\u80a1\u7968\u8b49\u5238\u78bc<br><br>df = pd.DataFrame()<br>for i in twid: #\u8cc7\u6599\u7b46\u6578\u8d85\u904e100\u842c\u7b46\uff0c\u900f\u904e\u8ff4\u5708\u65b9\u5f0f\u6293\u53d6<br>    df = pd.concat([df, tejapi.get('TWN\/APRCD1',  #\u5f9eTEJ api\u6488\u53d6\u6240\u9700\u8981\u7684\u8cc7\u6599<br>                  chinese_column_name = True,<br>                  paginate = True,<br>                  mdate = {'gt':'2013-12-31', 'lt':'2022-07-01'},<br>                  coid=i,<br>                  opts={'columns':['coid','mdate', 'close_adj' ,'roi' ,'mv', \"pbr_tej\"]})])<\/pre>\n\n\n\n<p id=\"4eff\">The book-to-market ratio is obtained by first taking the inverse of the price-to-book ratio, then obtaining the median daily market capitalization, and label stocks larger than the median as B and those smaller than the median as S to form two portfolios.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">df['\u5e33\u9762\u5e02\u503c\u6bd4'] = 1\/df['\u80a1\u50f9\u6de8\u503c\u6bd4-TEJ']<br><br>ME = df.groupby('\u5e74\u6708\u65e5')['\u5e02\u503c(\u767e\u842c\u5143)'].apply(lambda x: x.median())<br>ME.name = '\u5e02\u503c_\u4e2d\u4f4d\u6578'<br>df = df.merge(ME, on='\u5e74\u6708\u65e5')<br>df['\u5e02\u503cmatrix'] = np.where(df['\u5e02\u503c(\u767e\u842c\u5143)']&gt;df['\u5e02\u503c_\u4e2d\u4f4d\u6578'], 'B', 'S')<\/pre>\n\n\n\n<p id=\"2690\">The weight of the portfolio is obtained by weighting the large-cap portfolio and the small-cap portfolio by value weighted, and confirming whether the total weight of both portfolios is equal to 1.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">df1 = (df.groupby(['\u5e74\u6708\u65e5','\u5e02\u503cmatrix'])['\u5e02\u503c(\u767e\u842c\u5143)'].sum()).reset_index()<br>df = df.merge(df1, on=['\u5e74\u6708\u65e5','\u5e02\u503cmatrix'])<br>df['weight'] = df['\u5e02\u503c(\u767e\u842c\u5143)_x']\/df['\u5e02\u503c(\u767e\u842c\u5143)_y']<br>df.groupby(['\u5e74\u6708\u65e5','\u5e02\u503cmatrix'])['weight'].sum()<br><br>df.groupby(['\u5e74\u6708\u65e5','\u5e02\u503cmatrix'])['weight'].sum()<\/pre>\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\/16u2kcWk0cXnZ1UC_pL_mEg.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">\u5e02\u503c\u8aaa\u660e\u5716<\/figcaption><\/figure>\n\n\n\n<p id=\"5602\">The SMB factor is calculated by subtracting the return on the big-cap portfolio from the return on the small-cap portfolio to form a long-short portfolio.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">df['return1'] = df['\u5831\u916c\u7387\uff05']* df['weight']<br>SMB = df.groupby(['\u5e74\u6708\u65e5','\u5e02\u503cmatrix'])['return1'].sum()<br>SMB.reset_index(inplace=True)<br>SMB.set_index('\u5e74\u6708\u65e5',drop=True, inplace=True)<br>SMB = SMB[SMB['\u5e02\u503cmatrix']=='S']['return1'] - SMB[SMB['\u5e02\u503cmatrix']=='B']['return1']<br>SMB.name = 'SMB'<br>SMB<\/pre>\n\n\n\n<p id=\"1061\">Fama&amp;French divided the BM ratio into 30%, 40%, and 30%, so I obtained the BM ratio of the 30th percentile and 70th percentile and labeled those larger than the 70th percentile as V(value), those smaller than 30% as G(growth), and the rest as N(Neutral) to form three portfolios.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a = df.groupby('\u5e74\u6708\u65e5')['\u5e33\u9762\u5e02\u503c\u6bd4'].quantile(0.7)<br>a.name = 'BM_0.7'<br>b = df.groupby('\u5e74\u6708\u65e5')['\u5e33\u9762\u5e02\u503c\u6bd4'].quantile(0.3)<br>b.name = 'BM_0.3'<br>df = df.merge(a, on='\u5e74\u6708\u65e5')<br>df = df.merge(b, on='\u5e74\u6708\u65e5')<br>df['BM_matrix'] = np.where(df['\u5e33\u9762\u5e02\u503c\u6bd4']&gt;df['BM_0.7'], 'V', (np.where(df['\u5e33\u9762\u5e02\u503c\u6bd4']&lt;df['BM_0.3'],'G', 'N')))<\/pre>\n\n\n\n<p id=\"d7b0\">The same value weighted is used to calculate the weights of the three portfolios and to determine whether the sum of the weights to one.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">df2 = (df.groupby(['\u5e74\u6708\u65e5','BM_matrix'])['\u5e02\u503c(\u767e\u842c\u5143)_x'].sum()).reset_index()<br>df = df.merge(df2, on=['\u5e74\u6708\u65e5','BM_matrix'])<br>df['weight2'] = df['\u5e02\u503c(\u767e\u842c\u5143)_x_x']\/df['\u5e02\u503c(\u767e\u842c\u5143)_x_y']<br>df.groupby(['\u5e74\u6708\u65e5','BM_matrix'])['weight2'].sum()<\/pre>\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\/1saTfUdVosj8C5uLoniqjxQ.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">\u5e02\u503c\u8aaa\u660e\u5716<\/figcaption><\/figure>\n\n\n\n<p id=\"1bf4\">The return on the portfolio is calculated by subtracting the return on the growth stock portfolio from the return on the value stock portfolio to form a long-short portfolio, completing the HML factor.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">df['return2'] = df['\u5831\u916c\u7387\uff05']* df['weight2']<br>HML = df.groupby(['\u5e74\u6708\u65e5','BM_matrix'])['return2'].sum()<br>HML.reset_index(inplace=True)<br>HML.set_index('\u5e74\u6708\u65e5',drop=True, inplace=True)<br>HML = HML[HML['BM_matrix']=='V']['return2'] - HML[HML['BM_matrix']=='G']['return2']<br>HML.name = 'HML'<br>HML<\/pre>\n\n\n\n<p id=\"86db\">Combine the calculated SMB and HML factors.<\/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\/1FykKPxK2-ErxdW3c5GRo0g.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"151c\">Combine the three factors into one table.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Y9999 = tejapi.get('TWN\/APRCD1',  #\u5f9eTEJ api\u6488\u53d6\u6240\u9700\u8981\u7684\u8cc7\u6599<br>                  chinese_column_name = True,<br>                  paginate = True,<br>                  mdate = {'gt':'2013-12-31', 'lt':'2022-07-01'},<br>                  coid='Y9999',<br>                  opts={'columns':['coid','mdate', 'roi']})<\/pre>\n\n\n\n<p id=\"ad97\">Screening out the return of individual stocks.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">fama = fama.merge(Y9999[['\u5e74\u6708\u65e5','\u5831\u916c\u7387\uff05']], on='\u5e74\u6708\u65e5')<br>fama.rename(columns = {'\u5831\u916c\u7387\uff05':'rm'}, inplace=True)<br>fama.set_index('\u5e74\u6708\u65e5',drop=True,inplace=True)<br>fama<\/pre>\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\/1CBEiWwJVayxoo8NSAgaB_A.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"3d00\">Screening out the return of individual stocks.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">stock = df[['\u8b49\u5238\u4ee3\u78bc', '\u5e74\u6708\u65e5','\u5831\u916c\u7387\uff05']]<br>stock.set_index('\u5e74\u6708\u65e5', drop=True, inplace=True)<br>stock = stock.loc[:'2022-06-30']<br>stock<\/pre>\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\/1NlO3rlEy7-C8ERdHiyd7FQ.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"e55d\">Our target portfolio is to combine a long-short strategy by deducting the alpha of the first 20% from the last 20% of all stocks, with equal weighted, and rebalancing by adjusting the portfolio every six months, using the data screened in the first six months for the second half of the year and backtesting.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">m = pd.date_range('2013-12-31', '2022-07-31', freq='6M').to_list()<br>X = sm.add_constant(fama)<br>stock_list = stock['\u8b49\u5238\u4ee3\u78bc'].unique()<br><br>b = pd.DataFrame()<br>for j in stock_list:<br>    a=[]<br>    for i in range(len(m)-1):<br>        try:<br>            Y = (stock[stock['\u8b49\u5238\u4ee3\u78bc']== j]).loc[m[i]:m[i+1]]<br>            result = sm.OLS(Y['\u5831\u916c\u7387\uff05'], X.loc[m[i]:m[i+1]]).fit()<br>            a.append(result.params[0])<br>        except:<br>            pass<br>    j = str(j)<br>    c = pd.DataFrame({'\u8b49\u5238\u4ee3\u78bc':([j]*len(a)), 'alpha':a}, index=m[1:len(a)+1])<br>    b = pd.concat([b,c])<br>b.index.name = '\u5e74\u6708\u65e5'<\/pre>\n\n\n\n<p id=\"c4ec\">The alpha values of the 80th percentile and the 20th percentile are calculated, and stocks above the 80th percentile are screened out to form the long portfolio, while those below 20% form the short portfolio.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">alpha1 = b.groupby('\u5e74\u6708\u65e5')['alpha'].apply(lambda x : x.quantile(0.8))<br>alpha1.name = 'alpha0.8'<br>alpha2 = b.groupby('\u5e74\u6708\u65e5')['alpha'].apply(lambda x : x.quantile(0.2))<br>alpha2.name = 'alpha0.2'<br>b = b.merge(alpha1, on='\u5e74\u6708\u65e5')<br>b = b.merge(alpha2, on='\u5e74\u6708\u65e5')<br>long = (b.where(b['alpha'] &gt; b['alpha0.8'])).dropna()<br>short = (b.where(b['alpha'] &lt; b['alpha0.2'])).dropna()<\/pre>\n\n\n\n<p id=\"cc9c\">Do some data pre-processing before backtesting?<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">stock1 = df[['\u8b49\u5238\u4ee3\u78bc','\u5e74\u6708\u65e5','\u6536\u76e4\u50f9(\u5143)']]<br>stock1.set_index('\u5e74\u6708\u65e5',drop=True, inplace=True)<br>stock1 = stock1.loc[:\"2022-06-30\"]<br>stock1['\u8b49\u5238\u4ee3\u78bc'] = stock1['\u8b49\u5238\u4ee3\u78bc'].astype('str')<\/pre>\n\n\n\n<p id=\"20dc\">Calculate the return of the long portfolio and the return of the short portfolio, and calculate the return of this alpha strategy.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ret = []<br>for i in range(1, len(m)-1):<br>    qq = (stock1.loc[m[i]:m[i+1]])['\u8b49\u5238\u4ee3\u78bc'].isin((long.loc[m[i]])['\u8b49\u5238\u4ee3\u78bc'].tolist())<br>    a = ((stock1.loc[m[i]:m[i+1]])[qq]).groupby('\u8b49\u5238\u4ee3\u78bc')['\u6536\u76e4\u50f9(\u5143)'].tail(1).sum()<br>    b = ((stock1.loc[m[i]:m[i+1]])[qq]).groupby('\u8b49\u5238\u4ee3\u78bc')['\u6536\u76e4\u50f9(\u5143)'].head(1).sum()<br>    c = len((long.loc[m[i]])['\u8b49\u5238\u4ee3\u78bc'].tolist())<br>    long_ret = ((a\/b)-1)\/c<br>    qq1 = (stock1.loc[m[i]:m[i+1]])['\u8b49\u5238\u4ee3\u78bc'].isin((short.loc[m[i]])['\u8b49\u5238\u4ee3\u78bc'].tolist())<br>    a1 = ((stock1.loc[m[i]:m[i+1]])[qq1]).groupby('\u8b49\u5238\u4ee3\u78bc')['\u6536\u76e4\u50f9(\u5143)'].tail(1).sum()<br>    b1 = ((stock1.loc[m[i]:m[i+1]])[qq1]).groupby('\u8b49\u5238\u4ee3\u78bc')['\u6536\u76e4\u50f9(\u5143)'].head(1).sum()<br>    c1 = len((short.loc[m[i]])['\u8b49\u5238\u4ee3\u78bc'].tolist())<br>    short_ret = ((a1\/b1)-1)\/c1<br>    ret.append(long_ret - short_ret)<\/pre>\n\n\n\n<p id=\"a9c6\">Theoretically, alpha represents the excess return, which can exceed that earned by the market, but from the results, we found that the return of the portfolio formed by using this strategy is between 0 and below, regardless of the general market ups and downs, which shows that these factors are already in doubt of failure.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">y9999  = tejapi.get('TWN\/APRCD1',  #\u5f9eTEJ api\u6488\u53d6\u6240\u9700\u8981\u7684\u8cc7\u6599<br>                  chinese_column_name = True,<br>                  paginate = True,<br>                  mdate = {'gt':'2013-12-31', 'lt':'2022-07-01'},<br>                  coid='Y9999',<br>                  opts={'columns':['coid','mdate', 'close_adj']})<br><br>y9999.set_index('\u5e74\u6708\u65e5' ,drop=True, inplace=True)<br><br>a = []<br>for i in range(1 , len(m)-1):<br>    b = (((y9999.loc[m[i]:m[i+1]]).tail(1)['\u6536\u76e4\u50f9(\u5143)'].values \/ (y9999.loc[m[i]:m[i+1]]).head(1)['\u6536\u76e4\u50f9(\u5143)'].values) -1)[0]<br>    a.append(b)<br><br>ret['\u5927\u76e4'] = a<br>ret[['ret', '\u5927\u76e4']].apply(lambda x :x*100)<\/pre>\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\/19fF1QTXLTjPKpb2HnFb0SA.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">\u6536\u76e4\u50f9<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"8f31\"><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=\"39f4\">From the results, we can see that the return of our alpha-seeking portfolio is almost zero, probably because the Fama&amp;French three-factor model was discovered early, and the abnormal return opportunities have already been discovered by investors, so it fails. Readers can learn more about other factor models, such as the five-factor model proposed by Fama &amp; French in 2015, to test the performance in practice. Although the return of the three-factor model does not seem to be particularly good, it is still a very important part of asset pricing and is used by many in academia and the industry.<\/p>\n\n\n\n<p id=\"298c\">We will also introduce the use of the TEJ database to construct various indicators and backtest the performance of the indicators, welcome to purchase the plans offered in TEJ E-Shop and use the well-complete database to find the potential event.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"af74\"><span class=\"ez-toc-section\" id=\"Source_Code\"><\/span>Source Code<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\/07b6441179d9c9da4338a961c4e354af#file-tejapi_python_seeking_alpha-ipynb\" class=\"ek-link\" target=\"_blank\" rel=\"noopener\">Github<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"619d\"><span class=\"ez-toc-section\" id=\"Extended_Reading\"><\/span>Extended Reading<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/tejwin20260323.j.webweb.today\/en\/insight\/analysis-of-the-chip-concentration\/\" class=\"ek-link\">Analysis of the chip concentration<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"69fb\"><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\" class=\"ek-link\" target=\"_blank\" rel=\"noopener\">TEJ API\u00a0<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/eshop.tej.com.tw\/E-Shop\/Edata_intro\" class=\"ek-link\" target=\"_blank\" rel=\"noopener\">TEJ E-Shop\u00a0<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The alpha obtained from the Fama&amp;French three-factor model is used to construct a long-short strategy and backtest the performance against the market return. Highlights\uff1a Difficulty\uff1a\u2605\u2605\u2606\u2606\u2606 The Fama&amp;French three-factor model is used to calculate the alpha of Taiwan-listed stocks, and the top 20% of stocks with the highest alpha and the bottom 20% of stocks with [&hellip;]<\/p>\n","protected":false},"featured_media":15448,"template":"","tags":[3063],"insight-category":[690,50],"class_list":["post-15447","insight","type-insight","status-publish","has-post-thumbnail","hentry","tag-backtesting-2","insight-category-data-analysis","insight-category-fintech"],"acf":[],"_links":{"self":[{"href":"https:\/\/tejwin20260323.j.webweb.today\/en\/wp-json\/wp\/v2\/insight\/15447","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":1,"href":"https:\/\/tejwin20260323.j.webweb.today\/en\/wp-json\/wp\/v2\/insight\/15447\/revisions"}],"predecessor-version":[{"id":43934,"href":"https:\/\/tejwin20260323.j.webweb.today\/en\/wp-json\/wp\/v2\/insight\/15447\/revisions\/43934"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tejwin20260323.j.webweb.today\/en\/wp-json\/wp\/v2\/media\/15448"}],"wp:attachment":[{"href":"https:\/\/tejwin20260323.j.webweb.today\/en\/wp-json\/wp\/v2\/media?parent=15447"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tejwin20260323.j.webweb.today\/en\/wp-json\/wp\/v2\/tags?post=15447"},{"taxonomy":"insight-category","embeddable":true,"href":"https:\/\/tejwin20260323.j.webweb.today\/en\/wp-json\/wp\/v2\/insight-category?post=15447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}