def get(cls, portfolio_id: str = None, name: str = None, **kwargs): if portfolio_id is None and name is None: raise MqValueError( 'Please specify a portfolio ID and/or portfolio name.') portfolios = GsPortfolioApi.get_portfolios( portfolio_ids=[portfolio_id] if portfolio_id else None, portfolio_names=[name] if name else None) if len(portfolios) == 0: raise ValueError( 'No portfolios in Marquee match the requested name and/or ID.') if len(portfolios) > 1: portfolios = { 'Name': [p.name for p in portfolios], 'ID': [p.id for p in portfolios], 'Created Time': [p.created_time for p in portfolios] } cls._print_dict(portfolios) raise ValueError( 'More than one portfolio matches the requested name and/or ID. To resolve,' ' please find the correct portfolio ID below and set it as your portfolio_id.' ) port = portfolios[0] return Portfolio(name=port.name, portfolio_id=port.id, currency=port.currency, entitlements=Entitlements.from_target( port.entitlements))
def _create(self): # If a priceables portfolio, try resolving to MQ portfolio if self.__priceables: self.save() self.priceables = None return # If a positions portfolio, create using MQ API port = GsPortfolioApi.create_portfolio( portfolio=MQPortfolio(name=self.name, currency=self.currency, entitlements=self.entitlements.to_target())) PositionedEntity.__init__(self, port.id, EntityType.PORTFOLIO) Entity.__init__(self, port.id, EntityType.PORTFOLIO) self.__id = port.id self._PositionedEntity__entity_type = EntityType.PORTFOLIO self.entitlements = Entitlements.from_target(port.entitlements) self.currency = Currency(port.currency) # If the portfolio contains positions, upload them to the MQ portfolio and schedule reports if self.position_sets: position_sets = self.position_sets self.position_sets = None self.update_positions(position_sets, False) self._schedule_first_reports( [pos_set.date for pos_set in position_sets]) self.position_sets = None
def get_entitlements(self) -> Entitlements: if self.positioned_entity_type == EntityType.PORTFOLIO: response = GsPortfolioApi.get_portfolio(self.id) elif self.positioned_entity_type == EntityType.ASSET: response = GsAssetApi.get_asset(self.id) else: raise NotImplementedError return Entitlements.from_target(response.entitlements)
def test_from_target(): replace = Replacer() mock = replace('gs_quant.api.gs.users.GsUsersApi.get_users', Mock()) mock.return_value = [ TargetUser.from_dict({ 'id': 'userId', 'email': '*****@*****.**', 'name': 'Jane Doe', 'company': 'Goldman Sachs Group' }) ] ent = Entitlements.from_target( TargetEntitlements(edit=('guid:userId', 'role:roleId'))) replace.restore() assert ent.edit.users == [get_fake_user()] assert ent.edit.roles == ['roleId']
def __finish_initialization(self): """ Fetches remaining data not retrieved during basket initialization """ if has(self, 'id'): if not has(self, '__initial_positions'): position_set = GsAssetApi.get_latest_positions( self.id, PositionType.ANY) position_set = PositionSet.from_target(position_set) self.__position_set = position_set self.__divisor = get(position_set, 'divisor') self.__initial_positions = set( deepcopy(self.__position_set.positions)) set_(self.__initial_state, 'divisor', self.__divisor) set_(self.__initial_state, 'position_set', self.__position_set) if not has(self.__initial_state, 'initial_price'): initial_price = GsIndexApi.initial_price( self.id, dt.date.today()) self.__initial_price = get(initial_price, 'price') set_(self.__initial_state, 'initial_price', self.__initial_price) if not has(self.__initial_state, 'publish_to_bloomberg'): report = get(self, '__latest_create_report', self.__get_latest_create_report()) self.__publish_to_bloomberg = get( report, 'parameters.publish_to_bloomberg') self.__publish_to_factset = get( report, 'parameters.publish_to_factset') self.__publish_to_reuters = get( report, 'parameters.publish_to_reuters') set_(self.__initial_state, 'publish_to_bloomberg', self.__publish_to_bloomberg) set_(self.__initial_state, 'publish_to_factset', self.__publish_to_factset) set_(self.__initial_state, 'publish_to_reuters', self.__publish_to_reuters) if not has(self, '__entitlements'): self.__entitlements = BasketEntitlements.from_target( self.__initial_entitlements) self.__set_error_messages()