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)
示例#2
0
def get_attributes(p, risks, ctx='PricingCtx1', resolve=False, no_frame=False):
    if resolve:
        p.resolve()
    if ctx == 'Multiple':
        with HistoricalPricingContext(date(2020, 1, 14),
                                      date(2020, 1, 15),
                                      market_data_location='LDN'):
            res = p.calc(risks)
    elif ctx == 'PricingCtx1':
        with PricingContext(date(2020, 1, 14), market_data_location='LDN'):
            res = p.calc(risks)
    elif ctx == 'Multiple2':
        with HistoricalPricingContext(date(2020, 1, 16),
                                      date(2020, 1, 17),
                                      market_data_location='LDN'):
            res = p.calc(risks)
    elif ctx == 'PricingCtx2':
        with PricingContext(date(2020, 1, 16), market_data_location='NYC'):
            res = p.calc(risks)
    elif ctx == 'PricingCtx3':
        with PricingContext(date(2020, 1, 16), market_data_location='LDN'):
            res = p.calc(risks)
    elif ctx == 'RollFwd':
        with RollFwd(date=date(2020, 11, 3)):
            res = p.calc(risks)
    elif ctx == 'CurveScen1':
        with CurveScenario(parallel_shift=5):
            res = p.calc(risks)
    elif ctx == 'CurveScen2':
        with CurveScenario(curve_shift=1, tenor_start=5, tenor_end=30):
            res = p.calc(risks)

    if not no_frame:
        frame = res.to_frame(None, None, None)

        cols = [col for col in frame.columns]
        return cols, res, frame
    else:
        return res
示例#3
0
    def calc(self, instrument: InstrumentBase,
             risk_measure: RiskMeasure) -> PricingFuture:
        futures = []

        provider = instrument.provider
        base_scenario = self._scenario
        parameters = self._parameters
        location = self.market.location
        base_market = self.market

        for date in self.__date_range:
            if date > self.pricing_date:
                scenario = MarketDataScenario(
                    RollFwd(date=date, realise_fwd=self._roll_to_fwds))
                risk_key = RiskKey(provider, date, base_market, parameters,
                                   scenario, risk_measure)
                futures.append(self._calc(instrument, risk_key))
            else:
                risk_key = RiskKey(provider, date,
                                   self._market(date, location), parameters,
                                   base_scenario, risk_measure)
                futures.append(self._calc(instrument, risk_key))

        return HistoricalPricingFuture(futures)