Exemplo n.º 1
0
def test_vol_swap_strike_matches_no_assets_when_expiry_tenor_is_not_none():
    with pytest.raises(MqValueError):
        replace = Replacer()
        base = Cross('MA667', 'EURUSD')
        xrefs = replace('gs_quant.timeseries.measures.Asset.get_identifier',
                        Mock())
        xrefs.return_value = 'EURUSD'

        mock_asset_1 = GsAsset(asset_class='FX',
                               id='MA123',
                               type_='VolatilitySwap',
                               name='Test_asset',
                               parameters={"lastFixingDate": "1y"})

        mock_asset_2 = GsAsset(asset_class='FX',
                               id='MA123',
                               type_='VolatilitySwap',
                               name='Test_asset',
                               parameters={"lastFixingDate": "1y"})

        assets = replace(
            'gs_quant.timeseries.measures.GsAssetApi.get_many_assets', Mock())
        assets.return_value = [mock_asset_1, mock_asset_2]

        tm_fxo.vol_swap_strike(base,
                               "10m",
                               location=PricingLocation.LDN,
                               real_time=False)
Exemplo n.º 2
0
def test_get_many_assets(mocker, monkeypatch):
    marquee_id_1 = 'MQA1234567890'
    marquee_id_2 = 'MQA4567890123'

    query = {'id': [marquee_id_1, marquee_id_2]}
    as_of = dt.datetime.utcnow()

    inputs = EntityQuery(where=FieldFilterMap(**query),
                         fields=None,
                         asOfTime=as_of,
                         limit=100)

    mock_response = {
        'results': (GsAsset.from_dict({
            'id': marquee_id_1,
            'assetClass': 'Equity',
            'type': 'Single Stock',
            'name': 'Test 1'
        }),
                    GsAsset.from_dict({
                        'id': marquee_id_2,
                        'assetClass': 'Equity',
                        'type': 'Single Stock',
                        'name': 'Test 2'
                    }))
    }

    expected_response = (GsAsset(id=marquee_id_1,
                                 assetClass='Equity',
                                 type='Single Stock',
                                 name='Test 1'),
                         GsAsset(id=marquee_id_2,
                                 assetClass='Equity',
                                 type='Single Stock',
                                 name='Test 2'))

    # mock GsSession
    mocker.patch.object(GsSession.__class__,
                        'default_value',
                        return_value=GsSession.get(Environment.QA, 'client_id',
                                                   'secret'))
    mocker.patch.object(GsSession.current, '_post', return_value=mock_response)

    # run test
    monkeypatch.delenv(ENABLE_ASSET_CACHING, raising=False)
    response = GsAssetApi.get_many_assets(id=[marquee_id_1, marquee_id_2],
                                          as_of=as_of)
    GsSession.current._post.assert_called_with('/assets/query',
                                               cls=GsAsset,
                                               payload=inputs)
    assert response == expected_response

    monkeypatch.setenv(ENABLE_ASSET_CACHING, 1)  # run 2x with cache on
    response = GsAssetApi.get_many_assets(id=[marquee_id_1, marquee_id_2],
                                          as_of=as_of)
    assert response == expected_response
    response = GsAssetApi.get_many_assets(id=[marquee_id_1, marquee_id_2],
                                          as_of=as_of)
    assert response == expected_response
def test_get_tdapi_inflation_rates_assets(mocker):
    mock_asset_1 = GsAsset(asset_class='Rate',
                           id='MA26QSMPX9990G66',
                           type_='InflationSwap',
                           name='Test_asset')
    mock_asset_2 = GsAsset(asset_class='Rate',
                           id='MA44SBCHF192S6FR',
                           type_='InflationSwap',
                           name='Test_asset')

    replace = Replacer()
    assets = replace('gs_quant.timeseries.measures.GsAssetApi.get_many_assets',
                     Mock())
    assets.return_value = [mock_asset_1]
    assert 'MA26QSMPX9990G66' == tm._get_tdapi_inflation_rates_assets()
    replace.restore()

    assets = replace('gs_quant.timeseries.measures.GsAssetApi.get_many_assets',
                     Mock())
    assets.return_value = [mock_asset_1, mock_asset_2]
    kwargs = dict(asset_parameters_termination_date='5y',
                  asset_parameters_effective_date='0b')
    with pytest.raises(MqValueError):
        tm._get_tdapi_inflation_rates_assets(**kwargs)
    replace.restore()

    assets = replace('gs_quant.timeseries.measures.GsAssetApi.get_many_assets',
                     Mock())
    assets.return_value = []
    kwargs = dict(asset_parameters_clearing_house='NONE',
                  pricing_location='LDN')
    with pytest.raises(MqValueError):
        tm._get_tdapi_inflation_rates_assets(**kwargs)
    replace.restore()

    assets = replace('gs_quant.timeseries.measures.GsAssetApi.get_many_assets',
                     Mock())
    assets.return_value = [mock_asset_1, mock_asset_2]
    kwargs = dict()
    assert ['MA26QSMPX9990G66', 'MA44SBCHF192S6FR'
            ] == tm._get_tdapi_inflation_rates_assets(**kwargs)
    replace.restore()

    #   test case will test matching sofr maturity with libor leg and flipping legs to get right asset
    kwargs = dict(type='InflationSwap',
                  asset_parameters_termination_date='5y',
                  asset_parameters_index=tm.InflationIndexType.UKRPI,
                  asset_parameters_clearing_house='None',
                  asset_parameters_effective_date='5y',
                  asset_parameters_notional_currency='GBP',
                  pricing_location='LDN')

    assets = replace('gs_quant.timeseries.measures.GsAssetApi.get_many_assets',
                     Mock())
    assets.return_value = [mock_asset_1]
    assert 'MA26QSMPX9990G66' == tm._get_tdapi_inflation_rates_assets(**kwargs)
    replace.restore()
Exemplo n.º 4
0
def test_get_tdapi_crosscurrency_rates_assets(mocker):
    mock_asset_1 = GsAsset(asset_class='Rate', id='MAW8SAXPSKYA94E2', type_='XccySwapMTM', name='Test_asset')
    mock_asset_2 = GsAsset(asset_class='Rate', id='MATDD783JM1C2GGD', type_='XccySwapMTM', name='Test_asset')

    replace = Replacer()
    assets = replace('gs_quant.timeseries.measures.GsAssetApi.get_many_assets', Mock())
    assets.return_value = [mock_asset_1]
    assert 'MAW8SAXPSKYA94E2' == tm._get_tdapi_crosscurrency_rates_assets()
    replace.restore()

    assets = replace('gs_quant.timeseries.measures.GsAssetApi.get_many_assets', Mock())
    assets.return_value = [mock_asset_1, mock_asset_2]
    kwargs = dict(asset_parameters_termination_date='5y', asset_parameters_effective_date='0b')
    with pytest.raises(MqValueError):
        tm._get_tdapi_crosscurrency_rates_assets(**kwargs)
    replace.restore()

    assets = replace('gs_quant.timeseries.measures.GsAssetApi.get_many_assets', Mock())
    assets.return_value = []
    kwargs = dict(asset_parameters_clearing_house='NONE',
                  asset_parameters_payer_rate_option="EUR-EURIBOR-TELERATE",
                  asset_parameters_payer_currency='EUR',
                  asset_parameters_payer_designated_maturity='3m',
                  asset_parameters_receiver_rate_option="USD-LIBOR-BBA",
                  asset_parameters_receiver_currency='USD',
                  asset_parameters_receiver_designated_maturity='3m',
                  pricing_location='LDN')
    with pytest.raises(MqValueError):
        tm._get_tdapi_crosscurrency_rates_assets(**kwargs)
    replace.restore()

    assets = replace('gs_quant.timeseries.measures.GsAssetApi.get_many_assets', Mock())
    assets.return_value = [mock_asset_1, mock_asset_2]
    kwargs = dict()
    assert ['MAW8SAXPSKYA94E2', 'MATDD783JM1C2GGD'] == tm._get_tdapi_crosscurrency_rates_assets(**kwargs)
    replace.restore()

    #   test case will test matching sofr maturity with libor leg and flipping legs to get right asset
    kwargs = dict(type='XccySwapMTM', asset_parameters_termination_date='5y',
                  asset_parameters_payer_rate_option="EUR-EURIBOR-TELERATE",
                  asset_parameters_payer_currency="EUR",
                  asset_parameters_payer_designated_maturity='3m',
                  asset_parameters_receiver_rate_option="USD-LIBOR-BBA",
                  asset_parameters_receiver_currency="USD",
                  asset_parameters_receiver_designated_maturity='3m',
                  asset_parameters_clearing_house='None', asset_parameters_effective_date='5y',
                  pricing_location='LDN')

    assets = replace('gs_quant.timeseries.measures.GsAssetApi.get_many_assets', Mock())
    assets.return_value = [mock_asset_1]
    assert 'MAW8SAXPSKYA94E2' == tm._get_tdapi_crosscurrency_rates_assets(**kwargs)
    replace.restore()
Exemplo n.º 5
0
def test_get_tdapi_fxo_assets():
    mock_asset_1 = GsAsset(asset_class='FX', id='MAW8SAXPSKYA94E2', type_='Option', name='Test_asset')
    mock_asset_2 = GsAsset(asset_class='FX', id='MATDD783JM1C2GGD', type_='Option', name='Test_asset')

    replace = Replacer()
    assets = replace('gs_quant.timeseries.measures.GsAssetApi.get_many_assets', Mock())
    assets.return_value = [mock_asset_1]
    assert 'MAW8SAXPSKYA94E2' == tm_fxo._get_tdapi_fxo_assets()
    replace.restore()

    assets = replace('gs_quant.timeseries.measures.GsAssetApi.get_many_assets', Mock())
    assets.return_value = [mock_asset_1, mock_asset_2]
    kwargs = dict(asset_parameters_expiration_date='5y', asset_parameters_call_currency='USD',
                  asset_parameters_put_currency='EUR')
    with pytest.raises(MqValueError):
        tm_fxo._get_tdapi_fxo_assets(**kwargs)
    replace.restore()

    assets = replace('gs_quant.timeseries.measures.GsAssetApi.get_many_assets', Mock())
    assets.return_value = []
    kwargs = dict(asset_parameters_expiration_date='5y', asset_parameters_call_currency='USD',
                  asset_parameters_put_currency='EUR')
    with pytest.raises(MqValueError):
        tm_fxo._get_tdapi_fxo_assets(**kwargs)
    replace.restore()

    assets = replace('gs_quant.timeseries.measures.GsAssetApi.get_many_assets', Mock())
    assets.return_value = [mock_asset_1, mock_asset_2]
    kwargs = dict()
    assert ['MAW8SAXPSKYA94E2', 'MATDD783JM1C2GGD'] == tm._get_tdapi_crosscurrency_rates_assets(**kwargs)
    replace.restore()

    #   test case will test matching sofr maturity with libor leg and flipping legs to get right asset
    kwargs = dict(Asset_class='FX',
                  type='Option',
                  asset_parameters_call_currency='USD',
                  asset_parameters_put_currency='EUR',
                  asset_parameters_expiration_date='1m',
                  asset_parameters_expiration_time='NYC',
                  asset_parameters_option_type='Put',
                  asset_parameters_premium_payment_date='Fwd Settle',
                  asset_parameters_strike_price_relative='10d',
                  pricing_location='NYC')

    assets = replace('gs_quant.timeseries.measures.GsAssetApi.get_many_assets', Mock())
    assets.return_value = [mock_asset_1]
    assert 'MAW8SAXPSKYA94E2' == tm_fxo._get_tdapi_fxo_assets(**kwargs)
    replace.restore()
Exemplo n.º 6
0
def test_rebalance_raises_exception(mocker):
    # construct inputs
    index_marquee_id = 'MQIDIndex'
    asset1_marquee_id = 'MQIDAsset1'
    asset2_marquee_id = 'MQIDAsset2'
    position_set = [{'assetId': asset1_marquee_id, 'quantity': 100}, {'assetId': asset2_marquee_id, 'quantity': 200}]
    publish_parameters = PublishParameters(False, False, False)
    pricing_parameters = IndicesPriceParameters()
    pricing_parameters.initialPrice = 100
    parameters = {
        'publishParameters': publish_parameters,
        'pricingParameters': pricing_parameters,
        'positionSet': position_set}
    inputs = IndicesRebalanceInputs(parameters)
    # mock GsSession
    mock_asset = GsAsset('Equity', AssetType.Custom_Basket, 'Test Basket')
    mock_response = CustomBasketsResponse('done', 'approvalId', 'reportId', index_marquee_id)
    mocker.patch.object(ContextMeta, 'current', return_value=GsSession(Environment.QA))
    mocker.patch.object(ContextMeta.current, '_get', return_value=mock_asset)
    mocker.patch.object(ContextMeta.current, '_post', return_value=mock_response)
    # run test
    index = GsIndexApi()
    with pytest.raises(Exception):
        response = index.rebalance(inputs)
        print(response)
Exemplo n.º 7
0
def test_rebalance(mocker):
    # construct inputs
    index_marquee_id = 'MQIDIndex'
    asset1_marquee_id = 'MQIDAsset1'
    asset2_marquee_id = 'MQIDAsset2'
    position_set = [{'assetId': asset1_marquee_id, 'quantity': 100}, {'assetId': asset2_marquee_id, 'quantity': 200}]
    publish_parameters = PublishParameters(False, False, False)
    pricing_parameters = IndicesPriceParameters()
    pricing_parameters.initialPrice = 100
    parameters = {
        'publishParameters': publish_parameters,
        'pricingParameters': pricing_parameters,
        'positionSet': position_set}
    inputs = IndicesRebalanceInputs(parameters)
    # mock GsSession
    mock_asset = GsAsset('Equity', AssetType.Custom_Basket, 'Test Basket')
    mock_response = CustomBasketsResponse('done', 'approvalId', 'reportId', index_marquee_id)
    mocker.patch.object(ContextMeta, 'current', return_value=GsSession(Environment.QA))
    mocker.patch.object(ContextMeta.current, '_post', return_value=mock_response)
    mocker.patch.object(GsAssetApi, 'get_asset', return_value=mock_asset)
    # run test
    index = GsIndexApi(index_marquee_id)
    response = index.rebalance(inputs)
    ContextMeta.current._post.assert_called_with("/indices/{id}/rebalance".format(id=index_marquee_id),
                                                 payload=inputs, cls=CustomBasketsResponse)
    assert response == mock_response
Exemplo n.º 8
0
def test_vol_swap_strike_matches_multiple_assets():
    with pytest.raises(MqValueError):
        replace = Replacer()
        base = Cross('MA667', 'EURUSD')
        xrefs = replace('gs_quant.timeseries.measures.Asset.get_identifier', Mock())
        xrefs.return_value = 'EURUSD'
        mock_asset_1 = GsAsset(asset_class='FX', id='MA123', type_='VolatilitySwap', name='Test_asset')
        assets = replace('gs_quant.timeseries.measures.GsAssetApi.get_many_assets', Mock())
        assets.return_value = [mock_asset_1, mock_asset_1]
        tm_fxo.vol_swap_strike(base, None, location=PricingLocation.LDN, real_time=False)
    replace.restore()
Exemplo n.º 9
0
def test_vol_swap_strike_raises_exception():
    with pytest.raises(NotImplementedError):
        mock_asset_1 = GsAsset(asset_class='FX',
                               id='MAW8SAXPSKYA94E2',
                               type_='Option',
                               name='Test_asset')
        tm_fxo.vol_swap_strike(mock_asset_1,
                               "none",
                               "none",
                               location=PricingLocation.LDN,
                               real_time=True)
Exemplo n.º 10
0
def test_vol_swap_strike():
    replace = Replacer()
    base = Cross('MA667', 'EURUSD')
    xrefs = replace('gs_quant.timeseries.measures.Asset.get_identifier', Mock())
    xrefs.return_value = 'EURUSD'

    mock_asset_1 = GsAsset(asset_class='FX', id='MA123', type_='VolatilitySwap', name='Test_asset',
                           parameters={"lastFixingDate": "1y"})
    mock_asset_2 = GsAsset(asset_class='FX', id='MA123', type_='VolatilitySwap', name='Test_asset',
                           parameters={"lastFixingDate": "2y"})

    assets = replace('gs_quant.timeseries.measures.GsAssetApi.get_many_assets', Mock())
    assets.return_value = [mock_asset_1, mock_asset_2]
    mock_data = replace('gs_quant.timeseries.measures.GsDataApi.get_market_data', Mock())
    mock_data.return_value = mock_df()
    actual = tm_fxo.vol_swap_strike(base, "1y", location=PricingLocation.LDN, real_time=False)
    assert_series_equal(tm_rates._extract_series_from_df(mock_df(), QueryType.STRIKE_VOL), actual)

    actual = tm_fxo.vol_swap_strike(base, "1y", None, real_time=False)
    assert_series_equal(tm_rates._extract_series_from_df(mock_df(), QueryType.STRIKE_VOL), actual)
    replace.restore()
Exemplo n.º 11
0
def test_get_asset(mocker):
    marquee_id = 'MQA1234567890'

    mock_response = GsAsset(id=marquee_id, assetClass='Equity', type='Single Stock', name='Test Asset')

    # mock GsSession
    mocker.patch.object(GsSession.__class__, 'default_value',
                        return_value=GsSession.get(Environment.QA, 'client_id', 'secret'))
    mocker.patch.object(GsSession.current, '_get', return_value=mock_response)

    # run test
    response = GsAssetApi.get_asset(marquee_id)

    GsSession.current._get.assert_called_with('/assets/{id}'.format(id=marquee_id), cls=GsAsset)

    assert response == mock_response
Exemplo n.º 12
0
from gs_quant.target.indices import CustomBasketsResponse, CustomBasketRiskParams
from gs_quant.target.reports import Report, User as TargetUser

# Helper mock value constants
asset_1 = {'name': 'asset 1', 'id': 'id1', 'bbid': 'bbid1'}
asset_2 = {'name': 'asset 2', 'id': 'id2', 'bbid': 'bbid2'}
assets_data = [asset_1, asset_2]
base_user = {
    'name': 'First Last',
    'email': '*****@*****.**',
    'company': 'Company A'
}
cb_response = CustomBasketsResponse('done', 'R1234567890', 'MA1234567890')
gs_asset = GsAsset(asset_class=AssetClass.Equity,
                   type_=AssetType.Custom_Basket,
                   name='Test Basket',
                   id_='MA1234567890',
                   entitlements=TargetEntitlements(admin=['guid:user_abc']),
                   xref=XRef(ticker='GSMBXXXX'))
initial_price = {'price': 100}
mqid = 'MA1234567890'
name = 'Test Basket'
positions = [
    Position('bbid1', asset_id='id1', quantity=100),
    Position('bbid2', asset_id='id2', quantity=200)
]
positions_weighted = positions = [
    Position('bbid1', asset_id='id1', weight=0.4),
    Position('bbid2', asset_id='id2', weight=0.6)
]
position_set = PositionSet(positions, divisor=1000)
report = Report(mqid,