def test_results_with_resolution(mocker): with MockCalc(mocker): swap1 = IRSwap('Pay', '10y', 'USD', name='swap1') swap2 = IRSwap('Pay', '10y', 'GBP', name='swap2') swap3 = IRSwap('Pay', '10y', 'EUR', name='swap3') portfolio = Portfolio((swap1, swap2, swap3)) with PricingContext(pricing_date=dt.date(2020, 10, 15)): result = portfolio.calc((risk.DollarPrice, risk.IRDelta)) # Check that we've got results assert result[swap1][risk.DollarPrice] is not None # Now resolve portfolio and assert that we can still get the result orig_swap1 = swap1.clone() with PricingContext(pricing_date=dt.date(2020, 10, 15)): portfolio.resolve() # Assert that the resolved swap is indeed different and that we can retrieve results by both assert swap1 != orig_swap1 assert result[swap1][risk.DollarPrice] is not None assert result[orig_swap1][risk.DollarPrice] is not None # Now reset the instruments and portfolio swap1 = IRSwap('Pay', '10y', 'USD', name='swap1') swap2 = IRSwap('Pay', '10y', 'GBP', name='swap2') swap3 = IRSwap('Pay', '10y', 'EUR', name='swap3') portfolio = Portfolio((swap1, swap2, swap3, swap1)) with PricingContext(dt.date(2020, 10, 14)): # Resolve under a different pricing date portfolio.resolve() assert portfolio.instruments[0].termination_date == dt.date( 2030, 10, 16) assert portfolio.instruments[1].termination_date == dt.date( 2030, 10, 14) assert round(swap1.fixed_rate, 4) == 0.0075 assert round(swap2.fixed_rate, 4) == 0.004 assert round(swap3.fixed_rate, 4) == -0.0027 # Assert that after resolution under a different context, we cannot retrieve the result try: _ = result[swap1][risk.DollarPrice] assert False except KeyError: assert True # Resolve again and check we get the same values with PricingContext(dt.date(2020, 10, 14)): # Resolve under a different pricing date portfolio.resolve() assert portfolio.instruments[0].termination_date == dt.date( 2030, 10, 16) assert portfolio.instruments[1].termination_date == dt.date( 2030, 10, 14) assert round(swap1.fixed_rate, 4) == 0.0075 assert round(swap2.fixed_rate, 4) == 0.004 assert round(swap3.fixed_rate, 4) == -0.0027
def test_results_with_resolution(): set_session() dollar_price_ir_delta_values = [[[{ '$type': 'Risk', 'val': 0.01 }], [{ '$type': 'Risk', 'val': 0.02 }], [{ '$type': 'Risk', 'val': 0.03 }]], [[{ '$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.02, 0.025], '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' }] }]]] swap1 = IRSwap('Pay', '10y', 'USD', name='swap1') swap2 = IRSwap('Pay', '10y', 'GBP', name='swap2') swap3 = IRSwap('Pay', '10y', 'EUR', name='swap3') portfolio = Portfolio((swap1, swap2, swap3)) with mock.patch('gs_quant.api.gs.risk.GsRiskApi._exec') as mocker: mocker.return_value = [dollar_price_ir_delta_values] result = portfolio.calc((risk.DollarPrice, risk.IRDelta)) # Check that we've got results assert result[swap1][risk.DollarPrice] is not None # Now resolve portfolio and assert that we can still get the result resolution_values = [[[{ '$type': 'LegDefinition', 'fixedRate': 0.01 }], [{ '$type': 'LegDefinition', 'fixedRate': 0.007 }], [{ '$type': 'LegDefinition', 'fixedRate': 0.05 }]]] orig_swap1 = swap1.clone() with mock.patch('gs_quant.api.gs.risk.GsRiskApi._exec') as mocker: mocker.return_value = [resolution_values] portfolio.resolve() # Assert that the resolved swap is indeed different and that we can retrieve results by both assert swap1 != orig_swap1 assert result[swap1][risk.DollarPrice] is not None assert result[orig_swap1][risk.DollarPrice] is not None # Now reset the instruments and portfolio swap1 = IRSwap('Pay', '10y', 'USD', name='swap1') swap2 = IRSwap('Pay', '10y', 'GBP', name='swap2') swap3 = IRSwap('Pay', '10y', 'EUR', name='swap3') portfolio = Portfolio((swap1, swap2, swap3, swap1)) with mock.patch('gs_quant.api.gs.risk.GsRiskApi._exec') as mocker: with PricingContext(dt.date(1066, 11, 14)): # Resolve under a different pricing date mocker.return_value = [resolution_values] portfolio.resolve() # Assert that after resolution under a different context, we cannot retrieve the result try: _ = result[swap1][risk.DollarPrice] assert False except KeyError: assert True