def test_structured_calc(mocker): set_session() for priceable in priceables: if priceable.assetClass == AssetClass.Rates: for measure in (risk.IRDelta, risk.IRVega): structured_calc(mocker, priceable, measure) elif priceable.assetClass == AssetClass.FX: for measure in (risk.FXDelta, risk.FXGamma, risk.FXVega): structured_calc(mocker, priceable, measure) 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'} ] } mocker.return_value = [[[[values]] * len(priceables)]] with PricingContext(): delta_f = [p.calc(risk.IRDelta) for p in priceables] delta = risk.aggregate_risk(delta_f, threshold=0) assert len(delta) == 2
def test_structured_calc(mocker): set_session() for priceable in priceables: if priceable.assetClass == AssetClass.Rates: for measure in (risk.IRDelta, risk.IRVega): structured_calc(mocker, priceable, measure) elif priceable.assetClass == AssetClass.FX: for measure in (risk.FXDelta, risk.FXGamma, risk.FXVega): structured_calc(mocker, priceable, measure) values = [{ 'marketDataType': 'IR', 'assetId': 'USD', 'pointClass': 'Swap', 'point': '1y', 'value': 0.01 }, { 'marketDataType': 'IR', 'assetId': 'USD', 'pointClass': 'Swap', 'point': '2y', 'value': 0.015 }] mocker.return_value = [[values] * len(priceables)] with PricingContext(): delta_f = [p.calc(risk.IRDelta) for p in priceables] delta = risk.aggregate_risk(delta_f, threshold=0) assert len(delta) == 2
def test_portfolio(mocker): set_session() dollar_price_values = [[[{ 'date': '2019-10-07', 'value': 0.01 }], [{ 'date': '2019-10-07', 'value': 0.02 }], [{ 'date': '2019-10-07', 'value': 0.03 }]]] dollar_price_ir_delta_values = [[[{ 'date': '2019-10-07', 'value': 0.01 }], [{ 'date': '2019-10-07', 'value': 0.02 }], [{ 'date': '2019-10-07', 'value': 0.03 }]], [[{ 'date': '2019-10-07', 'marketDataType': 'IR', 'assetId': 'USD', 'pointClass': 'Swap', 'point': '1y', 'value': 0.01 }, { 'date': '2019-10-07', 'marketDataType': 'IR', 'assetId': 'USD', 'pointClass': 'Swap', 'point': '2y', 'value': 0.015 }], [{ 'date': '2019-10-07', 'marketDataType': 'IR', 'assetId': 'USD', 'pointClass': 'Swap', 'point': '1y', 'value': 0.02 }, { 'date': '2019-10-07', 'marketDataType': 'IR', 'assetId': 'USD', 'pointClass': 'Swap', 'point': '2y', 'value': 0.025 }], [{ 'date': '2019-10-07', 'marketDataType': 'IR', 'assetId': 'USD', 'pointClass': 'Swap', 'point': '1y', 'value': 0.03 }, { 'date': '2019-10-07', 'marketDataType': 'IR', 'assetId': 'USD', 'pointClass': 'Swap', 'point': '2y', 'value': 0.035 }]]] 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)) mocker.return_value = dollar_price_values prices: PortfolioRiskResult = portfolio.dollar_price() assert tuple(sorted(prices)) == (0.01, 0.02, 0.03) assert round(prices.aggregate(), 2) == 0.06 assert prices[0] == 0.01 assert prices[swap2] == 0.02 assert prices['swap3'] == 0.03 mocker.return_value = dollar_price_ir_delta_values result = portfolio.calc((risk.DollarPrice, risk.IRDelta)) assert tuple(result[risk.DollarPrice]) == (0.01, 0.02, 0.03) assert result[risk.DollarPrice].aggregate() == 0.06 assert result[risk.DollarPrice]['swap3'] == 0.03 assert result[risk.DollarPrice]['swap3'] == result['swap3'][ risk.DollarPrice] expected = risk.aggregate_risk( [pd.DataFrame(v) for v in dollar_price_ir_delta_values[1]]) assert result[risk.IRDelta].aggregate().equals(expected) prices_only = result[risk.DollarPrice] assert tuple(prices) == tuple(prices_only)
def test_portfolio(mocker): set_session() dollar_price_values = [[[{ 'date': '2019-10-07', 'value': 0.01 }], [{ 'date': '2019-10-07', 'value': 0.02 }], [{ 'date': '2019-10-07', 'value': 0.03 }]]] dollar_price_ir_delta_values = [[[{ 'date': '2019-10-07', 'value': 0.01 }], [{ 'date': '2019-10-07', 'value': 0.02 }], [{ 'date': '2019-10-07', 'value': 0.03 }]], [[{ 'date': '2019-10-07', 'marketDataType': 'IR', 'assetId': 'USD', 'pointClass': 'Swap', 'point': '1y', 'value': 0.01 }, { 'date': '2019-10-07', 'marketDataType': 'IR', 'assetId': 'USD', 'pointClass': 'Swap', 'point': '2y', 'value': 0.015 }], [{ 'date': '2019-10-07', 'marketDataType': 'IR', 'assetId': 'USD', 'pointClass': 'Swap', 'point': '1y', 'value': 0.02 }, { 'date': '2019-10-07', 'marketDataType': 'IR', 'assetId': 'USD', 'pointClass': 'Swap', 'point': '2y', 'value': 0.025 }], [{ 'date': '2019-10-07', 'marketDataType': 'IR', 'assetId': 'USD', 'pointClass': 'Swap', 'point': '1y', 'value': 0.03 }, { 'date': '2019-10-07', 'marketDataType': 'IR', 'assetId': 'USD', 'pointClass': 'Swap', 'point': '2y', 'value': 0.035 }]]] 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)) mocker.return_value = dollar_price_values prices: PortfolioRiskResult = portfolio.dollar_price() assert tuple(sorted(prices)) == (0.01, 0.02, 0.03) assert round(prices.aggregate(), 2) == 0.06 assert prices[0] == 0.01 assert prices[swap2] == 0.02 assert prices['swap3'] == 0.03 mocker.return_value = dollar_price_ir_delta_values result = portfolio.calc((risk.DollarPrice, risk.IRDelta)) assert tuple(result[risk.DollarPrice]) == (0.01, 0.02, 0.03) assert result[risk.DollarPrice].aggregate() == 0.06 assert result[risk.DollarPrice]['swap3'] == 0.03 assert result[risk.DollarPrice]['swap3'] == result['swap3'][ risk.DollarPrice] expected = risk.aggregate_risk([ risk.DataFrameWithInfo(PricingContext.current.pricing_key, pd.DataFrame(v)) for v in dollar_price_ir_delta_values[1] ]).reset_index(drop=True) actual = result[risk.IRDelta].aggregate().raw_value assert actual.equals(expected) prices_only = result[risk.DollarPrice] assert tuple(prices) == tuple(prices_only) swap4 = IRSwap('Pay', '10y', 'USD', fixed_rate=0.01, name='swap4') portfolio.append(swap4) assert len(portfolio.instruments) == 4 extracted_swap = portfolio.pop('swap2') assert extracted_swap == swap2 assert len(portfolio.instruments) == 3 swap_dict = {'swap_5': swap1, 'swap_6': swap2, 'swap_7': swap3} portfolio = Portfolio(swap_dict) assert len(portfolio) == 3