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_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)