Exemplo n.º 1
0
 def __init__(self,
              market_data_location: Optional[str] = None,
              is_async: bool = False,
              is_batch: bool = False,
              visible_to_gs: bool = False,
              csa_term: Optional[str] = None,
              poll_for_batch_results: Optional[bool] = False,
              batch_results_timeout: Optional[int] = None):
     # TODO we use 23:59:59.999999 as a sentinel value to indicate live pricing for now. Fix this
     d = business_day_offset(dt.date.today(), -1, roll='preceding')
     super().__init__(pricing_date=dt.date.today(),
                      market_data_as_of=dt.datetime(d.year, d.month, d.day,
                                                    23, 59, 59, 999999),
                      market_data_location=market_data_location,
                      is_async=is_async,
                      is_batch=is_batch,
                      use_cache=False,
                      visible_to_gs=visible_to_gs,
                      csa_term=csa_term,
                      poll_for_batch_results=poll_for_batch_results,
                      batch_results_timeout=batch_results_timeout)
Exemplo n.º 2
0
    def __init__(self,
                 pricing_date: Optional[dt.date] = None,
                 market_data_location: Optional[Union[PricingLocation, str]] = None,
                 is_async: bool = False,
                 is_batch: bool = False,
                 use_cache: bool = False,
                 visible_to_gs: Optional[bool] = None,
                 csa_term: Optional[str] = None,
                 timeout: Optional[int] = None,
                 market: Optional[Market] = None,
                 show_progress: Optional[bool] = False,
                 use_server_cache: Optional[bool] = False):
        """
        The methods on this class should not be called directly. Instead, use the methods on the instruments,
        as per the examples

        :param pricing_date: the date for pricing calculations. Default is today
        :param market_data_location: the location for sourcing market data ('NYC', 'LDN' or 'HKG' (defaults to LDN)
        :param is_async: if True, return (a future) immediately. If False, block (defaults to False)
        :param is_batch: use for calculations expected to run longer than 3 mins, to avoid timeouts.
            It can be used with is_async=True|False (defaults to False)
        :param use_cache: store results in the pricing cache (defaults to False)
        :param visible_to_gs: are the contents of risk requests visible to GS (defaults to False)
        :param csa_term: the csa under which the calculations are made. Default is local ccy ois index
        :param market_data_location: the location for sourcing market data ('NYC', 'LDN' or 'HKG' (defaults to LDN)
        :param timeout: the timeout for batch operations
        :param show_progress: add a progress bar (tqdm)
        :param use_server_cache: cache query results on the GS servers

        **Examples**

        To change the market data location of the default context:

        >>> from gs_quant.markets import PricingContext
        >>>
        >>> PricingContext.current = PricingContext(market_data_location='LDN')

        For a blocking, synchronous request:

        >>> from gs_quant.instrument import IRCap
        >>> cap = IRCap('5y', 'GBP')
        >>>
        >>> with PricingContext():
        >>>     price_f = cap.dollar_price()
        >>>
        >>> price = price_f.result()

        For an asynchronous request:

        >>> with PricingContext(is_async=True):
        >>>     price_f = cap.dollar_price()
        >>>
        >>> while not price_f.done:
        >>>     ...
        """
        super().__init__()

        if market and market_data_location and market.location is not \
                get_enum_value(PricingLocation, market_data_location):
            raise ValueError('market.location and market_data_location cannot be different')

        if pricing_date:
            if pricing_date > dt.date.today():
                raise ValueError(
                    'The PricingContext does not support a pricing_date in the future. Please use the RollFwd Scenario '
                    'to roll the pricing_date to a future date')

        if market:
            market_date = None
            if isinstance(market, OverlayMarket) or isinstance(market, CloseMarket):
                market_date = getattr(market, 'date', None) or getattr(market.base_market, 'date', None)

            if isinstance(market, RelativeMarket):
                market_date = market.from_market.date if market.from_market.date > dt.date.today() \
                    else market.to_market.date

            if market_date:
                if market_date > dt.date.today():
                    raise ValueError(
                        'The PricingContext does not support a market dated in the future. Please use the RollFwd '
                        'Scenario to roll the pricing_date to a future date')

        if not market_data_location:
            if not market:
                # use parent context's market_data_location
                if self != self.active_context:
                    market_data_location = self.active_context.market_data_location
                # if no parent but there was a context set previously, use that
                elif self.prior_context:
                    market_data_location = self.prior_context.market_data_location
            else:
                market_data_location = market.location

        self.__pricing_date = pricing_date or (self.prior_context.pricing_date if self.prior_context else
                                               business_day_offset(dt.date.today(), 0, roll='preceding'))
        self.__csa_term = csa_term
        self.__is_async = is_async
        self.__is_batch = is_batch
        self.__timeout = timeout
        self.__use_cache = use_cache
        self.__visible_to_gs = visible_to_gs
        self.__market_data_location = get_enum_value(PricingLocation, market_data_location)
        self.__market = market or CloseMarket(
            date=close_market_date(self.__market_data_location, self.__pricing_date) if pricing_date else None,
            location=self.__market_data_location if market_data_location else None)
        self.__pending = {}
        self.__show_progress = show_progress
        self._max_concurrent = 1000
        self.__use_server_cache = use_server_cache
Exemplo n.º 3
0
    def __init__(self,
                 pricing_date: Optional[dt.date] = None,
                 market_data_location: Optional[Union[PricingLocation,
                                                      str]] = None,
                 is_async: bool = False,
                 is_batch: bool = False,
                 use_cache: bool = False,
                 visible_to_gs: bool = False,
                 csa_term: Optional[str] = None,
                 poll_for_batch_results: Optional[bool] = False,
                 batch_results_timeout: Optional[int] = None,
                 market: Optional[Market] = None):
        """
        The methods on this class should not be called directly. Instead, use the methods on the instruments,
        as per the examples

        :param pricing_date: the date for pricing calculations. Default is today
        :param market_data_location: the location for sourcing market data ('NYC', 'LDN' or 'HKG' (defaults to LDN)
        :param is_async: if True, return (a future) immediately. If False, block (defaults to False)
        :param is_batch: use for calculations expected to run longer than 3 mins, to avoid timeouts.
            It can be used with is_aync=True|False (defaults to False)
        :param use_cache: store results in the pricing cache (defaults to False)
        :param visible_to_gs: are the contents of risk requests visible to GS (defaults to False)
        :param csa_term: the csa under which the calculations are made. Default is local ccy ois index

        **Examples**

        To change the market data location of the default context:

        >>> from gs_quant.markets import PricingContext
        >>> import datetime as dt
        >>>
        >>> PricingContext.current = PricingContext(market_data_location='LDN')

        For a blocking, synchronous request:

        >>> from gs_quant.instrument import IRCap
        >>> cap = IRCap('5y', 'GBP')
        >>>
        >>> with PricingContext():
        >>>     price_f = cap.dollar_price()
        >>>
        >>> price = price_f.result()

        For an asynchronous request:

        >>> with PricingContext(is_async=True):
        >>>     price_f = cap.dollar_price()
        >>>
        >>> while not price_f.done:
        >>>     ...
        """
        super().__init__()

        self.__pricing_date = pricing_date or business_day_offset(
            dt.date.today(), 0, roll='forward')
        self.__csa_term = csa_term
        self.__is_async = is_async
        self.__is_batch = is_batch
        self.__poll_for_batch_results = poll_for_batch_results
        self.__batch_results_timeout = batch_results_timeout
        self.__use_cache = use_cache
        self.__visible_to_gs = visible_to_gs
        self.__market = market
        self.__lock = Lock()
        self.__pending = {}

        if self.__market is None:
            # Do not use self.__class__.current - it will cause a cycle
            default_location = market_data_location or (
                self.__class__.path[0].market_data_location
                if self.__class__.path else PricingLocation.LDN)
            market_data_date = business_day_offset(self.__pricing_date, -1, roll='preceding') if\
                self.__pricing_date == dt.date.today() else self.__pricing_date

            self.__market = market or ClosingMarket(default_location,
                                                    market_data_date)
Exemplo n.º 4
0
 def _pricing_market_data_as_of(self) -> Tuple[PricingDateAndMarketDataAsOf, ...]:
     return tuple(PricingDateAndMarketDataAsOf(d, business_day_offset(d, -1, roll='preceding') if d == dt.date.today() else d) for d in self.__date_range)