예제 #1
0
파일: core.py 프로젝트: rainscut/gs-quant
def __risk_measure_with_doc_string(
    doc: str,
    measure_type: RiskMeasureType,
    asset_class: Optional[AssetClass] = None,
    unit: Optional[RiskMeasureUnit] = None
) -> RiskMeasure:
    measure = RiskMeasure(measure_type=measure_type, asset_class=asset_class, unit=unit)
    measure.__doc__ = doc
    return measure
예제 #2
0
def __risk_measure_with_doc_string(
        doc: str,
        measureType: RiskMeasureType,
        assetClass: AssetClass = None,
        unit: RiskMeasureUnit = None) -> RiskMeasure:
    measure = RiskMeasure(measureType=measureType,
                          assetClass=assetClass,
                          unit=unit)
    measure.__doc__ = doc
    return measure
예제 #3
0
    def resolve_fields(self, priceable: Priceable,
                       in_place: bool) -> Optional[Union[Priceable, Future]]:
        """
        Resolve fields on the priceable which were not supplied. Do not use directly, use via instruments

        :param priceable:  The priceable (e.g. instrument)
        :param in_place:   Resolve in place or return a new Priceable

        **Examples**

        >>> from gs_quant.instrument import IRSwap
        >>>
        >>> swap = IRSwap('Pay', '10y', 'USD')
        >>> rate = swap.fixed_rate

        fixedRate is None

        >>> swap.resolve()
        >>> rate = swap.fixed_rate

        fixed_rate is now the solved value
        """
        # TODO Handle these correctly in the risk service
        invalid_defaults = ('-- N/A --', 'NaN')
        value_mappings = {
            'Payer': 'Pay',
            'Rec': 'Receive',
            'Receiver': 'Receive'
        }

        def apply_field_values(field_values: Union[dict, list, tuple, Future],
                               priceable_inst: Priceable,
                               resolution_info: dict,
                               future: Optional[Future] = None):
            if isinstance(field_values, str):
                raise RuntimeError(field_values)

            if isinstance(field_values, Future):
                field_values = field_values.result()

            if isinstance(field_values, (list, tuple)):
                if len(field_values) == 1:
                    field_values = field_values[0]
                else:
                    future.set_result({
                        dt.date.fromtimestamp(fv['date'] / 1e9):
                        apply_field_values(fv, priceable_inst)
                        for fv in field_values
                    })
                    return

            field_values = {
                field: value_mappings.get(value, value)
                for field, value in field_values.items()
                if inflection.underscore(field) in priceable_inst.properties()
                and value not in invalid_defaults
            }

            if in_place and not future:
                priceable_inst.unresolved = copy.copy(priceable_inst)
                for field, value in field_values.items():
                    setattr(priceable_inst, field, value)

                priceable_inst._resolution_info = resolution_info
            else:
                new_inst = priceable_inst._from_dict(field_values)
                new_inst.unresolved = priceable_inst
                new_inst._resolution_info = resolution_info

                if future:
                    future.set_result(new_inst)
                else:
                    return new_inst

        resolution_info = {
            'pricing_date': self.pricing_date,
            'market_data_as_of': self.market_data_as_of,
            'market_data_location': self.market_data_location
        }

        if priceable._resolution_info:
            if in_place:
                if resolution_info != priceable._resolution_info:
                    _logger.warning(
                        'Calling resolve() on an instrument which was already resolved under a difference PricingContext'
                    )

                return
            elif resolution_info == priceable._resolution_info:
                return copy.copy(priceable)

        res = self.calc(priceable,
                        RiskMeasure(measure_type='Resolved Instrument Values'))
        if isinstance(res, Future):
            ret = Future() if not in_place else None
            res.add_done_callback(
                functools.partial(apply_field_values,
                                  priceable_inst=priceable,
                                  resolution_info=resolution_info,
                                  future=ret))
            return ret
        else:
            return apply_field_values(res, priceable, resolution_info)
예제 #4
0
파일: core.py 프로젝트: samwu101/GS-Bot
    def resolve_fields(self, priceable: Priceable,
                       in_place: bool) -> Optional[Union[Priceable, Future]]:
        """
        Resolve fields on the priceable which were not supplied. Do not use directly, use via instruments

        :param priceable:  The priceable (e.g. instrument)
        :param in_place:   Resolve in place or return a new Priceable

        **Examples**

        >>> from gs_quant.instrument import IRSwap
        >>>
        >>> swap = IRSwap('Pay', '10y', 'USD')
        >>> rate = swap.fixedRate

        fixedRate is None

        >>> swap.resolve()
        >>> rate = swap.fixedRate

        fixedRate is now the solved value
        """
        # TODO Handle these correctly in the risk service
        invalid_defaults = ('-- N/A --', 'NaN')
        value_mappings = {
            'Payer': 'Pay',
            'Rec': 'Receive',
            'Receiver': 'Receive'
        }

        def apply_field_values(field_values: Union[dict, list, tuple, Future],
                               priceable_inst: Priceable,
                               future: Optional[Future] = None):
            if isinstance(field_values, str):
                raise RuntimeError(field_values)

            if isinstance(field_values, Future):
                field_values = field_values.result()

            if isinstance(field_values, (list, tuple)):
                if len(field_values) == 1:
                    field_values = field_values[0]
                else:
                    future.set_result({
                        dt.date.fromtimestamp(fv['date'] / 1e9):
                        apply_field_values(fv, priceable_inst)
                        for fv in field_values
                    })
                    return

            field_values = {
                field: value_mappings.get(value, value)
                for field, value in field_values.items()
                if field in priceable_inst.properties()
                and value not in invalid_defaults
            }

            if in_place and not future:
                for field, value in field_values.items():
                    setattr(priceable_inst, field, value)

                priceable_inst._resolved = True
            else:
                new_inst = priceable_inst._from_dict(field_values)
                new_inst._resolved = True

                if future:
                    future.set_result(new_inst)
                else:
                    return new_inst

        res = self.calc(priceable,
                        RiskMeasure(measureType='Resolved Instrument Values'))
        if isinstance(res, Future):
            ret = Future() if not in_place else None
            res.add_done_callback(
                functools.partial(apply_field_values,
                                  priceable_inst=priceable,
                                  future=ret))
            return ret
        else:
            return apply_field_values(res, priceable)