请 [注册] 或 [登录]  | 返回主站

量化交易吧 /  量化平台 帖子:3365810 新帖:17

找到pe,pb, eps结合到一张表,输出到excel

fx1118发表于:5 月 10 日 01:11回复(1)


Screen Shot 2018-11-04 at 11.29.00 PM.png

虽然这个思路有点简单,但是这本书写得条理很清楚,也很简单易懂。
按照书中的思路,把这个策略需要的数据结合到一个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;    }


capitalizationcirculating_capcodemarket_cappb_ratiope_ratiope_ratio_lyrturnover_ratiodisplay_name2018q12018q22018q3
01.717041e+061.717025e+06000001.XSHE1904.19860.88457.77488.21161.2888平安银行0.330.400.41
15.208555e+054.630622e+05002142.XSHE950.56121.54938.729110.18430.9092宁波银行0.560.560.64
22.935208e+062.810376e+06600000.XSHG3281.56250.77135.90506.04810.1700浦发银行0.460.490.50
31.282269e+061.282269e+06600015.XSHG1037.35530.64465.15975.23410.3771华夏银行0.290.430.35
44.378242e+063.546212e+06600016.XSHG2701.36770.69705.35395.61640.3391民生银行0.410.270.29
52.521984e+062.062894e+06600036.XSHG7604.11281.55619.716310.90400.5914招商银行0.900.870.88
61.154445e+066.008226e+05600919.XSHG761.93370.76465.88516.41630.8670江苏银行0.290.300.30
75.130200e+052.081648e+05600926.XSHG415.03320.90507.83529.12091.8503杭州银行0.420.170.27
88.482208e+058.482208e+05601009.XSHG617.50470.95255.71276.38701.9134南京银行0.340.360.31
92.077419e+061.905234e+06601166.XSHG3402.81250.79415.61045.94900.5945兴业银行0.850.700.81
102.114298e+061.824801e+06601169.XSHG1302.40780.76776.56326.95250.3233北京银行0.270.270.22
111.092810e+065.195578e+05601229.XSHG1347.43460.97677.50778.79040.6588上海银行0.560.300.45
123.499830e+072.940553e+07601288.XSHG13326.28030.87366.62117.01920.1758农业银行0.170.180.17
137.426272e+063.925086e+06601328.XSHG4177.45610.71096.05416.30290.3055交通银行0.270.240.23
143.564062e+072.696122e+07601398.XSHG19478.46090.92176.75277.02720.1263工商银行0.220.230.22
155.248926e+063.981053e+06601818.XSHG2005.08750.75786.24506.70570.2623光大银行0.170.150.18
163.612251e+053.612251e+04601838.XSHG324.01891.08377.20948.28999.2101成都银行0.300.300.35
172.500110e+079.593658e+05601939.XSHG14693.14940.97297.02617.39931.8678建设银行0.300.290.27
182.943878e+072.107655e+07601988.XSHG10380.92380.73856.06186.33490.0916中国银行0.160.210.13
192.298592e+051.238965e+05601997.XSHG277.89980.98955.47366.13371.7119贵阳银行0.510.480.63
204.893480e+063.190516e+06601998.XSHG2678.86520.74696.51346.82880.1219中信银行0.250.280.19
 

全部回复

0/140

达人推荐

量化课程

    移动端课程