Skip to content

Instantly share code, notes, and snippets.

@tej87681088
Created December 13, 2022 05:24
Show Gist options
  • Save tej87681088/07b6441179d9c9da4338a961c4e354af to your computer and use it in GitHub Desktop.
Save tej87681088/07b6441179d9c9da4338a961c4e354af to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import tejapi\n",
"import statsmodels.api as sm\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib.transforms as transforms\n",
"plt.rcParams['font.sans-serif'] = ['Arial Unicode MS'] # 解決MAC電腦 plot中文問題\n",
"plt.rcParams['axes.unicode_minus'] = False\n",
"tejapi.ApiConfig.api_key =\"Your Key\"\n",
"tejapi.ApiConfig.ignoretz = True"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"data=tejapi.get('TWN/ANPRCSTD' ,chinese_column_name=True )\n",
"select=data[\"上市別\"].unique()\n",
"select=select[1:3]\n",
"condition =(data[\"上市別\"].isin(select)) & ( data[\"證券種類名稱\"]==\"普通股\" )\n",
"data=data[condition]\n",
"twid=data[\"證券碼\"].to_list() #取得上市櫃股票證券碼"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame()\n",
"for i in twid: #資料筆數超過100萬筆,透過迴圈方式抓取\n",
" df = pd.concat([df, tejapi.get('TWN/APRCD1', #從TEJ api撈取所需要的資料\n",
" chinese_column_name = True,\n",
" paginate = True,\n",
" mdate = {'gt':'2013-12-31', 'lt':'2022-07-01'},\n",
" coid=i,\n",
" opts={'columns':['coid','mdate', 'close_adj' ,'roi' ,'mv', \"pbr_tej\"]})])"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# df = pd.read_csv('alpha.csv', sep=',', encoding='big5')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# df = df.iloc[:,1:]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# df['年月日'] = df['年月日'].map(lambda x: x[:10])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# df['年月日'] = pd.to_datetime(df['年月日'])"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"df['帳面市值比'] = 1/df['股價淨值比-TEJ']"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"ME = df.groupby('年月日')['市值(百萬元)'].apply(lambda x: x.median())\n",
"ME.name = '市值_中位數'\n",
"df = df.merge(ME, on='年月日')\n",
"df['市值matrix'] = np.where(df['市值(百萬元)']>df['市值_中位數'], 'B', 'S')"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"df1 = (df.groupby(['年月日','市值matrix'])['市值(百萬元)'].sum()).reset_index()\n",
"df = df.merge(df1, on=['年月日','市值matrix'])\n",
"df['weight'] = df['市值(百萬元)_x']/df['市值(百萬元)_y']\n",
"df.groupby(['年月日','市值matrix'])['weight'].sum()"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"年月日 市值matrix\n",
"2014-01-02 B 1.0\n",
" S 1.0\n",
"2014-01-03 B 1.0\n",
" S 1.0\n",
"2014-01-06 B 1.0\n",
" ... \n",
"2022-07-27 S 1.0\n",
"2022-07-28 B 1.0\n",
" S 1.0\n",
"2022-07-29 B 1.0\n",
" S 1.0\n",
"Name: weight, Length: 4196, dtype: float64"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.groupby(['年月日','市值matrix'])['weight'].sum()"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
"df['return1'] = df['報酬率%']* df['weight']\n",
"SMB = df.groupby(['年月日','市值matrix'])['return1'].sum()\n",
"SMB.reset_index(inplace=True)\n",
"SMB.set_index('年月日',drop=True, inplace=True)\n",
"SMB = SMB[SMB['市值matrix']=='S']['return1'] - SMB[SMB['市值matrix']=='B']['return1']\n",
"SMB.name = 'SMB'\n",
"SMB"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"年月日\n",
"2014-01-02 0.727428\n",
"2014-01-03 1.214870\n",
"2014-01-06 0.623517\n",
"2014-01-07 0.642468\n",
"2014-01-08 0.241418\n",
" ... \n",
"2022-07-25 0.322174\n",
"2022-07-26 0.053480\n",
"2022-07-27 -0.290234\n",
"2022-07-28 -0.106124\n",
"2022-07-29 0.065884\n",
"Name: SMB, Length: 2098, dtype: float64"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"SMB"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"a = df.groupby('年月日')['帳面市值比'].quantile(0.7)\n",
"a.name = 'BM_0.7'\n",
"b = df.groupby('年月日')['帳面市值比'].quantile(0.3)\n",
"b.name = 'BM_0.3'\n",
"df = df.merge(a, on='年月日')\n",
"df = df.merge(b, on='年月日')\n",
"df['BM_matrix'] = np.where(df['帳面市值比']>df['BM_0.7'], 'V', (np.where(df['帳面市值比']<df['BM_0.3'],'G', 'N')))"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
"df2 = (df.groupby(['年月日','BM_matrix'])['市值(百萬元)_x'].sum()).reset_index()\n",
"df = df.merge(df2, on=['年月日','BM_matrix'])\n",
"df['weight2'] = df['市值(百萬元)_x_x']/df['市值(百萬元)_x_y']\n",
"df.groupby(['年月日','BM_matrix'])['weight2'].sum()"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [],
"source": [
"df['return2'] = df['報酬率%']* df['weight2']\n",
"HML = df.groupby(['年月日','BM_matrix'])['return2'].sum()\n",
"HML.reset_index(inplace=True)\n",
"HML.set_index('年月日',drop=True, inplace=True)\n",
"HML = HML[HML['BM_matrix']=='V']['return2'] - HML[HML['BM_matrix']=='G']['return2']\n",
"HML.name = 'HML'\n",
"HML"
]
},
{
"cell_type": "code",
"execution_count": 534,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"年月日\n",
"2014-01-02 0.423522\n",
"2014-01-03 0.678727\n",
"2014-01-06 0.075185\n",
"2014-01-07 0.697764\n",
"2014-01-08 0.260657\n",
" ... \n",
"2022-07-25 0.767774\n",
"2022-07-26 0.787060\n",
"2022-07-27 -1.005587\n",
"2022-07-28 0.418247\n",
"2022-07-29 -0.549442\n",
"Name: HML, Length: 2098, dtype: float64"
]
},
"execution_count": 534,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"HML"
]
},
{
"cell_type": "code",
"execution_count": 535,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>SMB</th>\n",
" <th>HML</th>\n",
" </tr>\n",
" <tr>\n",
" <th>年月日</th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2014-01-02</th>\n",
" <td>0.727428</td>\n",
" <td>0.423522</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-01-03</th>\n",
" <td>1.214870</td>\n",
" <td>0.678727</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-01-06</th>\n",
" <td>0.623517</td>\n",
" <td>0.075185</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-01-07</th>\n",
" <td>0.642468</td>\n",
" <td>0.697764</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-01-08</th>\n",
" <td>0.241418</td>\n",
" <td>0.260657</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2022-07-25</th>\n",
" <td>0.322174</td>\n",
" <td>0.767774</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2022-07-26</th>\n",
" <td>0.053480</td>\n",
" <td>0.787060</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2022-07-27</th>\n",
" <td>-0.290234</td>\n",
" <td>-1.005587</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2022-07-28</th>\n",
" <td>-0.106124</td>\n",
" <td>0.418247</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2022-07-29</th>\n",
" <td>0.065884</td>\n",
" <td>-0.549442</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>2098 rows × 2 columns</p>\n",
"</div>"
],
"text/plain": [
" SMB HML\n",
"年月日 \n",
"2014-01-02 0.727428 0.423522\n",
"2014-01-03 1.214870 0.678727\n",
"2014-01-06 0.623517 0.075185\n",
"2014-01-07 0.642468 0.697764\n",
"2014-01-08 0.241418 0.260657\n",
"... ... ...\n",
"2022-07-25 0.322174 0.767774\n",
"2022-07-26 0.053480 0.787060\n",
"2022-07-27 -0.290234 -1.005587\n",
"2022-07-28 -0.106124 0.418247\n",
"2022-07-29 0.065884 -0.549442\n",
"\n",
"[2098 rows x 2 columns]"
]
},
"execution_count": 535,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fama = pd.concat([SMB,HML], axis=1)\n",
"fama"
]
},
{
"cell_type": "code",
"execution_count": 79,
"metadata": {},
"outputs": [],
"source": [
"Y9999 = tejapi.get('TWN/APRCD1', #從TEJ api撈取所需要的資料\n",
" chinese_column_name = True,\n",
" paginate = True,\n",
" mdate = {'gt':'2013-12-31', 'lt':'2022-07-01'},\n",
" coid='Y9999',\n",
" opts={'columns':['coid','mdate', 'roi']})"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {},
"outputs": [],
"source": [
"fama = fama.merge(Y9999[['年月日','報酬率%']], on='年月日')\n",
"fama.rename(columns = {'報酬率%':'rm'}, inplace=True)\n",
"fama.set_index('年月日',drop=True,inplace=True)\n",
"fama"
]
},
{
"cell_type": "code",
"execution_count": 157,
"metadata": {},
"outputs": [],
"source": [
"fama = fama.loc[:'2022-06-30']"
]
},
{
"cell_type": "code",
"execution_count": 161,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>SMB</th>\n",
" <th>HML</th>\n",
" <th>rm</th>\n",
" </tr>\n",
" <tr>\n",
" <th>年月日</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2014-01-02</th>\n",
" <td>0.727428</td>\n",
" <td>0.423522</td>\n",
" <td>0.0120</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-01-03</th>\n",
" <td>1.214870</td>\n",
" <td>0.678727</td>\n",
" <td>-0.7663</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-01-06</th>\n",
" <td>0.623517</td>\n",
" <td>0.075185</td>\n",
" <td>-0.5444</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-01-07</th>\n",
" <td>0.642468</td>\n",
" <td>0.697764</td>\n",
" <td>0.1446</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-01-08</th>\n",
" <td>0.241418</td>\n",
" <td>0.260657</td>\n",
" <td>0.5135</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2022-06-24</th>\n",
" <td>0.067493</td>\n",
" <td>0.737971</td>\n",
" <td>0.8360</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2022-06-27</th>\n",
" <td>0.029549</td>\n",
" <td>-0.991883</td>\n",
" <td>1.5989</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2022-06-28</th>\n",
" <td>-0.021501</td>\n",
" <td>0.649327</td>\n",
" <td>-0.6952</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2022-06-29</th>\n",
" <td>0.699726</td>\n",
" <td>-0.251527</td>\n",
" <td>-1.2940</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2022-06-30</th>\n",
" <td>0.283322</td>\n",
" <td>0.301092</td>\n",
" <td>-2.7191</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>2077 rows × 3 columns</p>\n",
"</div>"
],
"text/plain": [
" SMB HML rm\n",
"年月日 \n",
"2014-01-02 0.727428 0.423522 0.0120\n",
"2014-01-03 1.214870 0.678727 -0.7663\n",
"2014-01-06 0.623517 0.075185 -0.5444\n",
"2014-01-07 0.642468 0.697764 0.1446\n",
"2014-01-08 0.241418 0.260657 0.5135\n",
"... ... ... ...\n",
"2022-06-24 0.067493 0.737971 0.8360\n",
"2022-06-27 0.029549 -0.991883 1.5989\n",
"2022-06-28 -0.021501 0.649327 -0.6952\n",
"2022-06-29 0.699726 -0.251527 -1.2940\n",
"2022-06-30 0.283322 0.301092 -2.7191\n",
"\n",
"[2077 rows x 3 columns]"
]
},
"execution_count": 161,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fama"
]
},
{
"cell_type": "code",
"execution_count": 160,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>證券代碼</th>\n",
" <th>報酬率%</th>\n",
" </tr>\n",
" <tr>\n",
" <th>年月日</th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2014-01-02</th>\n",
" <td>1101</td>\n",
" <td>-1.8378</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-01-02</th>\n",
" <td>1102</td>\n",
" <td>-1.2953</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-01-02</th>\n",
" <td>1104</td>\n",
" <td>0.1770</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-01-02</th>\n",
" <td>1110</td>\n",
" <td>1.7143</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-01-02</th>\n",
" <td>1203</td>\n",
" <td>0.0000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2022-06-30</th>\n",
" <td>8440</td>\n",
" <td>9.9836</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2022-06-30</th>\n",
" <td>8446</td>\n",
" <td>-0.2710</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2022-06-30</th>\n",
" <td>8928</td>\n",
" <td>-6.1002</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2022-06-30</th>\n",
" <td>9928</td>\n",
" <td>3.0488</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2022-06-30</th>\n",
" <td>9950</td>\n",
" <td>-9.1912</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>3113725 rows × 2 columns</p>\n",
"</div>"
],
"text/plain": [
" 證券代碼 報酬率%\n",
"年月日 \n",
"2014-01-02 1101 -1.8378\n",
"2014-01-02 1102 -1.2953\n",
"2014-01-02 1104 0.1770\n",
"2014-01-02 1110 1.7143\n",
"2014-01-02 1203 0.0000\n",
"... ... ...\n",
"2022-06-30 8440 9.9836\n",
"2022-06-30 8446 -0.2710\n",
"2022-06-30 8928 -6.1002\n",
"2022-06-30 9928 3.0488\n",
"2022-06-30 9950 -9.1912\n",
"\n",
"[3113725 rows x 2 columns]"
]
},
"execution_count": 160,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"stock = df[['證券代碼', '年月日','報酬率%']]\n",
"stock.set_index('年月日', drop=True, inplace=True)\n",
"stock = stock.loc[:'2022-06-30']\n",
"stock"
]
},
{
"cell_type": "code",
"execution_count": 220,
"metadata": {},
"outputs": [],
"source": [
"m = pd.date_range('2013-12-31', '2022-07-31', freq='6M').to_list()\n",
"X = sm.add_constant(fama)\n",
"stock_list = stock['證券代碼'].unique()\n"
]
},
{
"cell_type": "code",
"execution_count": 302,
"metadata": {},
"outputs": [],
"source": [
"b = pd.DataFrame()\n",
"for j in stock_list:\n",
" a=[]\n",
" for i in range(len(m)-1):\n",
" try:\n",
" Y = (stock[stock['證券代碼']== j]).loc[m[i]:m[i+1]]\n",
" result = sm.OLS(Y['報酬率%'], X.loc[m[i]:m[i+1]]).fit()\n",
" a.append(result.params[0])\n",
" except:\n",
" pass\n",
" j = str(j)\n",
" c = pd.DataFrame({'證券代碼':([j]*len(a)), 'alpha':a}, index=m[1:len(a)+1])\n",
" b = pd.concat([b,c])\n",
"b.index.name = '年月日'"
]
},
{
"cell_type": "code",
"execution_count": 323,
"metadata": {},
"outputs": [],
"source": [
"alpha1 = b.groupby('年月日')['alpha'].apply(lambda x : x.quantile(0.8))\n",
"alpha1.name = 'alpha0.8'\n",
"alpha2 = b.groupby('年月日')['alpha'].apply(lambda x : x.quantile(0.2))\n",
"alpha2.name = 'alpha0.2'\n",
"b = b.merge(alpha1, on='年月日')\n",
"b = b.merge(alpha2, on='年月日')\n",
"long = (b.where(b['alpha'] > b['alpha0.8'])).dropna()\n",
"short = (b.where(b['alpha'] < b['alpha0.2'])).dropna()"
]
},
{
"cell_type": "code",
"execution_count": 532,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>證券代碼</th>\n",
" <th>alpha</th>\n",
" <th>alpha0.2</th>\n",
" <th>alpha0.8</th>\n",
" </tr>\n",
" <tr>\n",
" <th>年月日</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2014-06-30</th>\n",
" <td>1210</td>\n",
" <td>0.216635</td>\n",
" <td>-0.142295</td>\n",
" <td>0.141690</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-06-30</th>\n",
" <td>1527</td>\n",
" <td>0.168521</td>\n",
" <td>-0.142295</td>\n",
" <td>0.141690</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-06-30</th>\n",
" <td>1582</td>\n",
" <td>0.237503</td>\n",
" <td>-0.142295</td>\n",
" <td>0.141690</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-06-30</th>\n",
" <td>1583</td>\n",
" <td>0.585237</td>\n",
" <td>-0.142295</td>\n",
" <td>0.141690</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-06-30</th>\n",
" <td>1711</td>\n",
" <td>0.192855</td>\n",
" <td>-0.142295</td>\n",
" <td>0.141690</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2022-06-30</th>\n",
" <td>8409</td>\n",
" <td>0.490293</td>\n",
" <td>-0.087633</td>\n",
" <td>0.134834</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2022-06-30</th>\n",
" <td>8433</td>\n",
" <td>0.220098</td>\n",
" <td>-0.087633</td>\n",
" <td>0.134834</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2022-06-30</th>\n",
" <td>8928</td>\n",
" <td>0.279140</td>\n",
" <td>-0.087633</td>\n",
" <td>0.134834</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2022-06-30</th>\n",
" <td>8941</td>\n",
" <td>0.141910</td>\n",
" <td>-0.087633</td>\n",
" <td>0.134834</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2022-06-30</th>\n",
" <td>9950</td>\n",
" <td>0.295723</td>\n",
" <td>-0.087633</td>\n",
" <td>0.134834</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5067 rows × 4 columns</p>\n",
"</div>"
],
"text/plain": [
" 證券代碼 alpha alpha0.2 alpha0.8\n",
"年月日 \n",
"2014-06-30 1210 0.216635 -0.142295 0.141690\n",
"2014-06-30 1527 0.168521 -0.142295 0.141690\n",
"2014-06-30 1582 0.237503 -0.142295 0.141690\n",
"2014-06-30 1583 0.585237 -0.142295 0.141690\n",
"2014-06-30 1711 0.192855 -0.142295 0.141690\n",
"... ... ... ... ...\n",
"2022-06-30 8409 0.490293 -0.087633 0.134834\n",
"2022-06-30 8433 0.220098 -0.087633 0.134834\n",
"2022-06-30 8928 0.279140 -0.087633 0.134834\n",
"2022-06-30 8941 0.141910 -0.087633 0.134834\n",
"2022-06-30 9950 0.295723 -0.087633 0.134834\n",
"\n",
"[5067 rows x 4 columns]"
]
},
"execution_count": 532,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"long"
]
},
{
"cell_type": "code",
"execution_count": 359,
"metadata": {},
"outputs": [],
"source": [
"stock1 = df[['證券代碼','年月日','收盤價(元)']]\n",
"stock1.set_index('年月日',drop=True, inplace=True)\n",
"stock1 = stock1.loc[:\"2022-06-30\"]\n",
"stock1['證券代碼'] = stock1['證券代碼'].astype('str')"
]
},
{
"cell_type": "code",
"execution_count": 468,
"metadata": {},
"outputs": [],
"source": [
"ret = []\n",
"for i in range(1, len(m)-1):\n",
" qq = (stock1.loc[m[i]:m[i+1]])['證券代碼'].isin((long.loc[m[i]])['證券代碼'].tolist())\n",
" a = ((stock1.loc[m[i]:m[i+1]])[qq]).groupby('證券代碼')['收盤價(元)'].tail(1).sum()\n",
" b = ((stock1.loc[m[i]:m[i+1]])[qq]).groupby('證券代碼')['收盤價(元)'].head(1).sum()\n",
" c = len((long.loc[m[i]])['證券代碼'].tolist())\n",
" long_ret = ((a/b)-1)/c\n",
" qq1 = (stock1.loc[m[i]:m[i+1]])['證券代碼'].isin((short.loc[m[i]])['證券代碼'].tolist())\n",
" a1 = ((stock1.loc[m[i]:m[i+1]])[qq1]).groupby('證券代碼')['收盤價(元)'].tail(1).sum()\n",
" b1 = ((stock1.loc[m[i]:m[i+1]])[qq1]).groupby('證券代碼')['收盤價(元)'].head(1).sum()\n",
" c1 = len((short.loc[m[i]])['證券代碼'].tolist())\n",
" short_ret = ((a1/b1)-1)/c1\n",
" ret.append(long_ret - short_ret)"
]
},
{
"cell_type": "code",
"execution_count": 524,
"metadata": {},
"outputs": [],
"source": [
"ret = pd.DataFrame({'ret':ret}, index=m[2:])"
]
},
{
"cell_type": "code",
"execution_count": 479,
"metadata": {},
"outputs": [],
"source": [
"y9999 = tejapi.get('TWN/APRCD1', #從TEJ api撈取所需要的資料\n",
" chinese_column_name = True,\n",
" paginate = True,\n",
" mdate = {'gt':'2013-12-31', 'lt':'2022-07-01'},\n",
" coid='Y9999',\n",
" opts={'columns':['coid','mdate', 'close_adj']})\n",
"\n",
"y9999.set_index('年月日' ,drop=True, inplace=True)\n",
"\n",
"a = []\n",
"for i in range(1 , len(m)-1):\n",
" b = (((y9999.loc[m[i]:m[i+1]]).tail(1)['收盤價(元)'].values / (y9999.loc[m[i]:m[i+1]]).head(1)['收盤價(元)'].values) -1)[0]\n",
" a.append(b)\n",
"\n",
"ret['大盤'] = a\n",
"ret[['ret', '大盤']].apply(lambda x :x*100)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12 (main, Apr 4 2022, 05:22:27) [MSC v.1916 64 bit (AMD64)]"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "3e06028060a7864164bfee2227bae8dfd39c60041c455d9e6b5a8dd3aec9b09f"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment