股票市场中最为有趣的传说,莫过于巴菲特式的挖掘伟大公司并长期持有,比如大家津津乐道的可口可乐、富国银行之类的故事。比有趣更为精彩的则是,巴菲特的投资者们,假设50年前买进并持有波克夏哈撒韦,那则是传奇了。股神只有一个,股神的投资者们则以万计。
于是,我想统计一下A股、基金、以及指数,到底哪些赚钱,一则满足一点点好奇心,二则看看这些证券有哪些特点。
第一步,取stock、etf、lof、index类证券
第二步,过滤,10年以前上市、当前依然存活。10年相当于一个朱古拉经济周期,避免牛熊轮回的影响。
第三步,取历史数据
第四步,计算对数收益
第五步,建立新表格,index为证券编号,为下边的统计数据做准备
第六步,计算alpha,即该证券相对沪深300的收益年化,top20全是大牛股,但是,其中也有一些是题材股,我很担心未来能否牛下去。
第七步,计算beta。主要是债券指数,以及,ST!债券低beta可以理解,跟沪深300相关度低,ST是什么原因?它停牌太多了,停牌期间使用停牌前的价格,对数收益为0,但是这样的低相关不等于低风险。真正的低相关、低风险、优质资产只有一个,长江电力。
第七步,计算夏普比,top20的绝大部分是优质资产了,所以,我认为,夏普比是比alpha、beta、波动率更为可信的指标。
600340.XSHG 华夏幸福 33.744728 0.916719 0.719420
600436.XSHG 片仔癀 28.040044 0.772822 0.676325
600519.XSHG 贵州茅台 21.365828 0.637456 0.641435
000661.XSHE 长春高新 28.652854 0.807463 0.609317
600276.XSHG 恒瑞医药 21.260560 0.588538 0.589875
600887.XSHG 伊利股份 22.735450 0.799525 0.583004
600763.XSHG 通策医疗 24.810522 0.701061 0.544251
002236.XSHE 大华股份 24.599395 0.843312 0.525328
002032.XSHE 苏泊尔 21.126221 0.851954 0.505074
002044.XSHE 美年健康 22.914683 0.778570 0.503893
000651.XSHE 格力电器 19.602505 0.958015 0.500840
000596.XSHE 古井贡酒 23.999541 0.927241 0.490846
应用例子,列出2012年以前上市的夏普比最优的10只ETF:
第一步,修改如下:
all_scu = get_all_securities(['etf'])
all_scu.loc[index] = ['沪深300', 'HS300', dt.date(2005, 4, 8), dt.date(2200,1,1), 'index']
all_scu[-10:]
最后一步,结果如下:
159905.XSHE 深红利 6.419842 1.035078 0.319340
510030.XSHG 价值ETF 4.650838 0.871513 0.284089
510230.XSHG 金融ETF 4.671781 0.989351 0.268609
159916.XSHE 深F60 6.230197 0.716095 0.246726
159910.XSHE 深F120 4.505099 0.815895 0.234221
510880.XSHG 红利ETF 2.785759 0.925952 0.227141
510150.XSHG 消费ETF 3.998074 0.720098 0.226531
510050.XSHG 50ETF 2.442447 0.972402 0.204373
159915.XSHE 创业板 4.434525 1.039409 0.197085
510180.XSHG 180ETF 2.046681 0.990024 0.190323
import numpy as np
import pandas as pd
import datetime as dt
index = '000300.XSHG'
all_scu = get_all_securities(['stock','etf', 'lof', 'index'])
all_scu[:10]
display_name | name | start_date | end_date | type | |
---|---|---|---|---|---|
000001.XSHE | 平安银行 | PAYH | 1991-04-03 | 2200-01-01 | stock |
000001.XSHG | 上证指数 | SZZS | 1991-07-15 | 2200-01-01 | index |
000002.XSHE | 万科A | WKA | 1991-01-29 | 2200-01-01 | stock |
000002.XSHG | A股指数 | AGZS | 1992-02-21 | 2200-01-01 | index |
000003.XSHG | B股指数 | BGZS | 1992-02-21 | 2200-01-01 | index |
000004.XSHE | 国农科技 | GNKJ | 1990-12-01 | 2200-01-01 | stock |
000004.XSHG | 工业指数 | GYZS | 1993-05-03 | 2200-01-01 | index |
000005.XSHE | 世纪星源 | SJXY | 1990-12-10 | 2200-01-01 | stock |
000005.XSHG | 商业指数 | SYZS | 1993-05-03 | 2200-01-01 | index |
000006.XSHE | 深振业A | SZYA | 1992-04-27 | 2200-01-01 | stock |
start_date = dt.date(2009,1,1)
end_date = dt.datetime.now().date()
slist = [s for s in all_scu.index if all_scu.loc[s].start_date < start_date \
and all_scu.loc[s].end_date > end_date]
slist = slist
slist[:10]
['000001.XSHE', '000001.XSHG', '000002.XSHE', '000002.XSHG', '000003.XSHG', '000004.XSHE', '000004.XSHG', '000005.XSHE', '000005.XSHG', '000006.XSHE']
N = 2400 # days in 10-year
h = history(N, '1d', 'close', slist)
h.iloc[:10]
000001.XSHE | 000001.XSHG | 000002.XSHE | 000002.XSHG | 000003.XSHG | 000004.XSHE | 000004.XSHG | 000005.XSHE | 000005.XSHG | 000006.XSHE | ... | 601898.XSHG | 601899.XSHG | 601918.XSHG | 601919.XSHG | 601939.XSHG | 601958.XSHG | 601988.XSHG | 601991.XSHG | 601998.XSHG | 601999.XSHG | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2009-05-19 | 5.95 | 2676.68 | 7.93 | 2809.66 | 174.80 | 7.26 | 2212.22 | 4.06 | 2955.96 | 3.75 | ... | 10.73 | 4.74 | 10.43 | 12.74 | 2.80 | 12.87 | 2.14 | 6.25 | 3.68 | 10.26 |
2009-05-20 | 5.86 | 2651.41 | 7.83 | 2782.98 | 175.53 | 7.12 | 2196.40 | 4.00 | 2931.85 | 3.75 | ... | 10.96 | 4.79 | 10.71 | 12.51 | 2.76 | 12.43 | 2.12 | 6.12 | 3.65 | 10.57 |
2009-05-21 | 5.64 | 2610.62 | 7.54 | 2740.14 | 173.18 | 6.96 | 2170.09 | 3.99 | 2883.14 | 3.63 | ... | 10.46 | 4.93 | 10.20 | 12.01 | 2.72 | 12.27 | 2.09 | 6.01 | 3.58 | 10.36 |
2009-05-22 | 5.69 | 2597.60 | 7.46 | 2726.38 | 173.76 | 6.73 | 2147.75 | 3.98 | 2886.06 | 3.65 | ... | 10.13 | 4.76 | 9.81 | 12.05 | 2.73 | 12.06 | 2.10 | 6.05 | 3.56 | 10.36 |
2009-05-25 | 5.71 | 2610.01 | 7.70 | 2739.40 | 174.57 | 6.69 | 2161.14 | 3.98 | 2892.85 | 3.76 | ... | 10.07 | 4.67 | 10.17 | 11.97 | 2.73 | 12.36 | 2.10 | 6.06 | 3.55 | 10.78 |
2009-05-26 | 5.69 | 2588.57 | 7.47 | 2716.86 | 173.90 | 6.79 | 2145.32 | 3.93 | 2871.64 | 3.60 | ... | 9.65 | 4.62 | 9.61 | 11.79 | 2.70 | 12.26 | 2.08 | 6.00 | 3.53 | 10.56 |
2009-05-27 | 5.94 | 2632.93 | 7.53 | 2763.54 | 174.90 | 6.80 | 2182.77 | 4.06 | 2890.90 | 3.63 | ... | 9.75 | 4.62 | 9.83 | 12.34 | 2.78 | 12.27 | 2.12 | 5.99 | 3.62 | 10.37 |
2009-06-01 | 6.09 | 2721.28 | 7.86 | 2856.41 | 178.68 | 6.79 | 2253.58 | 4.09 | 2942.41 | 3.76 | ... | 10.52 | 5.08 | 10.81 | 12.91 | 2.86 | 13.15 | 2.18 | 6.16 | 3.68 | 10.34 |
2009-06-02 | 6.15 | 2724.30 | 7.90 | 2859.59 | 178.85 | 6.87 | 2259.52 | 4.07 | 2946.71 | 3.88 | ... | 10.33 | 5.35 | 10.79 | 13.63 | 2.84 | 13.05 | 2.16 | 6.20 | 3.64 | 10.43 |
2009-06-03 | 6.64 | 2778.59 | 8.38 | 2916.66 | 181.10 | 6.90 | 2281.24 | 4.14 | 3005.52 | 4.10 | ... | 10.59 | 5.57 | 11.03 | 13.78 | 2.94 | 13.37 | 2.22 | 6.20 | 3.78 | 10.18 |
10 rows × 1703 columns
r = h/h.shift(1)
r = r[1:]
r = np.log(r)
r[:10]
000001.XSHE | 000001.XSHG | 000002.XSHE | 000002.XSHG | 000003.XSHG | 000004.XSHE | 000004.XSHG | 000005.XSHE | 000005.XSHG | 000006.XSHE | ... | 601898.XSHG | 601899.XSHG | 601918.XSHG | 601919.XSHG | 601939.XSHG | 601958.XSHG | 601988.XSHG | 601991.XSHG | 601998.XSHG | 601999.XSHG | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2009-05-20 | -0.015242 | -0.009486 | -0.012691 | -0.009541 | 0.004168 | -0.019472 | -0.007177 | -0.014889 | -0.008190 | 0.000000 | ... | 0.021209 | 0.010493 | 0.026492 | -0.018218 | -0.014389 | -0.034786 | -0.009390 | -0.021019 | -0.008186 | 0.029767 |
2009-05-21 | -0.038266 | -0.015504 | -0.037740 | -0.015513 | -0.013478 | -0.022728 | -0.012051 | -0.002503 | -0.016754 | -0.032523 | ... | -0.046694 | 0.028809 | -0.048790 | -0.040789 | -0.014599 | -0.012956 | -0.014252 | -0.018137 | -0.019364 | -0.020068 |
2009-05-22 | 0.008826 | -0.005000 | -0.010667 | -0.005034 | 0.003344 | -0.033604 | -0.010348 | -0.002509 | 0.001012 | 0.005495 | ... | -0.032057 | -0.035091 | -0.038985 | 0.003325 | 0.003670 | -0.017263 | 0.004773 | 0.006634 | -0.005602 | 0.000000 |
2009-05-25 | 0.003509 | 0.004766 | 0.031665 | 0.004764 | 0.004651 | -0.005961 | 0.006215 | 0.000000 | 0.002350 | 0.029692 | ... | -0.005941 | -0.019089 | 0.036040 | -0.006661 | 0.000000 | 0.024571 | 0.000000 | 0.001652 | -0.002813 | 0.039740 |
2009-05-26 | -0.003509 | -0.008248 | -0.030325 | -0.008262 | -0.003845 | 0.014837 | -0.007347 | -0.012642 | -0.007359 | -0.043485 | ... | -0.042603 | -0.010764 | -0.056638 | -0.015152 | -0.011050 | -0.008124 | -0.009569 | -0.009950 | -0.005650 | -0.020619 |
2009-05-27 | 0.042999 | 0.016992 | 0.008000 | 0.017036 | 0.005734 | 0.001472 | 0.017306 | 0.032544 | 0.006685 | 0.008299 | ... | 0.010309 | 0.000000 | 0.022635 | 0.045594 | 0.029199 | 0.000815 | 0.019048 | -0.001668 | 0.025176 | -0.018156 |
2009-06-01 | 0.024939 | 0.033005 | 0.042892 | 0.033053 | 0.021382 | -0.001472 | 0.031925 | 0.007362 | 0.017661 | 0.035186 | ... | 0.076011 | 0.094917 | 0.095033 | 0.045156 | 0.028371 | 0.069264 | 0.027909 | 0.027985 | 0.016439 | -0.002897 |
2009-06-02 | 0.009804 | 0.001109 | 0.005076 | 0.001113 | 0.000951 | 0.011713 | 0.002632 | -0.004902 | 0.001460 | 0.031416 | ... | -0.018226 | 0.051785 | -0.001852 | 0.054271 | -0.007018 | -0.007634 | -0.009217 | 0.006473 | -0.010929 | 0.008666 |
2009-06-03 | 0.076660 | 0.019732 | 0.058985 | 0.019761 | 0.012502 | 0.004357 | 0.009567 | 0.017053 | 0.019761 | 0.055152 | ... | 0.024858 | 0.040298 | 0.021999 | 0.010945 | 0.034606 | 0.024225 | 0.027399 | 0.000000 | 0.037740 | -0.024261 |
2009-06-04 | 0.020865 | -0.004093 | 0.002384 | -0.004095 | -0.003762 | 0.017242 | -0.002185 | -0.017053 | -0.016455 | 0.002436 | ... | -0.008535 | -0.023616 | -0.002724 | -0.020529 | 0.010152 | 0.030930 | -0.004515 | 0.059485 | -0.007968 | -0.036004 |
10 rows × 1703 columns
tvalue = pd.DataFrame(all_scu.loc[slist,'display_name'])
tvalue[:10]
display_name | |
---|---|
000001.XSHE | 平安银行 |
000001.XSHG | 上证指数 |
000002.XSHE | 万科A |
000002.XSHG | A股指数 |
000003.XSHG | B股指数 |
000004.XSHE | 国农科技 |
000004.XSHG | 工业指数 |
000005.XSHE | 世纪星源 |
000005.XSHG | 商业指数 |
000006.XSHE | 深振业A |
Rt = r.mean()
tvalue['alpha'] = 24000*(Rt - Rt[index])
tvalue.sort_index(by='alpha', ascending=False).head(20)
display_name | alpha | |
---|---|---|
600340.XSHG | 华夏幸福 | 33.744728 |
000661.XSHE | 长春高新 | 28.652854 |
600705.XSHG | 中航资本 | 28.148656 |
600436.XSHG | 片仔癀 | 28.040044 |
000631.XSHE | 顺发恒业 | 26.437897 |
000622.XSHE | 恒立实业 | 25.784118 |
600763.XSHG | 通策医疗 | 24.810522 |
002236.XSHE | 大华股份 | 24.599395 |
000156.XSHE | 华数传媒 | 24.400672 |
000596.XSHE | 古井贡酒 | 23.999541 |
002230.XSHE | 科大讯飞 | 22.929297 |
002044.XSHE | 美年健康 | 22.914683 |
600887.XSHG | 伊利股份 | 22.735450 |
000977.XSHE | 浪潮信息 | 22.266800 |
002049.XSHE | 紫光国微 | 22.200750 |
002271.XSHE | 东方雨虹 | 22.140175 |
600446.XSHG | 金证股份 | 21.897964 |
600519.XSHG | 贵州茅台 | 21.365828 |
600276.XSHG | 恒瑞医药 | 21.260560 |
002032.XSHE | 苏泊尔 | 21.126221 |
Bt = pd.Series([],[])
for s in slist:
c = cov(r[s], r[index])
Bt[s] = c[0,1]/c[1,1]
tvalue['beta'] = Bt
tvalue.sort_index(by='beta', ascending=True).head(20)
display_name | alpha | beta | |
---|---|---|---|
000012.XSHG | 国债指数 | 0.377678 | -0.001236 |
000022.XSHG | 上证公司债指数 | 1.912531 | -0.000389 |
399481.XSHE | 企债指数 | -3.309561 | -0.000244 |
399923.XSHE | 公司债 | 1.959856 | -0.000244 |
000923.XSHG | 公司债 | 1.959856 | -0.000065 |
000013.XSHG | 上证企业债指数 | 2.243672 | -0.000025 |
161010.XSHE | 富国天丰 | 2.599961 | 0.057739 |
000403.XSHE | 振兴生化 | 18.935969 | 0.211499 |
600817.XSHG | ST宏盛 | 6.914515 | 0.257433 |
000981.XSHE | 银亿股份 | 6.438635 | 0.293009 |
000038.XSHE | 深大通 | 5.106820 | 0.300765 |
600870.XSHG | *ST厦华 | -2.328321 | 0.342413 |
000693.XSHE | *ST华泽 | -11.109592 | 0.360467 |
600401.XSHG | *ST海润 | -2.136555 | 0.365159 |
000688.XSHE | 国城矿业 | 11.059581 | 0.384690 |
600145.XSHG | *ST新亿 | 0.313353 | 0.407310 |
000622.XSHE | 恒立实业 | 25.784118 | 0.409320 |
600900.XSHG | 长江电力 | 6.784228 | 0.429737 |
000672.XSHE | 上峰水泥 | 5.117269 | 0.445091 |
600681.XSHG | 百川能源 | 11.623827 | 0.453649 |
tvalue['SR'] = (240*r.mean() - 0.05)/(sqrt(240)*r.std())
tvalue.sort_index(by='SR', ascending=False).head(20)
display_name | alpha | beta | SR | |
---|---|---|---|---|
600340.XSHG | 华夏幸福 | 33.744728 | 0.916719 | 0.719420 |
600436.XSHG | 片仔癀 | 28.040044 | 0.772822 | 0.676325 |
600519.XSHG | 贵州茅台 | 21.365828 | 0.637456 | 0.641435 |
000661.XSHE | 长春高新 | 28.652854 | 0.807463 | 0.609317 |
600276.XSHG | 恒瑞医药 | 21.260560 | 0.588538 | 0.589875 |
600887.XSHG | 伊利股份 | 22.735450 | 0.799525 | 0.583004 |
600763.XSHG | 通策医疗 | 24.810522 | 0.701061 | 0.544251 |
002236.XSHE | 大华股份 | 24.599395 | 0.843312 | 0.525328 |
002032.XSHE | 苏泊尔 | 21.126221 | 0.851954 | 0.505074 |
002044.XSHE | 美年健康 | 22.914683 | 0.778570 | 0.503893 |
000651.XSHE | 格力电器 | 19.602505 | 0.958015 | 0.500840 |
000596.XSHE | 古井贡酒 | 23.999541 | 0.927241 | 0.490846 |
000013.XSHG | 上证企业债指数 | 2.243672 | -0.000025 | 0.474559 |
000963.XSHE | 华东医药 | 18.649505 | 0.628749 | 0.470887 |
002252.XSHE | 上海莱士 | 18.313229 | 0.534981 | 0.454392 |
002271.XSHE | 东方雨虹 | 22.140175 | 1.063842 | 0.434609 |
002230.XSHE | 科大讯飞 | 22.929297 | 0.944795 | 0.432094 |
002049.XSHE | 紫光国微 | 22.200750 | 0.886714 | 0.412963 |
000858.XSHE | 五粮液 | 15.745040 | 0.859229 | 0.408251 |
002035.XSHE | 华帝股份 | 20.468450 | 1.074784 | 0.405404 |
本社区仅针对特定人员开放
查看需注册登录并通过风险意识测评
5秒后跳转登录页面...
移动端课程