def tns_cancel_logic(self, dt, force=False, reopen=False):
    '撤单逻辑'
    if (len(self.active_orders) < 1):
        self.entrust = 0
        return
    canceled_ids = []
    for vt_orderid in list(self.active_orders.keys()):
        order_info = self.active_orders[vt_orderid]
        order_vt_symbol = order_info.get('vt_symbol', self.vt_symbol)
        order_time = order_info['order_time']
        order_volume = (order_info['volume'] - order_info['traded'])
        order_grid = order_info['grid']
        order_status = order_info.get('status', Status.NOTTRADED)
        order_type = order_info.get('order_type', OrderType.LIMIT)
        over_seconds = (dt - order_time).total_seconds()
        if ((order_status in [Status.NOTTRADED, Status.SUBMITTING]) and ((order_type == OrderType.LIMIT) or ('.SPD' in order_vt_symbol))):
            if ((over_seconds > self.cancel_seconds) or force):
                self.write_log(u'撤单逻辑 => 超时{}秒未成交，取消委托单：vt_orderid:{},order:{}'.format(over_seconds, vt_orderid, order_info))
                order_info.update({'status': Status.CANCELLING})
                self.active_orders.update({vt_orderid: order_info})
                ret = self.cancel_order(str(vt_orderid))
                if (not ret):
                    self.write_error(f'{self.strategy_name}撤单逻辑 => {order_vt_symbol}撤单失败')
            continue
        elif (order_status == Status.CANCELLED):
            self.write_log(u'撤单逻辑 => 委托单{}已成功撤单，将删除未完成订单{}'.format(vt_orderid, order_info))
            canceled_ids.append(vt_orderid)
            if reopen:
                if (order_info['offset'] == Offset.OPEN):
                    self.write_log(u'撤单逻辑 => 重新开仓')
                    if (order_info['direction'] == Direction.SHORT):
                        short_price = (self.cur_mi_price - self.price_tick)
                        if ((order_grid.volume != order_volume) and (order_volume > 0)):
                            self.write_log(u'网格volume:{},order_volume:{}不一致，修正'.format(order_grid.volume, order_volume))
                            order_grid.volume = order_volume
                        self.write_log(u'重新提交{}开空委托,开空价{}，v:{}'.format(order_vt_symbol, short_price, order_volume))
                        vt_orderids = self.short(price=short_price, volume=order_volume, vt_symbol=order_vt_symbol, order_type=order_type, order_time=self.cur_datetime, grid=order_grid)
                        if (len(vt_orderids) > 0):
                            self.write_log(u'委托成功，orderid:{}'.format(vt_orderids))
                            order_grid.snapshot.update({'open_price': short_price})
                        else:
                            self.write_error(u'撤单后，重新委托开空仓失败')
                    else:
                        buy_price = (self.cur_mi_price + self.price_tick)
                        if ((order_grid.volume != order_volume) and (order_volume > 0)):
                            self.write_log(u'网格volume:{},order_volume:{}不一致，修正'.format(order_grid.volume, order_volume))
                            order_grid.volume = order_volume
                        self.write_log(u'重新提交{}开多委托,开多价{}，v:{}'.format(order_vt_symbol, buy_price, order_volume))
                        vt_orderids = self.buy(price=buy_price, volume=order_volume, vt_symbol=order_vt_symbol, order_type=order_type, order_time=self.cur_datetime, grid=order_grid)
                        if (len(vt_orderids) > 0):
                            self.write_log(u'委托成功，orderids:{}'.format(vt_orderids))
                            order_grid.snapshot.update({'open_price': buy_price})
                        else:
                            self.write_error(u'撤单后，重新委托开多仓失败')
                elif (order_info['direction'] == Direction.SHORT):
                    sell_price = (self.cur_mi_price - self.price_tick)
                    self.write_log(u'重新提交{}平多委托,{}，v:{}'.format(order_vt_symbol, sell_price, order_volume))
                    vt_orderids = self.sell(price=sell_price, volume=order_volume, vt_symbol=order_vt_symbol, order_type=order_type, order_time=self.cur_datetime, grid=order_grid)
                    if (len(vt_orderids) > 0):
                        self.write_log(u'委托成功，orderids:{}'.format(vt_orderids))
                    else:
                        self.write_error(u'撤单后，重新委托平多仓失败')
                else:
                    cover_price = (self.cur_mi_price + self.price_tick)
                    self.write_log(u'重新提交{}平空委托,委托价{}，v:{}'.format(order_vt_symbol, cover_price, order_volume))
                    vt_orderids = self.cover(price=cover_price, volume=order_volume, vt_symbol=order_vt_symbol, order_type=order_type, order_time=self.cur_datetime, grid=order_grid)
                    if (len(vt_orderids) > 0):
                        self.write_log(u'委托成功，orderids:{}'.format(vt_orderids))
                    else:
                        self.write_error(u'撤单后，重新委托平空仓失败')
            else:
                self.write_log(u'撤单逻辑 => 无须重新开仓')
                if ((order_info['offset'] == Offset.OPEN) and order_grid and (len(order_grid.order_ids) == 0)):
                    if ((order_info['traded'] == 0) and (order_grid.traded_volume == 0)):
                        self.write_log(u'撤单逻辑 => 无任何成交 => 移除委托网格{}'.format(order_grid.__dict__))
                        order_info['grid'] = None
                        self.gt.remove_grids_by_ids(direction=order_grid.direction, ids=[order_grid.id])
                    elif (order_info['traded'] > 0):
                        self.write_log('撤单逻辑 = > 部分开仓')
                        if (order_grid.traded_volume < order_info['traded']):
                            self.write_log('撤单逻辑 = > 调整网格开仓数 {} => {}'.format(order_grid.traded_volume, order_info['traded']))
                            order_grid.traded_volume = order_info['traded']
                        self.write_log(f'撤单逻辑 => 调整网格委托状态=> False, 开仓状态:True, 开仓数量:{order_grid.volume}=>{order_grid.traded_volume}')
                        order_grid.order_status = False
                        order_grid.open_status = True
                        order_grid.volume = order_grid.traded_volume
                        order_grid.traded_volume = 0
    for vt_orderid in canceled_ids:
        self.write_log(u'撤单逻辑 => 删除未完成订单:{}'.format(vt_orderid))
        self.active_orders.pop(vt_orderid, None)
    if (len(self.active_orders) == 0):
        self.entrust = 0
撤单逻辑
def tns_cancel_logic(self, dt, force=False, reopen=False):
    
    if (len(self.active_orders) < 1):
        self.entrust = 0
        return
    canceled_ids = []
    for vt_orderid in list(self.active_orders.keys()):
        order_info = self.active_orders[vt_orderid]
        order_vt_symbol = order_info.get('vt_symbol', self.vt_symbol)
        order_time = order_info['order_time']
        order_volume = (order_info['volume'] - order_info['traded'])
        order_grid = order_info['grid']
        order_status = order_info.get('status', Status.NOTTRADED)
        order_type = order_info.get('order_type', OrderType.LIMIT)
        over_seconds = (dt - order_time).total_seconds()
        if ((order_status in [Status.NOTTRADED, Status.SUBMITTING]) and ((order_type == OrderType.LIMIT) or ('.SPD' in order_vt_symbol))):
            if ((over_seconds > self.cancel_seconds) or force):
                self.write_log(u' => 超时{}秒未成交，取消委托单：vt_orderid:{},order:{}'.format(over_seconds, vt_orderid, order_info))
                order_info.update({'status': Status.CANCELLING})
                self.active_orders.update({vt_orderid: order_info})
                ret = self.cancel_order(str(vt_orderid))
                if (not ret):
                    self.write_error(f'{self.strategy_name} => {order_vt_symbol}撤单失败')
            continue
        elif (order_status == Status.CANCELLED):
            self.write_log(u' => 委托单{}已成功撤单，将删除未完成订单{}'.format(vt_orderid, order_info))
            canceled_ids.append(vt_orderid)
            if reopen:
                if (order_info['offset'] == Offset.OPEN):
                    self.write_log(u' => 重新开仓')
                    if (order_info['direction'] == Direction.SHORT):
                        short_price = (self.cur_mi_price - self.price_tick)
                        if ((order_grid.volume != order_volume) and (order_volume > 0)):
                            self.write_log(u'网格volume:{},order_volume:{}不一致，修正'.format(order_grid.volume, order_volume))
                            order_grid.volume = order_volume
                        self.write_log(u'重新提交{}开空委托,开空价{}，v:{}'.format(order_vt_symbol, short_price, order_volume))
                        vt_orderids = self.short(price=short_price, volume=order_volume, vt_symbol=order_vt_symbol, order_type=order_type, order_time=self.cur_datetime, grid=order_grid)
                        if (len(vt_orderids) > 0):
                            self.write_log(u'委托成功，orderid:{}'.format(vt_orderids))
                            order_grid.snapshot.update({'open_price': short_price})
                        else:
                            self.write_error(u'撤单后，重新委托开空仓失败')
                    else:
                        buy_price = (self.cur_mi_price + self.price_tick)
                        if ((order_grid.volume != order_volume) and (order_volume > 0)):
                            self.write_log(u'网格volume:{},order_volume:{}不一致，修正'.format(order_grid.volume, order_volume))
                            order_grid.volume = order_volume
                        self.write_log(u'重新提交{}开多委托,开多价{}，v:{}'.format(order_vt_symbol, buy_price, order_volume))
                        vt_orderids = self.buy(price=buy_price, volume=order_volume, vt_symbol=order_vt_symbol, order_type=order_type, order_time=self.cur_datetime, grid=order_grid)
                        if (len(vt_orderids) > 0):
                            self.write_log(u'委托成功，orderids:{}'.format(vt_orderids))
                            order_grid.snapshot.update({'open_price': buy_price})
                        else:
                            self.write_error(u'撤单后，重新委托开多仓失败')
                elif (order_info['direction'] == Direction.SHORT):
                    sell_price = (self.cur_mi_price - self.price_tick)
                    self.write_log(u'重新提交{}平多委托,{}，v:{}'.format(order_vt_symbol, sell_price, order_volume))
                    vt_orderids = self.sell(price=sell_price, volume=order_volume, vt_symbol=order_vt_symbol, order_type=order_type, order_time=self.cur_datetime, grid=order_grid)
                    if (len(vt_orderids) > 0):
                        self.write_log(u'委托成功，orderids:{}'.format(vt_orderids))
                    else:
                        self.write_error(u'撤单后，重新委托平多仓失败')
                else:
                    cover_price = (self.cur_mi_price + self.price_tick)
                    self.write_log(u'重新提交{}平空委托,委托价{}，v:{}'.format(order_vt_symbol, cover_price, order_volume))
                    vt_orderids = self.cover(price=cover_price, volume=order_volume, vt_symbol=order_vt_symbol, order_type=order_type, order_time=self.cur_datetime, grid=order_grid)
                    if (len(vt_orderids) > 0):
                        self.write_log(u'委托成功，orderids:{}'.format(vt_orderids))
                    else:
                        self.write_error(u'撤单后，重新委托平空仓失败')
            else:
                self.write_log(u' => 无须重新开仓')
                if ((order_info['offset'] == Offset.OPEN) and order_grid and (len(order_grid.order_ids) == 0)):
                    if ((order_info['traded'] == 0) and (order_grid.traded_volume == 0)):
                        self.write_log(u' => 无任何成交 => 移除委托网格{}'.format(order_grid.__dict__))
                        order_info['grid'] = None
                        self.gt.remove_grids_by_ids(direction=order_grid.direction, ids=[order_grid.id])
                    elif (order_info['traded'] > 0):
                        self.write_log(' = > 部分开仓')
                        if (order_grid.traded_volume < order_info['traded']):
                            self.write_log(' = > 调整网格开仓数 {} => {}'.format(order_grid.traded_volume, order_info['traded']))
                            order_grid.traded_volume = order_info['traded']
                        self.write_log(f' => 调整网格委托状态=> False, 开仓状态:True, 开仓数量:{order_grid.volume}=>{order_grid.traded_volume}')
                        order_grid.order_status = False
                        order_grid.open_status = True
                        order_grid.volume = order_grid.traded_volume
                        order_grid.traded_volume = 0
    for vt_orderid in canceled_ids:
        self.write_log(u' => 删除未完成订单:{}'.format(vt_orderid))
        self.active_orders.pop(vt_orderid, None)
    if (len(self.active_orders) == 0):
        self.entrust = 0