def test_var_term(): with DataContext('2018-01-01', '2019-01-01'): _var_term_typical() _var_term_empty() _var_term_fwd() with DataContext('2019-01-01', '2019-07-04'): _var_term_fwd() with DataContext('2018-01-16', '2018-12-31'): out = _var_term_typical() assert out.empty with pytest.raises(MqError): tm.var_term(..., pricing_date=300)
def _var_term_typical(): assert DataContext.current_is_set data = {'tenor': ['1w', '2w', '1y', '2y'], 'varSwap': [1, 2, 3, 4]} out = pd.DataFrame(data=data, index=pd.DatetimeIndex(['2018-01-01'] * 4)) replace = Replacer() market_mock = replace( 'gs_quant.timeseries.measures.GsDataApi.get_market_data', Mock()) market_mock.return_value = out actual = tm.var_term(Index('MA123', AssetClass.Equity, '123')) idx = pd.DatetimeIndex( ['2018-01-08', '2018-01-15', '2019-01-01', '2020-01-01'], name='varSwap') expected = pd.Series([1, 2, 3, 4], name='varSwap', index=idx) expected = expected.loc[DataContext.current.start_date:DataContext.current. end_date] if expected.empty: assert actual.empty else: assert_series_equal(expected, actual, check_names=False) market_mock.assert_called_once() replace.restore() return actual
def _var_term_fwd(): idx = pd.date_range('2018-01-01', periods=2, freq='D') def mock_var_swap(_asset, tenor, _forward_start_date, **_kwargs): if tenor == '1m': return pd.Series([1, 2], idx, name='varSwap') if tenor == '2m': return pd.Series([3, 4], idx, name='varSwap') return pd.Series() replace = Replacer() market_mock = replace('gs_quant.timeseries.measures.var_swap', Mock()) market_mock.side_effect = mock_var_swap tenors_mock = replace('gs_quant.timeseries.measures._var_swap_tenors', Mock()) tenors_mock.return_value = ['1m', '2m', '3m'] actual = tm.var_term(Index('MA123', AssetClass.Equity, '123'), forward_start_date='1m') idx = pd.DatetimeIndex(['2018-02-02', '2018-03-02'], name='varSwap') expected = pd.Series([2, 4], name='varSwap', index=idx) expected = expected.loc[DataContext.current.start_date:DataContext.current. end_date] if expected.empty: assert actual.empty else: assert_series_equal(expected, actual, check_names=False) market_mock.assert_called() replace.restore() return actual
def _var_term_empty(): replace = Replacer() market_mock = replace('gs_quant.timeseries.measures.GsDataApi.get_market_data', Mock()) market_mock.return_value = pd.DataFrame() actual = tm.var_term(Index('MAXYZ', AssetClass.Equity, 'XYZ')) assert actual.empty market_mock.assert_called_once() replace.restore()