Example #1
0
def test_pricing_location_normalized():
    assert _pricing_location_normalized(
        PricingLocation.LDN, CurrencyEnum.USD) == PricingLocation.LDN
    assert _pricing_location_normalized(
        PricingLocation.TKO, CurrencyEnum.USD) == PricingLocation.TKO
    assert _pricing_location_normalized(
        PricingLocation.LDN, CurrencyEnum.HKD) == PricingLocation.LDN
    assert _pricing_location_normalized(
        PricingLocation.TKO, CurrencyEnum.HKD) == PricingLocation.HKG
Example #2
0
def _get_crosscurrency_swap_data(
        asset1: Asset,
        asset2: Asset,
        swap_tenor: str,
        rateoption_type: str = None,
        forward_tenor: Optional[GENERIC_DATE] = None,
        clearing_house: tm_rates._ClearingHouse = None,
        source: str = None,
        real_time: bool = False,
        query_type: QueryType = QueryType.SWAP_RATE,
        location: PricingLocation = None) -> pd.DataFrame:
    if real_time:
        raise NotImplementedError(
            'realtime swap_rate not implemented for anything but rates')

    currency1 = CurrencyEnum(
        asset1.get_identifier(AssetIdentifier.BLOOMBERG_ID))
    currency2 = CurrencyEnum(
        asset2.get_identifier(AssetIdentifier.BLOOMBERG_ID))

    if currency1.value not in CURRENCY_TO_XCCY_SWAP_RATE_BENCHMARK.keys():
        raise NotImplementedError(
            'Data not available for {} crosscurrency swap rates'.format(
                currency1.value))
    if currency2.value not in CURRENCY_TO_XCCY_SWAP_RATE_BENCHMARK.keys():
        raise NotImplementedError(
            'Data not available for {} crosscurrency swap rates'.format(
                currency2.value))

    rateoption_type1 = _check_crosscurrency_rateoption_type(
        currency1, rateoption_type)
    rateoption_type2 = _check_crosscurrency_rateoption_type(
        currency2, rateoption_type)

    if rateoption_type1 != rateoption_type2:
        raise MqValueError(
            'The two currencies do not both support the rate Option type ' +
            rateoption_type)
    rateoption_type = rateoption_type1

    clearing_house = tm_rates._check_clearing_house(clearing_house)

    defaults1 = _get_crosscurrency_swap_leg_defaults(currency1,
                                                     rateoption_type)
    defaults2 = _get_crosscurrency_swap_leg_defaults(currency2,
                                                     rateoption_type)

    if not (tm_rates._is_valid_relative_date_tenor(swap_tenor)):
        raise MqValueError('invalid swap tenor ' + swap_tenor)

    if defaults1["pricing_location"] == PricingLocation.NYC:
        default_location = defaults2["pricing_location"]
        currency = currency2
    else:
        default_location = defaults1["pricing_location"]
        currency = currency1

    if location is None:
        pricing_location = PricingLocation(default_location)
    else:
        pricing_location = PricingLocation(location)
    pricing_location = tm_rates._pricing_location_normalized(
        pricing_location, currency)
    where = dict(pricingLocation=pricing_location.value)

    forward_tenor = tm_rates._check_forward_tenor(forward_tenor)
    fixed_rate = 'ATM'
    kwargs = dict(
        asset_class='Rates',
        type='XccySwapMTM',
        asset_parameters_termination_date=swap_tenor,
        asset_parameters_effective_date=forward_tenor,
        asset_parameters_payer_spread=fixed_rate,
        # asset_parameters_payer_currency=defaults1['currency'].value,
        asset_parameters_payer_rate_option=defaults1['rateOption'],
        # asset_parameters_payer_designated_maturity=defaults1['designatedMaturity'],
        # asset_parameters_receiver_currency=defaults2['currency'].value,
        asset_parameters_receiver_rate_option=defaults2['rateOption'],
        # asset_parameters_receiver_designated_maturity=defaults2['designatedMaturity'],
        asset_parameters_clearing_house=clearing_house.value,
        pricing_location=pricing_location)

    rate_mqid = _get_tdapi_crosscurrency_rates_assets(**kwargs)

    _logger.debug(
        f'where asset= {rate_mqid}, swap_tenor={swap_tenor}, forward_tenor={forward_tenor}, '
        f'payer_currency={defaults1["currency"].value}, payer_rate_option={defaults1["rateOption"]}, '
        f'payer_designated_maturity={defaults1["designatedMaturity"]}, '
        f'receiver_currency={defaults2["currency"].value}, receiver_rate_option={defaults2["rateOption"]}, '
        f'receiver_designated_maturity={defaults2["designatedMaturity"]}, '
        f'clearing_house={clearing_house.value}, pricing_location={pricing_location.value}'
    )
    q = GsDataApi.build_market_data_query([rate_mqid],
                                          query_type,
                                          where=where,
                                          source=source,
                                          real_time=real_time)
    _logger.debug('q %s', q)
    df = _market_data_timed(q)
    return df
def _get_inflation_swap_data(asset: Asset,
                             swap_tenor: str,
                             index_type: str = None,
                             forward_tenor: Optional[GENERIC_DATE] = None,
                             clearing_house: tm_rates._ClearingHouse = None,
                             source: str = None,
                             real_time: bool = False,
                             query_type: QueryType = QueryType.SWAP_RATE,
                             location: PricingLocation = None) -> pd.DataFrame:
    if real_time:
        raise NotImplementedError(
            'realtime inflation swap data not implemented')
    currency = CurrencyEnum(asset.get_identifier(AssetIdentifier.BLOOMBERG_ID))

    if currency.value not in CURRENCY_TO_INDEX_BENCHMARK.keys():
        raise NotImplementedError(
            'Data not available for {} inflation swap rates'.format(
                currency.value))
    index_type = _check_inflation_index_type(currency, index_type)

    clearing_house = tm_rates._check_clearing_house(clearing_house)

    defaults = _get_inflation_swap_leg_defaults(currency, index_type)

    if not (tm_rates._is_valid_relative_date_tenor(swap_tenor)):
        raise MqValueError('invalid swap tenor ' + swap_tenor)

    forward_tenor = tm_rates._check_forward_tenor(forward_tenor)

    fixed_rate = 'ATM'
    kwargs = dict(asset_class='Rates',
                  type='InflationSwap',
                  asset_parameters_termination_date=swap_tenor,
                  asset_parameters_index=defaults['index_type'],
                  asset_parameters_fixed_rate=fixed_rate,
                  asset_parameters_clearing_house=clearing_house.value,
                  asset_parameters_effective_date=forward_tenor,
                  asset_parameters_notional_currency=currency.name)

    rate_mqid = _get_tdapi_inflation_rates_assets(**kwargs)

    if location is None:
        pricing_location = tm_rates._default_pricing_location(currency)
    else:
        pricing_location = PricingLocation(location)
    pricing_location = tm_rates._pricing_location_normalized(
        pricing_location, currency)
    where = dict(pricingLocation=pricing_location.value)

    _logger.debug(
        f'where asset= {rate_mqid}, swap_tenor={swap_tenor}, index={defaults["index_type"]}, '
        f'forward_tenor={forward_tenor}, pricing_location={pricing_location.value}, '
        f'clearing_house={clearing_house.value}, notional_currency={currency.name}'
    )
    q = GsDataApi.build_market_data_query([rate_mqid],
                                          query_type,
                                          where=where,
                                          source=source,
                                          real_time=real_time)
    _logger.debug('q %s', q)
    df = _market_data_timed(q)
    return df