cannot subtract DatetimeIndex and Int64Index

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

(faiy0000) #1
克隆策略
In [6]:
# 基础参数配置
class conf:
    start_date = '2010-01-01'
    end_date='2018-01-01'
    # split_date 之前的数据用于训练,之后的数据用作效果评估
    split_date = '2015-01-01'
    # D.instruments: https://bigquant.com/docs/data_instruments.html
    instruments = D.instruments(start_date, end_date)
#     df = D.history_data(D.instruments(start_date, end_date),start_date,end_date,fields=['in_csi300'])
#     instruments = list(set(df[df['in_csi300']==1]['instrument']))

    # 机器学习目标标注函数
    # 如下标注函数等价于 min(max((持有期间的收益 * 100), -20), 20) + 20 (后面的M.fast_auto_labeler会做取整操作)
    # 说明:max/min这里将标注分数限定在区间[-20, 20],+20将分数变为非负数 (StockRanker要求标注分数非负整数)
#     label_expr = ['return* 30', 'where(label > {0}, {0}, where(label < -{0}, -{0}, label)) + {0}'.format(3)]
#     label_expr = ['return* 30', 'where(label > {0}, {0}, where(label < -{0}, -{0}, label)) + {0}'.format(3)]
    label_expr = ['return * 100', 'where(label > {0}, {0}, where(label < -{0}, -{0}, label)) + {0}'.format(20)]
    # 持有天数,用于计算label_expr中的return值(收益)
    hold_days = 10
    features = [
        'ta_sma_10_0/ta_sma_20_0',
        'ta_sma_20_0/ta_sma_30_0',
        'ta_sma_30_0/ta_sma_60_0',
        'ta_atr_14_0',
        'ta_atr_28_0',
#         'ta_cci_14_0',

        'ta_rsi_14_0',
        'ta_rsi_28_0',
        'market_cap_float_0',
        'pe_ttm_0',
        'pb_lf_0',
#         'ta_sma_5_0/ta_sma_10_0',
#         'ta_sma_10_0/ta_sma_20_0',
#         'ta_atr_14_0',
#         'ta_rsi_14_0',
#         'mf_net_pct_xl_0',  #add by lj 
#         'mf_net_pct_l_0',
#         'deal_number_0',
#         'volume_0',
#         'volume_1',
#         'volume_2',
#         'volume_3',
#         'volume_4',
#         'volume_5',
#         'daily_return_0',
#         'daily_return_1',
#         'daily_return_2',
#         'daily_return_3',
#         'daily_return_4',
#         'daily_return_5',
#         'daily_return_6',
#         'avg_amount_0',
#         'avg_amount_1',
#         'avg_amount_2',
#         'avg_amount_3',
#         'avg_amount_4',
#         'avg_amount_5',
#         'turn_0',
#         'turn_1',
#         'turn_2',
#         'turn_3',
#         'turn_4',
#         'turn_5',
#         'mf_net_amount_main_0',
#         'rank_amount_0',
#         'rank_avg_amount_0',
#         'rank_avg_amount_3',
#         'avg_mf_net_amount_3',
#         'avg_turn_0',
#         'avg_turn_3',
#         'pe_ttm_0',
#         'pb_lf_0',
#         'rank_market_cap_float_0',
#         'market_cap_float_0',
    ]


# 给数据做标注:给每一行数据(样本)打分,一般分数越高表示越好
m1 = M.fast_auto_labeler.v6(
    instruments=conf.instruments, start_date=conf.start_date, end_date=conf.end_date,
    label_expr=conf.label_expr, hold_days=conf.hold_days,
    benchmark='000300.SHA', sell_at='close', buy_at='open', is_regression=False)
# 计算特征数据
m2 = M.general_feature_extractor.v5(
    instruments=conf.instruments, start_date=conf.start_date, end_date=conf.end_date,
    features=conf.features)
# 数据预处理:缺失数据处理,数据规范化,T.get_stock_ranker_default_transforms为StockRanker模型做数据预处理
m3=M.add_columns.v1(data=m2.data, eval_list=conf.features)
m4 = M.transform.v2(
    data=m3.data, transforms=None,
    drop_null=True, astype='float32', except_columns=['date', 'instrument'],
    clip_lower=0, clip_upper=200000000)
# 合并标注和特征数据
m5 = M.join.v2(data1=m4.data, data2=m1.data, on=['date', 'instrument'], sort=True)

# 训练数据集
m6_training = M.filter.v2(data=m5.data, expr='date < "%s"' % conf.split_date)
# 评估数据集
m6_evaluation = M.filter.v2(data=m5.data, expr='"%s" <= date' % conf.split_date)

m7 = M.random_forest_train.v2(training_ds=m6_training.data, features=conf.features,n_jobs=4)
[2018-07-19 11:01:35.663633] WARNING: bigquant: 此模块版本 M.fast_auto_labeler.v6 已不再维护。你仍然可以使用,但建议升级到最新版本:请更新到 fast_auto_labeler 最新版本
[2018-07-19 11:01:35.666543] INFO: bigquant: fast_auto_labeler.v6 开始运行..
[2018-07-19 11:01:35.691249] INFO: bigquant: 命中缓存
[2018-07-19 11:01:35.703160] INFO: bigquant: fast_auto_labeler.v6 运行完成[0.03661s].
[2018-07-19 11:01:35.754395] INFO: bigquant: general_feature_extractor.v5 开始运行..
[2018-07-19 11:01:35.760162] INFO: bigquant: 命中缓存
[2018-07-19 11:01:35.761227] INFO: bigquant: general_feature_extractor.v5 运行完成[0.006873s].
[2018-07-19 11:01:35.852344] INFO: bigquant: add_columns.v1 开始运行..
[2018-07-19 11:01:35.877069] INFO: bigquant: 命中缓存
[2018-07-19 11:01:35.878833] INFO: bigquant: add_columns.v1 运行完成[0.0265s].
[2018-07-19 11:01:35.952435] INFO: bigquant: transform.v2 开始运行..
[2018-07-19 11:01:35.958282] INFO: bigquant: 命中缓存
[2018-07-19 11:01:35.959421] INFO: bigquant: transform.v2 运行完成[0.007s].
[2018-07-19 11:01:36.058467] INFO: bigquant: join.v2 开始运行..
[2018-07-19 11:01:36.101473] INFO: bigquant: 命中缓存
[2018-07-19 11:01:36.102632] INFO: bigquant: join.v2 运行完成[0.044184s].
[2018-07-19 11:01:36.150695] INFO: bigquant: filter.v2 开始运行..
[2018-07-19 11:01:36.167436] INFO: bigquant: 命中缓存
[2018-07-19 11:01:36.168649] INFO: bigquant: filter.v2 运行完成[0.017968s].
[2018-07-19 11:01:36.171222] INFO: bigquant: filter.v2 开始运行..
[2018-07-19 11:01:36.176588] INFO: bigquant: 命中缓存
[2018-07-19 11:01:36.177662] INFO: bigquant: filter.v2 运行完成[0.00643s].
[2018-07-19 11:01:36.252067] INFO: bigquant: random_forest_train.v2 开始运行..
[2018-07-19 11:01:36.260712] INFO: bigquant: 命中缓存
[2018-07-19 11:01:36.262068] INFO: bigquant: random_forest_train.v2 运行完成[0.010001s].
In [9]:
stock_num=10
print (stock_num)

# 3. 策略主体函数
# 初始化虚拟账户状态,只在第一个交易日运行
def initialize(context):
    # 设置手续费,买入时万3,卖出是千分之1.3,不足5元以5元计
    context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
    m8 = M.random_forest_predict.v1(model=context.options['model'],data=m6_evaluation.data)
    context.pred_df = m8.predictions.read_df()
    context.pred_df = context.pred_df.groupby('date').apply(lambda x:x.sort_values('pred_label',ascending=False))

# 策略交易逻辑,每个交易日运行一次
def handle_data(context,data):
    today = data.current_dt
    today_str=str(today.date())
    equities = {e.symbol: p for e, p in context.portfolio.positions.items() if p.amount>0}
    # 调仓:卖出所有持有股票
    for instrument in equities:
        # 停牌的股票,将不能卖出,将在下一个调仓期处理
        if data.can_trade(context.symbol(instrument)) and today-equities[instrument].last_sale_date>=datetime.timedelta(context.options['rebalance_period']):
            context.order_target_percent(context.symbol(instrument), 0)
#             print ("****")
#             print (today)
#             print (equities[instrument].last_sale_date)
#             print (datetime.timedelta(context.options['rebalance_period']))
#             print ("----")
    # 调仓:买入新的股票
    if today_str not in context.pred_df.index:
        return
    instruments_to_buy = context.pred_df.ix[today_str].instrument
    if len(instruments_to_buy) == 0:
        return
    # 等量分配资金买入股票
    weight = 1.0 / stock_num
    can_buy_num = stock_num - len(equities)
    for instrument in instruments_to_buy:
        if can_buy_num>0 and data.can_trade(context.symbol(instrument)) and instrument not in equities:
            context.order_target_percent(context.symbol(instrument), weight)
            can_buy_num -= 1

# 4. 策略回测:https://bigquant.com/docs/module_trade.html
m = M.trade.v3(
    instruments=conf.instruments,
    m_deps = np.random.randn(),
    start_date=conf.split_date,
#     start_date='2015-01-01',
    end_date=conf.end_date,
#     end_date='2015-12-30',
#     start_date='2017-06-21',
#     end_date='2018-02-20',
    initialize=initialize,
    handle_data=handle_data,
    # 买入订单以开盘价成交
    order_price_field_buy='open',
    # 卖出订单以开盘价成交
    order_price_field_sell='close',
    capital_base=1000000,
    benchmark='000300.SHA',
    # 传入数据给回测模块,所有回测函数里用到的数据都要从这里传入,并通过 context.options 使用,否则可能会遇到缓存问题
#     options={'rebalance_period': conf.hold_days, 'model':m7.model}
    options={'rebalance_period': 3, 'model':m7.model}
)
10
[2018-07-19 11:10:16.052173] INFO: bigquant: backtest.v6 开始运行..
[2018-07-19 11:10:21.058369] INFO: algo: TradingAlgorithm V1.2.1
[2018-07-19 11:10:45.361114] INFO: bigquant: random_forest_predict.v1 开始运行..
[2018-07-19 11:10:45.508619] INFO: bigquant: 命中缓存
[2018-07-19 11:10:45.510051] INFO: bigquant: random_forest_predict.v1 运行完成[0.148984s].
[2018-07-19 11:11:04.725258] INFO: Performance: Simulated 732 trading days out of 732.
[2018-07-19 11:11:04.726787] INFO: Performance: first open: 2015-01-05 09:30:00+00:00
[2018-07-19 11:11:04.728766] INFO: Performance: last close: 2017-12-29 15:00:00+00:00
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-736e6dceff9b> in <module>()
     60     # 传入数据给回测模块,所有回测函数里用到的数据都要从这里传入,并通过 context.options 使用,否则可能会遇到缓存问题
     61 #     options={'rebalance_period': conf.hold_days, 'model':m7.model}
---> 62     options={'rebalance_period': 3, 'model':m7.model}
     63 )

TypeError: cannot subtract DatetimeIndex and Int64Index
In [ ]:
df=D.features('600000.SHA', start_date='2017-07-01', end_date=None, fields=['sh_holder_num_0'], groupped_by_instrument=False, frequency='daily')
# print (df.describe())
print (df.groupby('sh_holder_num_0').sum())

(达达) #2
  1. 策略回测:https://bigquant.com/docs/module_trade.html
    m = M.trade.v1改成v2可以跑