回测中的下单日期和交易详情里的下单日期不一致

策略分享
用户成长系列
标签: #<Tag:0x00007f73d18a1e88> #<Tag:0x00007f73d18a1d48>

(kiqi) #1

回测时下单日期是 2016-01-05 ,为什么在交易详情中显示的是2016-01-06 呢?

克隆策略
In [10]:
# 1. 策略基本参数

# 证券池:这里使用所有股票
instruments = D.instruments()
# 起始日期
start_date = '2016-01-01'
# 结束日期
end_date = '2017-02-28'
# 初始资金
capital_base = 100000
# 策略比较参考标准,以沪深300为例
benchmark = '000300.INDX'
# 调仓周期(多少个交易日调仓)
rebalance_period = 22
# 每轮调仓买入的股票数量
stock_num = 30


# 3. 策略主体函数
# 初始化虚拟账户状态,只在第一个交易日运行
def initialize(context):
    # 设置手续费,买入时万3,卖出是千分之1.3,不足5元以5元计
    context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
    context.holds = {}

# 策略交易逻辑,每个交易日运行一次
def handle_data(context,data):
    today = data.current_dt.strftime('%Y-%m-%d') # 交易日期
#     positions = context.portfolio.positions
#     for equity, position in positions.items():
#         if position.amount <= 0:
#             continue
#         _hold_days = context.trading_day_index - context.holds[equity.symbol]
#         #conf.hold_days # 持仓期是否超过10天
#         if  _hold_days >= 5:
# #             print(today,equity.symbol,_hold_days,context.holds[equity.symbol])
#                 # 超过30天就卖出股票                
#             context.order_target(context.symbol(equity.symbol), 0)
    mycode = "601166.SHA"
    sid = context.symbol(mycode)
    cur_position = context.portfolio.positions[sid].amount
    if cur_position <1000 :
        if data.can_trade(sid):
            order_object = order_lots(sid,context.trading_day_index)
            hist = data.current(symbols(mycode), ['open'])
            print(mycode,hist['open'][symbol(mycode)],today,get_order(order_object))
            context.holds[mycode] = context.trading_day_index
        

# 4. 策略回测:https://bigquant.com/docs/module_trade.html
m = M.trade.v2(
    instruments=instruments,
    start_date=start_date,
    end_date=end_date,
    initialize=initialize,
    handle_data=handle_data,
    # 买入订单以开盘价成交
    order_price_field_buy='open',
    # 卖出订单以开盘价成交
    order_price_field_sell='open',
    capital_base=capital_base,
    benchmark=benchmark,
)
[2017-08-30 13:15:11.637620] INFO: bigquant: backtest.v7 start ..
601166.SHA 60.1722450256 2016-01-04 None
601166.SHA 57.8077049255 2016-01-05 Event({'stop': None, 'commission': 0, 'stop_reached': False, 'limit': None, 'sid': Equity(946, symbol='601166.SHA', asset_name='', exchange='TEST', start_date=Timestamp('2015-01-05 00:00:00+0000', tz='UTC'), end_date=Timestamp('2017-02-28 00:00:00+0000', tz='UTC'), first_traded=None, auto_close_date=None, exchange_full='TEST FULL'), 'reason': None, 'amount': 28.335322716527656, 'status': 0, 'id': 'dc83f05a12544fc4bd042290bc6bc745', 'filled': 0, 'dt': Timestamp('2016-01-05 20:00:00+0000', tz='UTC'), 'limit_reached': False, 'created': Timestamp('2016-01-05 20:00:00+0000', tz='UTC')})
601166.SHA 58.5841217041 2016-01-06 Event({'stop': None, 'commission': 0, 'stop_reached': False, 'limit': None, 'sid': Equity(946, symbol='601166.SHA', asset_name='', exchange='TEST', start_date=Timestamp('2015-01-05 00:00:00+0000', tz='UTC'), end_date=Timestamp('2017-02-28 00:00:00+0000', tz='UTC'), first_traded=None, auto_close_date=None, exchange_full='TEST FULL'), 'reason': None, 'amount': 56.670643327943424, 'status': 0, 'id': '3de5b907974a4b3197f6a6edf5420c17', 'filled': 0, 'dt': Timestamp('2016-01-06 20:00:00+0000', tz='UTC'), 'limit_reached': False, 'created': Timestamp('2016-01-06 20:00:00+0000', tz='UTC')})
601166.SHA 57.9841651917 2016-01-07 Event({'stop': None, 'commission': 0, 'stop_reached': False, 'limit': None, 'sid': Equity(946, symbol='601166.SHA', asset_name='', exchange='TEST', start_date=Timestamp('2015-01-05 00:00:00+0000', tz='UTC'), end_date=Timestamp('2017-02-28 00:00:00+0000', tz='UTC'), first_traded=None, auto_close_date=None, exchange_full='TEST FULL'), 'reason': None, 'amount': 85.005966896584937, 'status': 0, 'id': 'd7f6d78d96ed42a3a7f5e40be376488e', 'filled': 0, 'dt': Timestamp('2016-01-07 20:00:00+0000', tz='UTC'), 'limit_reached': False, 'created': Timestamp('2016-01-07 20:00:00+0000', tz='UTC')})
601166.SHA 57.6665382385 2016-01-08 Event({'stop': None, 'commission': 0, 'stop_reached': False, 'limit': None, 'sid': Equity(946, symbol='601166.SHA', asset_name='', exchange='TEST', start_date=Timestamp('2015-01-05 00:00:00+0000', tz='UTC'), end_date=Timestamp('2017-02-28 00:00:00+0000', tz='UTC'), first_traded=None, auto_close_date=None, exchange_full='TEST FULL'), 'reason': None, 'amount': 113.34129224379683, 'status': 0, 'id': '1bd29874e7f1407082c3490e36aa2f2b', 'filled': 0, 'dt': Timestamp('2016-01-08 20:00:00+0000', tz='UTC'), 'limit_reached': False, 'created': Timestamp('2016-01-08 20:00:00+0000', tz='UTC')})
601166.SHA 57.3842048645 2016-01-11 Event({'stop': None, 'commission': 0, 'stop_reached': False, 'limit': None, 'sid': Equity(946, symbol='601166.SHA', asset_name='', exchange='TEST', start_date=Timestamp('2015-01-05 00:00:00+0000', tz='UTC'), end_date=Timestamp('2017-02-28 00:00:00+0000', tz='UTC'), first_traded=None, auto_close_date=None, exchange_full='TEST FULL'), 'reason': None, 'amount': 141.67661089484236, 'status': 0, 'id': 'e1f56cc7536c4347abcf106131ce02ad', 'filled': 0, 'dt': Timestamp('2016-01-11 20:00:00+0000', tz='UTC'), 'limit_reached': False, 'created': Timestamp('2016-01-11 20:00:00+0000', tz='UTC')})
601166.SHA 55.93724823 2016-01-12 Event({'stop': None, 'commission': 0, 'stop_reached': False, 'limit': None, 'sid': Equity(946, symbol='601166.SHA', asset_name='', exchange='TEST', start_date=Timestamp('2015-01-05 00:00:00+0000', tz='UTC'), end_date=Timestamp('2017-02-28 00:00:00+0000', tz='UTC'), first_traded=None, auto_close_date=None, exchange_full='TEST FULL'), 'reason': None, 'amount': 170.01193898866416, 'status': 0, 'id': 'd5829f6a7ad44a46b632e505747782a5', 'filled': 0, 'dt': Timestamp('2016-01-12 20:00:00+0000', tz='UTC'), 'limit_reached': False, 'created': Timestamp('2016-01-12 20:00:00+0000', tz='UTC')})
601166.SHA 56.2901649475 2016-01-13 Event({'stop': None, 'commission': 0, 'stop_reached': False, 'limit': None, 'sid': Equity(946, symbol='601166.SHA', asset_name='', exchange='TEST', start_date=Timestamp('2015-01-05 00:00:00+0000', tz='UTC'), end_date=Timestamp('2017-02-28 00:00:00+0000', tz='UTC'), first_traded=None, auto_close_date=None, exchange_full='TEST FULL'), 'reason': None, 'amount': 198.34725637635162, 'status': 0, 'id': '7e2916cfc21e44dc847f98e332362d37', 'filled': 0, 'dt': Timestamp('2016-01-13 20:00:00+0000', tz='UTC'), 'limit_reached': False, 'created': Timestamp('2016-01-13 20:00:00+0000', tz='UTC')})
601166.SHA 54.3491249084 2016-01-14 Event({'stop': None, 'commission': 0, 'stop_reached': False, 'limit': None, 'sid': Equity(946, symbol='601166.SHA', asset_name='', exchange='TEST', start_date=Timestamp('2015-01-05 00:00:00+0000', tz='UTC'), end_date=Timestamp('2017-02-28 00:00:00+0000', tz='UTC'), first_traded=None, auto_close_date=None, exchange_full='TEST FULL'), 'reason': None, 'amount': 226.6825789746124, 'status': 0, 'id': 'dffc36e7cbe942439c193cbcfe118e57', 'filled': 0, 'dt': Timestamp('2016-01-14 20:00:00+0000', tz='UTC'), 'limit_reached': False, 'created': Timestamp('2016-01-14 20:00:00+0000', tz='UTC')})
[2017-08-30 13:15:32.072238] INFO: Performance: Simulated 280 trading days out of 280.
[2017-08-30 13:15:32.073329] INFO: Performance: first open: 2016-01-04 14:30:00+00:00
[2017-08-30 13:15:32.074220] INFO: Performance: last close: 2017-02-28 20:00:00+00:00
  • 收益率5.53%
  • 年化收益率4.96%
  • 基准收益率-7.46%
  • 阿尔法0.03
  • 贝塔0.22
  • 夏普比率0.11
  • 收益波动率8.18%
  • 信息比率0.66
  • 最大回撤6.39%
[2017-08-30 13:15:32.884635] INFO: bigquant: backtest.v7 end [21.246998s].

(kiqi) #2

@iQuant


(iQuant) #3

您好,这里是根据 2016-01-05 那一天的数据,来生成 2016-01-06 的交易。即策略在 01-05 收盘后运行,生成第二日的交易信号,01-06开盘的时候执行交易。


(kiqi) #4

好的,谢谢