Exemple #1
0
    def resolve(
        self,
        in_place: bool = True
    ) -> Optional[Union[PricingFuture, PriceableImpl, dict]]:
        pricing_context = self.__pricing_context
        with pricing_context:
            futures = [i.resolve(in_place) for i in self.__priceables]

        if not in_place:
            ret = {} if isinstance(PricingContext.current,
                                   HistoricalPricingContext) else Portfolio(
                                       name=self.name)
            result_future = PricingFuture() if not isinstance(pricing_context, PricingContext) or\
                pricing_context.is_async or pricing_context.is_entered else None

            def cb(future):
                if isinstance(ret, Portfolio):
                    ret.priceables = [f.result() for f in future.futures]
                else:
                    priceables_by_date = {}
                    for future in futures:
                        for date, priceable in future.result().items():
                            priceables_by_date.setdefault(date,
                                                          []).append(priceable)

                    for date, priceables in priceables_by_date.items():
                        ret[date] = Portfolio(priceables, name=self.name)

                if result_future:
                    result_future.set_result(ret)

            CompositeResultFuture(futures).add_done_callback(cb)
            return result_future or ret
Exemple #2
0
    def market(self) -> Union[OverlayMarket, PricingFuture, dict]:
        """
        Market Data map of coordinates and values. Note that this is not yet supported on all instruments

        ***Examples**

        >>> from gs_quant.markets.portfolio import Portfolio
        >>>
        >>> portfolio = Portfolio(...)
        >>> market = portfolio.market()

        """
        pricing_context = self.__pricing_context
        with pricing_context:
            futures = [i.market() for i in self.all_instruments]

        result_future = PricingFuture()
        return_future = not isinstance(
            pricing_context, PricingContext
        ) or pricing_context.is_async or pricing_context.is_entered

        def cb(future: CompositeResultFuture):
            def update_market_data(all_market_data, this_market_data):
                for item in this_market_data:
                    existing_value = all_market_data.setdefault(
                        item.coordinate, item.value)
                    if abs(existing_value - item.value) > 1e-6:
                        raise ValueError(
                            f'Conflicting values for {item.coordinate}: {existing_value} vs {item.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.market_data)
                else:
                    for market in result.values():
                        update_market_data(
                            overlay_markets.setdefault(market.base_market, {}),
                            market.market_data)

            if market_data:
                ret = OverlayMarket(base_market=results[0].base_market,
                                    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)

        CompositeResultFuture(futures).add_done_callback(cb)
        return result_future if return_future else result_future.result()
Exemple #3
0
    def resolve(self, in_place: bool = True) -> Optional[Union[PricingFuture, PriceableImpl, dict]]:
        priceables = self._get_instruments(self.__position_context.position_date, in_place, True)
        pricing_context = self._pricing_context
        with pricing_context:
            futures = [p.resolve(in_place) for p in priceables]

        if not in_place:
            ret = {} if isinstance(PricingContext.current, HistoricalPricingContext) else Portfolio(name=self.name)
            result_future = PricingFuture() if self._return_future else None

            def cb(future: CompositeResultFuture):
                if isinstance(ret, Portfolio):
                    ret.priceables = [f.result() for f in future.futures]
                else:
                    priceables_by_date = {}
                    for future in futures:
                        for date, priceable in future.result().items():
                            priceables_by_date.setdefault(date, []).append(priceable)

                    for date, priceables in priceables_by_date.items():
                        if any(p for p in priceables if not isinstance(p, PriceableImpl)):
                            _logger.error(f'Error resolving on {date}, skipping that date')
                        else:
                            ret[date] = Portfolio(priceables, name=self.name)

                if result_future:
                    result_future.set_result(ret)

            CompositeResultFuture(futures).add_done_callback(cb)
            return result_future or ret