Exemple #1
0
    def _handle_results(cls, request: RiskRequest, results: Iterable) -> dict:
        formatted_results = {}

        pricing_key = PricingKey(
            request.pricing_and_market_data_as_of,
            request.pricing_location.value,
            request.parameters,
            request.scenario
        )

        for measure_idx, position_results in enumerate(results):
            risk_measure = request.measures[measure_idx]

            for position_idx, date_results in enumerate(position_results):
                if len(date_results) != len(pricing_key):
                    raise RuntimeError('Number of results did not match requested days')

                handler = result_handlers.get(date_results[0].get('$type'))
                position = request.positions[position_idx]

                try:
                    date_results = handler(date_results, pricing_key, position.instrument) if handler else date_results
                except Exception as e:
                    error = str(e)
                    date_results = ErrorValue(pricing_key, error)
                    _logger.error(error)

                formatted_results.setdefault(risk_measure, {})[position] = date_results

        return formatted_results
Exemple #2
0
    def _handle_results(cls, request: RiskRequest,
                        results: Union[Iterable, Exception]) -> dict:
        formatted_results = {}

        if isinstance(results, Exception):
            date_results = [{
                '$type': 'Error',
                'errorString': str(results)
            }] * len(request.pricing_and_market_data_as_of)
            position_results = [date_results] * len(request.positions)
            results = [position_results] * len(request.measures)

        for risk_measure, position_results in zip(request.measures, results):
            for position, date_results in zip(request.positions,
                                              position_results):
                for as_of, date_result in zip(
                        request.pricing_and_market_data_as_of, date_results):
                    handler = result_handlers.get(date_result.get('$type'))
                    risk_key = RiskKey(cls, as_of.pricing_date, as_of.market,
                                       request.parameters, request.scenario,
                                       risk_measure)

                    try:
                        result = handler(
                            date_result, risk_key,
                            position.instrument) if handler else date_result
                    except Exception as e:
                        result = ErrorValue(risk_key, str(e))
                        _logger.error(result)

                    formatted_results[(risk_key, position.instrument)] = result

        return formatted_results
Exemple #3
0
    def _handle_results(cls, request: RiskRequest, results: Iterable) -> dict:
        formatted_results = {}

        for risk_measure, position_results in zip(request.measures, results):
            for position, date_results in zip(request.positions,
                                              position_results):
                for as_of, date_result in zip(
                        request.pricing_and_market_data_as_of, date_results):
                    handler = result_handlers.get(date_result.get('$type'))

                    # TODO Handle this better
                    market = LiveMarket(request.pricing_location) if isinstance(as_of.market_data_as_of, dt.datetime)\
                        else ClosingMarket(request.pricing_location, as_of.market_data_as_of)

                    risk_key = RiskKey(cls, as_of.pricing_date, market,
                                       request.parameters, request.scenario,
                                       risk_measure)

                    try:
                        result = handler(
                            date_result, risk_key,
                            position.instrument) if handler else date_result
                    except Exception as e:
                        result = ErrorValue(risk_key, str(e))
                        _logger.error(result)

                    formatted_results[(risk_key, position.instrument)] = result

        return formatted_results