def tns_switch_long_pos(self, open_new=True):
    '\n        切换合约，从持仓的非主力合约，切换至主力合约\n        :param open_new: 是否开仓主力合约\n        :return:\n        '
    if (self.entrust != 0):
        return
    if (self.position.long_pos == 0):
        return
    if (self.cur_mi_price == 0):
        return
    none_mi_grid = None
    none_mi_symbol = None
    self.write_log(f'持仓换月=>启动.')
    for g in self.gt.get_opened_grids(direction=Direction.LONG):
        none_mi_symbol = g.snapshot.get('mi_symbol', g.vt_symbol)
        if ((none_mi_symbol is None) or (none_mi_symbol == self.vt_symbol)):
            self.write_log(f'none_mi_symbol:{none_mi_symbol}, vt_symbol:{self.vt_symbol} 一致，不处理')
            continue
        if ((not g.open_status) or g.order_status or ((g.volume - g.traded_volume) <= 0)):
            self.write_log(f'开仓状态:{g.open_status}, 委托状态:{g.order_status},网格持仓:{g.volume} ,已交易数量:{g.traded_volume}, 不处理')
            continue
        none_mi_grid = g
        if ((g.traded_volume > 0) and ((g.volume - g.traded_volume) > 0)):
            g.volume -= g.traded_volume
            g.traded_volume = 0
        break
    if (none_mi_grid is None):
        return
    self.write_log(f'持仓换月=>找到多单持仓:{none_mi_symbol},持仓数量:{none_mi_grid.volume}')
    none_mi_tick = self.tick_dict.get(none_mi_symbol)
    mi_tick = self.tick_dict.get(self.vt_symbol, None)
    if ((none_mi_tick is None) or (mi_tick is None)):
        return
    if (self.is_upper_limit(none_mi_symbol) or self.is_upper_limit(self.vt_symbol)):
        self.write_log(f'{none_mi_symbol} 或 {self.vt_symbol} 为涨停价，不做换仓')
        return
    none_mi_price = max(none_mi_tick.last_price, none_mi_tick.bid_price_1)
    grid = deepcopy(none_mi_grid)
    grid.id = str(uuid.uuid1())
    grid.open_status = False
    self.write_log(f'持仓换月=>复制持仓信息{none_mi_symbol},ID:{none_mi_grid.id} => {self.vt_symbol},ID:{grid.id}')
    vt_orderids = self.sell(price=none_mi_price, volume=none_mi_grid.volume, vt_symbol=none_mi_symbol, order_type=self.order_type, grid=none_mi_grid)
    if (len(vt_orderids) > 0):
        self.write_log(f'持仓换月=>委托卖出非主力合约{none_mi_symbol}持仓:{none_mi_grid.volume}')
        if none_mi_grid.snapshot.get('switched', False):
            self.write_log(f'持仓换月=>已经执行过换月，不再创建新的买入操作')
            return
        none_mi_grid.snapshot.update({'switched': True})
        if (not open_new):
            self.write_log(f'不买入新的主力合约:{self.vt_symbol},数量:{grid.volume}')
            self.gt.save()
            return
        grid.snapshot.update({'mi_symbol': self.vt_symbol, 'open_price': self.cur_mi_price})
        self.gt.dn_grids.append(grid)
        vt_orderids = self.buy(price=(self.cur_mi_price + (5 * self.price_tick)), volume=grid.volume, vt_symbol=self.vt_symbol, order_type=self.order_type, grid=grid)
        if (len(vt_orderids) > 0):
            self.write_log(u'持仓换月=>委托买入主力合约:{},价格:{},数量:{}'.format(self.vt_symbol, self.cur_mi_price, grid.volume))
        else:
            self.write_error(f'持仓换月=>委托买入主力合约:{self.vt_symbol}失败')
        self.gt.save()
    else:
        self.write_error(f'持仓换月=>委托卖出非主力合约:{none_mi_symbol}失败')
切换合约，从持仓的非主力合约，切换至主力合约
:param open_new: 是否开仓主力合约
:return:
def tns_switch_long_pos(self, open_new=True):
    '\n        切换合约，从持仓的非主力合约，切换至主力合约\n        :param open_new: 是否开仓主力合约\n        :return:\n        '
    if (self.entrust != 0):
        return
    if (self.position.long_pos == 0):
        return
    if (self.cur_mi_price == 0):
        return
    none_mi_grid = None
    none_mi_symbol = None
    self.write_log(f'持仓换月=>启动.')
    for g in self.gt.get_opened_grids(direction=Direction.LONG):
        none_mi_symbol = g.snapshot.get('mi_symbol', g.vt_symbol)
        if ((none_mi_symbol is None) or (none_mi_symbol == self.vt_symbol)):
            self.write_log(f'none_mi_symbol:{none_mi_symbol}, vt_symbol:{self.vt_symbol} 一致，不处理')
            continue
        if ((not g.open_status) or g.order_status or ((g.volume - g.traded_volume) <= 0)):
            self.write_log(f'开仓状态:{g.open_status}, 委托状态:{g.order_status},网格持仓:{g.volume} ,已交易数量:{g.traded_volume}, 不处理')
            continue
        none_mi_grid = g
        if ((g.traded_volume > 0) and ((g.volume - g.traded_volume) > 0)):
            g.volume -= g.traded_volume
            g.traded_volume = 0
        break
    if (none_mi_grid is None):
        return
    self.write_log(f'持仓换月=>找到多单持仓:{none_mi_symbol},持仓数量:{none_mi_grid.volume}')
    none_mi_tick = self.tick_dict.get(none_mi_symbol)
    mi_tick = self.tick_dict.get(self.vt_symbol, None)
    if ((none_mi_tick is None) or (mi_tick is None)):
        return
    if (self.is_upper_limit(none_mi_symbol) or self.is_upper_limit(self.vt_symbol)):
        self.write_log(f'{none_mi_symbol} 或 {self.vt_symbol} 为涨停价，不做换仓')
        return
    none_mi_price = max(none_mi_tick.last_price, none_mi_tick.bid_price_1)
    grid = deepcopy(none_mi_grid)
    grid.id = str(uuid.uuid1())
    grid.open_status = False
    self.write_log(f'持仓换月=>复制持仓信息{none_mi_symbol},ID:{none_mi_grid.id} => {self.vt_symbol},ID:{grid.id}')
    vt_orderids = self.sell(price=none_mi_price, volume=none_mi_grid.volume, vt_symbol=none_mi_symbol, order_type=self.order_type, grid=none_mi_grid)
    if (len(vt_orderids) > 0):
        self.write_log(f'持仓换月=>委托卖出非主力合约{none_mi_symbol}持仓:{none_mi_grid.volume}')
        if none_mi_grid.snapshot.get('switched', False):
            self.write_log(f'持仓换月=>已经执行过换月，不再创建新的买入操作')
            return
        none_mi_grid.snapshot.update({'switched': True})
        if (not open_new):
            self.write_log(f'不买入新的主力合约:{self.vt_symbol},数量:{grid.volume}')
            self.gt.save()
            return
        grid.snapshot.update({'mi_symbol': self.vt_symbol, 'open_price': self.cur_mi_price})
        self.gt.dn_grids.append(grid)
        vt_orderids = self.buy(price=(self.cur_mi_price + (5 * self.price_tick)), volume=grid.volume, vt_symbol=self.vt_symbol, order_type=self.order_type, grid=grid)
        if (len(vt_orderids) > 0):
            self.write_log(u'持仓换月=>委托买入主力合约:{},价格:{},数量:{}'.format(self.vt_symbol, self.cur_mi_price, grid.volume))
        else:
            self.write_error(f'持仓换月=>委托买入主力合约:{self.vt_symbol}失败')
        self.gt.save()
    else:
        self.write_error(f'持仓换月=>委托卖出非主力合约:{none_mi_symbol}失败')