def resolve(self, in_place: bool = True) -> Optional[PortfolioRiskResult]: with self.__pricing_context: futures = [i.resolve(in_place) for i in self.__instruments] if not in_place: return PortfolioRiskResult(self, (ResolvedInstrumentValues, ), futures)
def calc(self, risk_measure: Union[RiskMeasure, Iterable[RiskMeasure]], fn=None) -> PortfolioRiskResult: with self.__pricing_context: return PortfolioRiskResult( self, (risk_measure, ) if isinstance(risk_measure, RiskMeasure) else risk_measure, [p.calc(risk_measure, fn=fn) for p in self.__priceables])
def calc( self, risk_measure: Union[RiskMeasure, Iterable[RiskMeasure]] ) -> PortfolioRiskResult: with self.__pricing_context: return PortfolioRiskResult( self, (risk_measure, ) if isinstance(risk_measure, RiskMeasure) else risk_measure, [i.calc(risk_measure) for i in self.__instruments], result_future=PricingFuture(PricingContext.current))
def calc(self, risk_measure: Union[RiskMeasure, Iterable[RiskMeasure]], fn=None) -> PortfolioRiskResult: priceables = self._get_instruments(self.__position_context.position_date, False, True) with self._pricing_context: # PortfolioRiskResult should hold a copy of the portfolio instead of a reference to the portfolio # this is to prevent the portfolio object within portfolioriskresult to hold a reference to the portfolio # object should it later be modified in place (eg: resolution) return PortfolioRiskResult(self.clone(), (risk_measure,) if isinstance(risk_measure, RiskMeasure) else risk_measure, [p.calc(risk_measure, fn=fn) for p in priceables])
def calc( self, risk_measure: Union[RiskMeasure, Iterable[RiskMeasure]] ) -> PortfolioRiskResult: with PricingContext.current if not PricingContext.current._is_entered else nullcontext( ): return PortfolioRiskResult( self, (risk_measure, ) if isinstance(risk_measure, RiskMeasure) else risk_measure, [p.calc(risk_measure) for p in self.__instruments])
def calc(self, risk_measure: Union[RiskMeasure, Iterable[RiskMeasure]], fn=None) -> PortfolioRiskResult: with self.__pricing_context: # PortfolioRiskResult should hold a copy of the portfolio instead of a reference to the portfolio # this is to prevent the portfolio object within portfolioriskresult to hold a reference to the portfolio # object should it later be modified in place (eg: resolution) return PortfolioRiskResult( copy.deepcopy(self), (risk_measure, ) if isinstance( risk_measure, RiskMeasure) else risk_measure, [p.calc(risk_measure, fn=fn) for p in self.__priceables])
def calc(self, risk_measure: Union[RiskMeasure, Iterable[RiskMeasure]], fn=None) -> PortfolioRiskResult: # PortfolioRiskResult should hold a copy of the portfolio instead of a reference to the portfolio # this is to prevent the portfolio object within portfolioriskresult to hold a reference to the portfolio # object should it later be modified in place (eg: resolution) # If a position set portfolio, resolve positions held on each date in pricing context if self.priceables: with self.__pricing_context: return PortfolioRiskResult( copy.deepcopy(self), (risk_measure, ) if isinstance( risk_measure, RiskMeasure) else risk_measure, [p.calc(risk_measure, fn=fn) for p in self.__priceables]) if self.id or self.position_sets: priceables_map = self.convert_positions_to_priceables() list_of_risk_measures = (risk_measure, ) if isinstance( risk_measure, RiskMeasure) else risk_measure risk_pricing_context = list_of_risk_measures[0].pricing_context # If the pricing context is historical, calc based on position sets held on each date if isinstance(risk_pricing_context, HistoricalPricingContext): date_range = risk_pricing_context._HistoricalPricingContext__date_range start_date = min(date_range) end_date = max(date_range) position_dates = priceables_map.keys() futures = [] while start_date <= end_date: position_dates_after_start = sorted( list( filter(lambda date: start_date < date, position_dates))) context_end = min(position_dates_after_start[0], end_date) \ if position_dates_after_start else end_date if context_end != end_date: context_end = context_end - dt.timedelta(days=1) historical_pricing_context = HistoricalPricingContext( start=start_date, end=prev_business_date(context_end) if context_end != end_date else end_date) with historical_pricing_context: futures = futures + [ p.calc(risk_measure, fn=fn) for p in self._get_priceables_from_map( priceables_map, start_date) ] start_date = context_end + dt.timedelta(days=1) with self.__pricing_context: return PortfolioRiskResult( copy.deepcopy(self), (risk_measure, ) if isinstance( risk_measure, RiskMeasure) else risk_measure, futures) # If the pricing context exists, calc based on the position set held on the pricing date elif isinstance(risk_pricing_context, PricingContext): futures = [ p.calc(risk_measure, fn=fn) for p in self._get_priceables_from_map( priceables_map, risk_pricing_context.pricing_date) ] with self.__pricing_context: return PortfolioRiskResult( copy.deepcopy(self), (risk_measure, ) if isinstance( risk_measure, RiskMeasure) else risk_measure, futures) return None