Exemple #1
0
def test_vol():
    replace = Replacer()
    mock_spx = Index('MA890', AssetClass.Equity, 'SPX')
    replace('gs_quant.timeseries.measures.GsDataApi.get_market_data', mock_eq)
    actual = tm.implied_volatility(mock_spx, '1m', tm.VolReference.DELTA_CALL, 25)
    assert_series_equal(pd.Series([5, 1, 2], index=_index * 3, name='impliedVolatility'), actual)
    actual = tm.implied_volatility(mock_spx, '1m', tm.VolReference.DELTA_PUT, 75)
    assert_series_equal(pd.Series([5, 1, 2], index=_index * 3, name='impliedVolatility'), actual)
    with pytest.raises(NotImplementedError):
        tm.implied_volatility(mock_spx, '1m', tm.VolReference.DELTA_NEUTRAL)
    replace.restore()
Exemple #2
0
def implied_volatility_fxvol(asset: Asset,
                             tenor: str,
                             strike_reference: VolReference = None,
                             relative_strike: Real = None,
                             location: Optional[PricingLocation] = None,
                             legacy_implementation: bool = True,
                             *,
                             source: str = None,
                             real_time: bool = False,
                             request_id: Optional[str] = None) -> Series:
    """
    Volatility of an asset implied by observations of market prices.

    :param asset: asset object loaded from security master
    :param tenor: relative date representation of expiration date e.g. 1m
            or absolute calendar strips e.g. 'Cal20', 'F20-G20'
    :param strike_reference: reference for strike level
    :param relative_strike: strike relative to reference
    :param location: location of the data snapshot Example - "HKG", "LDN", "NYC"
    :param legacy_implementation: use FX_IVOl over  FX_VANILLA_OPTIONS_VOLS
    :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: implied volatility curve
    """

    if legacy_implementation:
        return (tm.implied_volatility(asset,
                                      tenor,
                                      strike_reference,
                                      relative_strike,
                                      source=source,
                                      real_time=real_time,
                                      request_id=request_id))

    bbid = asset.get_identifier(AssetIdentifier.BLOOMBERG_ID)
    if bbid is not None:
        cross = _cross_stored_direction_helper(bbid)
        if cross != bbid:
            cross_asset = SecurityMaster.get_asset(
                cross, AssetIdentifier.BLOOMBERG_ID)
        else:
            cross_asset = asset
    else:
        raise MqValueError('Badly setup cross ' + asset.name)

    ref_string, relative_strike = _preprocess_implied_vol_strikes_fx(
        strike_reference, relative_strike)

    if ref_string == 'delta':
        if relative_strike == 0:
            strike = 'DN'
            option_type = OptionType.CALL.value
        else:
            if relative_strike > 0:
                strike = str(relative_strike) + 'D'
                option_type = OptionType.CALL.value
            else:
                strike = str(-relative_strike) + 'D'
                option_type = OptionType.PUT.value
    elif ref_string == VolReference.SPOT.value:
        strike = 'Spot'
        option_type = OptionType.CALL.value
    elif ref_string == VolReference.FORWARD.value:
        strike = 'ATMF'
        option_type = OptionType.CALL.value
    else:
        raise MqValueError(
            'unknown strike_reference and relative_strike combination')

    tenor = _tenor_month_to_year(tenor)
    s = implied_volatility_new(cross_asset,
                               tenor,
                               strike,
                               option_type,
                               location=location)
    return s
Exemple #3
0
def test_vol_fx():
    replace = Replacer()
    # for different delta strikes
    replace('gs_quant.timeseries.measures.GsDataApi.get_market_data', mock_fx)
    actual = tm.implied_volatility(Cross('MA123', 'ABCXYZ'), '1m',
                                   tm.VolReference.DELTA_PUT, 50)
    assert_series_equal(
        pd.Series([5, 1, 2], index=_index * 3, name='impliedVolatility'),
        actual)
    actual = tm.implied_volatility(Cross('MA123', 'ABCXYZ'), '1m',
                                   tm.VolReference.DELTA_PUT, 25)
    assert_series_equal(
        pd.Series([5, 1, 2], index=_index * 3, name='impliedVolatility'),
        actual)
    actual = tm.implied_volatility(Cross('MA123', 'ABCXYZ'), '1m',
                                   tm.VolReference.FORWARD, 100)
    assert_series_equal(
        pd.Series([5, 1, 2], index=_index * 3, name='impliedVolatility'),
        actual)
    actual = tm.implied_volatility(Cross('MA123', 'ABCXYZ'), '1m',
                                   tm.VolReference.SPOT, 100)
    assert_series_equal(
        pd.Series([5, 1, 2], index=_index * 3, name='impliedVolatility'),
        actual)
    # NORMALIZED not supported
    with pytest.raises(MqError):
        tm.implied_volatility(Cross('MA123', 'ABCXYZ'), '1m',
                              tm.VolReference.NORMALIZED, 25)
    with pytest.raises(MqError):
        tm.implied_volatility(Cross('MA123', 'ABCXYZ'), '1m',
                              tm.VolReference.SPOT, 25)
    with pytest.raises(MqError):
        tm.implied_volatility(Cross('MA123', 'ABCXYZ'), '1m',
                              tm.VolReference.FORWARD, 25)
    # triggers error if empty and name does not contain '/'
    replace('gs_quant.timeseries.measures.GsDataApi.get_market_data',
            mock_fx_empty)
    with pytest.raises(MqError):
        tm.implied_volatility(Cross('MA123', 'ABCXYZ'), '1m',
                              tm.VolReference.DELTA_CALL, 25)
    # checking if reverse cross data is available
    replace('gs_quant.timeseries.measures.GsDataApi.get_market_data',
            mock_fx_empty)
    replace('gs_quant.markets.securities.SecurityMaster.get_asset',
            mock_fx_switch)
    xyz = Cross("MA1889", "XYZ/ABC")
    assert tm.implied_volatility(xyz, '1m', tm.VolReference.DELTA_PUT,
                                 25).empty
    replace.restore()
Exemple #4
0
def test_vol_fx():
    replace = Replacer()

    mock = Cross('MAA0NE9QX2ABETG6', 'USD/EUR')
    xrefs = replace('gs_quant.timeseries.measures.GsAssetApi.get_asset_xrefs',
                    Mock())
    xrefs.return_value = [
        GsTemporalXRef(dt.date(2019, 1, 1), dt.date(2952, 12, 31),
                       XRef(bbid='EURUSD', ))
    ]
    replace('gs_quant.markets.securities.SecurityMaster.get_asset', mock)

    # for different delta strikes
    replace('gs_quant.timeseries.measures.GsDataApi.get_market_data', mock_fx)
    actual = tm.implied_volatility(mock, '1m', tm.VolReference.DELTA_CALL, 25)
    assert_series_equal(
        pd.Series([5, 1, 2], index=_index * 3, name='impliedVolatility'),
        actual)
    actual = tm.implied_volatility(mock, '1m', tm.VolReference.DELTA_PUT, 25)
    assert_series_equal(
        pd.Series([5, 1, 2], index=_index * 3, name='impliedVolatility'),
        actual)
    actual = tm.implied_volatility(mock, '1m', tm.VolReference.DELTA_NEUTRAL)
    assert_series_equal(
        pd.Series([5, 1, 2], index=_index * 3, name='impliedVolatility'),
        actual)
    actual = tm.implied_volatility(mock, '1m', tm.VolReference.FORWARD, 100)
    assert_series_equal(
        pd.Series([5, 1, 2], index=_index * 3, name='impliedVolatility'),
        actual)
    actual = tm.implied_volatility(mock, '1m', tm.VolReference.SPOT, 100)
    assert_series_equal(
        pd.Series([5, 1, 2], index=_index * 3, name='impliedVolatility'),
        actual)
    # NORMALIZED not supported
    with pytest.raises(MqError):
        tm.implied_volatility(mock, '1m', tm.VolReference.DELTA_CALL)
    with pytest.raises(MqError):
        tm.implied_volatility(mock, '1m', tm.VolReference.NORMALIZED, 25)
    with pytest.raises(MqError):
        tm.implied_volatility(mock, '1m', tm.VolReference.SPOT, 25)
    with pytest.raises(MqError):
        tm.implied_volatility(mock, '1m', tm.VolReference.FORWARD, 25)
    replace.restore()