def close_market_date(location: Optional[Union[PricingLocation, str]] = None, date: Optional[dt.date] = None) -> dt.date: from .core import PricingContext if date is None: return prev_business_date(PricingContext.current.pricing_date, calendars=location.value if isinstance(location, PricingLocation) else location) \ if PricingContext.current.pricing_date == dt.date.today() else PricingContext.current.pricing_date elif date > PricingContext.current.pricing_date: raise ValueError( 'Market data date cannot be greater than pricing date') else: return date
def close_market_date(location: Optional[Union[PricingLocation, str]] = None, date: Optional[dt.date] = None) -> dt.date: """Determine market data date based on current location (to infer calendar) and current pricing date :param location: location :param date: pricing date :return: close market date """ from .core import PricingContext date = date or PricingContext.current.pricing_date if date == dt.date.today(): # Don't use the calendars argument here as external users do not (yet) have access to that dataset date = prev_business_date(date) return date
def close_market_date(location: Optional[Union[PricingLocation, str]] = None, date: Optional[dt.date] = None) -> dt.date: """Determine market data date based on current location (to infer calendar) and current pricing date :param location: location :param date: pricing date :return: close market date """ from .core import PricingContext date = date or PricingContext.current.pricing_date if date == dt.date.today(): date = prev_business_date(date, calendars=location.value if isinstance( location, PricingLocation) else location) return date
def _market(self, date: dt.date, location: str) -> CloseMarket: date = prev_business_date(date) if date == dt.date.today() else date return CloseMarket(location=location, date=date, check=False)
def get_flagships_constituents( fields: [str] = [], basket_type: List[BasketType] = BasketType.to_list(), asset_class: List[AssetClass] = [AssetClass.Equity], region: List[Region] = None, styles: List[Union[CustomBasketStyles, ResearchBasketStyles]] = None, start: dt.date = None, end: dt.date = None, ) -> pd.DataFrame: """ Retrieve flagship baskets constituents :param fields: Fields to retrieve in addition to mqid, name, ticker, region, basket type, \ styles, live date, and asset class :param basket_type: Basket type(s) :param asset_class: Asset class (defaults to Equity) :param region: Basket region(s) :param styles: Basket style(s) :param start: Start date for which to retrieve constituents (defaults to previous business day) :param end: End date for which to retrieve constituents (defaults to previous business day) :return: flagship baskets constituents **Usage** Retrieve flagship baskets constituents **Examples** Retrieve a list of flagship baskets constituents >>> from gs_quant.markets.indices_utils import * >>> >>> get_flagships_constituents() **See also** :func:`get_flagships_with_assets` :func:`get_flagships_performance` :func:`get_flagship_baskets` """ start = start or prev_business_date() end = end or prev_business_date() fields = list( set(fields).union( set([ 'id', 'name', 'ticker', 'region', 'type', 'styles', 'liveDate', 'assetClass' ]))) query = dict(fields=fields, type=basket_type, asset_class=asset_class, is_pair_basket=[False], flagship=[True]) if region is not None: query.update(region=region) if styles is not None: query.update(styles=styles) basket_data = GsAssetApi.get_many_assets_data_scroll(**query, limit=2000, scroll='1m') basket_map = {get(basket, 'id'): basket for basket in basket_data} coverage = GsDataApi.get_coverage( dataset_id=IndicesDatasets.GSCB_FLAGSHIP.value, fields=['type', 'bbid'], include_history=True) cbs, rbs = [], [] for b in coverage: _id = get(b, 'assetId') _type = get(b, 'type') if _id in list(basket_map.keys()): start_date = dt.datetime.strptime(b['historyStartDate'], '%Y-%m-%d').date() start_date = start_date if start < start_date else start if _type == BasketType.CUSTOM_BASKET.value: data = GsDataApi.query_data( query=DataQuery(where=dict(assetId=_id), startDate=start_date, endDate=end), dataset_id=IndicesDatasets.GSBASKETCONSTITUENTS.value) basket_map[_id].update(constituents=data) cbs.append(basket_map[_id]) elif _type == BasketType.RESEARCH_BASKET.value: data = GsDataApi.query_data( query=DataQuery(where=dict(assetId=_id), startDate=start_date, endDate=end), dataset_id=IndicesDatasets.GIRBASKETCONSTITUENTS.value) basket_map[_id].update(constituents=data) rbs.append(basket_map[_id]) return pd.DataFrame(cbs + rbs)
def get_flagships_performance( fields: [str] = [], basket_type: List[BasketType] = BasketType.to_list(), asset_class: List[AssetClass] = [AssetClass.Equity], region: List[Region] = None, styles: List[Union[CustomBasketStyles, ResearchBasketStyles]] = None, start: dt.date = None, end: dt.date = None, ) -> pd.DataFrame: """ Retrieve performance data for flagship baskets :param fields: Fields to retrieve in addition to bbid, mqid, name, region, basket type, \ styles, live date, and asset class :param basket_type: Basket type(s) :param asset_class: Asset class (defaults to Equity) :param region: Basket region(s) :param styles: Basket style(s) :param start: Date for which to retrieve pricing (defaults to previous business day) :param end: Date for which to retrieve pricing (defaults to previous business day) :return: pricing data for flagship baskets **Usage** Retrieve performance data for flagship baskets **Examples** Retrieve performance data for flagship Asia custom baskets >>> from gs_quant.markets.indices_utils import * >>> >>> get_flagships_performance(basket_type=[BasketType.CUSTOM_BASKET], region=[Region.ASIA]) **See also** :func:`get_flagships_with_assets` :func:`get_flagship_baskets` :func:`get_flagships_constituents` """ start = start or prev_business_date() end = end or prev_business_date() fields = list( set(fields).union( set([ 'name', 'region', 'type', 'flagship', 'isPairBasket', 'styles', 'liveDate', 'assetClass' ]))) coverage = GsDataApi.get_coverage( dataset_id=IndicesDatasets.GSCB_FLAGSHIP.value, fields=fields) basket_regions = [] if region is None else [r.value for r in region] basket_styles = [] if styles is None else [s.value for s in styles] basket_types = [b_type.value for b_type in basket_type] baskets_map = {} for basket in coverage: if get(basket, 'flagship') is False or get(basket, 'isPairBasket') is True or \ region is not None and get(basket, 'region') not in basket_regions or \ get(basket, 'type') not in basket_types or \ get(basket, 'assetClass') not in [a.value for a in asset_class] or \ styles is not None and not any(s in get(basket, 'styles', []) for s in basket_styles): continue baskets_map[get(basket, 'assetId')] = basket response = GsDataApi.query_data( query=DataQuery(where=dict(assetId=list(baskets_map.keys())), startDate=start, endDate=end), dataset_id=IndicesDatasets.GSCB_FLAGSHIP.value) performance = [] for basket in response: basket_data = baskets_map[get(basket, 'assetId')] basket_data.update(closePrice=get(basket, 'closePrice')) basket_data.update(date=get(basket, 'date')) performance.append(basket_data) return pd.DataFrame(performance)
def closing_market_date(location: Optional[Union[PricingLocation, str]] = None) -> dt.date: return prev_business_date(dt.date.today(), calendars=location.value if isinstance(location, PricingLocation) else location)
def get_flagships_constituents( fields: [str] = [], basket_type: List[BasketType] = BasketType.to_list(), asset_class: List[AssetClass] = [AssetClass.Equity], region: List[Region] = None, styles: List[Union[CustomBasketStyles, ResearchBasketStyles]] = None, start: dt.date = None, end: dt.date = None, ) -> pd.DataFrame: """ Retrieve flagship baskets constituents :param fields: Fields to retrieve in addition to mqid, name, ticker, region, basket type, \ styles, live date, and asset class :param basket_type: Basket type(s) :param asset_class: Asset class (defaults to Equity) :param region: Basket region(s) :param styles: Basket style(s) :param start: Start date for which to retrieve constituents (defaults to previous business day) :param end: End date for which to retrieve constituents (defaults to previous business day) :return: flagship baskets constituents **Usage** Retrieve flagship baskets constituents **Examples** Retrieve a list of flagship baskets constituents >>> from gs_quant.markets.indices_utils import * >>> >>> get_flagships_constituents() **See also** :func:`get_flagships_with_assets` :func:`get_flagships_performance` :func:`get_flagship_baskets` """ start = start or prev_business_date() end = end or prev_business_date() basket_fields = list( set(fields).union( set([ 'id', 'name', 'ticker', 'region', 'type', 'styles', 'liveDate', 'assetClass' ]))) fields = list(set(fields).union(set(['id', 'name']))) query = dict(fields=basket_fields, type=basket_type, asset_class=asset_class, is_pair_basket=[False], flagship=[True]) if region is not None: query.update(region=region) if styles is not None: query.update(styles=styles) coverage_results = ThreadPoolManager.run_async([ partial(GsAssetApi.get_many_assets_data_scroll, **query, limit=2000, scroll='1m'), partial(GsDataApi.get_coverage, dataset_id=IndicesDatasets.GSCB_FLAGSHIP.value, fields=['type', 'bbid'], include_history=True) ]) basket_data, coverage = coverage_results[0], coverage_results[1] basket_map = {get(basket, 'id'): basket for basket in basket_data} tasks, results = [], [] for b in coverage: tasks.append( partial(__get_constituents_data, basket=b, basket_map=basket_map, start=start, end=end, fields=fields)) tasks = [ tasks[i * 50:(i + 1) * 50] for i in range((len(tasks) + 50 - 1) // 50) ] for task in tasks: results += ThreadPoolManager.run_async(task) sleep(1) return pd.DataFrame([r for r in results if r is not None])