根据自己需求自主取消订单

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

(iQuant) #1

之前收到用户这样的需求:如果不想使用平台自带的取消订单逻辑,而是采取自己的订单取消逻辑如何实现呢?

比如,昨天的生成的一个订单,今天根据股票停牌状态自主选择,在成交之前取消它。

解决办法是,自己手动取消订单,将M.trade函数的auto_cancel_non_tradable_orders 设置为False。具体代码见下面:

克隆策略
In [21]:
instruments = ['600556.SHA']  
start_date = '2016-06-28'  
end_date = '2016-07-18'
In [45]:
def initialize(context):
    pass

def before_trading_start(context,data):
    
    dt = data.current_dt.strftime('%Y-%m-%d')
    k = instruments[0] # 标的为字符串格式
    sid = context.symbol(k) # 将标的转化为equity格式
    
    for open_orders in context.get_open_orders().values():
        for order in open_orders:
            sid = order.sid
            price = data.current(sid, 'price')
            if math.isnan(price) or data.can_trade(sid) == False:  # 股票停牌:没有价格数据,撤销订单以释放资金占用
                context.cancel_order(order)
                print(dt,'在before_trading_start取消订单:',sid)
                continue
    
def handle_data(context, data):
    
    dt = data.current_dt.strftime('%Y-%m-%d')
    k = instruments[0] # 标的为字符串格式
    sid = context.symbol(k) # 将标的转化为equity格式
    
    if dt == '2016-07-13':
        order(sid,100)
        print(dt,'生成一个买入100股的虚拟订单')
    
    print(dt, '检查下是否有订单: ', context.get_open_orders())
    
In [46]:
m=M.trade.v2(
    instruments=instruments,
    start_date=start_date,
    end_date=end_date,
    initialize=initialize,
    handle_data=handle_data,
    before_trading_start=before_trading_start,
    order_price_field_buy='open',
    order_price_field_sell='open',
    capital_base=10000,
    auto_cancel_non_tradable_orders=False,
    )
[2017-09-29 17:45:41.445168] INFO: bigquant: backtest.v7 开始运行..
2016-07-13 生成一个买入100股的虚拟订单
2016-07-13 检查下是否有订单:  {Equity(0, symbol='600556.SHA', asset_name='', exchange='TEST', start_date=Timestamp('2015-07-15 00:00:00+0000', tz='UTC'), end_date=Timestamp('2016-07-14 00:00:00+0000', tz='UTC'), first_traded=None, auto_close_date=None, exchange_full='TEST FULL'): [Event({'sid': Equity(0, symbol='600556.SHA', asset_name='', exchange='TEST', start_date=Timestamp('2015-07-15 00:00:00+0000', tz='UTC'), end_date=Timestamp('2016-07-14 00:00:00+0000', tz='UTC'), first_traded=None, auto_close_date=None, exchange_full='TEST FULL'), 'id': '52df012891f543c9beb8df227b42e97f', 'stop': None, 'created': Timestamp('2016-07-13 19:00:00+0000', tz='UTC'), 'limit_reached': False, 'commission': 0, 'dt': Timestamp('2016-07-13 19:00:00+0000', tz='UTC'), 'status': 0, 'filled': 0, 'limit': None, 'reason': None, 'amount': 100, 'stop_reached': False})]}
2016-07-14 在before_trading_start取消订单: Equity(0 [600556.SHA])
2016-07-14 检查下是否有订单:  {}
[2017-09-29 17:45:42.881725] INFO: Performance: Simulated 2 trading days out of 2.
[2017-09-29 17:45:42.884973] INFO: Performance: first open: 2016-07-13 13:30:00+00:00
[2017-09-29 17:45:42.885883] INFO: Performance: last close: 2016-07-14 19:00:00+00:00
  • 收益率0.0%
  • 年化收益率0.0%
  • 基准收益率0.11%
  • 阿尔法-0.03
  • 贝塔0.0
  • 夏普比率n/a
  • 收益波动率0.0%
  • 信息比率-3.86
  • 最大回撤0.0%
[2017-09-29 17:45:43.274339] INFO: bigquant: backtest.v7 运行完成[1.829152s].