你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

用聚宽量化炒股-5获取数据函数-6)获取指数成分股代码函数get_index_stocks

2021/10/27 8:38:35

get_index_stocks(index_symbol,date=None)

获取一个指数给定日期在平台可交易的成分股列表。

1.各项参数意义

1)date:查询日期,类型为字符串,如:“2021-10-20”或为datetime.datetime对象和datatime.date。

默认值为None,研究中默认为当天;回测时默认随着回测日期的变化而变化,等于context.current_dt。

2).index_symbol

指数代码。这里支持600种股票指数数据。

该函数的返回值是股票代码的list列表。

2.实例

get_index_stocks('000134.XSHG')
['600000.XSHG',
 '600015.XSHG',
 '600016.XSHG',
 '600036.XSHG',
 '600908.XSHG',
 '600919.XSHG',
 '600926.XSHG',
 '600928.XSHG',
 '601009.XSHG',
 '601077.XSHG',
 '601128.XSHG',
 '601166.XSHG',
 '601169.XSHG',
 '601187.XSHG',
 '601229.XSHG',
 '601288.XSHG',
 '601328.XSHG',
 '601398.XSHG',
 '601577.XSHG',
 '601658.XSHG',
 '601818.XSHG',
 '601838.XSHG',
 '601860.XSHG',
 '601916.XSHG',
 '601939.XSHG',
 '601988.XSHG',
 '601997.XSHG',
 '601998.XSHG',
 '603323.XSHG']
stocks=get_index_stocks('000134.XSHG')
df1=history(10,unit='1d',field='open',security_list=stocks,df=True,skip_paused=False,fq='pre')
print("上证银行指数的成分股近10个交易日的开盘价信息:\n",df1)
上证银行指数的成分股近10个交易日的开盘价信息:
             600000.XSHG  600015.XSHG     ...       601998.XSHG  603323.XSHG
2021-10-13         9.14         5.70     ...              4.62         5.34
2021-10-14         9.07         5.69     ...              4.63         5.23
2021-10-15         9.05         5.64     ...              4.59         5.20
2021-10-18         9.06         5.65     ...              4.60         5.10
2021-10-19         9.03         5.65     ...              4.60         5.16
2021-10-20         9.03         5.65     ...              4.62         5.34
2021-10-21         9.05         5.65     ...              4.62         5.37
2021-10-22         9.07         5.72     ...              4.65         5.44
2021-10-25         9.03         5.71     ...              4.64         5.40
2021-10-26         9.06         5.71     ...              4.62         5.49

[10 rows x 29 columns]
df2=get_fundamentals(query(valuation.pe_ratio,valuation.turnover_ratio).filter(valuation.code.in_(stocks)),'2021-10-20')
df2
pe_ratioturnover_ratio
04.47160.0936
13.78000.0941
25.28170.1484
312.15700.1482
47.97951.0369
55.20260.5911
611.00840.2440
76.98110.3142
86.90480.3400
94.83371.2618
109.40610.8173
115.36530.3505
124.15300.2206
1310.55701.7244
144.80390.1175
154.50130.0495
164.00620.1362
175.06700.0399
185.86320.6024
196.83021.0738
204.44920.1346
216.67591.2184
228.44250.6649
236.06210.1343
245.24050.5076
254.39930.0366
264.07950.2123
274.29940.0318
289.17501.2025