def tns_open_from_lock(self, open_symbol, open_volume, grid_type, open_direction):
    '\n        从锁仓单中,获取已开的网格(对手仓设置为止损)\n        1, 检查多空锁仓单中,是否有满足数量得昨仓,\n        2, 定位到需求网格,\n        :param open_symbol: 开仓合约(主力合约)\n        :param open_volume:\n        :param grid_type 更新网格的类型\n        :param open_direction: 开仓方向\n        :return: None, 保留的格\n        '
    locked_long_grids = self.gt.get_opened_grids_within_types(direction=Direction.LONG, types=[LOCK_GRID])
    if (len(locked_long_grids) == 0):
        return None
    locked_long_dict = {}
    for g in locked_long_grids:
        symbol = g.snapshot.get('mi_symbol', self.vt_symbol)
        if (g.order_status or g.order_ids):
            self.write_log(u'当前对锁格:{}存在委托,不纳入计算'.format(g.to_json()))
            continue
        if (symbol != open_symbol):
            self.write_log(u'不处理symbol不一致: 委托请求:{}, Grid mi Symbol:{}'.format(open_symbol, symbol))
            continue
        volume = (g.volume - g.traded_volume)
        locked_long_dict.update({symbol: (locked_long_dict.get(symbol, 0) + volume)})
    locked_long_volume = locked_long_dict.get(open_symbol, 0)
    if (locked_long_volume < open_volume):
        self.write_log(u'锁单中,没有足够得多单:{},需求:{}'.format(locked_long_volume, open_volume))
        return None
    locked_short_grids = self.gt.get_opened_grids_within_types(direction=Direction.SHORT, types=[LOCK_GRID])
    if (len(locked_short_grids) == 0):
        return None
    locked_short_dict = {}
    for g in locked_short_grids:
        symbol = g.snapshot.get('mi_symbol', self.vt_symbol)
        if (g.order_status or g.order_ids):
            self.write_log(u'当前对锁格:{}存在委托,不进行解锁'.format(g.to_json()))
            continue
        if (symbol != open_symbol):
            self.write_log(u'不处理symbol不一致: 委托请求:{}, Grid mi Symbol:{}'.format(open_symbol, symbol))
            continue
        volume = (g.volume - g.traded_volume)
        locked_short_dict.update({symbol: (locked_short_dict.get(symbol, 0) + volume)})
    locked_short_volume = locked_short_dict.get(open_symbol, 0)
    if (locked_short_volume < open_volume):
        self.write_log(u'锁单中,没有足够得空单:{},需求:{}'.format(locked_short_volume, open_volume))
        return None
    symbol_pos = self.cta_engine.get_position_holding(open_symbol)
    if (((open_direction == Direction.LONG) and (symbol_pos.short_yd < open_volume)) or ((open_direction == Direction.SHORT) and (symbol_pos.long_yd < open_volume))):
        self.write_log(u'昨仓数量,多单:{},空单:{},不满足:{}'.format(symbol_pos.long_yd, symbol_pos.short_yd, open_volume))
        return None
    target_long_grid = None
    remove_long_grid_ids = []
    for g in sorted(locked_long_grids, key=(lambda grid: grid.volume)):
        if (g.order_status or (len(g.order_ids) > 0)):
            continue
        if (target_long_grid is None):
            target_long_grid = g
            if (g.volume == open_volume):
                self.write_log(u'第一个网格持仓数量一致:g.volume:{},open_volume:{}'.format(g.volume, open_volume))
                break
            elif (g.volume > open_volume):
                self.write_log(u'第一个网格持仓数量大于需求:g.volume:{},open_volume:{}'.format(g.volume, open_volume))
                remain_grid = copy(g)
                g.volume = open_volume
                remain_grid.volume -= open_volume
                remain_grid.id = str(uuid.uuid1())
                self.gt.dn_grids.append(remain_grid)
                self.write_log(u'添加剩余仓位到新多单网格:g.volume:{}'.format(remain_grid.volume))
                break
        elif (g.volume <= (open_volume - target_long_grid.volume)):
            self.write_log(u'网格持仓数量:g.volume:{},open_volume:{},保留格:{}'.format(g.volume, open_volume, target_long_grid.volume))
            target_long_grid.volume += g.volume
            g.volume = 0
            self.write_log(u'计划移除:{}'.format(g.id))
            remove_long_grid_ids.append(g.id)
        else:
            self.write_log(u'转移前网格持仓数量:g.volume:{},open_volume:{},保留格:{}'.format(g.volume, open_volume, target_long_grid.volume))
            g.volume -= (open_volume - target_long_grid.volume)
            target_long_grid.volume = open_volume
            self.write_log(u'转移后网格持仓数量:g.volume:{},open_volume:{},保留格:{}'.format(g.volume, open_volume, target_long_grid.volume))
            break
    target_short_grid = None
    remove_short_grid_ids = []
    for g in sorted(locked_short_grids, key=(lambda grid: grid.volume)):
        if (g.order_status or g.order_ids):
            continue
        if (target_short_grid is None):
            target_short_grid = g
            if (g.volume == open_volume):
                self.write_log(u'第一个空单网格持仓数量满足需求:g.volume:{},open_volume:{}'.format(g.volume, open_volume))
                break
            elif (g.volume > open_volume):
                self.write_log(u'第一个空单网格持仓数量大于需求:g.volume:{},open_volume:{}'.format(g.volume, open_volume))
                remain_grid = copy(g)
                g.volume = open_volume
                remain_grid.volume -= open_volume
                remain_grid.id = str(uuid.uuid1())
                self.gt.up_grids.append(remain_grid)
                self.write_log(u'添加剩余仓位到新空单网格:g.volume:{}'.format(remain_grid.volume))
                break
        elif (g.volume <= (open_volume - target_short_grid.volume)):
            target_short_grid.volume += g.volume
            g.volume = 0
            remove_short_grid_ids.append(g.id)
        else:
            self.write_log(u'转移前空单网格持仓数量:g.volume:{},open_volume:{},保留格:{}'.format(g.volume, open_volume, target_short_grid.volume))
            g.volume -= (open_volume - target_short_grid.volume)
            target_short_grid.volume = open_volume
            self.write_log(u'转移后空单网格持仓数量:g.volume:{},open_volume:{},保留格:{}'.format(g.volume, open_volume, target_short_grid.volume))
            break
    if ((target_long_grid.volume is None) or (target_short_grid is None)):
        self.write_log(u'未能定位多单网格和空单网格,不能解锁')
        return None
    self.gt.remove_grids_by_ids(direction=Direction.LONG, ids=remove_long_grid_ids)
    self.gt.remove_grids_by_ids(direction=Direction.SHORT, ids=remove_short_grid_ids)
    if (open_direction == Direction.LONG):
        self.write_log(u'保留多单,对空单:{}平仓'.format(target_short_grid.id))
        cover_price = self.cta_engine.get_price(open_symbol)
        self.write_log(u'空单止损价 :{} =>{}'.format(target_short_grid.stop_price, (cover_price - (10 * self.price_tick))))
        target_short_grid.stop_price = (cover_price - (10 * self.price_tick))
        self.write_log(u'空单类型 :{} =>{}'.format(target_short_grid.type, grid_type))
        target_short_grid.type = grid_type
        return target_long_grid
    else:
        self.write_log(u'保留空单,对多单平仓')
        sell_price = self.cta_engine.get_price(open_symbol)
        self.write_log(u'多单止损价 :{} =>{}'.format(target_short_grid.stop_price, (sell_price + (10 * self.price_tick))))
        target_long_grid.stop_price = (sell_price + (10 * self.price_tick))
        self.write_log(u'多单类型 :{} =>{}'.format(target_short_grid.type, grid_type))
        target_long_grid.type = grid_type
        return target_short_grid
从锁仓单中,获取已开的网格(对手仓设置为止损)
1, 检查多空锁仓单中,是否有满足数量得昨仓,
2, 定位到需求网格,
:param open_symbol: 开仓合约(主力合约)
:param open_volume:
:param grid_type 更新网格的类型
:param open_direction: 开仓方向
:return: None, 保留的格
def tns_open_from_lock(self, open_symbol, open_volume, grid_type, open_direction):
    '\n        从锁仓单中,获取已开的网格(对手仓设置为止损)\n        1, 检查多空锁仓单中,是否有满足数量得昨仓,\n        2, 定位到需求网格,\n        :param open_symbol: 开仓合约(主力合约)\n        :param open_volume:\n        :param grid_type 更新网格的类型\n        :param open_direction: 开仓方向\n        :return: None, 保留的格\n        '
    locked_long_grids = self.gt.get_opened_grids_within_types(direction=Direction.LONG, types=[LOCK_GRID])
    if (len(locked_long_grids) == 0):
        return None
    locked_long_dict = {}
    for g in locked_long_grids:
        symbol = g.snapshot.get('mi_symbol', self.vt_symbol)
        if (g.order_status or g.order_ids):
            self.write_log(u'当前对锁格:{}存在委托,不纳入计算'.format(g.to_json()))
            continue
        if (symbol != open_symbol):
            self.write_log(u'不处理symbol不一致: 委托请求:{}, Grid mi Symbol:{}'.format(open_symbol, symbol))
            continue
        volume = (g.volume - g.traded_volume)
        locked_long_dict.update({symbol: (locked_long_dict.get(symbol, 0) + volume)})
    locked_long_volume = locked_long_dict.get(open_symbol, 0)
    if (locked_long_volume < open_volume):
        self.write_log(u'锁单中,没有足够得多单:{},需求:{}'.format(locked_long_volume, open_volume))
        return None
    locked_short_grids = self.gt.get_opened_grids_within_types(direction=Direction.SHORT, types=[LOCK_GRID])
    if (len(locked_short_grids) == 0):
        return None
    locked_short_dict = {}
    for g in locked_short_grids:
        symbol = g.snapshot.get('mi_symbol', self.vt_symbol)
        if (g.order_status or g.order_ids):
            self.write_log(u'当前对锁格:{}存在委托,不进行解锁'.format(g.to_json()))
            continue
        if (symbol != open_symbol):
            self.write_log(u'不处理symbol不一致: 委托请求:{}, Grid mi Symbol:{}'.format(open_symbol, symbol))
            continue
        volume = (g.volume - g.traded_volume)
        locked_short_dict.update({symbol: (locked_short_dict.get(symbol, 0) + volume)})
    locked_short_volume = locked_short_dict.get(open_symbol, 0)
    if (locked_short_volume < open_volume):
        self.write_log(u'锁单中,没有足够得空单:{},需求:{}'.format(locked_short_volume, open_volume))
        return None
    symbol_pos = self.cta_engine.get_position_holding(open_symbol)
    if (((open_direction == Direction.LONG) and (symbol_pos.short_yd < open_volume)) or ((open_direction == Direction.SHORT) and (symbol_pos.long_yd < open_volume))):
        self.write_log(u'昨仓数量,多单:{},空单:{},不满足:{}'.format(symbol_pos.long_yd, symbol_pos.short_yd, open_volume))
        return None
    target_long_grid = None
    remove_long_grid_ids = []
    for g in sorted(locked_long_grids, key=(lambda grid: grid.volume)):
        if (g.order_status or (len(g.order_ids) > 0)):
            continue
        if (target_long_grid is None):
            target_long_grid = g
            if (g.volume == open_volume):
                self.write_log(u'第一个网格持仓数量一致:g.volume:{},open_volume:{}'.format(g.volume, open_volume))
                break
            elif (g.volume > open_volume):
                self.write_log(u'第一个网格持仓数量大于需求:g.volume:{},open_volume:{}'.format(g.volume, open_volume))
                remain_grid = copy(g)
                g.volume = open_volume
                remain_grid.volume -= open_volume
                remain_grid.id = str(uuid.uuid1())
                self.gt.dn_grids.append(remain_grid)
                self.write_log(u'添加剩余仓位到新多单网格:g.volume:{}'.format(remain_grid.volume))
                break
        elif (g.volume <= (open_volume - target_long_grid.volume)):
            self.write_log(u'网格持仓数量:g.volume:{},open_volume:{},保留格:{}'.format(g.volume, open_volume, target_long_grid.volume))
            target_long_grid.volume += g.volume
            g.volume = 0
            self.write_log(u'计划移除:{}'.format(g.id))
            remove_long_grid_ids.append(g.id)
        else:
            self.write_log(u'转移前网格持仓数量:g.volume:{},open_volume:{},保留格:{}'.format(g.volume, open_volume, target_long_grid.volume))
            g.volume -= (open_volume - target_long_grid.volume)
            target_long_grid.volume = open_volume
            self.write_log(u'转移后网格持仓数量:g.volume:{},open_volume:{},保留格:{}'.format(g.volume, open_volume, target_long_grid.volume))
            break
    target_short_grid = None
    remove_short_grid_ids = []
    for g in sorted(locked_short_grids, key=(lambda grid: grid.volume)):
        if (g.order_status or g.order_ids):
            continue
        if (target_short_grid is None):
            target_short_grid = g
            if (g.volume == open_volume):
                self.write_log(u'第一个空单网格持仓数量满足需求:g.volume:{},open_volume:{}'.format(g.volume, open_volume))
                break
            elif (g.volume > open_volume):
                self.write_log(u'第一个空单网格持仓数量大于需求:g.volume:{},open_volume:{}'.format(g.volume, open_volume))
                remain_grid = copy(g)
                g.volume = open_volume
                remain_grid.volume -= open_volume
                remain_grid.id = str(uuid.uuid1())
                self.gt.up_grids.append(remain_grid)
                self.write_log(u'添加剩余仓位到新空单网格:g.volume:{}'.format(remain_grid.volume))
                break
        elif (g.volume <= (open_volume - target_short_grid.volume)):
            target_short_grid.volume += g.volume
            g.volume = 0
            remove_short_grid_ids.append(g.id)
        else:
            self.write_log(u'转移前空单网格持仓数量:g.volume:{},open_volume:{},保留格:{}'.format(g.volume, open_volume, target_short_grid.volume))
            g.volume -= (open_volume - target_short_grid.volume)
            target_short_grid.volume = open_volume
            self.write_log(u'转移后空单网格持仓数量:g.volume:{},open_volume:{},保留格:{}'.format(g.volume, open_volume, target_short_grid.volume))
            break
    if ((target_long_grid.volume is None) or (target_short_grid is None)):
        self.write_log(u'未能定位多单网格和空单网格,不能解锁')
        return None
    self.gt.remove_grids_by_ids(direction=Direction.LONG, ids=remove_long_grid_ids)
    self.gt.remove_grids_by_ids(direction=Direction.SHORT, ids=remove_short_grid_ids)
    if (open_direction == Direction.LONG):
        self.write_log(u'保留多单,对空单:{}平仓'.format(target_short_grid.id))
        cover_price = self.cta_engine.get_price(open_symbol)
        self.write_log(u'空单止损价 :{} =>{}'.format(target_short_grid.stop_price, (cover_price - (10 * self.price_tick))))
        target_short_grid.stop_price = (cover_price - (10 * self.price_tick))
        self.write_log(u'空单类型 :{} =>{}'.format(target_short_grid.type, grid_type))
        target_short_grid.type = grid_type
        return target_long_grid
    else:
        self.write_log(u'保留空单,对多单平仓')
        sell_price = self.cta_engine.get_price(open_symbol)
        self.write_log(u'多单止损价 :{} =>{}'.format(target_short_grid.stop_price, (sell_price + (10 * self.price_tick))))
        target_long_grid.stop_price = (sell_price + (10 * self.price_tick))
        self.write_log(u'多单类型 :{} =>{}'.format(target_short_grid.type, grid_type))
        target_long_grid.type = grid_type
        return target_short_grid