def tns_close_long_pos(self, grid):
    '\n        事务平多单仓位\n        1.来源自止损止盈平仓\n        逻辑: 如果当前账号昨仓满足平仓数量,直接平仓,如果不满足,则创建锁仓网格.\n        :param 平仓网格\n        :return:\n        '
    self.write_log(u'执行事务平多仓位:{}'.format(grid.to_json()))
    sell_symbol = grid.snapshot.get('mi_symbol', self.vt_symbol)
    grid_pos = self.cta_engine.get_position_holding(vt_symbol=sell_symbol)
    if (grid_pos is None):
        self.write_error(u'无法获取{}得持仓信息'.format(sell_symbol))
        return False
    if (((grid_pos.long_yd >= grid.volume > 0) and (grid_pos.long_td == 0) and (grid_pos.short_td == 0)) or (not self.activate_today_lock)):
        if self.activate_today_lock:
            self.write_log(u'昨仓多单:{},没有今仓,满足条件,直接平昨仓'.format(grid_pos.long_yd))
        sell_price = self.cta_engine.get_price(sell_symbol)
        if (sell_price is None):
            self.write_error(f'暂时不能获取{sell_symbol}价格，不能平仓')
            return False
        if (not self.backtesting):
            sell_tick = self.cta_engine.get_tick(sell_symbol)
            if (sell_tick and (0 < sell_tick.bid_price_1 < sell_price)):
                sell_price = sell_tick.bid_price_1
        if (grid.traded_volume > 0):
            grid.volume -= grid.traded_volume
            grid.traded_volume = 0
        if ((self.exchange != Exchange.CFFEX) and (grid_pos.long_pos < grid.volume)):
            self.write_error(f'账号{sell_symbol}多单持仓:{grid_pos.long_pos}不满足平仓:{grid.volume}要求:')
            return False
        vt_orderids = self.sell(price=sell_price, volume=grid.volume, vt_symbol=sell_symbol, order_type=self.order_type, order_time=self.cur_datetime, lock=(self.exchange == Exchange.CFFEX), grid=grid)
        if (len(vt_orderids) == 0):
            self.write_error(u'多单平仓委托失败')
            return False
        else:
            self.write_log(u'多单平仓委托成功，编号:{}'.format(vt_orderids))
            return True
    else:
        self.write_log(u'昨仓多单:{}不满足条件,创建对锁仓'.format(grid_pos.long_yd))
        dist_record = dict()
        dist_record['datetime'] = self.cur_datetime
        dist_record['symbol'] = sell_symbol
        dist_record['price'] = self.cur_mi_price
        dist_record['volume'] = grid.volume
        dist_record['operation'] = 'add short lock[long]'
        self.save_dist(dist_record)
        lock_grid = copy(grid)
        lock_grid.type = LOCK_GRID
        lock_grid.id = str(uuid.uuid1())
        lock_grid.direction = Direction.SHORT
        lock_grid.open_status = False
        lock_grid.order_status = False
        lock_grid.order_ids = []
        vt_orderids = self.short(self.cur_mi_price, volume=lock_grid.volume, vt_symbol=self.vt_symbol, order_type=self.order_type, order_time=self.cur_datetime, grid=lock_grid)
        if (len(vt_orderids) > 0):
            grid.type = LOCK_GRID
            self.write_log(u'委托创建对锁单(空单)成功,委托编号:{},{},p:{},v:{}'.format(vt_orderids, sell_symbol, self.cur_mi_price, lock_grid.volume))
            lock_grid.snapshot.update({'mi_symbol': self.vt_symbol, 'open_price': self.cur_mi_price})
            self.gt.up_grids.append(lock_grid)
            return True
        else:
            self.write_error(u'未能委托对锁单(空单)')
            return False
事务平多单仓位
1.来源自止损止盈平仓
逻辑: 如果当前账号昨仓满足平仓数量,直接平仓,如果不满足,则创建锁仓网格.
:param 平仓网格
:return:
def tns_close_long_pos(self, grid):
    '\n        事务平多单仓位\n        1.来源自止损止盈平仓\n        逻辑: 如果当前账号昨仓满足平仓数量,直接平仓,如果不满足,则创建锁仓网格.\n        :param 平仓网格\n        :return:\n        '
    self.write_log(u'执行事务平多仓位:{}'.format(grid.to_json()))
    sell_symbol = grid.snapshot.get('mi_symbol', self.vt_symbol)
    grid_pos = self.cta_engine.get_position_holding(vt_symbol=sell_symbol)
    if (grid_pos is None):
        self.write_error(u'无法获取{}得持仓信息'.format(sell_symbol))
        return False
    if (((grid_pos.long_yd >= grid.volume > 0) and (grid_pos.long_td == 0) and (grid_pos.short_td == 0)) or (not self.activate_today_lock)):
        if self.activate_today_lock:
            self.write_log(u'昨仓多单:{},没有今仓,满足条件,直接平昨仓'.format(grid_pos.long_yd))
        sell_price = self.cta_engine.get_price(sell_symbol)
        if (sell_price is None):
            self.write_error(f'暂时不能获取{sell_symbol}价格，不能平仓')
            return False
        if (not self.backtesting):
            sell_tick = self.cta_engine.get_tick(sell_symbol)
            if (sell_tick and (0 < sell_tick.bid_price_1 < sell_price)):
                sell_price = sell_tick.bid_price_1
        if (grid.traded_volume > 0):
            grid.volume -= grid.traded_volume
            grid.traded_volume = 0
        if ((self.exchange != Exchange.CFFEX) and (grid_pos.long_pos < grid.volume)):
            self.write_error(f'账号{sell_symbol}多单持仓:{grid_pos.long_pos}不满足平仓:{grid.volume}要求:')
            return False
        vt_orderids = self.sell(price=sell_price, volume=grid.volume, vt_symbol=sell_symbol, order_type=self.order_type, order_time=self.cur_datetime, lock=(self.exchange == Exchange.CFFEX), grid=grid)
        if (len(vt_orderids) == 0):
            self.write_error(u'多单平仓委托失败')
            return False
        else:
            self.write_log(u'多单平仓委托成功，编号:{}'.format(vt_orderids))
            return True
    else:
        self.write_log(u'昨仓多单:{}不满足条件,创建对锁仓'.format(grid_pos.long_yd))
        dist_record = dict()
        dist_record['datetime'] = self.cur_datetime
        dist_record['symbol'] = sell_symbol
        dist_record['price'] = self.cur_mi_price
        dist_record['volume'] = grid.volume
        dist_record['operation'] = 'add short lock[long]'
        self.save_dist(dist_record)
        lock_grid = copy(grid)
        lock_grid.type = LOCK_GRID
        lock_grid.id = str(uuid.uuid1())
        lock_grid.direction = Direction.SHORT
        lock_grid.open_status = False
        lock_grid.order_status = False
        lock_grid.order_ids = []
        vt_orderids = self.short(self.cur_mi_price, volume=lock_grid.volume, vt_symbol=self.vt_symbol, order_type=self.order_type, order_time=self.cur_datetime, grid=lock_grid)
        if (len(vt_orderids) > 0):
            grid.type = LOCK_GRID
            self.write_log(u'委托创建对锁单(空单)成功,委托编号:{},{},p:{},v:{}'.format(vt_orderids, sell_symbol, self.cur_mi_price, lock_grid.volume))
            lock_grid.snapshot.update({'mi_symbol': self.vt_symbol, 'open_price': self.cur_mi_price})
            self.gt.up_grids.append(lock_grid)
            return True
        else:
            self.write_error(u'未能委托对锁单(空单)')
            return False