def test_cache_subset(mocker): set_session() ir_swap = IRSwap('Pay', '10y', 'DKK') values = [ {'date': '2019-10-07', 'value': 0.01}, {'date': '2019-10-08', 'value': 0.01} ] mocker.return_value = [[values]] dates = (dt.date(2019, 10, 7), dt.date(2019, 10, 8)) with HistoricalPricingContext(dates=dates, use_cache=True) as hpc: pricing_key = hpc.pricing_key price_f = ir_swap.price() price_f.result() cached = PricingCache.get(ir_swap, risk.Price, pricing_key) assert len(cached) == len(dates) cached_scalar = PricingCache.get(ir_swap, risk.Price, PricingContext(pricing_date=dates[0]).pricing_key) assert isinstance(cached_scalar, float) dates = dates + (dt.date(2019, 10, 9),) pricing_key = HistoricalPricingContext(dates=dates).pricing_key cached2 = PricingCache.get(ir_swap, risk.Price, pricing_key) assert cached2 is None cached3 = PricingCache.get(ir_swap, risk.Price, pricing_key, return_partial=True) assert len(cached3) < len(dates) values = [ {'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.01}, {'date': '2019-10-08', 'marketDataType': 'IR', 'assetId': 'USD', 'pointClass': 'Swap', 'point': '2y', 'value': 0.015}, {'date': '2019-10-09', 'marketDataType': 'IR', 'assetId': 'USD', 'pointClass': 'Swap', 'point': '1y', 'value': 0.01}, {'date': '2019-10-09', 'marketDataType': 'IR', 'assetId': 'USD', 'pointClass': 'Swap', 'point': '2y', 'value': 0.015} ] mocker.return_value = [[values]] with HistoricalPricingContext(dates=dates, use_cache=True) as hpc: pricing_key = hpc.pricing_key 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) cached4 = PricingCache.get(ir_swap, risk.IRDelta, pricing_key) assert len(cached4.index.unique()) == len(dates) cached5 = PricingCache.get(ir_swap, risk.IRDelta, PricingContext(pricing_date=dates[0]).pricing_key) assert len(cached5.index.unique()) == len(cached5)
def test_pricing_context(mocker): swap1 = IRSwap('Pay', '1y', 'EUR', name='EUR1y') future_date = business_day_offset(dt.date.today(), 10, roll='forward') with MockCalc(mocker): with RollFwd(date='10b', realise_fwd=True): market = swap1.market() with pytest.raises(ValueError): # cannot pass in future date into pricing context, use RollFwd instead with PricingContext(pricing_date=future_date): _ = swap1.calc(risk.Price) # cannot pass in market dated in the future into pricing context, use RollFwd instead with PricingContext(market=CloseMarket(date=future_date)): _ = swap1.calc(risk.Price) with PricingContext(market=OverlayMarket(base_market=CloseMarket(date=future_date, location='NYC'), market_data=market.result())): _ = swap1.calc(risk.Price)
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_subset(mocker): set_session() ir_swap = IRSwap('Pay', '10y', 'DKK') values = [{ 'date': '2019-10-07', 'value': 0.01 }, { 'date': '2019-10-08', 'value': 0.01 }] mocker.return_value = [[values]] dates = (dt.date(2019, 10, 7), dt.date(2019, 10, 8)) with HistoricalPricingContext(dates=dates, use_cache=True): market_data_location = PricingContext.current.market_data_location price_f = ir_swap.price() price_f.result() cached = PricingCache.get(ir_swap, market_data_location, risk.Price, dates) assert len(cached) == len(dates) cached_scalar = PricingCache.get(ir_swap, market_data_location, risk.Price, dates[0]) assert isinstance(cached_scalar, float) dates = dates + (dt.date(2019, 10, 9), ) cached2 = PricingCache.get(ir_swap, market_data_location, risk.Price, dates) assert len(cached2) assert len(cached2) < len(dates) values = [{ '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.01 }, { 'date': '2019-10-08', 'marketDataType': 'IR', 'assetId': 'USD', 'pointClass': 'Swap', 'point': '2y', 'value': 0.015 }, { 'date': '2019-10-09', 'marketDataType': 'IR', 'assetId': 'USD', 'pointClass': 'Swap', 'point': '1y', 'value': 0.01 }, { 'date': '2019-10-09', 'marketDataType': 'IR', 'assetId': 'USD', 'pointClass': 'Swap', 'point': '2y', 'value': 0.015 }] mocker.return_value = [[values]] with HistoricalPricingContext(dates=dates, use_cache=True): market_data_location = PricingContext.current.market_data_location 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) cached3 = PricingCache.get(ir_swap, market_data_location, risk.IRDelta, dates) assert len(cached3.index.unique()) == len(dates) cached4 = PricingCache.get(ir_swap, market_data_location, risk.IRDelta, dates[0]) assert len(cached4.index.unique()) == len(cached4)