def test_backtothefuture_pricing(mocker): with MockCalc(mocker): 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)) pricing_date = dt.date(2021, 2, 10) with PricingContext(pricing_date=pricing_date): with BackToTheFuturePricingContext(dates=business_day_offset( pricing_date, [-1, 0, 1], roll='forward')) as hpc: risk_key = hpc._PricingContext__risk_key( risk.DollarPrice, swap1.provider) results = portfolio.calc(risk.DollarPrice) expected = risk.SeriesWithInfo( pd.Series( data=[-22711963.80864744, -22655907.930484552, -21582551.58922608], index=business_day_offset(pricing_date, [-1, 0, 1], roll='forward')), risk_key=historical_risk_key(risk_key), ) actual = results[risk.DollarPrice].aggregate() assert actual.equals(expected)
def test_backtothefuture_pricing(mocker): with MockCalc(mocker): 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)) pricing_date = dt.date(2020, 10, 7) with PricingContext(pricing_date=pricing_date): with BackToTheFuturePricingContext(dates=business_day_offset( pricing_date, [-1, 0, 1], roll='forward')) as hpc: risk_key = hpc._PricingContext__risk_key( risk.DollarPrice, swap1.provider) results = portfolio.calc(risk.DollarPrice) expected = risk.SeriesWithInfo( pd.Series( data=[-35280379.86540368, -35348910.76427929, -30830994.939595155], index=business_day_offset(pricing_date, [-1, 0, 1], roll='forward')), risk_key=risk_key.ex_date_and_market, ) actual = results[risk.DollarPrice].aggregate() assert actual.equals(expected)
def test_historical_pricing(mocker): with MockCalc(mocker): swap1 = IRSwap('Pay', '10y', 'USD', fixed_rate='ATM+1', name='10y@a+1') swap2 = IRSwap('Pay', '10y', 'USD', fixed_rate='ATM+2', name='10y@a+2') swap3 = IRSwap('Pay', '10y', 'USD', fixed_rate='ATM+3', name='10y@a+3') portfolio = Portfolio((swap1, swap2, swap3)) dates = (dt.date(2021, 2, 9), dt.date(2021, 2, 10), dt.date(2021, 2, 11)) with HistoricalPricingContext(dates=dates) 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=[-580316.7895084377, -580373.4091600645, -580811.1441974249], index=[dt.date(2021, 2, 9), dt.date(2021, 2, 10), dt.date(2021, 2, 11)] ), risk_key=historical_risk_key(risk_key), ) assert results.dates == dates actual = results[risk.DollarPrice].aggregate() assert actual.equals(expected) assert (results[dt.date(2021, 2, 9)][risk.DollarPrice]['10y@a+1'] == results[risk.DollarPrice][dt.date(2021, 2, 9)]['10y@a+1']) assert (results[dt.date(2021, 2, 9)][risk.DollarPrice]['10y@a+1'] == results[risk.DollarPrice]['10y@a+1'][dt.date(2021, 2, 9)]) assert (results[dt.date(2021, 2, 9)][risk.DollarPrice]['10y@a+1'] == results['10y@a+1'][risk.DollarPrice][dt.date(2021, 2, 9)]) assert (results[dt.date(2021, 2, 9)][risk.DollarPrice]['10y@a+1'] == results['10y@a+1'][dt.date(2021, 2, 9)][risk.DollarPrice]) assert (results[dt.date(2021, 2, 9)][risk.DollarPrice]['10y@a+1'] == results[dt.date(2021, 2, 9)]['10y@a+1'][risk.DollarPrice])
def test_backtothefuture_pricing(mocker): set_session() day1 = [ [ [{'$type': 'Risk', 'val': 0.01}], [{'$type': 'Risk', 'val': 0.02}], [{'$type': 'Risk', 'val': 0.03}], ] ] day2 = [ [ [{'$type': 'Risk', 'val': 0.011}], [{'$type': 'Risk', 'val': 0.021}], [{'$type': 'Risk', 'val': 0.031}], ] ] day3 = [ [ [{'$type': 'Risk', 'val': 0.012}], [{'$type': 'Risk', 'val': 0.022}], [{'$type': 'Risk', 'val': 0.032}], ] ] mocker.return_value = [day1, day2, day3] 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 BackToTheFuturePricingContext(dates=business_day_offset(dt.datetime.today().date(), [-1, 0, 1], roll='forward')) as hpc: risk_key = hpc._PricingContext__risk_key(risk.DollarPrice, swap1.provider) results = portfolio.calc(risk.DollarPrice) expected = risk.SeriesWithInfo( pd.Series( data=[0.06, 0.063, 0.066], index=business_day_offset(dt.datetime.today().date(), [-1, 0, 1], roll='forward') ), risk_key=risk_key.ex_date_and_market,) actual = results[risk.DollarPrice].aggregate() assert actual.equals(expected)
def test_historical_pricing(mocker): with MockCalc(mocker): swap1 = IRSwap('Pay', '10y', 'USD', fixed_rate='ATM+1', name='10y@a+1') swap2 = IRSwap('Pay', '10y', 'USD', fixed_rate='ATM+2', name='10y@a+2') swap3 = IRSwap('Pay', '10y', 'USD', fixed_rate='ATM+3', name='10y@a+3') portfolio = Portfolio((swap1, swap2, swap3)) dates = (dt.date(2019, 10, 7), dt.date(2019, 10, 8), dt.date(2019, 10, 9)) with HistoricalPricingContext(dates=dates) 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=[ -564854.3640043903, -565604.2636791412, -564751.5121349357 ], index=[ dt.date(2019, 10, 7), dt.date(2019, 10, 8), dt.date(2019, 10, 9) ]), risk_key=risk_key.ex_date_and_market, ) assert results.dates == dates actual = results[risk.DollarPrice].aggregate() assert actual.equals(expected) assert (results[dt.date(2019, 10, 9)][risk.DollarPrice]['10y@a+1'] == results[risk.DollarPrice][dt.date(2019, 10, 9)]['10y@a+1']) assert (results[dt.date(2019, 10, 9)][risk.DollarPrice]['10y@a+1'] == results[risk.DollarPrice]['10y@a+1'][dt.date(2019, 10, 9)]) assert (results[dt.date(2019, 10, 9)][risk.DollarPrice]['10y@a+1'] == results['10y@a+1'][risk.DollarPrice][dt.date(2019, 10, 9)]) assert (results[dt.date( 2019, 10, 9)][risk.DollarPrice]['10y@a+1'] == results['10y@a+1'][dt.date( 2019, 10, 9)][risk.DollarPrice]) assert (results[dt.date( 2019, 10, 9)][risk.DollarPrice]['10y@a+1'] == results[dt.date( 2019, 10, 9)]['10y@a+1'][risk.DollarPrice])
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)
def test_historical_pricing(mocker): set_session() dollar_price_ir_delta_values = [ [ [ {'date': '2019-10-07', 'value': 0.01}, {'date': '2019-10-08', 'value': 0.011}, {'date': '2019-10-09', 'value': 0.012} ], [ {'date': '2019-10-07', 'value': 0.02}, {'date': '2019-10-08', 'value': 0.021}, {'date': '2019-10-09', 'value': 0.022} ], [ {'date': '2019-10-07', 'value': 0.03}, {'date': '2019-10-08', 'value': 0.031}, {'date': '2019-10-09', 'value': 0.032} ] ], [ [ {'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-08', 'marketDataType': 'IR', 'assetId': 'USD', 'pointClass': 'Swap', 'point': '1y', 'value': 0.011}, {'date': '2019-10-08', 'marketDataType': 'IR', 'assetId': 'USD', 'pointClass': 'Swap', 'point': '2y', 'value': 0.0151}, {'date': '2019-10-09', 'marketDataType': 'IR', 'assetId': 'USD', 'pointClass': 'Swap', 'point': '1y', 'value': 0.012}, {'date': '2019-10-09', 'marketDataType': 'IR', 'assetId': 'USD', 'pointClass': 'Swap', 'point': '2y', 'value': 0.0152} ], [ {'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-08', 'marketDataType': 'IR', 'assetId': 'USD', 'pointClass': 'Swap', 'point': '1y', 'value': 0.021}, {'date': '2019-10-08', 'marketDataType': 'IR', 'assetId': 'USD', 'pointClass': 'Swap', 'point': '2y', 'value': 0.0251}, {'date': '2019-10-09', 'marketDataType': 'IR', 'assetId': 'USD', 'pointClass': 'Swap', 'point': '1y', 'value': 0.022}, {'date': '2019-10-09', 'marketDataType': 'IR', 'assetId': 'USD', 'pointClass': 'Swap', 'point': '2y', 'value': 0.0252} ], [ {'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}, {'date': '2019-10-08', 'marketDataType': 'IR', 'assetId': 'USD', 'pointClass': 'Swap', 'point': '1y', 'value': 0.031}, {'date': '2019-10-08', 'marketDataType': 'IR', 'assetId': 'USD', 'pointClass': 'Swap', 'point': '2y', 'value': 0.0351}, {'date': '2019-10-09', 'marketDataType': 'IR', 'assetId': 'USD', 'pointClass': 'Swap', 'point': '1y', 'value': 0.032}, {'date': '2019-10-09', 'marketDataType': 'IR', 'assetId': 'USD', 'pointClass': 'Swap', 'point': '2y', 'value': 0.0352} ] ] ] 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: pricing_key = hpc.pricing_key results = portfolio.calc((risk.DollarPrice, risk.IRDelta)) expected = risk.SeriesWithInfo( pricing_key, 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)] )) actual = results[risk.DollarPrice].aggregate() assert actual.equals(expected)