{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"from api import create_portfolio, get_portfile_data, get_benchmark_portfolio\n",
"from datetime import datetime"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# df = pd.read_pickle('../dummy_portfolio.pkl')\n",
"# df.head(20)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# ## create a portfolio\n",
"\n",
"# # weight can use mony\n",
"# portfolio_profile_data = [\n",
"# {'ticker': '002709.XSHE', 'date': datetime(2021, 1, 5), 'weight': 100},\n",
"# {'ticker': '002920.XSHE', 'date': datetime(2021, 1, 5), 'weight': 100},\n",
"# {'ticker': '300274.XSHE', 'date': datetime(2021, 1, 5), 'weight': 100},\n",
"# {'ticker': '600409.XSHG', 'date': datetime(2021, 1, 5), 'weight': 100},\n",
"# {'ticker': '600415.XSHG', 'date': datetime(2021, 1, 5), 'weight': 100},\n",
"# {'ticker': '603882.XSHG', 'date': datetime(2021, 1, 5), 'weight': 100},\n",
"# ]\n",
"# # ten thousand\n",
"# mkt_cap = 100000\n",
"# portfolio_profile, error = create_portfolio(portfolio_profile_data, mkt_cap)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# print(error)\n",
"# # save \n",
"# portfolio_profile.to_pickle('../data/portfolio_portfile.pkl')\n",
"# portfolio_profile.head(10)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" ticker | \n",
" date | \n",
" weight | \n",
" display_name | \n",
" name | \n",
" sector | \n",
" aggregate_sector | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 002709.XSHE | \n",
" 2021-01-05 | \n",
" 100 | \n",
" 天赐材料 | \n",
" TCCL | \n",
" 电气设备I 电池II 电池化学品III 化学原料和化学制品制造业 电池部件及材料 工业 | \n",
" 工业 | \n",
"
\n",
" \n",
" 1 | \n",
" 002920.XSHE | \n",
" 2021-01-05 | \n",
" 100 | \n",
" 德赛西威 | \n",
" DSXW | \n",
" 计算机I 软件开发II 垂直应用软件III 汽车制造业 汽车电子 可选消费 | \n",
" 信息与通信 | \n",
"
\n",
" \n",
" 2 | \n",
" 300274.XSHE | \n",
" 2021-01-05 | \n",
" 100 | \n",
" 阳光电源 | \n",
" YGDY | \n",
" 电气设备I 光伏设备II 逆变器III 电气机械和器材制造业 光伏设备 工业 | \n",
" 工业 | \n",
"
\n",
" \n",
" 3 | \n",
" 600409.XSHG | \n",
" 2021-01-05 | \n",
" 100 | \n",
" 三友化工 | \n",
" SYHG | \n",
" 化工I 化学原料II 纯碱III 化学原料和化学制品制造业 粘胶 原材料 | \n",
" 原料与能源 | \n",
"
\n",
" \n",
" 4 | \n",
" 600415.XSHG | \n",
" 2021-01-05 | \n",
" 100 | \n",
" 小商品城 | \n",
" XSPC | \n",
" 商业贸易I 一般零售II 商业物业经营III 商务服务业 市场服务 工业 | \n",
" 消费 | \n",
"
\n",
" \n",
" 5 | \n",
" 603882.XSHG | \n",
" 2021-01-05 | \n",
" 100 | \n",
" 金域医学 | \n",
" JYYX | \n",
" 医药生物I 医疗服务II 诊断服务III 卫生 体外诊断 医药卫生 | \n",
" 医药卫生 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" ticker date weight display_name name \\\n",
"0 002709.XSHE 2021-01-05 100 天赐材料 TCCL \n",
"1 002920.XSHE 2021-01-05 100 德赛西威 DSXW \n",
"2 300274.XSHE 2021-01-05 100 阳光电源 YGDY \n",
"3 600409.XSHG 2021-01-05 100 三友化工 SYHG \n",
"4 600415.XSHG 2021-01-05 100 小商品城 XSPC \n",
"5 603882.XSHG 2021-01-05 100 金域医学 JYYX \n",
"\n",
" sector aggregate_sector \n",
"0 电气设备I 电池II 电池化学品III 化学原料和化学制品制造业 电池部件及材料 工业 工业 \n",
"1 计算机I 软件开发II 垂直应用软件III 汽车制造业 汽车电子 可选消费 信息与通信 \n",
"2 电气设备I 光伏设备II 逆变器III 电气机械和器材制造业 光伏设备 工业 工业 \n",
"3 化工I 化学原料II 纯碱III 化学原料和化学制品制造业 粘胶 原材料 原料与能源 \n",
"4 商业贸易I 一般零售II 商业物业经营III 商务服务业 市场服务 工业 消费 \n",
"5 医药生物I 医疗服务II 诊断服务III 卫生 体外诊断 医药卫生 医药卫生 "
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"## load portfolio_profile\n",
"portfolio_profile = pd.read_pickle('../data/portfolio_portfile.pkl')\n",
"portfolio_profile.head(10)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"start_date = datetime(2021, 1, 5)\n",
"end_date = datetime(2022, 1, 10)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"auth success \n"
]
}
],
"source": [
"# get portfolio data\n",
"portfile_data, error = get_portfile_data(portfolio_profile, start_date, end_date)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[]\n",
"(1482, 8)\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" ticker | \n",
" date | \n",
" open | \n",
" close | \n",
" high | \n",
" low | \n",
" volume | \n",
" money | \n",
"
\n",
" \n",
" \n",
" \n",
" 2022-01-07 | \n",
" 603882.XSHG | \n",
" 2022-01-07 | \n",
" 91.13 | \n",
" 87.99 | \n",
" 91.17 | \n",
" 87.72 | \n",
" 6971998.0 | \n",
" 6.176535e+08 | \n",
"
\n",
" \n",
" 2022-01-07 | \n",
" 002709.XSHE | \n",
" 2022-01-07 | \n",
" 51.28 | \n",
" 51.72 | \n",
" 52.62 | \n",
" 50.47 | \n",
" 32210458.0 | \n",
" 1.661823e+09 | \n",
"
\n",
" \n",
" 2022-01-07 | \n",
" 600409.XSHG | \n",
" 2022-01-07 | \n",
" 8.23 | \n",
" 8.22 | \n",
" 8.29 | \n",
" 8.19 | \n",
" 35003739.0 | \n",
" 2.884990e+08 | \n",
"
\n",
" \n",
" 2022-01-07 | \n",
" 600415.XSHG | \n",
" 2022-01-07 | \n",
" 4.74 | \n",
" 4.70 | \n",
" 4.79 | \n",
" 4.68 | \n",
" 24902567.0 | \n",
" 1.178837e+08 | \n",
"
\n",
" \n",
" 2022-01-10 | \n",
" 300274.XSHE | \n",
" 2022-01-10 | \n",
" 127.49 | \n",
" 124.09 | \n",
" 127.49 | \n",
" 123.29 | \n",
" 17238708.0 | \n",
" 2.148032e+09 | \n",
"
\n",
" \n",
" 2022-01-10 | \n",
" 600409.XSHG | \n",
" 2022-01-10 | \n",
" 8.24 | \n",
" 8.35 | \n",
" 8.39 | \n",
" 8.21 | \n",
" 32516017.0 | \n",
" 2.699300e+08 | \n",
"
\n",
" \n",
" 2022-01-10 | \n",
" 002920.XSHE | \n",
" 2022-01-10 | \n",
" 130.36 | \n",
" 138.43 | \n",
" 141.96 | \n",
" 130.11 | \n",
" 5005400.0 | \n",
" 6.901614e+08 | \n",
"
\n",
" \n",
" 2022-01-10 | \n",
" 002709.XSHE | \n",
" 2022-01-10 | \n",
" 51.63 | \n",
" 50.73 | \n",
" 51.93 | \n",
" 50.03 | \n",
" 29821246.0 | \n",
" 1.518902e+09 | \n",
"
\n",
" \n",
" 2022-01-10 | \n",
" 600415.XSHG | \n",
" 2022-01-10 | \n",
" 4.70 | \n",
" 4.75 | \n",
" 4.85 | \n",
" 4.67 | \n",
" 39278041.0 | \n",
" 1.859827e+08 | \n",
"
\n",
" \n",
" 2022-01-10 | \n",
" 603882.XSHG | \n",
" 2022-01-10 | \n",
" 88.45 | \n",
" 95.53 | \n",
" 95.59 | \n",
" 88.39 | \n",
" 6991445.0 | \n",
" 6.468392e+08 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" ticker date open close high low \\\n",
"2022-01-07 603882.XSHG 2022-01-07 91.13 87.99 91.17 87.72 \n",
"2022-01-07 002709.XSHE 2022-01-07 51.28 51.72 52.62 50.47 \n",
"2022-01-07 600409.XSHG 2022-01-07 8.23 8.22 8.29 8.19 \n",
"2022-01-07 600415.XSHG 2022-01-07 4.74 4.70 4.79 4.68 \n",
"2022-01-10 300274.XSHE 2022-01-10 127.49 124.09 127.49 123.29 \n",
"2022-01-10 600409.XSHG 2022-01-10 8.24 8.35 8.39 8.21 \n",
"2022-01-10 002920.XSHE 2022-01-10 130.36 138.43 141.96 130.11 \n",
"2022-01-10 002709.XSHE 2022-01-10 51.63 50.73 51.93 50.03 \n",
"2022-01-10 600415.XSHG 2022-01-10 4.70 4.75 4.85 4.67 \n",
"2022-01-10 603882.XSHG 2022-01-10 88.45 95.53 95.59 88.39 \n",
"\n",
" volume money \n",
"2022-01-07 6971998.0 6.176535e+08 \n",
"2022-01-07 32210458.0 1.661823e+09 \n",
"2022-01-07 35003739.0 2.884990e+08 \n",
"2022-01-07 24902567.0 1.178837e+08 \n",
"2022-01-10 17238708.0 2.148032e+09 \n",
"2022-01-10 32516017.0 2.699300e+08 \n",
"2022-01-10 5005400.0 6.901614e+08 \n",
"2022-01-10 29821246.0 1.518902e+09 \n",
"2022-01-10 39278041.0 1.859827e+08 \n",
"2022-01-10 6991445.0 6.468392e+08 "
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(error)\n",
"print(portfile_data.shape)\n",
"portfile_data.sort_values(by=['date'], inplace=True)\n",
"portfile_data.tail(10)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"# save\n",
"portfile_data.to_pickle('../data/portfolio_data.pkl')"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"# load benchmark portfolio\n",
"benchmark_portfolio, error = get_benchmark_portfolio(start_date, end_date)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[]\n",
"(185500, 15)\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" date | \n",
" weight | \n",
" display_name_x | \n",
" actual_data | \n",
" ticker | \n",
" open | \n",
" close | \n",
" high | \n",
" low | \n",
" volume | \n",
" money | \n",
" display_name_y | \n",
" name | \n",
" sector | \n",
" aggregate_sector | \n",
"
\n",
" \n",
" \n",
" \n",
" 185163 | \n",
" 2022-01-10 | \n",
" 0.274 | \n",
" 厦门钨业 | \n",
" 2021-12-31 | \n",
" 600549.XSHG | \n",
" 21.50 | \n",
" 21.59 | \n",
" 21.75 | \n",
" 21.43 | \n",
" 10451128.0 | \n",
" 2.254199e+08 | \n",
" 厦门钨业 | \n",
" XMWY | \n",
" 有色金属I 稀有金属II 钨III 有色金属冶炼和压延加工业 钨钼 原材料 | \n",
" 原料与能源 | \n",
"
\n",
" \n",
" 185162 | \n",
" 2022-01-10 | \n",
" 0.116 | \n",
" 山煤国际 | \n",
" 2021-12-31 | \n",
" 600546.XSHG | \n",
" 6.74 | \n",
" 6.86 | \n",
" 6.86 | \n",
" 6.71 | \n",
" 66929559.0 | \n",
" 4.539690e+08 | \n",
" 山煤国际 | \n",
" SMGJ | \n",
" 煤炭I 煤炭开采II 动力煤III 批发业 煤炭 能源 | \n",
" 原料与能源 | \n",
"
\n",
" \n",
" 185161 | \n",
" 2022-01-10 | \n",
" 0.211 | \n",
" 中国软件 | \n",
" 2021-12-31 | \n",
" 600536.XSHG | \n",
" 29.01 | \n",
" 30.90 | \n",
" 31.45 | \n",
" 28.98 | \n",
" 35489167.0 | \n",
" 1.087404e+09 | \n",
" 中国软件 | \n",
" ZGRJ | \n",
" 计算机I IT服务II IT服务III 软件和信息技术服务业 行业应用软件 信息技术 | \n",
" 信息与通信 | \n",
"
\n",
" \n",
" 185160 | \n",
" 2022-01-10 | \n",
" 0.205 | \n",
" 天士力 | \n",
" 2021-12-31 | \n",
" 600535.XSHG | \n",
" 15.04 | \n",
" 15.52 | \n",
" 15.75 | \n",
" 14.95 | \n",
" 46823950.0 | \n",
" 7.247465e+08 | \n",
" 天士力 | \n",
" TSL | \n",
" 医药生物I 中药II 中药III 医药制造业 中成药 医药卫生 | \n",
" 医药卫生 | \n",
"
\n",
" \n",
" 185159 | \n",
" 2022-01-10 | \n",
" 0.297 | \n",
" 山东药玻 | \n",
" 2021-12-31 | \n",
" 600529.XSHG | \n",
" 42.39 | \n",
" 40.14 | \n",
" 42.39 | \n",
" 39.47 | \n",
" 12543641.0 | \n",
" 5.049473e+08 | \n",
" 山东药玻 | \n",
" SDYB | \n",
" 医药生物I 医疗器械II 医疗耗材III 非金属矿物制品业 医疗耗材 医药卫生 | \n",
" 医药卫生 | \n",
"
\n",
" \n",
" 185158 | \n",
" 2022-01-10 | \n",
" 0.156 | \n",
" 中铁工业 | \n",
" 2021-12-31 | \n",
" 600528.XSHG | \n",
" 8.69 | \n",
" 9.08 | \n",
" 9.26 | \n",
" 8.68 | \n",
" 98213530.0 | \n",
" 8.961637e+08 | \n",
" 中铁工业 | \n",
" ZTGY | \n",
" 机械设备I 运输设备II 铁路设备III 专用设备制造业 城轨铁路 工业 | \n",
" 工业 | \n",
"
\n",
" \n",
" 185157 | \n",
" 2022-01-10 | \n",
" 0.642 | \n",
" 中天科技 | \n",
" 2021-12-31 | \n",
" 600522.XSHG | \n",
" 15.21 | \n",
" 14.76 | \n",
" 15.26 | \n",
" 14.53 | \n",
" 112744173.0 | \n",
" 1.670952e+09 | \n",
" 中天科技 | \n",
" ZTKJ | \n",
" 通信I 通信设备II 通信线缆及配套III 电气机械和器材制造业 通信系统设备及组件 通信服务 | \n",
" 信息与通信 | \n",
"
\n",
" \n",
" 185156 | \n",
" 2022-01-10 | \n",
" 0.276 | \n",
" 华海药业 | \n",
" 2021-12-31 | \n",
" 600521.XSHG | \n",
" 22.32 | \n",
" 23.32 | \n",
" 23.43 | \n",
" 21.90 | \n",
" 29810065.0 | \n",
" 6.827298e+08 | \n",
" 华海药业 | \n",
" HHYY | \n",
" 医药生物I 化学制药II 化学制剂III 医药制造业 药品制剂 医药卫生 | \n",
" 医药卫生 | \n",
"
\n",
" \n",
" 185169 | \n",
" 2022-01-10 | \n",
" 0.240 | \n",
" 卧龙电驱 | \n",
" 2021-12-31 | \n",
" 600580.XSHG | \n",
" 16.66 | \n",
" 16.51 | \n",
" 16.68 | \n",
" 16.23 | \n",
" 19294606.0 | \n",
" 3.175398e+08 | \n",
" 卧龙电驱 | \n",
" WLDQ | \n",
" 电气设备I 电机II 电机III 电气机械和器材制造业 电动机与工控自动化 工业 | \n",
" 工业 | \n",
"
\n",
" \n",
" 185499 | \n",
" 2022-01-10 | \n",
" 0.350 | \n",
" 思瑞浦 | \n",
" 2021-12-31 | \n",
" 688536.XSHG | \n",
" 476.70 | \n",
" 463.10 | \n",
" 476.70 | \n",
" 446.03 | \n",
" 924992.0 | \n",
" 4.254053e+08 | \n",
" 思瑞浦 | \n",
" SRP | \n",
" 电子I 半导体II 模拟芯片设计III 软件和信息技术服务业 集成电路设计 信息技术 | \n",
" 信息与通信 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" date weight display_name_x actual_data ticker open \\\n",
"185163 2022-01-10 0.274 厦门钨业 2021-12-31 600549.XSHG 21.50 \n",
"185162 2022-01-10 0.116 山煤国际 2021-12-31 600546.XSHG 6.74 \n",
"185161 2022-01-10 0.211 中国软件 2021-12-31 600536.XSHG 29.01 \n",
"185160 2022-01-10 0.205 天士力 2021-12-31 600535.XSHG 15.04 \n",
"185159 2022-01-10 0.297 山东药玻 2021-12-31 600529.XSHG 42.39 \n",
"185158 2022-01-10 0.156 中铁工业 2021-12-31 600528.XSHG 8.69 \n",
"185157 2022-01-10 0.642 中天科技 2021-12-31 600522.XSHG 15.21 \n",
"185156 2022-01-10 0.276 华海药业 2021-12-31 600521.XSHG 22.32 \n",
"185169 2022-01-10 0.240 卧龙电驱 2021-12-31 600580.XSHG 16.66 \n",
"185499 2022-01-10 0.350 思瑞浦 2021-12-31 688536.XSHG 476.70 \n",
"\n",
" close high low volume money display_name_y \\\n",
"185163 21.59 21.75 21.43 10451128.0 2.254199e+08 厦门钨业 \n",
"185162 6.86 6.86 6.71 66929559.0 4.539690e+08 山煤国际 \n",
"185161 30.90 31.45 28.98 35489167.0 1.087404e+09 中国软件 \n",
"185160 15.52 15.75 14.95 46823950.0 7.247465e+08 天士力 \n",
"185159 40.14 42.39 39.47 12543641.0 5.049473e+08 山东药玻 \n",
"185158 9.08 9.26 8.68 98213530.0 8.961637e+08 中铁工业 \n",
"185157 14.76 15.26 14.53 112744173.0 1.670952e+09 中天科技 \n",
"185156 23.32 23.43 21.90 29810065.0 6.827298e+08 华海药业 \n",
"185169 16.51 16.68 16.23 19294606.0 3.175398e+08 卧龙电驱 \n",
"185499 463.10 476.70 446.03 924992.0 4.254053e+08 思瑞浦 \n",
"\n",
" name sector aggregate_sector \n",
"185163 XMWY 有色金属I 稀有金属II 钨III 有色金属冶炼和压延加工业 钨钼 原材料 原料与能源 \n",
"185162 SMGJ 煤炭I 煤炭开采II 动力煤III 批发业 煤炭 能源 原料与能源 \n",
"185161 ZGRJ 计算机I IT服务II IT服务III 软件和信息技术服务业 行业应用软件 信息技术 信息与通信 \n",
"185160 TSL 医药生物I 中药II 中药III 医药制造业 中成药 医药卫生 医药卫生 \n",
"185159 SDYB 医药生物I 医疗器械II 医疗耗材III 非金属矿物制品业 医疗耗材 医药卫生 医药卫生 \n",
"185158 ZTGY 机械设备I 运输设备II 铁路设备III 专用设备制造业 城轨铁路 工业 工业 \n",
"185157 ZTKJ 通信I 通信设备II 通信线缆及配套III 电气机械和器材制造业 通信系统设备及组件 通信服务 信息与通信 \n",
"185156 HHYY 医药生物I 化学制药II 化学制剂III 医药制造业 药品制剂 医药卫生 医药卫生 \n",
"185169 WLDQ 电气设备I 电机II 电机III 电气机械和器材制造业 电动机与工控自动化 工业 工业 \n",
"185499 SRP 电子I 半导体II 模拟芯片设计III 软件和信息技术服务业 集成电路设计 信息技术 信息与通信 "
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(error)\n",
"print(benchmark_portfolio.shape)\n",
"benchmark_portfolio.sort_values(by=['date'], inplace=True)\n",
"benchmark_portfolio.tail(10)\n"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"# save\n",
"benchmark_portfolio.to_pickle('../data/benchmark_portfolio.pkl')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "portfolio_risk_assesment",
"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.11.4"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}