数据分析--单因子选股策略、多因子选股策略

 一、单因子选股策略--小市值策略

二、多因子选股策略--市值+ROE(净资产收益率)选股策略


一、单因子选股策略--小市值策略

因子选股策略 

因子:选择股票的某种标准

  增长率、市值、市盈率、ROE(净资产收益率)............

选股策略:

  对于某个因子,选取表现最好(因子最大或最小)的N支股票持仓

  每隔一段时间调仓一次,如果一段时间没有涨可以卖了换

小市值策略:选取股票池中市值最小的N只股票持仓

例如:选择20支市值最小的股票持有,一个月调一次仓:

from jqdata import *

def initialize(context):
    set_benchmark('000300.XSHG')
    set_option('use_real_price', True)
    set_order_cost(OrderCost(close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5), type='stock')
    
    g.security = get_index_stocks('000300.XSHG')
    
    # 选市值作为因子,要从表valuation中market_cap字段获取sqlachmy的query对象
    g.q = query(valuation).filter(valuation.code.in_(g.security))
    g.N = 20      #20支市值最小的股票
    # 假设因子选股策略是每30天执行一次
    #方式一:
    # g.days = -1
    # def handle_data(context,data):
    #     g.days += 1
    #     if g.days % 30 == 0:
    #         pass
    #方式二:
    # 定时执行函数,每个月第1个交易日执行handle函数
    run_monthly(handle, 1)
def handle(context):
    df = get_fundamentals(g.q)[['code','market_cap']]
    df = df.sort_values('market_cap').iloc[:g.N,:]  #选出20支
    print(df)
    
    to_hold = df['code'].values
    
    for stock in context.portfolio.positions:
        if stock not in to_hold:
            order_target(stock, 0)
    to_buy = [stock for stock in to_hold if stock not in context.portfolio.positions]
    if len(to_buy) > 0:
        cash_per_stock = context.portfolio.available_cash / len(to_buy)
        for stock in to_buy:
            order_value(stock, cash_per_stock)
小市值策略代码

二、多因子选股策略--市值+ROE(净资产收益率)选股策略 

多因子选股策略

如何同时综合多个因子来选股?

评分模型:

  每个股票针对每个因子进行评分,将评分相加

  选出总评分最大的N只股票持仓

  如何计算股票在某个因子下的评分:归一化(标准化),下面是两种标准化的方式

比如选择两个因子:市值和ROE(净资产收益率)作为选股评价标准

from jqdata import *

def initialize(context):
    set_benchmark('000300.XSHG')
    set_option('use_real_price', True)
    set_order_cost(OrderCost(close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5), type='stock')
    
    g.security = get_index_stocks('000300.XSHG')
    
    # 选市值作为因子,要从表valuation中market_cap字段获取sqlachmy的query对象
    g.q = query(valuation, indicator).filter(valuation.code.in_(g.security))
    g.N = 20      #20支股票

    run_monthly(handle, 1)
def handle(context):
    df = get_fundamentals(g.q)[['code','market_cap','roe']]
    df['market_cap'] = (df['market_cap']-df['market_cap'].min())/(df['market_cap'].max()-df['market_cap'].min())
    df['roe'] = (df['roe']-df['roe'].min())/(df['roe'].max()-df['roe'].min())
    
    # 双因子评分:市盈率越大越好,市值越小越好
    df['score'] = df['roe'] - df['market_cap']
    # 对评分排序,选最大的20支股票
    df = df.sort_values('score').iloc[-g.N:,:]
    to_hold = df['code'].values
    
    for stock in context.portfolio.positions:
        if stock not in to_hold:
            order_target(stock, 0)
    to_buy = [stock for stock in to_hold if stock not in context.portfolio.positions]
    if len(to_buy) > 0:
        cash_per_stock = context.portfolio.available_cash / len(to_buy)
        for stock in to_buy:
            order_value(stock, cash_per_stock)
市值+ROE选股策略

猜你喜欢

转载自www.cnblogs.com/staff/p/10956443.html