예제 #1
0
 def _adjust_date(self, date):
     date = (date + BDay(1) - BDay(1)).date()  # 1st move to latest weekday.
     if self.calendars == 'Weekend' or is_business_day(
             date, self.calendars):
         return date
     else:
         return prev_business_date(date, self.calendars)
예제 #2
0
파일: portfolio.py 프로젝트: mn98/gs-quant
 def _schedule_first_reports(self,
                             position_dates: List[dt.date],
                             show_progress: bool = True):
     dates_before_today = list(
         filter(lambda x: x < dt.date.today(), position_dates))
     latest_date_before_today = max(dates_before_today)
     self._schedule_reports(start_date=min(position_dates),
                            end_date=max(latest_date_before_today,
                                         prev_business_date()))
     if dt.date.today() in position_dates:
         self._schedule_reports(start_date=latest_date_before_today,
                                end_date=dt.date.today())
     if show_progress:
         self._show_report_progress()
예제 #3
0
 def set_start_date(self, start: dt.date):
     self.performance[prev_business_date(start)] = 100
예제 #4
0
 def set_start_date(self, start: dt.date):
     self.performance[prev_business_date(start)] = self.initial_value
     self.holdings[self.cash_asset] = self.initial_value
예제 #5
0
파일: portfolio.py 프로젝트: mn98/gs-quant
    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
예제 #6
0
 def set_start_date(self, start: dt.date):
     self.performance[prev_business_date(start)] = 100
     self.holdings[self.cash_asset] = 100
     self.holdings_projected[self.cash_asset] = 100
 def _adjust_date(self, date):
     if is_business_day(date, self.calendars):
         return date
     else:
         return prev_business_date(date, self.calendars)