Ejemplo n.º 1
0
def test_basket_without_weights():
    replace = Replacer()

    mock_data = replace('gs_quant.timeseries.backtesting.ts.get_historical_and_last_for_measure', Mock())
    mock_data.side_effect = [_mock_spot_data(), _mock_spot_data(), _mock_spot_data(), _mock_spot_data()]

    mock_asset = replace('gs_quant.timeseries.backtesting.GsAssetApi.get_many_assets_data', Mock())
    mock_asset.return_value = [{'id': 'MA4B66MW5E27U9VBB94', 'bbid': 'AAPL UW'},
                               {'id': 'MA4B66MW5E27UAL9SUX', 'bbid': 'MSFT UW'}]

    a_basket = Basket(['AAPL UW', 'MSFT UW'], [0.5, 0.5], RebalFreq.DAILY)
    b_basket = Basket(['AAPL UW', 'MSFT UW'])

    with DataContext('2021-01-01', '2021-01-06'):
        a_price = a_basket.price()
        b_price = b_basket.price()
        a_vol = a_basket.average_realized_volatility('2d')
        b_vol = b_basket.average_realized_volatility('2d')
    assert_series_equal(a_price, b_price)
    assert_series_equal(a_vol, b_vol)

    with pytest.raises(MqValueError):
        c_basket = Basket(['AAPL UW'], [])
        c_basket.price()

    replace.restore()
Ejemplo n.º 2
0
def test_basket_average_realized_vol_intraday():
    replace = Replacer()

    end_date = date.today() - datetime.timedelta(days=1)
    start_date = end_date - datetime.timedelta(days=4)

    a = pd.Series([1 for i in range(5)], index=pd.date_range(start_date, end_date))
    z = pd.DataFrame({'spot': (a ** 3).tolist()}, index=a.index)
    z['assetId'] = 'SPX_MOCK_MQID'

    mock_data = replace('gs_quant.timeseries.measures.GsDataApi.get_market_data', Mock())
    mock_data.side_effect = [z]

    mock_asset = replace('gs_quant.timeseries.backtesting.GsAssetApi.get_many_assets_data', Mock())
    mock_asset.return_value = [{'id': 'SPX_MOCK_MQID', 'bbid': 'SPX'}]

    mock_today = replace('gs_quant.timeseries.get_last_for_measure', Mock())
    mock_today.side_effect = [pd.DataFrame({'assetId': 'SPX_MOCK_MQID', 'spot': 5001.0},
                                           index=[datetime.datetime.now()])]

    a_basket = Basket(['SPX'], [1], RebalFreq.DAILY)

    with DataContext(start=start_date, end=date.today()):
        avg_vol = a_basket.average_realized_volatility('2d')
        assert avg_vol.index[-1].date() == date.today()

    replace.restore()
Ejemplo n.º 3
0
def test_basket_average_realized_vol():
    replace = Replacer()

    dates = pd.DatetimeIndex([date(2021, 1, 1), date(2021, 1, 2), date(2021, 1, 3), date(2021, 1, 4), date(2021, 1, 5),
                              date(2021, 1, 6)])
    dates_feb = pd.DatetimeIndex([date(2021, 2, 1), date(2021, 2, 2), date(2021, 2, 3), date(2021, 2, 4),
                                  date(2021, 2, 5), date(2021, 2, 6)])

    mock_data = replace('gs_quant.timeseries.measures.GsDataApi.get_market_data', Mock())
    mock_data.side_effect = [_mock_spot_data(), _mock_spot_data_feb()]

    mock_asset = replace('gs_quant.timeseries.backtesting.GsAssetApi.get_many_assets_data', Mock())
    mock_asset.return_value = [{'id': 'MA4B66MW5E27U9VBB94', 'bbid': 'AAPL UW'},
                               {'id': 'MA4B66MW5E27UAL9SUX', 'bbid': 'MSFT UW'}]

    a_basket = Basket(['AAPL UW', 'MSFT UW'], [0.1, 0.9], RebalFreq.DAILY)

    expected = pd.Series([1.1225, 4.49, 2.245, 2.245], index=dates[2:])
    with DataContext('2021-01-01', '2021-01-06'):
        actual = a_basket.average_realized_volatility('2d')
    assert_series_equal(actual, expected)

    expected = pd.Series([3.304542, 3.174902, 3.174902], index=dates[3:])
    with DataContext('2021-01-01', '2021-01-06'):
        actual = a_basket.average_realized_volatility('3d')
    assert_series_equal(actual, expected)
    mock_data.assert_called_once()

    expected = pd.Series([34.698082, 19.719302, 18.860533], index=dates_feb[3:])
    with DataContext('2021-02-01', '2021-02-06'):
        actual = a_basket.average_realized_volatility('3d')
    assert_series_equal(actual, expected)

    with pytest.raises(NotImplementedError):
        a_basket.average_realized_volatility('2d', real_time=True)

    replace.restore()
Ejemplo n.º 4
0
def test_basket_average_realized_vol_wts():
    replace = Replacer()

    mock_data = replace('gs_quant.timeseries.measures.GsDataApi.get_market_data', Mock())
    mock_data.side_effect = [_mock_data_simple()]

    mock_asset = replace('gs_quant.timeseries.backtesting.GsAssetApi.get_many_assets_data', Mock())
    mock_asset.return_value = [{'id': 'XLB_MOCK_MQID', 'bbid': 'XLP UP'},
                               {'id': 'XLC_MOCK_MQID', 'bbid': 'XLC UP'}, {'id': 'SPX_MOCK_MQID', 'bbid': 'SPX'}]

    mock_vol = replace('gs_quant.timeseries.backtesting.volatility', Mock())
    mock_vol.side_effect = [_mock_vol_simple(), _mock_vol_simple() * 2, _mock_vol_simple() * 3, _mock_vol_simple() * 4]

    a_basket = Basket(['XLC UP', 'XLP UP', 'SPX'], [0.2, 0.3, 0.5], RebalFreq.DAILY)

    with DataContext(start=date(2021, 9, 1), end=date(2021, 9, 25)):
        av_realized_vol = a_basket.average_realized_volatility('2d')
        np.testing.assert_approx_equal(av_realized_vol.iloc[0], 1.7)

    replace.restore()
Ejemplo n.º 5
0
def test_basket_average_realized_vol():
    replace = Replacer()

    dates = pd.DatetimeIndex([date(2021, 1, 1), date(2021, 1, 2), date(2021, 1, 3), date(2021, 1, 4), date(2021, 1, 5),
                              date(2021, 1, 6)])
    dates_feb = pd.DatetimeIndex([date(2021, 2, 1), date(2021, 2, 2), date(2021, 2, 3), date(2021, 2, 4),
                                  date(2021, 2, 5), date(2021, 2, 6)])

    mock_data = replace('gs_quant.timeseries.backtesting.ts.get_historical_and_last_for_measure', Mock())
    mock_data.side_effect = [_mock_spot_data(), _mock_spot_data_feb(), _mock_spot_data_feb()]

    mock_asset = replace('gs_quant.timeseries.backtesting.GsAssetApi.get_many_assets_data', Mock())
    mock_asset.return_value = [{'id': 'MA4B66MW5E27U9VBB94', 'bbid': 'AAPL UW'},
                               {'id': 'MA4B66MW5E27UAL9SUX', 'bbid': 'MSFT UW'}]

    a_basket = Basket(['AAPL UW', 'MSFT UW'], [0.1, 0.9], RebalFreq.DAILY)

    expected = pd.Series([1.1059, 4.4906, 2.2677, 2.2228], index=dates[2:])
    with DataContext('2021-01-01', '2021-01-06'):
        actual = a_basket.average_realized_volatility('2d')
    assert_series_equal(actual, expected, atol=10e-3)

    expected = pd.Series([3.3088, 3.1754, 3.1754], index=dates[3:])
    with DataContext('2021-01-01', '2021-01-06'):
        actual = a_basket.average_realized_volatility('3d')
    assert_series_equal(actual, expected)
    mock_data.assert_called_once()

    expected = pd.Series([34.81054014068537, 19.98982339010735, 19.08853721611424], index=dates_feb[3:])
    with DataContext('2021-02-01', '2021-02-06'):
        actual = a_basket.average_realized_volatility('3d')
    assert_series_equal(actual, expected)

    with pytest.raises(NotImplementedError):
        a_basket.average_realized_volatility('2d', real_time=True)

    mock_get_last = replace('gs_quant.timeseries.measures.get_last_for_measure', Mock())
    mock_get_last.return_value = None
    # Test case where ts.get_last_for_measure returns none
    with DataContext('2021-02-01', datetime.date.today() + datetime.timedelta(days=2)):
        a_basket.average_realized_volatility('2d')

    replace.restore()