def save(self, overwrite: Optional[bool] = False): if self.portfolios: raise ValueError('Cannot save portfolios with nested portfolios') if self.__id: if not overwrite: raise ValueError( f'Portfolio with id {id} already exists. Use overwrite=True to overwrite' ) else: if not self.__name: raise ValueError('name not set') self.__id = GsPortfolioApi.create_portfolio( MarqueePortfolio('USD', self.__name)).id _logger.info( f'Created Marquee portfolio {self.__name} with id {self.__id}') position_set = PositionSet( position_date=self.__position_context.position_date, positions=tuple( Position( asset_id=GsAssetApi.get_or_create_asset_from_instrument(i)) for i in self.instruments)) if len(position_set.positions) > 0: GsPortfolioApi.update_positions(self.__id, [position_set])
def get_latest_positions(cls, portfolio_id: str, position_type: str = 'close') -> Union[PositionSet, dict]: url = '/portfolios/{id}/positions/last?type={ptype}'.format(id=portfolio_id, ptype=position_type) results = GsSession.current._get(url)['results'] # Annoyingly, different types are returned depending on position_type if isinstance(results, dict) and 'positions' in results: results['positions'] = tuple(Position.from_dict(p) for p in results['positions']) return PositionSet.from_dict(results)
def save(self): if not self.name: raise ValueError('name not set') try: portfolio_id = GsPortfolioApi.get_portfolio_by_name(self.name).id except ValueError: from gs_quant.target.portfolios import Portfolio as MarqueePortfolio portfolio_id = GsPortfolioApi.create_portfolio(MarqueePortfolio('USD', self.name)).id _logger.info('Created Marquee portfolio with ID: {}'.format(portfolio_id)) position_set = PositionSet( position_date=dt.date.today(), positions=tuple(Position(asset_id=GsAssetApi.get_or_create_asset_from_instrument(i)) for i in self.instruments)) GsPortfolioApi.update_positions(portfolio_id, (position_set,))
def save(self, overwrite: Optional[bool] = False): if self.portfolios: raise ValueError('Cannot save portfolios with nested portfolios') if self.__id: if not overwrite: raise ValueError( f'Portfolio with id {id} already exists. Use overwrite=True to overwrite' ) else: if not self.__name: raise ValueError('name not set') try: self.__id = GsPortfolioApi.get_portfolio_by_name( self.__name).id if not overwrite: raise RuntimeError( f'Portfolio {self.__name} with id {self.__id} already exists. Use overwrite=True to overwrite' ) except ValueError: from gs_quant.target.portfolios import Portfolio as MarqueePortfolio self.__id = GsPortfolioApi.create_portfolio( MarqueePortfolio('USD', self.__name)).id _logger.info( f'Created Marquee portfolio {self.__name} with id {self.__id}' ) position_set = PositionSet( position_date=dt.date.today(), positions=tuple( Position( asset_id=GsAssetApi.get_or_create_asset_from_instrument(i)) for i in self.instruments)) GsPortfolioApi.update_positions(self.__id, (position_set, ))