示例#1
0
文件: core.py 项目: samwu101/GS-Bot
    def calc(
        self, priceable: Priceable, risk_measure: RiskMeasure
    ) -> Union[float, dict, pd.DataFrame, pd.Series, Future]:
        """
        Calculate the risk measure for the priceable instrument. Do not use directly, use via instruments

        :param priceable: The priceable (e.g. instrument)
        :param risk_measure: The measure we wish to calculate
        :return: A float, Dataframe, Series or Future (depending on is_async or whether the context is entered)

        **Examples**

        >>> from gs_quant.instrument import IRSwap
        >>> from gs_quant.risk import IRDelta
        >>>
        >>> swap = IRSwap('Pay', '10y', 'USD', fixedRate=0.03)
        >>> delta = swap.calc(IRDelta)
        """
        position = RiskPosition(priceable, priceable.get_quantity())
        future = self.__futures.get(risk_measure, {}).get(position)
        if future is None:
            future = Future()
            self.__risk_measures_by_provider_and_position.setdefault(
                priceable.provider(), {}).setdefault(position,
                                                     set()).add(risk_measure)
            self.__futures.setdefault(risk_measure, {})[position] = future

        if not self._is_entered and not self.__is_async:
            self._calc()
            return future.result()
        else:
            return future
示例#2
0
    def calc(self, priceable: Priceable, risk_measure: Union[RiskMeasure, Iterable[RiskMeasure]])\
            -> Union[list, DataFrameWithInfo, ErrorValue, FloatWithInfo, Future, MultipleRiskMeasureFuture,
                     SeriesWithInfo]:
        """
        Calculate the risk measure for the priceable instrument. Do not use directly, use via instruments

        :param priceable: The priceable (e.g. instrument)
        :param risk_measure: The measure we wish to calculate
        :return: A float, Dataframe, Series or Future (depending on is_async or whether the context is entered)

        **Examples**

        >>> from gs_quant.instrument import IRSwap
        >>> from gs_quant.risk import IRDelta
        >>>
        >>> swap = IRSwap('Pay', '10y', 'USD', fixed_rate=0.01)
        >>> delta = swap.calc(IRDelta)
        """
        position = RiskPosition(priceable, priceable.get_quantity())
        multiple_measures = not isinstance(risk_measure, RiskMeasure)
        futures = {}
        active_context_lock = self.__active_context.__lock if self.__active_context != self else nullcontext(
        )

        with self.__lock, active_context_lock:
            for measure in risk_measure if multiple_measures else (
                    risk_measure, ):
                scenario = self.__scenario
                measure_future = self.__active_context.__futures.get(
                    (scenario, measure), {}).get(position)

                if measure_future is None:
                    measure_future = PricingFuture(self.__active_context)
                    if self.__use_cache:
                        cached_result = PricingCache.get(
                            priceable, risk_measure)
                        if cached_result:
                            measure_future.set_result(cached_result)

                    if not measure_future.done():
                        self.__risk_measures_in_scenario_by_provider_and_position.setdefault(
                            priceable.provider(),
                            {}).setdefault(position,
                                           {}).setdefault(scenario,
                                                          set()).add(measure)
                        self.__active_context.__futures.setdefault(
                            (scenario, measure), {})[position] = measure_future

                futures[measure] = measure_future

        future = MultipleRiskMeasureFuture(futures, result_future=PricingFuture(self.__active_context))\
            if multiple_measures else futures[risk_measure]

        if not (self.is_entered or self.__is_async):
            if not future.done():
                self._calc()

            return future.result()
        else:
            return future
示例#3
0
    def calc(self, priceable: Priceable, risk_measure: Union[RiskMeasure, Iterable[RiskMeasure]])\
            -> Union[dict, float, str, pd.DataFrame, pd.Series, Future]:
        """
        Calculate the risk measure for the priceable instrument. Do not use directly, use via instruments

        :param priceable: The priceable (e.g. instrument)
        :param risk_measure: The measure we wish to calculate
        :return: A float, Dataframe, Series or Future (depending on is_async or whether the context is entered)

        **Examples**

        >>> from gs_quant.instrument import IRSwap
        >>> from gs_quant.risk import IRDelta
        >>>
        >>> swap = IRSwap('Pay', '10y', 'USD', fixed_rate=0.01)
        >>> delta = swap.calc(IRDelta)
        """
        from gs_quant.risk.results import MultipleRiskMeasureFuture

        position = RiskPosition(priceable, priceable.get_quantity())
        multiple_measures = not isinstance(risk_measure, RiskMeasure)
        futures = {}

        for measure in risk_measure if multiple_measures else (risk_measure, ):
            measure_future = self.__futures.get(measure, {}).get(position)

            if measure_future is None:
                measure_future = Future()
                if self.__use_cache:
                    cached_result = PricingCache.get(priceable,
                                                     self.market_data_location,
                                                     risk_measure,
                                                     self.pricing_date)
                    if cached_result:
                        measure_future.set_result(cached_result)

                if not measure_future.done():
                    self.__risk_measures_by_provider_and_position.setdefault(
                        priceable.provider(),
                        {}).setdefault(position, set()).add(measure)
                    self.__futures.setdefault(measure,
                                              {})[position] = measure_future

            futures[measure] = measure_future

        future = MultipleRiskMeasureFuture(
            futures) if multiple_measures else futures[risk_measure]

        if not (self._is_entered or self.__is_async):
            if not future.done():
                self._calc()

            return future.result()
        else:
            return future
示例#4
0
    def calc(self, priceable: Priceable,
             risk_measure: RiskMeasure) -> PricingFuture:
        futures = []

        provider = priceable.provider()
        scenario = self._scenario
        parameters = self._parameters
        location = self.market.location

        for date in self.__date_range:
            market = CloseMarket(location=location,
                                 date=close_market_date(location, date))
            risk_key = RiskKey(provider, date, market, parameters, scenario,
                               risk_measure)
            futures.append(self._calc(priceable, risk_key))

        return HistoricalPricingFuture(futures)
示例#5
0
    def calc(self, priceable: Priceable, risk_measure: Union[RiskMeasure, Iterable[RiskMeasure]])\
            -> Union[list, DataFrameWithInfo, ErrorValue, FloatWithInfo, Future, MultipleRiskMeasureFuture,
                     SeriesWithInfo]:
        """
        Calculate the risk measure for the priceable instrument. Do not use directly, use via instruments

        :param priceable: The priceable (e.g. instrument)
        :param risk_measure: The measure we wish to calculate
        :return: A float, Dataframe, Series or Future (depending on is_async or whether the context is entered)

        **Examples**

        >>> from gs_quant.instrument import IRSwap
        >>> from gs_quant.risk import IRDelta
        >>>
        >>> swap = IRSwap('Pay', '10y', 'USD', fixed_rate=0.01)
        >>> delta = swap.calc(IRDelta)
        """
        futures = {}

        with self.__active_context.__lock:
            for risk_measure in (risk_measure, ) if isinstance(
                    risk_measure, RiskMeasure) else risk_measure:
                risk_key = self.__risk_key(risk_measure, priceable.provider())
                future = self.__active_context.__pending.get(
                    (risk_key, priceable))
                cached_result = PricingCache.get(
                    risk_key, priceable) if self.use_cache else None

                if future is None:
                    future = PricingFuture(self.__active_context)
                    if cached_result is not None:
                        future.set_result(cached_result)
                    else:
                        self.__active_context.__pending[(risk_key,
                                                         priceable)] = future

                futures[risk_measure] = future

        future = MultipleRiskMeasureFuture(futures, result_future=self._result_future())\
            if len(futures) > 1 else futures[risk_measure]

        return self._return_calc_result(future)
示例#6
0
    def calc(self, priceable: Priceable,
             risk_measure: RiskMeasure) -> PricingFuture:
        """
        Calculate the risk measure for the priceable instrument. Do not use directly, use via instruments

        :param priceable: The priceable (e.g. instrument)
        :param risk_measure: The measure we wish to calculate
        :return: A PricingFuture whose result will be the calculation result

        **Examples**

        >>> from gs_quant.instrument import IRSwap
        >>> from gs_quant.risk import IRDelta
        >>>
        >>> swap = IRSwap('Pay', '10y', 'USD', fixed_rate=0.01)
        >>> delta = swap.calc(IRDelta)
        """
        return self._calc(priceable,
                          self.__risk_key(risk_measure, priceable.provider()))
示例#7
0
    def calc(self, priceable: Priceable,
             risk_measure: RiskMeasure) -> PricingFuture:
        """
        Calculate the risk measure for the priceable instrument. Do not use directly, use via instruments

        :param priceable: The priceable (e.g. instrument)
        :param risk_measure: The measure we wish to calculate
        :return: A PricingFuture whose result will be the calculation result

        **Examples**

        >>> from gs_quant.instrument import IRSwap
        >>> from gs_quant.risk import IRDelta
        >>>
        >>> swap = IRSwap('Pay', '10y', 'USD', fixed_rate=0.01)
        >>> delta = swap.calc(IRDelta)
        """
        with self.active_context.__lock:
            risk_key = self.__risk_key(risk_measure, priceable.provider())
            future = self.active_context.__pending.get((risk_key, priceable))

            if future is None:
                future = PricingFuture()
                cached_result = PricingCache.get(
                    risk_key, priceable) if self.use_cache else None

                if cached_result is not None:
                    future.set_result(cached_result)
                else:
                    self.active_context.__pending[(risk_key,
                                                   priceable)] = future

        if not (self.is_entered or self.is_async):
            self.__calc()

        return future