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

量化交易吧 /  量化平台 帖子:3365768 新帖:22

【共享函数】过滤ST,停牌,涨停等

只求稳定发表于:9 月 7 日 12:00回复(1)
stocks = get_all_securities(date='2019-09-02').index.tolist()
print (len(stocks))
3680

过滤停牌¶

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()

res = filter_paused(stocks,'2019-09-02')
print (len(res))
3661

过滤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()

res = filter_st(stocks,'2019-09-02')
print (len(res))
3537

过滤/判断跌涨停等¶

由于涨停的定义比较复杂(一字板,触板等) , 使用条件也各有不同(判断当期是否涨停还是判断昨天有无涨停等) , 所以此处仅做示例,具体判断大家可以在使用时再考虑。

# 因为停牌时 . 涨跌停,当前价都是填充交易日的数据, 所以同时也会过滤掉停牌的标的
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))
3572
3654
3659
3661
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 涨停的股票
65
51
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])]
000820.XSHE     1.79
002356.XSHE     2.26
002520.XSHE     7.29
002536.XSHE     6.01
300183.XSHE    14.03
603613.XSHG    47.30
603737.XSHG    63.70
Name: 2019-09-02 11:00:00, dtype: float64
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]  #筛选出满足上述三个条件的标的
002520.XSHE     7.29
002536.XSHE     6.01
300183.XSHE    14.03
603737.XSHG    63.70
Name: 2019-09-02 11:00:00, dtype: float64

示例函数 : 统计某一天停牌,当日新上市,上涨,下跌,涨停,跌停户¶

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))
Market_statistics('2019-09-03')
st股票有144只
未退市的有3680只
停牌18只
当日新上市股票:[]
上涨2261只(不含当日上市)
下跌1268只
涨停56只
跌停2只
 

全部回复

0/140

量化课程

    移动端课程