Exemplo n.º 1
0
def excess_returns(
        price_series: pd.Series,
        benchmark_or_rate: Union[Asset, Currency, float],
        *,
        day_count_convention=DayCountConvention.ACTUAL_360) -> pd.Series:
    if isinstance(benchmark_or_rate, float):
        er = [price_series.iloc[0]]
        for j in range(1, len(price_series)):
            fraction = day_count_fraction(price_series.index[j - 1],
                                          price_series.index[j],
                                          day_count_convention)
            er.append(er[-1] + price_series.iloc[j] -
                      price_series.iloc[j - 1] *
                      (1 + benchmark_or_rate * fraction))
        return pd.Series(er, index=price_series.index)

    if isinstance(benchmark_or_rate, Currency):
        try:
            marquee_id = SharpeAssets[benchmark_or_rate.value].value
        except KeyError:
            raise MqValueError(f"unsupported currency {benchmark_or_rate}")
    else:
        marquee_id = benchmark_or_rate.get_marquee_id()

    with DataContext(price_series.index[0], price_series.index[-1]):
        q = GsDataApi.build_market_data_query([marquee_id], QueryType.SPOT)
        df = GsDataApi.get_market_data(q)
    if df.empty:
        raise MqValueError(f'could not retrieve risk-free rate {marquee_id}')
    df = df[~df.index.duplicated(
        keep='first')]  # handle bad data (duplicate rows)

    return excess_returns_pure(price_series, df['spot'])
Exemplo n.º 2
0
def excess_returns(
        price_series: pd.Series,
        benchmark_or_rate: Union[Asset, Currency, float],
        *,
        day_count_convention=DayCountConvention.ACTUAL_360) -> pd.Series:
    if isinstance(benchmark_or_rate, float):
        er = [price_series.iloc[0]]
        for j in range(1, len(price_series)):
            fraction = day_count_fraction(price_series.index[j - 1],
                                          price_series.index[j],
                                          day_count_convention)
            er.append(er[-1] + price_series.iloc[j] -
                      price_series.iloc[j - 1] *
                      (1 + benchmark_or_rate * fraction))
        return pd.Series(er, index=price_series.index)

    if isinstance(benchmark_or_rate, Currency):
        try:
            marquee_id = SharpeAssets[benchmark_or_rate.value].value
        except KeyError:
            raise MqValueError(f"unsupported currency {benchmark_or_rate}")
    else:
        marquee_id = benchmark_or_rate.get_marquee_id()

    with DataContext(price_series.index[0], price_series.index[-1]):
        q = GsDataApi.build_market_data_query([marquee_id], QueryType.SPOT)
        df = GsDataApi.get_market_data(q)
    curve, bench_curve = align(price_series, df['spot'], Interpolate.INTERSECT)

    e_returns = [curve.iloc[0]]
    for i in range(1, len(curve)):
        multiplier = 1 + curve.iloc[i] / curve.iloc[
            i - 1] - bench_curve.iloc[i] / bench_curve.iloc[i - 1]
        e_returns.append(e_returns[-1] * multiplier)
    return pd.Series(e_returns, index=curve.index)
Exemplo n.º 3
0
def get_df_with_retries(fetcher, start_date, end_date, exchange, retries=1):
    """
    Loads results from Data Service by calling fetcher function. Shifts query date range back by business days until
    result is not empty or retry limit reached. This is a fallback feature in case a data upload is late. Measure
    implementations should be written such that retries are usually not required.

    :param fetcher: a no-argument function runs a data query and returns a DataFrame
    :param start_date: initial start date for query
    :param end_date: initial end date for query
    :param exchange: exchange to use for holiday calendar
    :param retries: maximum number of retries
    :return: DataFrame
    """
    retries = max(retries, 0)
    while retries > -1:
        with DataContext(start_date, end_date):
            result = fetcher()
        if not result.empty:
            break
        kwargs = {'exchanges': [exchange]} if exchange else {}
        # no need to include any part of the previous date range since it's known to be empty
        end_date = RelativeDate('-1b',
                                base_date=start_date).apply_rule(**kwargs)
        start_date = end_date
        retries -= 1
    return result
Exemplo n.º 4
0
def test_forward_looking():
    today = datetime.date.today()
    source = 'plottool'
    with DataContext(today, today + datetime.timedelta(days=1)):
        assert check_forward_looking('1b', None) is None
        assert check_forward_looking(today, None) is None
        assert check_forward_looking(None, None) is None
        assert check_forward_looking('1b', source) is None
        assert check_forward_looking(today, source) is None
        assert check_forward_looking(None, source) is None
    with DataContext(today - datetime.timedelta(days=1), today):
        assert check_forward_looking('1b', None) is None
        assert check_forward_looking(today, None) is None
        assert check_forward_looking(None, None) is None
        assert check_forward_looking('1b', source) is None
        assert check_forward_looking(today, source) is None
        with pytest.raises(MqError):
            check_forward_looking(None, source)
Exemplo n.º 5
0
def test_forward_price():
    # Tests for US NG assets
    def mock_natgas_forward_price(_cls, _q):
        d = {
            'forwardPrice': [
                2.880,
                2.844,
                2.726,
            ],
            'contract': [
                "F21",
                "G21",
                "H21",
            ]
        }
        df = MarketDataResponseFrame(data=d,
                                     index=pd.to_datetime(
                                         [datetime.date(2019, 1, 2)] * 3))
        df.dataset_ids = _test_datasets
        return df

    replace = Replacer()
    replace('gs_quant.timeseries.measures.GsDataApi.get_market_data',
            mock_natgas_forward_price)
    mock = CommodityNaturalGasHub('MA001', 'AGT')

    with DataContext(datetime.date(2019, 1, 2), datetime.date(2019, 1, 2)):
        actual = pd.Series(
            tm.forward_price(mock, price_method='GDD', contract_range='F21'))
        expected = pd.Series([2.880],
                             index=[datetime.date(2019, 1, 2)],
                             name='price')
        assert_series_equal(expected, actual)

    with pytest.raises(MqError):
        tm.forward_price(Cross('MA002', 'USD/EUR'),
                         price_method='GDD',
                         contract_range='F21')

    replace.restore()
Exemplo n.º 6
0
def basis_swap_term_structure(asset: Asset, spread_benchmark_type: BenchmarkType = None, spread_tenor: str = None,
                              reference_benchmark_type: BenchmarkType = None, reference_tenor: str = None,
                              forward_tenor: Optional[GENERIC_DATE] = None,
                              clearing_house: _ClearingHouse = None,
                              pricing_date: Optional[GENERIC_DATE] = None,
                              *, source: str = None, real_time: bool = False, ) -> Series:
    """
    GS end-of-day Floating-Floating interest rate swap (IRS) term structure across major currencies.


    :param asset: asset object loaded from security master
    :param spread_benchmark_type: benchmark type of spread leg on which basis spread is added e.g. LIBOR
    :param spread_tenor: relative date representation of expiration date of spread leg e.g. 1m
    :param reference_benchmark_type: benchmark type of reference leg e.g. LIBOR
    :param reference_tenor: relative date representation of expiration date of reference leg e.g. 1m
    :param forward_tenor: absolute / relative date representation of forward starting point eg: '1y' or 'Spot' for
    spot starting swaps, 'imm1' or 'frb1'
    :param clearing_house: Example - "LCH", "EUREX", "JSCC", "CME"
    :param pricing_date: YYYY-MM-DD or relative date
    :param source: name of function caller
    :param real_time: whether to retrieve intraday data instead of EOD
    :return: swap rate curve
    """
    if real_time:
        raise NotImplementedError('realtime basis_swap_rate not implemented')

    currency = CurrencyEnum(asset.get_identifier(AssetIdentifier.BLOOMBERG_ID))
    if currency.value not in ['JPY', 'EUR', 'USD', 'GBP']:
        raise NotImplementedError('Data not available for {} basis swap rates'.format(currency.value))

    clearing_house = _check_clearing_house(clearing_house)

    for benchmark_type in [spread_benchmark_type, reference_benchmark_type]:
        _check_benchmark_type(currency, benchmark_type)

    # default benchmark types
    legs_w_defaults = dict()
    legs_w_defaults['spread'] = _get_swap_leg_defaults(currency, spread_benchmark_type, spread_tenor)
    legs_w_defaults['reference'] = _get_swap_leg_defaults(currency, reference_benchmark_type, reference_tenor)

    for key, leg in legs_w_defaults.items():
        if not re.fullmatch('(\\d+)([bdwmy])', leg['floating_rate_tenor']):
            raise MqValueError('invalid floating rate tenor ' + leg['floating_rate_tenor'] + ' index: ' +
                               leg['benchmark_type'])

    forward_tenor = check_forward_tenor(forward_tenor)

    calendar = legs_w_defaults['spread']['pricing_location'].value
    if pricing_date is not None and pricing_date in list(GsCalendar.get(calendar).holidays):
        raise MqValueError('Specified pricing date is a holiday in {} calendar'.format(calendar))

    csaTerms = currency.value + '-1'

    kwargs = dict(type='BasisSwap', asset_parameters_payer_rate_option=legs_w_defaults['spread']['benchmark_type'],
                  asset_parameters_payer_designated_maturity=legs_w_defaults['spread']['floating_rate_tenor'],
                  asset_parameters_receiver_rate_option=legs_w_defaults['reference']['benchmark_type'],
                  asset_parameters_receiver_designated_maturity=legs_w_defaults['reference']['floating_rate_tenor'],
                  asset_parameters_clearing_house=clearing_house.value, asset_parameters_effective_date=forward_tenor,
                  asset_parameters_notional_currency=currency.name,
                  pricing_location=legs_w_defaults['spread']['pricing_location'].value)

    assets = GsAssetApi.get_many_assets(**kwargs)
    if len(assets) == 0:
        raise MqValueError('Specified arguments did not match any asset in the dataset')
    else:
        rate_mqids = [asset.id for asset in assets]

    asset_string = ''
    for mqid in rate_mqids:
        asset_string = asset_string + ',' + mqid
    _logger.debug('assets returned %s', asset_string)

    _logger.debug('where spread_benchmark_type=%s, spread_tenor=%s,  reference_benchmark_type=%s, '
                  'reference_tenor=%s, forward_tenor=%s, pricing_location=%s ',
                  legs_w_defaults['spread']['benchmark_type'], legs_w_defaults['spread']['floating_rate_tenor'],
                  legs_w_defaults['reference']['benchmark_type'], legs_w_defaults['reference']['floating_rate_tenor'],
                  forward_tenor, legs_w_defaults['spread']['pricing_location'].value)

    start, end = _range_from_pricing_date(calendar, pricing_date)
    with DataContext(start, end):
        where = dict(csaTerms=csaTerms)
        q = GsDataApi.build_market_data_query(rate_mqids, QueryType.BASIS_SWAP_RATE, where=where,
                                              source=source, real_time=real_time)
        _logger.debug('q %s', q)
        df = _market_data_timed(q)

    if df.empty:
        series = ExtendedSeries()
    else:
        latest = df.index.max()
        _logger.info('selected pricing date %s', latest)
        df = df.loc[latest]
        business_day = _get_custom_bd(calendar)
        df = df.assign(expirationDate=df.index + df['terminationTenor'].map(_to_offset) + business_day - business_day)
        df = df.set_index('expirationDate')
        df.sort_index(inplace=True)
        df = df.loc[DataContext.current.start_date: DataContext.current.end_date]
        series = ExtendedSeries() if df.empty else ExtendedSeries(df['basisSwapRate'])
    series.dataset_ids = getattr(df, 'dataset_ids', ())
    return series
Exemplo n.º 7
0
def basis_swap_term_structure(
    asset: Asset,
    spread_benchmark_type: BenchmarkType = None,
    spread_tenor: str = None,
    reference_benchmark_type: BenchmarkType = None,
    reference_tenor: str = None,
    forward_tenor: str = 'Spot',
    pricing_date: Optional[GENERIC_DATE] = None,
    *,
    source: str = None,
    real_time: bool = False,
) -> Series:
    """
    GS end-of-day Floating-Floating interest rate swap (IRS) term structure across major currencies.


    :param asset: asset object loaded from security master
    :param spread_benchmark_type: benchmark type of spread leg on which basis spread is added e.g. LIBOR
    :param spread_tenor: relative date representation of expiration date of spread leg e.g. 1m
    :param reference_benchmark_type: benchmark type of reference leg e.g. LIBOR
    :param reference_tenor: relative date representation of expiration date of reference leg e.g. 1m
    :param forward_tenor: relative date representation of forward starting point eg: '1y'  or 'Spot' for spot
           starting swaps
    :param pricing_date: YYYY-MM-DD or relative date
    :param source: name of function caller
    :param real_time: whether to retrieve intraday data instead of EOD
    :return: swap rate curve
    """
    if real_time:
        raise NotImplementedError('realtime basis_swap_rate not implemented')

    currency = CurrencyEnum(asset.get_identifier(AssetIdentifier.BLOOMBERG_ID))
    if currency.value not in ['JPY', 'EUR', 'USD', 'GBP']:
        raise NotImplementedError(
            'Data not available for {} basis swap rates'.format(
                currency.value))

    for benchmark_type in [spread_benchmark_type, reference_benchmark_type]:
        if benchmark_type is not None and \
                benchmark_type.value not in CURRENCY_TO_SWAP_RATE_BENCHMARK[currency.value].keys():
            raise MqValueError('%s is not supported for %s', benchmark_type,
                               currency.value)

    for floating_rate_tenor in [spread_tenor, reference_tenor]:
        if not re.fullmatch('(\\d+)([bdwmy])', floating_rate_tenor):
            raise MqValueError('invalid floating rate tenor ' +
                               floating_rate_tenor)

    if forward_tenor == '0b' or forward_tenor is None or forward_tenor == 'Spot':
        forward_tenor = '0d'
    elif not re.fullmatch('(\\d+)([bdwmy])', forward_tenor):
        raise MqValueError('invalid forward tenor ' + forward_tenor)

    # default benchmark types
    legs_w_defaults = dict()
    legs_w_defaults['spread'] = _get_swap_leg_defaults(currency,
                                                       spread_benchmark_type,
                                                       spread_tenor)
    legs_w_defaults['reference'] = _get_swap_leg_defaults(
        currency, reference_benchmark_type, reference_tenor)

    csaTerms = currency.value + '-1'
    clearing_house = 'LCH'

    kwargs = dict(
        type='BasisSwap',
        asset_parameters_payer_rate_option=legs_w_defaults['spread']
        ['benchmark_type'],
        asset_parameters_payer_designated_maturity=legs_w_defaults['spread']
        ['floating_rate_tenor'],
        asset_parameters_receiver_rate_option=legs_w_defaults['reference']
        ['benchmark_type'],
        asset_parameters_receiver_designated_maturity=legs_w_defaults[
            'reference']['floating_rate_tenor'],
        asset_parameters_clearing_house=clearing_house,
        asset_parameters_effective_date=forward_tenor,
        asset_parameters_notional_currency=currency.name,
        pricing_location=legs_w_defaults['spread']['pricing_location'])

    assets = GsAssetApi.get_many_assets(**kwargs)
    if len(assets) == 0:
        raise MqValueError(
            'Specified arguments did not match any asset in the dataset')
    else:
        rate_mqids = [asset.id for asset in assets]

    _logger.debug(
        'where spread_benchmark_type=%s, spread_tenor=%s,  reference_benchmark_type=%s, '
        'reference_tenor=%s, forward_tenor=%s, pricing_location=%s ',
        legs_w_defaults['spread']['benchmark_type'],
        legs_w_defaults['spread']['floating_rate_tenor'],
        legs_w_defaults['reference']['benchmark_type'],
        legs_w_defaults['reference']['floating_rate_tenor'], forward_tenor,
        legs_w_defaults['spread']['pricing_location'])

    start, end = _range_from_pricing_date(assets[0].exchange, pricing_date)
    with DataContext(start, end):
        where = FieldFilterMap(csaTerms=csaTerms)
        q = GsDataApi.build_market_data_query(rate_mqids,
                                              QueryType.BASIS_SWAP_RATE,
                                              where=where,
                                              source=source,
                                              real_time=real_time)
        _logger.debug('q %s', q)
        df = _market_data_timed(q)
    if df.empty:
        return pd.Series()
    latest = df.index.max()
    _logger.info('selected pricing date %s', latest)
    df = df.loc[latest]
    business_day = _get_custom_bd(asset.exchange)
    df = df.assign(expirationDate=df.index +
                   df['terminationTenor'].map(_to_offset) + business_day -
                   business_day)
    df = df.set_index('expirationDate')
    df.sort_index(inplace=True)
    df = df.loc[DataContext.current.start_date:DataContext.current.end_date]
    return df['basisSwapRate'] if not df.empty else pd.Series()
Exemplo n.º 8
0
def test_inflation_swap_term(mocker):
    replace = Replacer()
    args = dict(forward_tenor='1y',
                pricing_date='0d',
                clearing_house=_ClearingHouse.LCH,
                real_time=False)

    class ObjectView(object):
        def __init__(self, d):
            self.__dict__ = d

    holidays = replace('gs_quant.datetime.GsCalendar.get', Mock(holidays=[]))
    holidays.return_value = ObjectView({'holidays': []})

    bd_calendar = replace(
        'gs_quant.timeseries.measures_inflation._get_custom_bd', Mock())
    bd_calendar.return_value = CustomBusinessDay()
    pricing_date_mock = replace(
        'gs_quant.timeseries.measures_inflation._range_from_pricing_date',
        Mock())
    pricing_date_mock.return_value = [dt.date(2019, 1, 1), dt.date(2019, 1, 1)]

    mock_nok = Currency('MA891', 'PLN')
    xrefs = replace('gs_quant.timeseries.measures.Asset.get_identifier',
                    Mock())
    xrefs.return_value = 'ACU'
    args['asset'] = mock_nok
    with pytest.raises(NotImplementedError):
        tm.inflation_swap_term(**args)

    mock_usd = Currency('MAZ7RWC904JYHYPS', 'USD')
    args['asset'] = mock_usd
    xrefs = replace('gs_quant.timeseries.measures.Asset.get_identifier',
                    Mock())
    xrefs.return_value = 'USD'
    args['real_time'] = True
    with pytest.raises(NotImplementedError):
        tm.inflation_swap_term(**args)
    args['real_time'] = False

    args['forward_tenor'] = '5yr'
    with pytest.raises(MqValueError):
        tm.inflation_swap_term(**args)
    args['forward_tenor'] = '1y'

    bd_mock = replace('gs_quant.data.dataset.Dataset.get_data', Mock())
    bd_mock.return_value = pd.DataFrame(data=dict(date="2020-04-10",
                                                  exchange="NYC",
                                                  description="Good Friday"),
                                        index=[pd.Timestamp('2020-04-10')])

    xrefs = replace('gs_quant.timeseries.measures.Asset.get_identifier',
                    Mock())
    xrefs.return_value = 'USD'
    identifiers_empty = replace(
        'gs_quant.timeseries.measures.GsAssetApi.get_many_assets', Mock())
    identifiers_empty.return_value = {}
    with pytest.raises(MqValueError):
        tm.inflation_swap_term(**args)

    identifiers = replace(
        'gs_quant.timeseries.measures.GsAssetApi.get_many_assets', Mock())
    mock_asset = Currency('USD', name='USD')
    mock_asset.id = 'MAEMPCXQG3T716EX'
    mock_asset.exchange = 'OTC'
    identifiers.return_value = [mock_asset]

    d = {
        'terminationTenor': ['1y', '2y', '3y', '4y'],
        'swapRate': [1, 2, 3, 4],
        'assetId': [
            'MAEMPCXQG3T716EX', 'MAFRSWPAF5QPNTP2', 'MA88BXZ3TCTXTFW1',
            'MAC4KAG9B9ZAZHFT'
        ]
    }

    bd_mock.return_value = pd.DataFrame()
    market_data_mock = replace(
        'gs_quant.timeseries.measures_inflation._get_inflation_swap_data',
        Mock())

    market_data_mock.return_value = pd.DataFrame()
    df = pd.DataFrame(data=d, index=_index * 4)
    actual = tm.inflation_swap_term(**args)
    assert actual.empty

    series_apply_mock = replace(
        'gs_quant.timeseries.measures_inflation.pd.Series.apply', Mock())
    series_apply_mock.return_value = pd.Series([
        dt.date(2022, 3, 30),
        dt.date(2023, 3, 30),
        dt.date(2024, 3, 30),
        dt.date(2025, 3, 30)
    ],
                                               index=df.index)

    market_data_mock.return_value = df
    with DataContext('2019-01-01', '2026-01-01'):
        actual = tm.inflation_swap_term(**args)
    actual.dataset_ids = _test_datasets
    expected = tm.ExtendedSeries([1, 2, 3, 4],
                                 index=[
                                     dt.date(2022, 3, 30),
                                     dt.date(2023, 3, 30),
                                     dt.date(2024, 3, 30),
                                     dt.date(2025, 3, 30)
                                 ])
    expected.dataset_ids = _test_datasets
    assert_series_equal(expected, actual, check_names=False)
    assert actual.dataset_ids == expected.dataset_ids

    args['location'] = PricingLocation.NYC
    with DataContext('2019-01-01', '2026-01-01'):
        tm.inflation_swap_term(**args)

    holidays.return_value = ObjectView({'holidays': ['0d']})
    with pytest.raises(MqValueError):
        tm.inflation_swap_term(**args)

    replace.restore()
Exemplo n.º 9
0
def swap_term_structure(asset: Asset,
                        benchmark_type: BenchmarkType = None,
                        floating_rate_tenor: str = None,
                        forward_tenor: Optional[GENERIC_DATE] = None,
                        pricing_date: Optional[GENERIC_DATE] = None,
                        *,
                        source: str = None,
                        real_time: bool = False) -> Series:
    """
    GS end-of-day Fixed-Floating interest rate swap (IRS) term structure across major currencies.

    :param asset: asset object loaded from security master
    :param benchmark_type: benchmark type e.g. LIBOR
    :param floating_rate_tenor: floating index rate
    :param forward_tenor: absolute / relative date representation of forward starting point eg: '1y' or 'Spot' for
    spot starting swaps, 'imm1' or 'frb1'
    :param pricing_date: YYYY-MM-DD or relative date
    :param source: name of function caller
    :param real_time: whether to retrieve intraday data instead of EOD
    :return: swap rate term structure
    """
    if real_time:
        raise NotImplementedError('realtime swap_rate not implemented')

    currency = asset.get_identifier(AssetIdentifier.BLOOMBERG_ID)
    currency = CurrencyEnum(currency)
    if currency.value not in ['JPY', 'EUR', 'USD', 'GBP', 'CHF', 'SEK']:
        raise NotImplementedError(
            'Data not available for {} swap rates'.format(currency.value))
    clearing_house = 'LCH'

    _check_benchmark_type(currency, benchmark_type)

    forward_tenor = check_forward_tenor(forward_tenor)

    defaults = _get_swap_leg_defaults(currency, benchmark_type,
                                      floating_rate_tenor)

    if not re.fullmatch('(\\d+)([bdwmy])', defaults['floating_rate_tenor']):
        raise MqValueError('invalid floating rate tenor ' +
                           defaults['floating_rate_tenor'] + ' for index: ' +
                           defaults['benchmark_type'])

    calendar = defaults['pricing_location'].value
    if pricing_date is not None and pricing_date in list(
            GsCalendar.get(calendar).holidays):
        raise MqValueError(
            'Specified pricing date is a holiday in {} calendar'.format(
                calendar))

    csaTerms = currency.value + '-1'
    fixed_rate = 'ATM'
    kwargs = dict(
        type='Swap',
        asset_parameters_floating_rate_option=defaults['benchmark_type'],
        asset_parameters_fixed_rate=fixed_rate,
        asset_parameters_clearing_house=clearing_house,
        asset_parameters_floating_rate_designated_maturity=defaults[
            'floating_rate_tenor'],
        asset_parameters_effective_date=forward_tenor,
        asset_parameters_notional_currency=currency.name,
        pricing_location=defaults['pricing_location'].value)

    assets = GsAssetApi.get_many_assets(**kwargs)
    if len(assets) == 0:
        raise MqValueError(
            'Specified arguments did not match any asset in the dataset')
    else:
        rate_mqids = [asset.id for asset in assets]

    asset_string = ''
    for mqid in rate_mqids:
        asset_string = asset_string + ',' + mqid
    _logger.debug('assets returned %s', asset_string)

    _logger.debug(
        'where benchmark_type=%s, floating_rate_tenor=%s, forward_tenor=%s, '
        'pricing_location=%s', defaults['benchmark_type'],
        defaults['floating_rate_tenor'], forward_tenor,
        defaults['pricing_location'].value)

    start, end = _range_from_pricing_date(calendar, pricing_date)
    with DataContext(start, end):
        where = FieldFilterMap(csaTerms=csaTerms)
        q = GsDataApi.build_market_data_query(rate_mqids,
                                              QueryType.SWAP_RATE,
                                              where=where,
                                              source=source,
                                              real_time=real_time)
        _logger.debug('q %s', q)
        df = _market_data_timed(q)

    if df.empty:
        return pd.Series()
    latest = df.index.max()
    _logger.info('selected pricing date %s', latest)
    df = df.loc[latest]
    business_day = _get_custom_bd(calendar)
    df = df.assign(expirationDate=df.index +
                   df['terminationTenor'].map(_to_offset) + business_day -
                   business_day)
    df = df.set_index('expirationDate')
    df.sort_index(inplace=True)
    df = df.loc[DataContext.current.start_date:DataContext.current.end_date]
    return df['swapRate'] if not df.empty else pd.Series()
Exemplo n.º 10
0
def inflation_swap_term(asset: Asset,
                        index_type: str = None,
                        forward_tenor: Optional[GENERIC_DATE] = None,
                        pricing_date: Optional[GENERIC_DATE] = None,
                        clearing_house: tm_rates._ClearingHouse = None,
                        location: PricingLocation = None,
                        *,
                        source: str = None,
                        real_time: bool = False,
                        request_id: Optional[str] = None) -> Series:
    """
    Forward term structure of GS end-of-day inflation swaps.

    :param asset: asset object loaded from security master
    :param index_type: benchmark type e.g. UKRPI
    :param forward_tenor: absolute / relative date representation of forward starting point eg: '1y' or 'Spot' for
            spot starting swaps, 'imm1' or 'frb1'
    :param pricing_date: YYYY-MM-DD or relative date
    :param clearing_house: Example - "LCH", "EUREX", "JSCC", "CME"
    :param location: Example - "TKO", "LDN", "NYC"
    :param source: name of function caller
    :param real_time: whether to retrieve intraday data instead of EOD
    :param request_id: service request id, if any
    :return: inflation swap forward term curve
    """
    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))
    if location is None:
        location = tm_rates._default_pricing_location(currency)
    else:
        location = PricingLocation(location)
    calendar = location.value
    if pricing_date is not None and pricing_date in list(
            GsCalendar.get(calendar).holidays):
        raise MqValueError(
            'Specified pricing date is a holiday in {} calendar'.format(
                calendar))
    start, end = _range_from_pricing_date(calendar, pricing_date)
    with DataContext(start, end):
        df = _get_inflation_swap_data(asset=asset,
                                      swap_tenor=None,
                                      index_type=index_type,
                                      forward_tenor=forward_tenor,
                                      clearing_house=clearing_house,
                                      source=source,
                                      real_time=real_time,
                                      query_type=QueryType.SWAP_RATE,
                                      location=location,
                                      allow_many=True,
                                      request_id=request_id)
    if df.empty:
        series = ExtendedSeries(dtype=float)
    else:
        latest = df.index.max()
        # TODO: add forward tenor to latest. Technically series index should be latest + forwardTenor + terminationTenor
        # TODO: but that would make it hard to compare term structure btwn different forwardTenors
        # TODO: may be implemented some day when plot can handle different x-axes
        # TODO: As-is, this is consistent with other swap term measures axis handling
        _logger.info('selected pricing date %s', latest)
        df = df.loc[latest]
        biz_day = _get_custom_bd(calendar)
        df['expirationDate'] = df['terminationTenor'].apply(
            _get_term_struct_date, args=(latest, biz_day))
        df = df.set_index('expirationDate')
        df.sort_index(inplace=True)
        df = df.loc[DataContext.current.start_date:DataContext.current.
                    end_date]
        series = ExtendedSeries(
            dtype=float) if df.empty else ExtendedSeries(df['swapRate'])
        series.dataset_ids = getattr(df, 'dataset_ids', ())
    if series.empty:  # Raise descriptive error if no data returned + historical date context
        check_forward_looking(None, source, 'inflation_swap_term')
    return series
Exemplo n.º 11
0
def basis_swap_term_structure(
    asset: Asset,
    spread_benchmark_type: str = None,
    spread_tenor: str = None,
    reference_benchmark_type: str = None,
    reference_tenor: str = None,
    forward_tenor: Optional[GENERIC_DATE] = None,
    clearing_house: _ClearingHouse = None,
    pricing_date: Optional[GENERIC_DATE] = None,
    *,
    source: str = None,
    real_time: bool = False,
) -> Series:
    """
    GS end-of-day Floating-Floating interest rate swap (IRS) term structure across major currencies.


    :param asset: asset object loaded from security master
    :param spread_benchmark_type: benchmark type of spread leg on which basis spread is added e.g. LIBOR
    :param spread_tenor: relative date representation of expiration date of spread leg e.g. 1m
    :param reference_benchmark_type: benchmark type of reference leg e.g. LIBOR
    :param reference_tenor: relative date representation of expiration date of reference leg e.g. 1m
    :param forward_tenor: absolute / relative date representation of forward starting point eg: '1y' or 'Spot' for
            spot starting swaps, 'imm1' or 'frb1'
    :param clearing_house: Example - "LCH", "EUREX", "JSCC", "CME"
    :param pricing_date: YYYY-MM-DD or relative date
    :param source: name of function caller
    :param real_time: whether to retrieve intraday data instead of EOD
    :return: swap rate curve
    """
    if real_time:
        raise NotImplementedError('realtime basis_swap_rate not implemented')

    kwargs = _get_basis_swap_kwargs(
        asset=asset,
        spread_benchmark_type=spread_benchmark_type,
        spread_tenor=spread_tenor,
        reference_benchmark_type=reference_benchmark_type,
        reference_tenor=reference_tenor,
        forward_tenor=forward_tenor,
        clearing_house=clearing_house)

    calendar = kwargs['pricing_location']
    if pricing_date is not None and pricing_date in list(
            GsCalendar.get(calendar).holidays):
        raise MqValueError(
            'Specified pricing date is a holiday in {} calendar'.format(
                calendar))

    rate_mqids = _get_mdapi_rates_assets(**kwargs)

    _logger.debug('assets returned %s', ', '.join(rate_mqids))
    _logger.debug(
        'where spread_benchmark_type=%s, spread_tenor=%s,  reference_benchmark_type=%s, '
        'reference_tenor=%s, forward_tenor=%s, pricing_location=%s ',
        kwargs['asset_parameters_payer_rate_option'],
        kwargs['asset_parameters_payer_designated_maturity'],
        kwargs['asset_parameters_receiver_rate_option'],
        kwargs['asset_parameters_receiver_designated_maturity'],
        kwargs['asset_parameters_effective_date'], kwargs['pricing_location'])
    start, end = _range_from_pricing_date(calendar, pricing_date)
    with DataContext(start, end):
        where = dict(csaTerms=kwargs['asset_parameters_notional_currency'] +
                     '-1')
        q = GsDataApi.build_market_data_query(rate_mqids,
                                              QueryType.BASIS_SWAP_RATE,
                                              where=where,
                                              source=source,
                                              real_time=real_time)
        _logger.debug('q %s', q)
        df = _market_data_timed(q)

    if df.empty:
        series = ExtendedSeries()
    else:
        latest = df.index.max()
        _logger.info('selected pricing date %s', latest)
        df = df.loc[latest]
        business_day = _get_custom_bd(calendar)
        df = df.assign(expirationDate=df.index +
                       df['terminationTenor'].map(_to_offset) + business_day -
                       business_day)
        df = df.set_index('expirationDate')
        df.sort_index(inplace=True)
        df = df.loc[DataContext.current.start_date:DataContext.current.
                    end_date]
        series = ExtendedSeries() if df.empty else ExtendedSeries(
            df['basisSwapRate'])
    series.dataset_ids = getattr(df, 'dataset_ids', ())
    return series
Exemplo n.º 12
0
def basis_swap_term_structure(
    asset: Asset,
    spread_benchmark_type: str = None,
    spread_tenor: str = None,
    reference_benchmark_type: str = None,
    reference_tenor: str = None,
    tenor_type: _SwapTenorType = None,
    tenor: Optional[GENERIC_DATE] = None,
    clearing_house: _ClearingHouse = None,
    pricing_date: Optional[GENERIC_DATE] = None,
    *,
    source: str = None,
    real_time: bool = False,
) -> Series:
    """
    GS end-of-day Floating-Floating interest rate swap (IRS) term structure across major currencies.


    :param asset: asset object loaded from security master
    :param spread_benchmark_type: benchmark type of spread leg on which basis spread is added e.g. LIBOR
    :param spread_tenor: relative date representation of expiration date of spread leg e.g. 1m
    :param reference_benchmark_type: benchmark type of reference leg e.g. LIBOR
    :param reference_tenor: relative date representation of expiration date of reference leg e.g. 1m
    :param tenor_type: specify which tenor should be fixed, SWAP_TENOR or FORWARD_TENOR
    :param tenor: absolute / relative date representation of forward starting point or swap maturity
    :param clearing_house: Example - "LCH", "EUREX", "JSCC", "CME"
    :param pricing_date: YYYY-MM-DD or relative date
    :param source: name of function caller
    :param real_time: whether to retrieve intraday data instead of EOD
    :return: swap rate curve
    """
    if real_time:
        raise NotImplementedError('realtime basis_swap_rate not implemented')
    tenor_type = _check_tenor_type(tenor_type)
    tenor_dict = _check_term_structure_tenor(tenor_type=tenor_type,
                                             tenor=tenor)
    kwargs = _get_basis_swap_kwargs(
        asset=asset,
        spread_benchmark_type=spread_benchmark_type,
        spread_tenor=spread_tenor,
        reference_benchmark_type=reference_benchmark_type,
        reference_tenor=reference_tenor,
        clearing_house=clearing_house)
    kwargs[tenor_dict['tenor_dataset_field']] = tenor_dict['tenor']
    calendar = kwargs['pricing_location']
    if pricing_date is not None and pricing_date in list(
            GsCalendar.get(calendar).holidays):
        raise MqValueError(
            'Specified pricing date is a holiday in {} calendar'.format(
                calendar))

    rate_mqids = _get_mdapi_rates_assets(**kwargs)
    if isinstance(rate_mqids, str):  # single asset returned
        rate_mqids = [rate_mqids]
    _logger.debug('assets returned %s', ', '.join(rate_mqids))
    _logger.debug(
        'where spread_benchmark_type=%s, spread_tenor=%s,  reference_benchmark_type=%s, '
        'reference_tenor=%s, %s=%s, pricing_location=%s ',
        kwargs['asset_parameters_payer_rate_option'],
        kwargs['asset_parameters_payer_designated_maturity'],
        kwargs['asset_parameters_receiver_rate_option'],
        kwargs['asset_parameters_receiver_designated_maturity'],
        kwargs[tenor_dict['tenor_dataset_field']], tenor_dict['tenor'],
        kwargs['pricing_location'])

    where = _get_basis_swap_csa_terms(
        kwargs['asset_parameters_notional_currency'],
        kwargs['asset_parameters_payer_rate_option'],
        kwargs['asset_parameters_receiver_rate_option'])
    start, end = _range_from_pricing_date(calendar, pricing_date)
    with DataContext(start, end):
        q = GsDataApi.build_market_data_query(rate_mqids,
                                              QueryType.BASIS_SWAP_RATE,
                                              where=where,
                                              source=source,
                                              real_time=real_time)
        _logger.debug('q %s', q)
        df = _market_data_timed(q)

    if df.empty:
        series = ExtendedSeries()
    else:
        latest = df.index.max()
        _logger.info('selected pricing date %s', latest)
        df = df.loc[latest]
        biz_day = _get_custom_bd(calendar)
        col_to_plot = tenor_dict['tenor_to_plot']
        if isinstance(df, pd.Series):  # single asset returned
            series = ExtendedSeries(
                pd.Series(df['basisSwapRate'],
                          index=[
                              _get_term_struct_date(df[col_to_plot], latest,
                                                    biz_day)
                          ]))
            series = series.loc[DataContext.current.start_date:DataContext.
                                current.end_date]
        else:
            if col_to_plot == 'effectiveTenor':  # for forward term structure imm date assets
                df = df[~df[col_to_plot].isin(['imm1', 'imm2', 'imm3', 'imm4']
                                              )]
            df['expirationDate'] = df[col_to_plot].apply(_get_term_struct_date,
                                                         args=(latest,
                                                               biz_day))
            df = df.set_index('expirationDate')
            df.sort_index(inplace=True)
            df = df.loc[DataContext.current.start_date:DataContext.current.
                        end_date]
            series = ExtendedSeries() if df.empty else ExtendedSeries(
                df['basisSwapRate'])
    series.dataset_ids = getattr(df, 'dataset_ids', ())
    return series
Exemplo n.º 13
0
def swap_term_structure(asset: Asset,
                        benchmark_type: str = None,
                        floating_rate_tenor: str = None,
                        tenor_type: _SwapTenorType = None,
                        tenor: Optional[GENERIC_DATE] = None,
                        clearing_house: _ClearingHouse = None,
                        pricing_date: Optional[GENERIC_DATE] = None,
                        *,
                        source: str = None,
                        real_time: bool = False) -> Series:
    """
    GS end-of-day Fixed-Floating interest rate swap (IRS) term structure across major currencies.

    :param asset: asset object loaded from security master
    :param benchmark_type: benchmark type e.g. LIBOR
    :param floating_rate_tenor: floating index rate
    :param tenor_type: specify which tenor should be fixed, SWAP_TENOR or FORWARD_TENOR
    :param tenor: absolute / relative date representation of forward starting point or swap maturity
    :param clearing_house: Example - "LCH", "EUREX", "JSCC", "CME"
    :param pricing_date: YYYY-MM-DD or relative date
    :param source: name of function caller
    :param real_time: whether to retrieve intraday data instead of EOD
    :return: swap rate term structure
    """
    if real_time:
        raise NotImplementedError('realtime swap_rate not implemented')

    currency = asset.get_identifier(AssetIdentifier.BLOOMBERG_ID)
    currency = CurrencyEnum(currency)
    if currency.value not in ['JPY', 'EUR', 'USD', 'GBP', 'CHF', 'SEK']:
        raise NotImplementedError(
            'Data not available for {} swap rates'.format(currency.value))

    clearing_house = _check_clearing_house(clearing_house)
    benchmark_type = _check_benchmark_type(currency, benchmark_type)
    tenor_type = _check_tenor_type(tenor_type)
    tenor_dict = _check_term_structure_tenor(tenor_type=tenor_type,
                                             tenor=tenor)
    defaults = _get_swap_leg_defaults(currency, benchmark_type,
                                      floating_rate_tenor)

    if not re.fullmatch('(\\d+)([bdwmy])', defaults['floating_rate_tenor']):
        raise MqValueError('invalid floating rate tenor ' +
                           defaults['floating_rate_tenor'] + ' for index: ' +
                           defaults['benchmark_type'])

    calendar = defaults['pricing_location'].value
    if pricing_date is not None and pricing_date in list(
            GsCalendar.get(calendar).holidays):
        raise MqValueError(
            'Specified pricing date is a holiday in {} calendar'.format(
                calendar))

    fixed_rate = 'ATM'
    kwargs = dict(
        type='Swap',
        asset_parameters_floating_rate_option=defaults['benchmark_type'],
        asset_parameters_fixed_rate=fixed_rate,
        asset_parameters_clearing_house=clearing_house.value,
        asset_parameters_floating_rate_designated_maturity=defaults[
            'floating_rate_tenor'],
        asset_parameters_notional_currency=currency.name,
        pricing_location=defaults['pricing_location'].value)
    kwargs[tenor_dict['tenor_dataset_field']] = tenor_dict['tenor']
    rate_mqids = _get_mdapi_rates_assets(**kwargs)
    if isinstance(rate_mqids, str):
        rate_mqids = [rate_mqids]
    _logger.debug('assets returned %s', ', '.join(rate_mqids))
    _logger.debug(
        'where benchmark_type=%s, floating_rate_tenor=%s, %s=%s, '
        'pricing_location=%s', defaults['benchmark_type'],
        defaults['floating_rate_tenor'], tenor_type.value, tenor_dict['tenor'],
        defaults['pricing_location'].value)
    where = _get_swap_csa_terms(currency.value, defaults['benchmark_type'])
    start, end = _range_from_pricing_date(calendar, pricing_date)
    with DataContext(start, end):
        q = GsDataApi.build_market_data_query(rate_mqids,
                                              QueryType.SWAP_RATE,
                                              where=where,
                                              source=source,
                                              real_time=real_time)
        _logger.debug('q %s', q)
        df = _market_data_timed(q)

    if df.empty:
        series = ExtendedSeries()
    else:
        latest = df.index.max()
        _logger.info('selected pricing date %s', latest)
        df = df.loc[latest]
        biz_day = _get_custom_bd(calendar)
        col_to_plot = tenor_dict['tenor_to_plot']
        if isinstance(df, pd.Series):
            series = ExtendedSeries(
                pd.Series(df['swapRate'],
                          index=[
                              _get_term_struct_date(df[col_to_plot], latest,
                                                    biz_day)
                          ]))
            series = series.loc[DataContext.current.start_date:DataContext.
                                current.end_date]
        else:
            if col_to_plot == 'effectiveTenor':
                df = df[~df[col_to_plot].isin(['imm1', 'imm2', 'imm3', 'imm4']
                                              )]
            df['expirationDate'] = df[col_to_plot].apply(_get_term_struct_date,
                                                         args=(latest,
                                                               biz_day))
            df = df.set_index('expirationDate')
            df.sort_index(inplace=True)
            df = df.loc[DataContext.current.start_date:DataContext.current.
                        end_date]
            series = ExtendedSeries() if df.empty else ExtendedSeries(
                df['swapRate'])
    series.dataset_ids = getattr(df, 'dataset_ids', ())
    return series