Beispiel #1
0
        def handle_result(result: Optional[Union[DataFrameWithInfo, ErrorValue, PricingFuture]]) -> \
                [OverlayMarket, dict]:
            if isinstance(result, ErrorValue):
                return result

            properties = MarketDataCoordinate.properties()
            is_historical = result.index.name == 'date'
            location = PricingContext.current.market_data_location

            def extract_market_data(this_result: DataFrameWithInfo):
                market_data = {}

                for _, row in this_result.iterrows():
                    coordinate_values = {p: row.get(p) for p in properties}
                    mkt_point = coordinate_values.get('mkt_point')
                    if mkt_point is not None:
                        coordinate_values['mkt_point'] = tuple(coordinate_values['mkt_point'].split(';'))

                    # return 'redacted' as coordinate value if its a redacted coordinate
                    market_data[MarketDataCoordinate.from_dict(coordinate_values)] = row['value'] if \
                        row['permissions'] == 'Granted' else 'redacted'

                return market_data

            if is_historical:
                return {date: OverlayMarket(
                    base_market=CloseMarket(date=date, location=location),
                    market_data=extract_market_data(result.loc[date]))
                    for date in set(result.index)}
            else:
                return OverlayMarket(base_market=result.risk_key.market,
                                     market_data=extract_market_data(result))
Beispiel #2
0
        def cb(future: CompositeResultFuture):
            def update_market_data(all_market_data, this_market_data):
                for coordinate, value in this_market_data.items():
                    existing_value = all_market_data.setdefault(coordinate, value)
                    if abs(existing_value - value) > 1e-6:
                        raise ValueError(f'Conflicting values for {coordinate}: {existing_value} vs {value}')

            results = [f.result() for f in future.futures]
            is_historical = isinstance(results[0], dict)
            market_data = None if is_historical else {}
            overlay_markets = {} if is_historical else None

            for result in results:
                if market_data is not None:
                    update_market_data(market_data, result.base_market.market_data_dict)
                else:
                    for market in result.values():
                        update_market_data(overlay_markets.setdefault(market, {}), market.market_data)

            if market_data:
                ret = OverlayMarket(base_market=results[0], market_data=market_data)
            else:
                ret = {base_market.date: OverlayMarket(base_market=base_market, market_data=market_data)
                       for base_market, market_data in overlay_markets.items()}

            if result_future:
                result_future.set_result(ret)
Beispiel #3
0
        def handle_result(result: Optional[Union[DataFrameWithInfo, ErrorValue, PricingFuture]]) ->\
                [OverlayMarket, dict]:
            properties = MarketDataCoordinate.properties()
            is_historical = isinstance(PricingContext.current,
                                       HistoricalPricingContext)
            location = PricingContext.current.market_data_location

            def extract_market_data(this_result: DataFrameWithInfo):
                market_data = {}

                for _, row in result.iterrows():
                    coordinate_values = {p: row.get(p) for p in properties}
                    if 'mkt_point' in coordinate_values:
                        coordinate_values['mkt_point'] = tuple(
                            coordinate_values['mkt_point'].split(';'))
                    market_data[MarketDataCoordinate.from_dict(
                        coordinate_values)] = row['value']

                return market_data

            if is_historical:
                return {
                    date: OverlayMarket(
                        base_market=CloseMarket(date=date, location=location),
                        market_data=extract_market_data(result.loc[date]))
                    for date in set(result.index)
                }
            else:
                return OverlayMarket(base_market=result.risk_key.market,
                                     market_data=extract_market_data(result))
def test_market_data_object():
    coord_val_pair = [
        {
            'coordinate': {
                'mkt_type': 'IR',
                'mkt_asset': 'USD',
                'mkt_class': 'Swap',
                'mkt_point': ('5y', ),
                'mkt_quoting_style': 'ATMRate'
            },
            'value': 0.9973194889
        },
        {
            'coordinate': {
                'mkt_type': 'IR',
                'mkt_asset': 'USD',
                'mkt_class': 'Swap',
                'mkt_point': ('40y', ),
                'mkt_quoting_style': 'ATMRate'
            },
            'value': 'redacted'
        },
    ]
    coordinates = {
        MarketDataCoordinate.from_dict(dic['coordinate']): dic['value']
        for dic in coord_val_pair
    }
    overlay_market = OverlayMarket(base_market=CloseMarket(
        market_data=coordinates))

    assert overlay_market.coordinates[0] == MarketDataCoordinate.from_dict(
        coord_val_pair[0]['coordinate'])
    assert overlay_market.redacted_coordinates[
        0] == MarketDataCoordinate.from_dict(coord_val_pair[1]['coordinate'])
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)