def on_order_close_canceled(self, order: OrderData):
    '委托平仓单撤销'
    self.write_log(u'报单更新 => 委托平仓 => 撤销:{}'.format(order.__dict__))
    if (order.vt_orderid not in self.active_orders):
        self.write_error(u'{}不在未完成的委托单中:{}。'.format(order.vt_orderid, self.active_orders))
        return
    if (not self.trading):
        self.write_error(u'当前不允许交易')
        return
    old_order = self.active_orders[order.vt_orderid]
    self.write_log(u'报单更新 => {} 未完成订单信息:{}'.format(order.vt_orderid, old_order))
    old_order['traded'] = order.traded
    order_vt_symbol = copy(old_order['vt_symbol'])
    order_volume = (old_order['volume'] - old_order['traded'])
    order_price = old_order['price']
    order_type = old_order.get('order_type', OrderType.LIMIT)
    order_retry = old_order.get('retry', 0)
    grid = old_order.get('grid', None)
    if grid:
        if (order.vt_orderid in grid.order_ids):
            grid.order_ids.remove(order.vt_orderid)
    if (order_volume <= 0):
        msg = u'{} {}{}重新平仓数量为{}，不再平仓'.format(self.strategy_name, order.vt_orderid, order_vt_symbol, order_volume)
        self.write_error(msg)
        self.send_wechat(msg)
        self.write_log(u'活动订单移除:{}'.format(order.vt_orderid))
        self.active_orders.pop(order.vt_orderid, None)
        return
    if (order_retry > 20):
        msg = u'{} 平仓撤单 {}/{}手， 重试平仓次数{}>20'.format(self.strategy_name, order_vt_symbol, order_volume, order_retry)
        self.write_error(msg)
        self.send_wechat(msg)
        if (not grid.order_ids):
            grid.order_status = False
            self.gt.save()
            self.write_log(u'更新网格=>{}'.format(grid.__dict__))
        self.write_log(u'移除活动订单:{}'.format(order.vt_orderid))
        self.active_orders.pop(order.vt_orderid, None)
        return
    order_retry += 1
    if ((old_order['direction'] == Direction.LONG) and (order_type == OrderType.FAK)):
        self.write_log(u'移除活动订单:{}'.format(order.vt_orderid))
        self.active_orders.pop(order.vt_orderid, None)
        if (order.traded > 0):
            old_traded_volume = grid.traded_volume
            grid.traded_volume += order.traded
            self.write_log((f'{grid.direction.value}单部分{order.offset}仓，' + f'网格volume:{grid.volume}, traded_volume:{old_traded_volume}=>{grid.traded_volume}'))
        self.write_log(u'FAK模式，需要重新发送cover委托.grid:{}'.format(grid.__dict__))
        cover_tick = self.tick_dict.get(order_vt_symbol, self.cur_mi_tick)
        cover_price = (max(cover_tick.ask_price_1, cover_tick.last_price, order_price) + self.price_tick)
        if ((cover_tick.limit_up > 0) and (cover_price > cover_tick.limit_up)):
            cover_price = cover_tick.limit_up
        if self.is_upper_limit(order_vt_symbol):
            self.write_log(u'{}涨停，不做cover'.format(order_vt_symbol))
            return
        pos = self.cta_engine.get_position_holding(vt_symbol=order_vt_symbol)
        if (pos is None):
            self.write_error(f'{self.strategy_name}无法获取{order_vt_symbol}的持仓信息，无法平仓')
            return
        if (pos.short_pos < order_volume):
            self.write_error(f'{self.strategy_name}{order_vt_symbol}的持仓空单{pos.short_pos}不满足平仓{order_volume}要求，无法平仓')
            return
        vt_orderids = self.cover(price=cover_price, volume=order_volume, vt_symbol=order_vt_symbol, order_type=OrderType.FAK, order_time=self.cur_datetime, grid=grid)
        if (not vt_orderids):
            self.write_error(u'重新提交{} {}手平空单{}失败'.format(order_vt_symbol, order_volume, cover_price))
            return
        for vt_orderid in vt_orderids:
            info = self.active_orders.get(vt_orderid)
            info.update({'retry': order_retry})
        self.gt.save()
    elif ((old_order['direction'] == Direction.SHORT) and (order_type == OrderType.FAK)):
        self.write_log(u'移除活动订单:{}'.format(order.vt_orderid))
        self.active_orders.pop(order.vt_orderid, None)
        if (order.traded > 0):
            old_traded_volume = grid.traded_volume
            grid.traded_volume += order.traded
            self.write_log((f'{grid.direction.value}单部分{order.offset}仓，' + f'网格volume:{grid.volume}, traded_volume:{old_traded_volume}=>{grid.traded_volume}'))
        self.write_log(u'FAK模式，需要重新发送sell委托.grid:{}'.format(grid.__dict__))
        sell_tick = self.tick_dict.get(order_vt_symbol, self.cur_mi_tick)
        sell_price = (min(sell_tick.bid_price_1, sell_tick.last_price, order_price) - self.price_tick)
        if ((sell_tick.limit_down > 0) and (sell_price < sell_tick.limit_down)):
            sell_price = sell_tick.limit_down
        if self.is_lower_limit(order_vt_symbol):
            self.write_log(u'{}涨停，不做sell'.format(order_vt_symbol))
            return
        pos = self.cta_engine.get_position_holding(vt_symbol=order_vt_symbol)
        if (pos is None):
            self.write_error(f'{self.strategy_name}无法获取{order_vt_symbol}的持仓信息，无法平仓')
            return
        if (pos.long_pos < order_volume):
            self.write_error(f'{self.strategy_name}{order_vt_symbol}的持仓多单{pos.long_pos}不满足平仓{order_volume}要求，无法平仓')
            return
        vt_orderids = self.sell(price=sell_price, volume=order_volume, vt_symbol=order_vt_symbol, order_type=OrderType.FAK, order_time=self.cur_datetime, grid=grid)
        if (not vt_orderids):
            self.write_error(u'重新提交{} {}手平多单{}失败'.format(order_vt_symbol, order_volume, sell_price))
            return
        for vt_orderid in vt_orderids:
            info = self.active_orders.get(vt_orderid)
            info.update({'retry': order_retry})
        self.gt.save()
    else:
        pre_status = old_order.get('status', Status.NOTTRADED)
        old_order.update({'status': Status.CANCELLED})
        self.write_log(u'委托单状态:{}=>{}'.format(pre_status, old_order.get('status')))
        if grid:
            if (order.traded > 0):
                old_traded_volume = grid.traded_volume
                grid.traded_volume += order.traded
                self.write_log((f'{grid.direction.value}单部分{order.offset}仓，' + f'网格volume:{grid.volume}, traded_volume:{old_traded_volume}=>{grid.traded_volume}'))
            if (order.vt_orderid in grid.order_ids):
                grid.order_ids.remove(order.vt_orderid)
            if (len(grid.order_ids) == 0):
                grid.order_status = False
            self.gt.save()
        self.active_orders.update({order.vt_orderid: old_order})
    self.display_grids()
委托平仓单撤销
def on_order_close_canceled(self, order: OrderData):
    
    self.write_log(u'报单更新 => 委托平仓 => 撤销:{}'.format(order.__dict__))
    if (order.vt_orderid not in self.active_orders):
        self.write_error(u'{}不在未完成的委托单中:{}。'.format(order.vt_orderid, self.active_orders))
        return
    if (not self.trading):
        self.write_error(u'当前不允许交易')
        return
    old_order = self.active_orders[order.vt_orderid]
    self.write_log(u'报单更新 => {} 未完成订单信息:{}'.format(order.vt_orderid, old_order))
    old_order['traded'] = order.traded
    order_vt_symbol = copy(old_order['vt_symbol'])
    order_volume = (old_order['volume'] - old_order['traded'])
    order_price = old_order['price']
    order_type = old_order.get('order_type', OrderType.LIMIT)
    order_retry = old_order.get('retry', 0)
    grid = old_order.get('grid', None)
    if grid:
        if (order.vt_orderid in grid.order_ids):
            grid.order_ids.remove(order.vt_orderid)
    if (order_volume <= 0):
        msg = u'{} {}{}重新平仓数量为{}，不再平仓'.format(self.strategy_name, order.vt_orderid, order_vt_symbol, order_volume)
        self.write_error(msg)
        self.send_wechat(msg)
        self.write_log(u'活动订单移除:{}'.format(order.vt_orderid))
        self.active_orders.pop(order.vt_orderid, None)
        return
    if (order_retry > 20):
        msg = u'{} 平仓撤单 {}/{}手， 重试平仓次数{}>20'.format(self.strategy_name, order_vt_symbol, order_volume, order_retry)
        self.write_error(msg)
        self.send_wechat(msg)
        if (not grid.order_ids):
            grid.order_status = False
            self.gt.save()
            self.write_log(u'更新网格=>{}'.format(grid.__dict__))
        self.write_log(u'移除活动订单:{}'.format(order.vt_orderid))
        self.active_orders.pop(order.vt_orderid, None)
        return
    order_retry += 1
    if ((old_order['direction'] == Direction.LONG) and (order_type == OrderType.FAK)):
        self.write_log(u'移除活动订单:{}'.format(order.vt_orderid))
        self.active_orders.pop(order.vt_orderid, None)
        if (order.traded > 0):
            old_traded_volume = grid.traded_volume
            grid.traded_volume += order.traded
            self.write_log((f'{grid.direction.value}单部分{order.offset}仓，' + f'网格volume:{grid.volume}, traded_volume:{old_traded_volume}=>{grid.traded_volume}'))
        self.write_log(u'FAK模式，需要重新发送cover委托.grid:{}'.format(grid.__dict__))
        cover_tick = self.tick_dict.get(order_vt_symbol, self.cur_mi_tick)
        cover_price = (max(cover_tick.ask_price_1, cover_tick.last_price, order_price) + self.price_tick)
        if ((cover_tick.limit_up > 0) and (cover_price > cover_tick.limit_up)):
            cover_price = cover_tick.limit_up
        if self.is_upper_limit(order_vt_symbol):
            self.write_log(u'{}涨停，不做cover'.format(order_vt_symbol))
            return
        pos = self.cta_engine.get_position_holding(vt_symbol=order_vt_symbol)
        if (pos is None):
            self.write_error(f'{self.strategy_name}无法获取{order_vt_symbol}的持仓信息，无法平仓')
            return
        if (pos.short_pos < order_volume):
            self.write_error(f'{self.strategy_name}{order_vt_symbol}的持仓空单{pos.short_pos}不满足平仓{order_volume}要求，无法平仓')
            return
        vt_orderids = self.cover(price=cover_price, volume=order_volume, vt_symbol=order_vt_symbol, order_type=OrderType.FAK, order_time=self.cur_datetime, grid=grid)
        if (not vt_orderids):
            self.write_error(u'重新提交{} {}手平空单{}失败'.format(order_vt_symbol, order_volume, cover_price))
            return
        for vt_orderid in vt_orderids:
            info = self.active_orders.get(vt_orderid)
            info.update({'retry': order_retry})
        self.gt.save()
    elif ((old_order['direction'] == Direction.SHORT) and (order_type == OrderType.FAK)):
        self.write_log(u'移除活动订单:{}'.format(order.vt_orderid))
        self.active_orders.pop(order.vt_orderid, None)
        if (order.traded > 0):
            old_traded_volume = grid.traded_volume
            grid.traded_volume += order.traded
            self.write_log((f'{grid.direction.value}单部分{order.offset}仓，' + f'网格volume:{grid.volume}, traded_volume:{old_traded_volume}=>{grid.traded_volume}'))
        self.write_log(u'FAK模式，需要重新发送sell委托.grid:{}'.format(grid.__dict__))
        sell_tick = self.tick_dict.get(order_vt_symbol, self.cur_mi_tick)
        sell_price = (min(sell_tick.bid_price_1, sell_tick.last_price, order_price) - self.price_tick)
        if ((sell_tick.limit_down > 0) and (sell_price < sell_tick.limit_down)):
            sell_price = sell_tick.limit_down
        if self.is_lower_limit(order_vt_symbol):
            self.write_log(u'{}涨停，不做sell'.format(order_vt_symbol))
            return
        pos = self.cta_engine.get_position_holding(vt_symbol=order_vt_symbol)
        if (pos is None):
            self.write_error(f'{self.strategy_name}无法获取{order_vt_symbol}的持仓信息，无法平仓')
            return
        if (pos.long_pos < order_volume):
            self.write_error(f'{self.strategy_name}{order_vt_symbol}的持仓多单{pos.long_pos}不满足平仓{order_volume}要求，无法平仓')
            return
        vt_orderids = self.sell(price=sell_price, volume=order_volume, vt_symbol=order_vt_symbol, order_type=OrderType.FAK, order_time=self.cur_datetime, grid=grid)
        if (not vt_orderids):
            self.write_error(u'重新提交{} {}手平多单{}失败'.format(order_vt_symbol, order_volume, sell_price))
            return
        for vt_orderid in vt_orderids:
            info = self.active_orders.get(vt_orderid)
            info.update({'retry': order_retry})
        self.gt.save()
    else:
        pre_status = old_order.get('status', Status.NOTTRADED)
        old_order.update({'status': Status.CANCELLED})
        self.write_log(u'委托单状态:{}=>{}'.format(pre_status, old_order.get('status')))
        if grid:
            if (order.traded > 0):
                old_traded_volume = grid.traded_volume
                grid.traded_volume += order.traded
                self.write_log((f'{grid.direction.value}单部分{order.offset}仓，' + f'网格volume:{grid.volume}, traded_volume:{old_traded_volume}=>{grid.traded_volume}'))
            if (order.vt_orderid in grid.order_ids):
                grid.order_ids.remove(order.vt_orderid)
            if (len(grid.order_ids) == 0):
                grid.order_status = False
            self.gt.save()
        self.active_orders.update({order.vt_orderid: old_order})
    self.display_grids()