小市值选股(二)

在上一篇小市值选股(一)的基础上继续添加下面功能:

  • 剔除创业板股
  • 剔除停牌股
  • 剔除st股
  • 剔除涨跌停股

历史数据筛选结果截图

joinquant 代码

# 过滤创业版股票
def filter_gem_stock(context, stock_list):  
    #return [stock for stock in stock_list if stock[0:3] != '300']
    for stock in stock_list:
        if stock[:3] == '300':
            print("剔除创业板股票 %s " % stock)
            stock_list.remove(stock)
    return(stock_list)

# 过滤停牌股票
def filter_paused_stock(stock_list):  
    current_data = get_current_data()
    #return [stock for stock in stock_list if not current_data[stock].paused]
    for stock in stock_list:
        if current_data[stock].paused == True:
            print("剔除停牌股票 %s %s" % (stock,current_data[stock].name))
            stock_list.remove(stock)
    return(stock_list)

# 过滤ST及其他具有退市标签的股票
def filter_st_stock(stock_list):  
    current_data = get_current_data()
    #return [stock for stock in stock_list if not current_data[stock].is_st 
    #    and 'ST' not in current_data[stock].name 
    #    and '*' not in current_data[stock].name 
    #    and '退' not in current_data[stock].name]
    for stock in stock_list:
        if current_data[stock].is_st or'ST' in current_data[stock].name \
            or '*' in current_data[stock].name \
            or '退' in current_data[stock].name:
            print("ST及其有退市标签的股票 %s %s" % (stock,current_data[stock].name))
            stock_list.remove(stock)
    return(stock_list)

# 过滤涨停的股票
def filter_limitup_stock(context, stock_list):  
    last_prices = history(1, unit='1m', field='close', security_list=stock_list)
    current_data = get_current_data()

    # 已存在于持仓的股票即使涨停也不过滤,避免此股票再次可买,但因被过滤而导致选择别的股票
    #return [stock for stock in stock_list if stock in context.portfolio.positions.keys() 
    #    or last_prices[stock][-1] < current_data[stock].high_limit]
    for stock in stock_list:
        if last_prices[stock][-1] >= current_data[stock].high_limit and stock not in context.portfolio.positions.keys():
            print("过滤涨停的股票 %s %s" % (stock,current_data[stock].name))
            stock_list.remove(stock)
    return(stock_list)


# 过滤跌停的股票
def filter_limitdown_stock(context, stock_list):  
    last_prices = history(1, unit='1m', field='close', security_list=stock_list)
    current_data = get_current_data()

    #return [stock for stock in stock_list if stock in context.portfolio.positions.keys() 
    #    or last_prices[stock][-1] > current_data[stock].low_limit]
    for stock in stock_list:
        if last_prices[stock][-1] <= current_data[stock].low_limit and stock not in context.portfolio.positions.keys():
            print("过滤跌停的股票 %s %s" % (stock,current_data[stock].name))
            stock_list.remove(stock)
    return stock_list

def pick_stocks(context, data):  
    q = None
    q = query(valuation.code).filter(
        valuation.pe_ratio > g.min_pe,
        valuation.pe_ratio < g.max_pe,
    ).order_by(valuation.market_cap.asc()
    ).limit(g.select_number)


    df = get_fundamentals(q)
    stock_list = list(df['code'])


    # 过滤创业版股票
    if g.filter_gem:
        stock_list = filter_gem_stock(context, stock_list)
        print("过滤创业板后剩余%s只" % len(stock_list))

    # 过滤停牌股票    
    stock_list = filter_paused_stock(stock_list)
    print("过滤停牌股票后剩余%s只" % len(stock_list))

    stock_list = filter_st_stock(stock_list)
    stock_list = filter_limitup_stock(context, stock_list)
    stock_list = filter_limitdown_stock(context, stock_list)

    print("最终剩余%s只" % len(stock_list))
    print(stock_list)


def initialize(context):  
    g.max_pe = 200
    g.min_pe = 0
    g.select_number = 100
    g.filter_gem = True

def handle_data(context, data):  
    pick_stocks(context, data)