problem

策略分享
标签: #<Tag:0x00007f73ef33a0f8>

(wicked_code) #1
克隆策略
In [9]:
import datetime

class conf:
    start_date = '2013-01-01'
    end_date='2017-09-10'
    split_date = '2016-01-01'
    instruments = D.instruments(start_date, end_date)
    hold_days = 5
    #自定义函数 
        
    features = [
        '0.75*fs_operating_revenue_0/(fs_current_assets_0+fs_current_liabilities_0+fs_non_current_liabilities_0)',
        'pb_lf_0',  # 市净率
        'std(volume_0,20)',
        'std(amount_0,20)',
        'return_90/return_10',
        'sum((((close_0-low_0)-(high_0-close_0))*volume_0/(high_0-low_0)),6)',
    ]
    
    # 数据标注标注
    label_expr = [
    # 计算未来一段时间(hold_days)的相对收益
    'shift(close, -5) / shift(open, -1) - shift(benchmark_close, -5) / shift(benchmark_open, -1)',
    # 极值处理:用1%和99%分位的值做clip
    'clip(label, all_quantile(label, 0.01), all_quantile(label, 0.99))',
    # 将分数映射到分类,这里使用20个分类,这里采取等宽离散化
    'all_wbins(label, 20)',
    # 过滤掉一字涨停的情况 (设置label为NaN,在后续处理和训练中会忽略NaN的label)
    'where(shift(high, -1) == shift(low, -1), NaN, label)'
    ]

## 量化回测 https://bigquant.com/docs/module_trade.html


# 回测引擎:准备数据,只执行一次
def prepare(context):
    # context.start_date / end_date,回测的时候,为trader传入参数;在实盘运行的时候,由系统替换为实盘日期
    instruments = D.instruments()
    ## 在样本外数据上进行预测
    n0 = M.general_feature_extractor.v5(
        instruments=D.instruments(),
        start_date=context.start_date, end_date=context.end_date,
        features=conf.features)
    n1 = M.derived_feature_extractor.v1(
        data=n0.data,
        features= conf.features)
    n2 = M.transform.v2(data=n1.data, transforms=None, drop_null=True)
    n3 = M.stock_ranker_predict.v5(model=context.options['model'], data=n2.data)
    context.instruments = n3.instruments
    context.options['predictions'] = n3.predictions

# 回测引擎:初始化函数,只执行一次
def initialize(context):
    # 加载预测数据
    context.ranker_prediction = context.options['predictions'].read_df()
    # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
    context.set_commission(PerOrder(buy_cost=0.00025, sell_cost=0.0013, min_cost=5))
    # 预测数据,通过options传入进来,使用 read_df 函数,加载到内存 (DataFrame)
    # 设置买入的股票数量,这里买入预测股票列表排名靠前的3只
    stock_count = 5
    # 每只的股票的权重,如下的权重分配会使得靠前的股票分配多max_cash_per_instrument一点的资金,[0.339160, 0.213986, 0.169580, ..]
    context.stock_weights = T.norm([1 / math.log(i + 1.5) for i in range(0, stock_count)])
    # 设置每只股票占用的最大资金比例
    context.max_cash_per_instrument = 0.2
    #context.set_max_leverage(max_leverage=1) 

# 回测引擎:每日数据处理函数,每天执行一次
def handle_data(context, data):
    # 按日期过滤得到今日的预测数据
    ranker_prediction = context.ranker_prediction[
        context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]
    
    # 1. 资金分配
    # 平均持仓时间是hold_days,每日都将买入股票,每日预期使用 1/hold_days 的资金
    # 实际操作中,会存在一定的买入误差,所以在前hold_days天,等量使用资金;之后,尽量使用剩余资金(这里设置最多用等量的1.5倍)
    is_staging = context.trading_day_index < context.options['hold_days'] # 是否在建仓期间(前 hold_days 天)
    cash_avg = context.portfolio.portfolio_value / context.options['hold_days']
    cash_for_buy = min(context.portfolio.cash, (1 if is_staging else 1.5) * cash_avg)
    cash_for_sell = cash_avg - (context.portfolio.cash - cash_for_buy)
    positions = {e.symbol: p.amount * p.last_sale_price
                 for e, p in context.perf_tracker.position_tracker.positions.items()}
    
    
        
    def mk(_close,_open):
        if(_close-_open)/_open<-0.018:
            return 1
        elif abs((_close-_open)/_open)<=0.018:
            return 2
        elif(_close-_open)/_open>0.018:
            return 3
    
    def mkarr(df):
        ob=[]
        transfer=[[0,0,0],[0,0,0],[0,0,0]]
        open_list=df.open.reset_index(drop=True)
        close_list=df.close.reset_index(drop=True)
        for i in range(len(df.open)):
            #print(close_list[i],open_list[i])
            if close_list[i]!=close_list[i] or open_list[i]!=open_list[i]:break
            flag= mk(close_list[i],open_list[i])
            ob.append(flag)
        for i in range(len(ob)-2):
            transfer[ob[i]-1][ob[i+1]-1]+=1
                
        return transfer
    
    
    def predict(buy_instruments):
        if buy_instruments==[]:
            return []
        end_date = data.current_dt.strftime('%Y-%m-%d')
        d = datetime.datetime.strptime(end_date, '%Y-%m-%d')
        start_date=(d+datetime.timedelta(days=-54)).strftime('%Y-%m-%d')
        df = D.history_data(buy_instruments, start_date, end_date,fields=['open','close'],groupped_by_instrument=False)
        df.set_index("date")
        dellist=[]
        for i in range(len(buy_cash_weights)):
            df1=df[df['instrument']==buy_instruments[i]]
            martix=np.mat(mkarr(df1))
            today_open=df1.open.reset_index(drop=True)
            today_close=df1.close.reset_index(drop=True)
            flag=mk(today_close[len(today_close)-1],today_open[len(today_open)-1])
            up=0
            down=0
            shock=0
            if flag==1:down+=1
            elif flag==2:shock+=1
            else:up+=1
            status=[[down],[shock],[up]] 
            k_martix=martix*status
            #print(k_martix)

            if k_martix[0][0]>=k_martix[1][0]:
                max=k_martix[0][0]
                max_status=0
            else:
                max=k_martix[1][0]
                max_status=1

            if k_martix[2][0]>max:
                max_status=2    
                
            if max_status==0:
                dellist.append(buy_instruments[i])
            return dellist
    
    buy_cash_weights = context.stock_weights
    buy_instruments = list(ranker_prediction.instrument[:len(buy_cash_weights)])
    buy_del=predict(buy_instruments)        
    buy_instruments = list(set(buy_instruments)^set(buy_del))
        

    
    # 2. 生成卖出订单:hold_days天之后才开始卖出;对持仓的股票,按StockRanker预测的排序末位淘汰
    if not is_staging and cash_for_sell > 0:
        equities = {e.symbol: e for e, p in context.perf_tracker.position_tracker.positions.items()}
        instruments = list(reversed(list(ranker_prediction.instrument[ranker_prediction.instrument.apply(
                lambda x: x in equities and not context.has_unfinished_sell_order(equities[x]))])))
        # print('rank order for sell %s' % instruments)
        for instrument in instruments:
            context.order_target(context.symbol(instrument), 0)
            cash_for_sell -= positions[instrument]
            if cash_for_sell <= 0:
                break
                
                
                
    # 3. 生成买入订单
    max_cash_per_instrument = context.portfolio.portfolio_value * context.max_cash_per_instrument
    for i, instrument in enumerate(buy_instruments):
        cash = cash_for_buy * buy_cash_weights[i]
        if cash > max_cash_per_instrument - positions.get(instrument, 0):
            # 确保股票持仓量不会超过每次股票最大的占用资金量
            cash = max_cash_per_instrument - positions.get(instrument, 0)
        if cash > 0:
            price = data.current(context.symbol(instrument), 'price')/data.current(context.symbol(instrument),'adjust_factor')
            lots = int(cash/price/100)
            context.order_lots(context.symbol(instrument), lots)
            

    
    
## 通过训练集数据训练模型            
# 高级数据标注
m1 = M.advanced_auto_labeler.v1(
    instruments=conf.instruments, start_date=conf.start_date, end_date=conf.split_date,
    label_expr=conf.label_expr, benchmark='000300.SHA')


    
# 抽取基础特征           
m2_1 = M.general_feature_extractor.v5(
        instruments=D.instruments(),
        start_date=conf.start_date, end_date=conf.split_date,
        features=conf.features)




# 抽取衍生特征 
m2_2 = M.derived_feature_extractor.v1(
        data=m2_1.data,
        features= conf.features)


# 特征转换
m3 = M.transform.v2(data=m2_2.data, transforms=None, drop_null=True)

# 合并标注和特征数据
m4 = M.join.v2(data1=m1.data, data2=m3.data, on=['date', 'instrument'], sort=True)


    

# 开始训练模型
m5 = M.stock_ranker_train.v4(training_ds=m4.data, features=conf.features)

## 测试集上进行回测
m6 = M.trade.v3(
    instruments=None,
    start_date=conf.split_date,
    end_date=conf.end_date,
    prepare=prepare,
    initialize=initialize,
    handle_data=handle_data,
    order_price_field_buy='open',       
    order_price_field_sell='close',      
    capital_base=500000,               
    benchmark='000300.SHA',             
    options={'hold_days': conf.hold_days, 'model': m5.model_id},
    m_deps=np.random.rand()
)
[2017-09-21 15:38:40.102834] INFO: bigquant: advanced_auto_labeler.v1 开始运行..
[2017-09-21 15:38:40.106125] INFO: bigquant: 命中缓存
[2017-09-21 15:38:40.106969] INFO: bigquant: advanced_auto_labeler.v1 运行完成[0.004152s].
[2017-09-21 15:38:40.131570] INFO: bigquant: general_feature_extractor.v5 开始运行..
[2017-09-21 15:38:40.134406] INFO: bigquant: 命中缓存
[2017-09-21 15:38:40.135173] INFO: bigquant: general_feature_extractor.v5 运行完成[0.003653s].
[2017-09-21 15:38:40.141358] INFO: bigquant: derived_feature_extractor.v1 开始运行..
[2017-09-21 15:38:40.143678] INFO: bigquant: 命中缓存
[2017-09-21 15:38:40.144531] INFO: bigquant: derived_feature_extractor.v1 运行完成[0.00317s].
[2017-09-21 15:38:40.151428] INFO: bigquant: transform.v2 开始运行..
[2017-09-21 15:38:40.153590] INFO: bigquant: 命中缓存
[2017-09-21 15:38:40.154663] INFO: bigquant: transform.v2 运行完成[0.003229s].
[2017-09-21 15:38:40.161230] INFO: bigquant: join.v2 开始运行..
[2017-09-21 15:38:40.163791] INFO: bigquant: 命中缓存
[2017-09-21 15:38:40.164965] INFO: bigquant: join.v2 运行完成[0.003706s].
[2017-09-21 15:38:40.173737] INFO: bigquant: stock_ranker_train.v4 开始运行..
[2017-09-21 15:38:40.175964] INFO: bigquant: 命中缓存
[2017-09-21 15:38:40.176797] INFO: bigquant: stock_ranker_train.v4 运行完成[0.003082s].
[2017-09-21 15:38:40.202922] INFO: bigquant: backtest.v7 开始运行..
[2017-09-21 15:38:40.243461] INFO: bigquant: general_feature_extractor.v5 开始运行..
[2017-09-21 15:38:40.246282] INFO: bigquant: 命中缓存
[2017-09-21 15:38:40.247316] INFO: bigquant: general_feature_extractor.v5 运行完成[0.00387s].
[2017-09-21 15:38:40.254287] INFO: bigquant: derived_feature_extractor.v1 开始运行..
[2017-09-21 15:38:40.256391] INFO: bigquant: 命中缓存
[2017-09-21 15:38:40.257392] INFO: bigquant: derived_feature_extractor.v1 运行完成[0.003096s].
[2017-09-21 15:38:40.263787] INFO: bigquant: transform.v2 开始运行..
[2017-09-21 15:38:40.265700] INFO: bigquant: 命中缓存
[2017-09-21 15:38:40.266502] INFO: bigquant: transform.v2 运行完成[0.002714s].
[2017-09-21 15:38:40.273246] INFO: bigquant: stock_ranker_predict.v5 开始运行..
[2017-09-21 15:38:40.280099] INFO: bigquant: 命中缓存
[2017-09-21 15:38:40.281106] INFO: bigquant: stock_ranker_predict.v5 运行完成[0.007848s].
[2017-09-21 15:39:19.052171] INFO: Performance: Simulated 413 trading days out of 413.
[2017-09-21 15:39:19.053297] INFO: Performance: first open: 2016-01-04 14:30:00+00:00
[2017-09-21 15:39:19.054129] INFO: Performance: last close: 2017-09-08 19:00:00+00:00
  • 收益率90.94%
  • 年化收益率48.39%
  • 基准收益率2.55%
  • 阿尔法0.46
  • 贝塔0.63
  • 夏普比率1.63
  • 收益波动率27.03%
  • 信息比率1.85
  • 最大回撤18.31%
[2017-09-21 15:39:20.579415] INFO: bigquant: backtest.v7 运行完成[40.376482s].

(iQuant) #2

由于特征里需要使用多天数据,因此需要回溯多天的数据来满足回测条件,可以修改原文中的代码如下: