借助pandas的筛选方法可以快速地筛选出各种数据 , [<< pandas使用指南>>](https://www.joinquant.com/view/community/detail/41f1c8a2076d4090132ea6ae767b53f3)。
****
以下数据可在交易日盘前09:00之后获取(建议09:10之后再获取)
停牌信息可以通过[行情数据](https://www.joinquant.com/help/api/help?name=api#数据获取函数)中的paused字段获取;
ST信息可以通过[get_extras](https://www.joinquant.com/help/api/help?name=api#get_extras)的is_st字段获取;
涨跌停可以用[行情数据](https://www.joinquant.com/help/api/help?name=api#数据获取函数)中的high_limit及low_limit字段和当前价格进行对比获取;
****
1. **过滤停牌**
```
def filter_paused(stocks,end_date,day=1,x=1):
'''stocks:股票池 end_date:查询日期
day : 过滤最近多少天(包括今天)停牌过的股票,默认只过滤今天
x : 过滤最近day日停牌数>=x日的股票,默认1次
返回 :过滤后的股票池 '''
s = get_price(stocks,end_date=end_date,count =day,fields='paused').paused.sum()
return s[s< x].index.tolist()
```
---
2. **过滤ST**
```
def filter_st(stocks,day):
datas = get_extras('is_st',stocks,end_date = day ,count=1).T
return datas[~datas.iloc[:,0]].index.tolist()
```
---
3. **过滤跌涨停等(同时也可以过滤掉停牌)**
由于涨停的定义比较复杂(一字板,触板等) , 使用条件也各有不同(判断当期是否涨停还是判断昨天有无涨停等) , 所以此处仅做示例,具体判断大家可以在使用时再考虑。
```
stocks = get_all_securities(date='2019-09-02').index.tolist()
print (len(stocks))
```
```
# 因为停牌时 . 涨跌停,当前价都是填充交易日的数据, 所以同时也会过滤掉停牌的标的
df = get_price(stocks,end_date='2019-09-02',count=1,fields=['close','low',
'high','high_limit','low_limit']).iloc[:,0]
s1 = df[df.close!=df.high_limit].index.tolist() #剔除掉收盘价等于涨停价的
print (len(s1))
s2 = df[df.low!=df.high_limit].index.tolist() #剔除掉最低价等于涨停价的(涨停一字板)
print (len(s2))
s3 = df[df.high!=df.low_limit].index.tolist() #剔除掉最高价等于涨停价的(跌停一字板)
print (len(s3))
res = filter_paused(stocks,'2019-09-02')
print (len(res))
```
```
df2 = get_price(stocks,end_date='2019-09-02 11:00:00',count=1,fields=['close',
'high_limit','low_limit'],frequency='1m').iloc[:,0]
res = df2[df2.close ==df2.high_limit].index.tolist() #挑选出 11:00 的收盘价等于涨停价的
print(len(res)) #包含了停牌股
res2 = filter_st(res,'2019-09-02') #剔除停牌
print (len(res2)) #在2019-09-02 11:00:00 涨停的股票
```
```
df3 = get_price(stocks,end_date='2019-09-02 11:00:00',start_date='2019-09-02 09:00:00',fields=['high','close',
'high_limit'],frequency='1m')
# 筛选出2019-09-02 开盘 截至 11:00:00 (触摸到过涨停价) 且 (11:00:00未封板) ; 也就是涨停后开板的标的
df3.close.iloc[-1][(df3.high.max()==df3.high_limit.iloc[-1])&(df3.close.max()!=df3.high_limit.iloc[-1])]
```
```
conditions1 = (df3.high == df3.high_limit).sum()< 2 #封板时间不超过2分钟
conditions2 = (df3.high == df3.high_limit).sum()>0 #有过封板
conditions3 = df3.close.max()!=df3.high_limit.iloc[-1] #11:00:00的时候未涨停
df3.close.iloc[-1][conditions1&conditions2&conditions3] #筛选出满足上述三个条件的标的
```
4. **示例函数 : 统计某一天停牌,当日新上市,上涨,下跌,涨停,跌停户**
```
def Market_statistics(check_date,filter_st=True):
'''check_date: 统计日期,
filter_st: 是否过滤st'''
info_data = get_all_securities(date=check_date)
all_list = info_data.index.tolist()
st_df = get_extras('is_st', all_list,end_date=check_date, df=True, count=1).T
st_list = st_df[st_df.iloc[:,0]].index.tolist()
print ('st股票有%s只'%len(st_list))
print ('未退市的有%s只'%len(all_list))
all_data = get_price(all_list,end_date=check_date,count=2,fields=['paused','close','high_limit','low_limit'])
paused_data = all_data.paused.iloc[-1]
paused_list = paused_data[paused_data==1].index.tolist()
print ('停牌%s只'%len(paused_list))
new_data = all_data.drop(paused_list,axis=2)
if filter_st ==True:
filter_list = list(set(st_list)-set(paused_list))
new_data = new_data.drop(filter_list,axis=2)
pct_data = new_data.close.pct_change().iloc[1]
print ('当日新上市股票:%s'%pct_data[np.isnan(pct_data)].index.tolist())
print ('上涨%s只(不含当日上市)'%len(pct_data[pct_data>=0]))
print ('下跌%s只'%len(pct_data[pct_data<0]))
limit_up_data = new_data.iloc[:,-1,:][new_data.iloc[:,-1,:].close==new_data.iloc[:,-1,:].high_limit]
print ('涨停%s只'%len(limit_up_data))
limit_down_data = new_data.iloc[:,-1,:][new_data.iloc[:,-1,:].close==new_data.iloc[:,-1,:].low_limit]
print ('跌停%s只'%len(limit_down_data))
```
stock_list=filter_paused(get_index_stocks('000300.XSHG'),context.current_dt)
过滤沪深300指数中停牌股票时,总是出现如下错误:
File "/tmp/strategy/user_code.py", line 296, in paused_filter
s = get_price(stocks,end_date=end_date,count =day,fields='paused').paused.sum()
File "/tmp/jqcore/jqboson/jqboson/api/data.py", line 54, in get_price
pre_factor_ref_date=pre_factor_ref_date)
File "/tmp/jqcore/jqdata/jqdata/apis/data.py", line 70, in get_price
security = convert_security(security)
File "/tmp/jqcore/jqdata/jqdata/apis/data_utils.py", line 133, in convert_security
raise ParamsError('security 必须是一个Security实例或者数组')
jqdata.exceptions.ParamsError: security 必须是一个Security实例或者数组
2019-08-17
/opt/conda/lib/python3.6/site-packages/jqdata/apis/data.py:156: UserWarning: 不建议继续使用panel(panel将在pandas未来版本不再支持,将来升级pandas后,您的策略会失败),建议 get_price 传入 panel=False 参数
warnings.warn("不建议继续使用panel(panel将在pandas未来版本不再支持,将来升级pandas后,您的策略会失败),"
/opt/conda/lib/python3.6/site-packages/jqresearch/api.py:109: FutureWarning:
Panel is deprecated and will be removed in a future version.
The recommended way to represent these types of 3-dimensional data are with a MultiIndex on a DataFrame, via the Panel.to_frame() method
Alternatively, you can use the xarray package http://xarray.pydata.org/en/stable/.
Pandas provides a `.to_xarray()` method to help automate this conversion.
pre_factor_ref_date=_get_today(), panel=panel)
2019-09-07
@坤兑 FutureWarning可以直接忽略 , UserWarning暂时不影响使用
2019-09-09
TypeError: 'numpy.float64' object is not iterable
您好,我在RSRS择时策略中加入了上述代码。发现类型不对了
2019-09-23
@时生 传入的stocks应该是一个标的代码组成的列表, 实际传入的是一个 float64
2019-09-24
numpy是什么版本?
'numpy.ndarray' object has no attribute 'index'
2020-08-26
@威少说 panel新版本不支持了,需要update一下
2020-08-27
这个get_extras函数不起作用了,怎么名字里带ST的,去的is_st值也是False?@JoinQuant-薛定谔の喵
2021-01-24
def filter_paused(stocks,end_date,day=1,x=1):
'''stocks:股票池 end_date:查询日期
day : 过滤最近多少天(包括今天)停牌过的股票,默认只过滤今天
x : 过滤最近day日停牌数>=x日的股票,默认1次
返回 :过滤后的股票池 '''
s = get_price(stocks,end_date=end_date,count =day,fields='paused').paused.sum()
return s[s< x].index.tolist()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--------------------------------------------------------------
这个过滤停牌数据的函数写错了吧,get_price的paused返回0或1,sum就是和,为何还有index.tolist()?
做了改进,获取当天未停牌的代码列表,供参考:
def filter_paused(stocks,end_date,day=1,x=1):
'''stocks:股票池 end_date:查询日期
day : 过滤最近多少天(包括今天)停牌过的股票,默认只过滤今天
x : 过滤最近day日停牌数>=x日的股票,默认1次
返回 :过滤后的股票池 '''
s = get_price(stocks,end_date=end_date,count =day,fields='paused')
s.drop(s[s['paused']==1].index, inplace=True)
return s['code'].tolist()
2022-03-22
@无明 注意函数说明 , x : 过滤最近day日停牌数>=x日的股票,默认1次
2022-03-22