虽然这个思路有点简单,但是这本书写得条理很清楚,也很简单易懂。
按照书中的思路,把这个策略需要的数据结合到一个excel表格中,排序找到股票,看看这个策略真的合适么。
import base64from IPython.display import HTML# 下载文件到本地,创建链接!牛逼!def create_download_link( df, title = "Download CSV file", filename = "data.csv"):csv = df.to_csv()b64 = base64.b64encode(csv.encode())payload = b64.decode()html = '<a download="{filename}" href="data:text/csv;base64,{payload}" target="_blank">{title}</a>'html = html.format(payload=payload,title=title,filename=filename)return HTML(html)
# find price-to-earnings, price-to-book below median# get_fundamentals 查询财务数据# get_fundamentals(query_object, date=None, statDate=None)stocks = get_index_stocks('000951.XSHG')df = get_fundamentals(query(valuation.code,valuation.pb_ratio, valuation.pe_ratio, valuation.capitalization, valuation.circulating_cap, valuation.market_cap,valuation.turnover_ratio,valuation.pe_ratio_lyr).filter(valuation.code.in_(stocks))).sort_index(1)print(df)create_download_link(df)
capitalization circulating_cap code market_cap pb_ratio \ 0 1.717041e+06 1.717025e+06 000001.XSHE 1904.1986 0.8845 1 5.208555e+05 4.630622e+05 002142.XSHE 950.5612 1.5493 2 2.935208e+06 2.810376e+06 600000.XSHG 3281.5625 0.7713 3 1.282269e+06 1.282269e+06 600015.XSHG 1037.3553 0.6446 4 4.378242e+06 3.546212e+06 600016.XSHG 2701.3677 0.6970 5 2.521984e+06 2.062894e+06 600036.XSHG 7604.1128 1.5561 6 1.154445e+06 6.008226e+05 600919.XSHG 761.9337 0.7646 7 5.130200e+05 2.081648e+05 600926.XSHG 415.0332 0.9050 8 8.482208e+05 8.482208e+05 601009.XSHG 617.5047 0.9525 9 2.077419e+06 1.905234e+06 601166.XSHG 3402.8125 0.7941 10 2.114298e+06 1.824801e+06 601169.XSHG 1302.4078 0.7677 11 1.092810e+06 5.195578e+05 601229.XSHG 1347.4346 0.9767 12 3.499830e+07 2.940553e+07 601288.XSHG 13326.2803 0.8736 13 7.426272e+06 3.925086e+06 601328.XSHG 4177.4561 0.7109 14 3.564062e+07 2.696122e+07 601398.XSHG 19478.4609 0.9217 15 5.248926e+06 3.981053e+06 601818.XSHG 2005.0875 0.7578 16 3.612251e+05 3.612251e+04 601838.XSHG 324.0189 1.0837 17 2.500110e+07 9.593658e+05 601939.XSHG 14693.1494 0.9729 18 2.943878e+07 2.107655e+07 601988.XSHG 10380.9238 0.7385 19 2.298592e+05 1.238965e+05 601997.XSHG 277.8998 0.9895 20 4.893480e+06 3.190516e+06 601998.XSHG 2678.8652 0.7469 pe_ratio pe_ratio_lyr turnover_ratio 0 7.7748 8.2116 1.2888 1 8.7291 10.1843 0.9092 2 5.9050 6.0481 0.1700 3 5.1597 5.2341 0.3771 4 5.3539 5.6164 0.3391 5 9.7163 10.9040 0.5914 6 5.8851 6.4163 0.8670 7 7.8352 9.1209 1.8503 8 5.7127 6.3870 1.9134 9 5.6104 5.9490 0.5945 10 6.5632 6.9525 0.3233 11 7.5077 8.7904 0.6588 12 6.6211 7.0192 0.1758 13 6.0541 6.3029 0.3055 14 6.7527 7.0272 0.1263 15 6.2450 6.7057 0.2623 16 7.2094 8.2899 9.2101 17 7.0261 7.3993 1.8678 18 6.0618 6.3349 0.0916 19 5.4736 6.1337 1.7119 20 6.5134 6.8288 0.1219
Download CSV file
print(df['pe_ratio'].mean())def get_name(code):return get_security_info(code).display_namedf['display_name'] = df['code'].apply(get_name)create_download_link(df)
6.652871428571429
Download CSV file
import pandas as pddef get_eps(code, quarter):q = query( income.statDate, income.code, income.basic_eps, balance.cash_equivalents, cash_flow.goods_sale_and_service_render_cash ).filter( income.code == code, )return get_fundamentals(q, statDate=quarter).basic_eps[0]def get_2018q1_eps(code):return get_eps(code, '2018q1')def get_2018q2_eps(code):return get_eps(code, '2018q2')def get_2018q3_eps(code):return get_eps(code, '2018q3')df['2018q1'] = df['code'].apply(get_2018q1_eps)df['2018q2'] = df['code'].apply(get_2018q2_eps)df['2018q3'] = df['code'].apply(get_2018q3_eps)create_download_link(df)df
.dataframe thead tr:only-child th { text-align: right; } .dataframe thead th { text-align: left; } .dataframe tbody tr th { vertical-align: top; }
capitalization | circulating_cap | code | market_cap | pb_ratio | pe_ratio | pe_ratio_lyr | turnover_ratio | display_name | 2018q1 | 2018q2 | 2018q3 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1.717041e+06 | 1.717025e+06 | 000001.XSHE | 1904.1986 | 0.8845 | 7.7748 | 8.2116 | 1.2888 | 平安银行 | 0.33 | 0.40 | 0.41 |
1 | 5.208555e+05 | 4.630622e+05 | 002142.XSHE | 950.5612 | 1.5493 | 8.7291 | 10.1843 | 0.9092 | 宁波银行 | 0.56 | 0.56 | 0.64 |
2 | 2.935208e+06 | 2.810376e+06 | 600000.XSHG | 3281.5625 | 0.7713 | 5.9050 | 6.0481 | 0.1700 | 浦发银行 | 0.46 | 0.49 | 0.50 |
3 | 1.282269e+06 | 1.282269e+06 | 600015.XSHG | 1037.3553 | 0.6446 | 5.1597 | 5.2341 | 0.3771 | 华夏银行 | 0.29 | 0.43 | 0.35 |
4 | 4.378242e+06 | 3.546212e+06 | 600016.XSHG | 2701.3677 | 0.6970 | 5.3539 | 5.6164 | 0.3391 | 民生银行 | 0.41 | 0.27 | 0.29 |
5 | 2.521984e+06 | 2.062894e+06 | 600036.XSHG | 7604.1128 | 1.5561 | 9.7163 | 10.9040 | 0.5914 | 招商银行 | 0.90 | 0.87 | 0.88 |
6 | 1.154445e+06 | 6.008226e+05 | 600919.XSHG | 761.9337 | 0.7646 | 5.8851 | 6.4163 | 0.8670 | 江苏银行 | 0.29 | 0.30 | 0.30 |
7 | 5.130200e+05 | 2.081648e+05 | 600926.XSHG | 415.0332 | 0.9050 | 7.8352 | 9.1209 | 1.8503 | 杭州银行 | 0.42 | 0.17 | 0.27 |
8 | 8.482208e+05 | 8.482208e+05 | 601009.XSHG | 617.5047 | 0.9525 | 5.7127 | 6.3870 | 1.9134 | 南京银行 | 0.34 | 0.36 | 0.31 |
9 | 2.077419e+06 | 1.905234e+06 | 601166.XSHG | 3402.8125 | 0.7941 | 5.6104 | 5.9490 | 0.5945 | 兴业银行 | 0.85 | 0.70 | 0.81 |
10 | 2.114298e+06 | 1.824801e+06 | 601169.XSHG | 1302.4078 | 0.7677 | 6.5632 | 6.9525 | 0.3233 | 北京银行 | 0.27 | 0.27 | 0.22 |
11 | 1.092810e+06 | 5.195578e+05 | 601229.XSHG | 1347.4346 | 0.9767 | 7.5077 | 8.7904 | 0.6588 | 上海银行 | 0.56 | 0.30 | 0.45 |
12 | 3.499830e+07 | 2.940553e+07 | 601288.XSHG | 13326.2803 | 0.8736 | 6.6211 | 7.0192 | 0.1758 | 农业银行 | 0.17 | 0.18 | 0.17 |
13 | 7.426272e+06 | 3.925086e+06 | 601328.XSHG | 4177.4561 | 0.7109 | 6.0541 | 6.3029 | 0.3055 | 交通银行 | 0.27 | 0.24 | 0.23 |
14 | 3.564062e+07 | 2.696122e+07 | 601398.XSHG | 19478.4609 | 0.9217 | 6.7527 | 7.0272 | 0.1263 | 工商银行 | 0.22 | 0.23 | 0.22 |
15 | 5.248926e+06 | 3.981053e+06 | 601818.XSHG | 2005.0875 | 0.7578 | 6.2450 | 6.7057 | 0.2623 | 光大银行 | 0.17 | 0.15 | 0.18 |
16 | 3.612251e+05 | 3.612251e+04 | 601838.XSHG | 324.0189 | 1.0837 | 7.2094 | 8.2899 | 9.2101 | 成都银行 | 0.30 | 0.30 | 0.35 |
17 | 2.500110e+07 | 9.593658e+05 | 601939.XSHG | 14693.1494 | 0.9729 | 7.0261 | 7.3993 | 1.8678 | 建设银行 | 0.30 | 0.29 | 0.27 |
18 | 2.943878e+07 | 2.107655e+07 | 601988.XSHG | 10380.9238 | 0.7385 | 6.0618 | 6.3349 | 0.0916 | 中国银行 | 0.16 | 0.21 | 0.13 |
19 | 2.298592e+05 | 1.238965e+05 | 601997.XSHG | 277.8998 | 0.9895 | 5.4736 | 6.1337 | 1.7119 | 贵阳银行 | 0.51 | 0.48 | 0.63 |
20 | 4.893480e+06 | 3.190516e+06 | 601998.XSHG | 2678.8652 | 0.7469 | 6.5134 | 6.8288 | 0.1219 | 中信银行 | 0.25 | 0.28 | 0.19 |
本社区仅针对特定人员开放
查看需注册登录并通过风险意识测评
5秒后跳转登录页面...
移动端课程