def test_cache_subset(mocker): set_session() ir_swap = IRSwap('Pay', '10y', 'DKK') values = [{'$type': 'Risk', 'val': 0.01}] mocker.return_value = [[[values]], [[values]]] dates = (dt.date(2019, 10, 7), dt.date(2019, 10, 8)) with HistoricalPricingContext(dates=dates, use_cache=True): price_f = ir_swap.price() price_f.result() for date in dates: risk_key = PricingContext(pricing_date=date)._PricingContext__risk_key( risk.Price, ir_swap.provider()) cached_scalar = PricingCache.get(risk_key, ir_swap) assert cached_scalar assert isinstance(cached_scalar, float) risk_key = PricingContext( pricing_date=dt.date(2019, 10, 9))._PricingContext__risk_key( risk.Price, ir_swap.provider()) cached2 = PricingCache.get(risk_key, ir_swap) assert cached2 is None values = [{ '$type': 'RiskVector', 'asset': [0.01, 0.015], 'points': [{ 'type': 'IR', 'asset': 'USD', 'class_': 'Swap', 'point': '1y' }, { 'type': 'IR', 'asset': 'USD', 'class_': 'Swap', 'point': '2y' }] }] # Check that we can return the same values from the cache, after calculating once (with return values set to None) for return_values in ([[[values]], [[values]], [[values]]], None): mocker.return_value = return_values with HistoricalPricingContext(dates=dates, use_cache=True): risk_f = ir_swap.calc(risk.IRDelta) risk_frame = risk_f.result() assert isinstance(risk_frame, pd.DataFrame) assert len(risk_frame.index.unique()) == len(dates)
def test_cache_addition_removal(): set_session() p1 = IRSwap('Pay', '10y', 'DKK') with mock.patch('gs_quant.api.gs.risk.GsRiskApi._exec') as mocker: mocker.return_value = [[[[{'$type': 'Risk', 'val': 0.07}]]]] with PricingContext(use_cache=True) as pc: p1.price() price_key = pc._PricingContext__risk_key(risk.Price, p1.provider()) delta_key = pc._PricingContext__risk_key(risk.IRDelta, p1.provider()) assert PricingCache.get(price_key, p1) assert not PricingCache.get(delta_key, p1) # Assert that deleting the cached instrument removes it from the PricingCache # N.B, this may not work when debugging tests del p1 del mocker import gc gc.collect() p2 = IRSwap('Pay', '10y', 'DKK') p2_price_key = PricingContext.current._PricingContext__risk_key( risk.Price, p2.provider()) # assert not PricingCache.get(p2_price_key) with mock.patch('gs_quant.api.gs.risk.GsRiskApi._exec') as mocker: mocker.return_value = [[[[{'$type': 'Risk', 'val': 0.07}]]]] with PricingContext(use_cache=True): p2_price = p2.price() assert PricingCache.get(p2_price_key, p2) == p2_price.result() # Assert that running under a scenario does not retrieve the base result with mock.patch('gs_quant.api.gs.risk.GsRiskApi._exec') as mocker: mocker.return_value = [[[[{'$type': 'Risk', 'val': 0.08}]]]] with risk.CarryScenario(time_shift=30), PricingContext( use_cache=True) as spc: # Don't want the price without the scenario scenario_risk_key = spc._PricingContext__risk_key( risk.Price, p2.provider()) assert not PricingCache.get(scenario_risk_key, p2) scenario_price = p2.price() assert PricingCache.get(scenario_risk_key, p2) == scenario_price.result() with PricingContext(use_cache=True) as pc, risk.CarryScenario( time_shift=30): cached_scenario_price = PricingCache.get( pc._PricingContext__risk_key(risk.Price, p2.provider()), p2) # Check that we get the cached scenario price assert cached_scenario_price == scenario_price.result() # Check the base result is still correct assert PricingCache.get(p2_price_key, p2) == p2_price.result() # Assert that caching respects parameters, such as csa with mock.patch('gs_quant.api.gs.risk.GsRiskApi._exec') as mocker: mocker.return_value = [[[[{'$type': 'Risk', 'val': 0.08}]]]] with PricingContext(use_cache=True, csa_term='INVALID') as pc: # Don't want the price with default csa assert not PricingCache.get( pc._PricingContext__risk_key(risk.Price, p2.provider()), p2) csa_price = p2.price() with PricingContext(use_cache=True, csa_term='INVALID') as pc: cached_csa_price = PricingCache.get( pc._PricingContext__risk_key(risk.Price, p2.provider()), p2) # Check that we get the cached csa price assert cached_csa_price == csa_price.result() # Check the base result is still correct assert PricingCache.get(p2_price_key, p2) == p2_price.result() # Change a property and assert that p2 is no longer cached p2.notional_currency = 'EUR' assert not PricingCache.get(p2_price_key, p2)
def test_historical_pricing(mocker): set_session() dollar_price_ir_delta_values = [[[{ '$type': 'Risk', 'val': 0.01 }, { '$type': 'Risk', 'val': 0.011 }, { '$type': 'Risk', 'val': 0.012 }], [{ '$type': 'Risk', 'val': 0.02 }, { '$type': 'Risk', 'val': 0.021 }, { '$type': 'Risk', 'val': 0.022 }], [{ '$type': 'Risk', 'val': 0.03 }, { '$type': 'Risk', 'val': 0.031 }, { '$type': 'Risk', 'val': 0.032 }]], [[{ '$type': 'RiskVector', 'asset': [0.01, 0.015], 'points': [{ 'type': 'IR', 'asset': 'USD', 'class_': 'Swap', 'point': '1y' }, { 'type': 'IR', 'asset': 'USD', 'class_': 'Swap', 'point': '2y' }] }, { '$type': 'RiskVector', 'asset': [0.011, 0.0151], 'points': [{ 'type': 'IR', 'asset': 'USD', 'class_': 'Swap', 'point': '1y' }, { 'type': 'IR', 'asset': 'USD', 'class_': 'Swap', 'point': '2y' }] }, { '$type': 'RiskVector', 'asset': [0.012, 0.0152], 'points': [{ 'type': 'IR', 'asset': 'USD', 'class_': 'Swap', 'point': '1y' }, { 'type': 'IR', 'asset': 'USD', 'class_': 'Swap', 'point': '2y' }] }], [{ '$type': 'RiskVector', 'asset': [0.02, 0.025], 'points': [{ 'type': 'IR', 'asset': 'USD', 'class_': 'Swap', 'point': '1y' }, { 'type': 'IR', 'asset': 'USD', 'class_': 'Swap', 'point': '2y' }] }, { '$type': 'RiskVector', 'asset': [0.021, 0.0251], 'points': [{ 'type': 'IR', 'asset': 'USD', 'class_': 'Swap', 'point': '1y' }, { 'type': 'IR', 'asset': 'USD', 'class_': 'Swap', 'point': '2y' }] }, { '$type': 'RiskVector', 'asset': [0.022, 0.0252], 'points': [{ 'type': 'IR', 'asset': 'USD', 'class_': 'Swap', 'point': '1y' }, { 'type': 'IR', 'asset': 'USD', 'class_': 'Swap', 'point': '2y' }] }], [{ '$type': 'RiskVector', 'asset': [0.03, 0.035], 'points': [{ 'type': 'IR', 'asset': 'USD', 'class_': 'Swap', 'point': '1y' }, { 'type': 'IR', 'asset': 'USD', 'class_': 'Swap', 'point': '2y' }] }, { '$type': 'RiskVector', 'asset': [0.031, 0.0351], 'points': [{ 'type': 'IR', 'asset': 'USD', 'class_': 'Swap', 'point': '1y' }, { 'type': 'IR', 'asset': 'USD', 'class_': 'Swap', 'point': '2y' }] }, { '$type': 'RiskVector', 'asset': [0.032, 0.0352], 'points': [{ 'type': 'IR', 'asset': 'USD', 'class_': 'Swap', 'point': '1y' }, { 'type': 'IR', 'asset': 'USD', 'class_': 'Swap', 'point': '2y' }] }]]] mocker.return_value = [dollar_price_ir_delta_values] swap1 = IRSwap('Pay', '10y', 'USD', fixed_rate=0.01, name='swap1') swap2 = IRSwap('Pay', '10y', 'USD', fixed_rate=0.02, name='swap2') swap3 = IRSwap('Pay', '10y', 'USD', fixed_rate=0.03, name='swap3') portfolio = Portfolio((swap1, swap2, swap3)) with HistoricalPricingContext(dates=(dt.date(2019, 10, 7), dt.date(2019, 10, 8), dt.date(2019, 10, 9))) as hpc: risk_key = hpc._PricingContext__risk_key(risk.DollarPrice, swap1.provider()) results = portfolio.calc((risk.DollarPrice, risk.IRDelta)) expected = risk.SeriesWithInfo( pd.Series(data=[0.06, 0.063, 0.066], index=[ dt.date(2019, 10, 7), dt.date(2019, 10, 8), dt.date(2019, 10, 9) ]), risk_key=risk_key.base, ) actual = results[risk.DollarPrice].aggregate() assert actual.equals(expected)