public System.Func <LocalDate, FxIndexObservation> resolve(ReferenceData refData)
        {
            HolidayCalendar fixingCal   = fixingCalendar.resolve(refData);
            DateAdjuster    maturityAdj = maturityDateOffset.resolve(refData);

            return(fixingDate => create(fixingDate, fixingCal, maturityAdj));
        }
        //-------------------------------------------------------------------------
        /// <summary>
        /// Resolves this adjustment using the specified reference data.
        /// <para>
        /// Calling this method resolves the holiday calendar, returning a function that
        /// can convert a {@code SchedulePeriod} and period index pair to an optional {@code FxReset}.
        ///
        /// The conversion locks the fixing date based on the specified schedule period
        /// and the data held in this object.
        ///
        /// </para>
        /// </summary>
        /// <param name="refData">  the reference data to use when resolving </param>
        /// <returns> the resolved function </returns>
        /// <exception cref="ReferenceDataNotFoundException"> if an identifier cannot be resolved in the reference data </exception>
        /// <exception cref="RuntimeException"> if the calculation is invalid </exception>
        internal System.Func <int, SchedulePeriod, Optional <FxReset> > resolve(ReferenceData refData)
        {
            DateAdjuster fixingDateAdjuster = fixingDateOffset.resolve(refData);

            System.Func <LocalDate, FxIndexObservation> obsFn = index.resolve(refData);
            return((periodIndex, period) => buildFxReset(periodIndex, period, fixingDateAdjuster, obsFn));
        }
        /// <summary>
        /// Prepare for valuation anything that is not dependent upon the scenario.
        /// </summary>
        public override void HeadNodeInitialize(PriceFactorList factors, BaseTimeGrid baseTimes, RequiredResults resultsRequired)
        {
            base.HeadNodeInitialize(factors, baseTimes, resultsRequired);
            var deal = (AverageForwardExplicitDealBase)Deal;

            fScale = (deal.Buy_Sell == BuySell.Buy ? +1 : -1) * deal.GetUnits();

            fSamplingTimes          = new double[deal.Sampling_Data.Count];
            fSamplingTimesPlusTenor = new double[deal.Sampling_Data.Count];

            int  index       = 0;
            Term tenorAsTerm = Period.ValueToTerm(deal.Tenor);

            // Loop over sampling dates and generate relevant sampling times.
            foreach (SamplingEntryAsset sample in deal.Sampling_Data)
            {
                double endDate    = DateAdjuster.Add(sample.Date, tenorAsTerm, deal.GetHolidayCalendar());
                double sampleTime = CalcUtils.DaysToYears(sample.Date - factors.BaseDate);

                // Store the start time and the end time.
                fSamplingTimes[index]          = sampleTime; // Discount Factor and Forward Factor times are in Act365.
                fSamplingTimesPlusTenor[index] = CalcUtils.DaysToYears(endDate - factors.BaseDate);

                index++;
            }

            // Create a deep copy of the sampling data list and replace missing values with data from the rate fixings file
            var    assetPrice    = ((BaseAssetFxDealHelper)deal.GetDealHelper()).GetAssetPrice(factors);
            string assetCurrency = fPayoffType == PayoffType.Compo ? fPayoffCurrency : fCurrency;

            fSamplingData = deal.Sampling_Data.FillMissingDataFromFixings(factors.RateFixings, factors, assetPrice, assetCurrency, deal, "calculation of asset average");

            // Add to valuation time grid
            fT.AddPayDate(deal.Maturity_Date, resultsRequired.CashRequired());
        }
Exemplo n.º 4
0
        //-------------------------------------------------------------------------
        public ResolvedTermDeposit resolve(ReferenceData refData)
        {
            DateAdjuster bda          = BusinessDayAdjustment.orElse(BusinessDayAdjustment.NONE).resolve(refData);
            LocalDate    start        = bda.adjust(startDate);
            LocalDate    end          = bda.adjust(endDate);
            double       yearFraction = dayCount.yearFraction(start, end);

            return(ResolvedTermDeposit.builder().startDate(start).endDate(end).yearFraction(yearFraction).currency(Currency).notional(buySell.normalize(notional)).rate(rate).build());
        }
Exemplo n.º 5
0
        //-------------------------------------------------------------------------
        public ResolvedFxSingle resolve(ReferenceData refData)
        {
            if (paymentDateAdjustment == null)
            {
                return(ResolvedFxSingle.of(baseCurrencyPayment, counterCurrencyPayment));
            }
            DateAdjuster adjuster = paymentDateAdjustment.resolve(refData);

            return(ResolvedFxSingle.of(baseCurrencyPayment.adjustDate(adjuster), counterCurrencyPayment.adjustDate(adjuster)));
        }
Exemplo n.º 6
0
        private double CalculateEndDate(double rateStartDate, double rateTenor, IHolidayCalendar rateCalendar)
        {
            if (rateTenor == 0.0)
            {
                return(rateStartDate);
            }

            var term = Period.ValueToTerm(rateTenor);

            return(DateAdjuster.Add(DateTime.FromOADate(rateStartDate), term, 1, rateCalendar, true, RateAdjustmentMethod, RateStickyMonthEnd == YesNo.Yes).ToOADate());
        }
        //-------------------------------------------------------------------------
        /// <summary>
        /// Converts this period to one where the start and end dates are adjusted using the specified adjuster.
        /// <para>
        /// The start date of the result will be the start date of this period as altered by the specified adjuster.
        /// The end date of the result will be the end date of this period as altered by the specified adjuster.
        /// The unadjusted start date and unadjusted end date will be the same as in this period.
        /// </para>
        /// <para>
        /// The adjuster will typically be obtained from <seealso cref="BusinessDayAdjustment#resolve(ReferenceData)"/>.
        ///
        /// </para>
        /// </summary>
        /// <param name="adjuster">  the adjuster to use </param>
        /// <returns> the adjusted schedule period </returns>
        public SchedulePeriod toAdjusted(DateAdjuster adjuster)
        {
            // implementation needs to return 'this' if unchanged to optimize downstream code
            LocalDate resultStart = adjuster.adjust(startDate);
            LocalDate resultEnd   = adjuster.adjust(endDate);

            if (resultStart.Equals(startDate) && resultEnd.Equals(endDate))
            {
                return(this);
            }
            return(of(resultStart, resultEnd, unadjustedStartDate, unadjustedEndDate));
        }
Exemplo n.º 8
0
        //-------------------------------------------------------------------------
        /// <summary>
        /// Converts this schedule to a schedule where all the start and end dates are
        /// adjusted using the specified adjuster.
        /// <para>
        /// The result will have the same number of periods, but each start date and
        /// end date is replaced by the adjusted date as returned by the adjuster.
        /// The unadjusted start date and unadjusted end date of each period will not be changed.
        ///
        /// </para>
        /// </summary>
        /// <param name="adjuster">  the adjuster to use </param>
        /// <returns> the adjusted schedule </returns>
        public Schedule toAdjusted(DateAdjuster adjuster)
        {
            // implementation needs to return 'this' if unchanged to optimize downstream code
            bool adjusted = false;

            ImmutableList.Builder <SchedulePeriod> builder = ImmutableList.builder();
            foreach (SchedulePeriod period in periods)
            {
                SchedulePeriod adjPeriod = period.toAdjusted(adjuster);
                builder.add(adjPeriod);
                adjusted |= (adjPeriod != period);
            }
            return(adjusted ? new Schedule(builder.build(), frequency, rollConvention) : this);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Returns the cutoff date if supplied. If a cutoff date is not supplied, then return 0.0.
        /// </summary>
        /// <remarks>
        /// If a cutoff date is supplied, the deal is valued as if cashflows on or prior to this cutoff date are simply ignored.
        /// </remarks>
        public double GetCutoffDate(double baseDate)
        {
            if (baseDate <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(baseDate), "Base date must be positive.");
            }

            if (Use_Settlement_Offset == YesNo.Yes)
            {
                return(DateAdjuster.Add(baseDate, Settlement_Offset, fCalendarsSupport.GetHolidayCalendar(SettlementOffsetCalendarIndex)));
            }

            return(0.0);
        }
        public LocalDate calculateFixingFromMaturity(LocalDate maturityDate, ReferenceData refData)
        {
            // handle case where the input date is not a valid maturity date
            LocalDate maturityBusinessDay = maturityDateCalendar().resolve(refData).nextOrSame(maturityDate);
            // find the fixing date iteratively
            HolidayCalendar fixingCal          = fixingCalendar.resolve(refData);
            DateAdjuster    maturityFromFixing = maturityDateOffset.resolve(refData);
            LocalDate       fixingDate         = maturityBusinessDay;

            while (fixingCal.isHoliday(fixingDate) || maturityFromFixing.adjust(fixingDate).isAfter(maturityBusinessDay))
            {
                fixingDate = fixingDate.minusDays(1);
            }
            return(fixingDate);
        }
Exemplo n.º 11
0
        // create the payment period
        private IList <SwapPaymentPeriod> createPaymentPeriods(Schedule resolvedPayments, ReferenceData refData)
        {
            // resolve amount schedule against payment schedule
            DoubleArray amounts = amount.resolveValues(resolvedPayments);
            // resolve against reference data once
            DateAdjuster paymentDateAdjuster = paymentSchedule.PaymentDateOffset.resolve(refData);

            // build up payment periods using schedule
            ImmutableList.Builder <SwapPaymentPeriod> paymentPeriods = ImmutableList.builder();
            for (int index = 0; index < resolvedPayments.size(); index++)
            {
                SchedulePeriod paymentPeriod = resolvedPayments.getPeriod(index);
                LocalDate      baseDate      = paymentSchedule.PaymentRelativeTo.selectBaseDate(paymentPeriod);
                LocalDate      paymentDate   = paymentDateAdjuster.adjust(baseDate);
                double         amount        = payReceive.normalize(amounts.get(index));
                Payment        payment       = Payment.of(CurrencyAmount.of(currency, amount), paymentDate);
                paymentPeriods.add(KnownAmountSwapPaymentPeriod.of(payment, paymentPeriod));
            }
            return(paymentPeriods.build());
        }
        //-------------------------------------------------------------------------
        public ResolvedIborCapFloorLeg resolve(ReferenceData refData)
        {
            Schedule     adjustedSchedule = paymentSchedule.createSchedule(refData);
            DoubleArray  cap                 = CapSchedule.Present ? capSchedule.resolveValues(adjustedSchedule) : null;
            DoubleArray  floor               = FloorSchedule.Present ? floorSchedule.resolveValues(adjustedSchedule) : null;
            DoubleArray  notionals           = notional.resolveValues(adjustedSchedule);
            DateAdjuster fixingDateAdjuster  = calculation.FixingDateOffset.resolve(refData);
            DateAdjuster paymentDateAdjuster = paymentDateOffset.resolve(refData);

            System.Func <LocalDate, IborIndexObservation>    obsFn        = calculation.Index.resolve(refData);
            ImmutableList.Builder <IborCapletFloorletPeriod> periodsBuild = ImmutableList.builder();
            for (int i = 0; i < adjustedSchedule.size(); i++)
            {
                SchedulePeriod period         = adjustedSchedule.getPeriod(i);
                LocalDate      paymentDate    = paymentDateAdjuster.adjust(period.EndDate);
                LocalDate      fixingDate     = fixingDateAdjuster.adjust((calculation.FixingRelativeTo.Equals(FixingRelativeTo.PERIOD_START)) ? period.StartDate : period.EndDate);
                double         signedNotional = payReceive.normalize(notionals.get(i));
                periodsBuild.add(IborCapletFloorletPeriod.builder().unadjustedStartDate(period.UnadjustedStartDate).unadjustedEndDate(period.UnadjustedEndDate).startDate(period.StartDate).endDate(period.EndDate).iborRate(IborRateComputation.of(obsFn(fixingDate))).paymentDate(paymentDate).notional(signedNotional).currency(currency).yearFraction(period.yearFraction(calculation.DayCount, adjustedSchedule)).caplet(cap != null ? cap.get(i) : null).floorlet(floor != null ? floor.get(i) : null).build());
            }
            return(ResolvedIborCapFloorLeg.builder().capletFloorletPeriods(periodsBuild.build()).payReceive(payReceive).build());
        }
        // build the FxReset
        private Optional <FxReset> buildFxReset(int periodIndex, SchedulePeriod period, DateAdjuster fixingDateAdjuster, System.Func <LocalDate, FxIndexObservation> obsFn)
        {
            if (periodIndex == 0 && initialNotionalValue != null)
            {
                //if first notional is fixed then no FxReset is applied
                return(null);
            }
            LocalDate fixingDate = fixingDateAdjuster.adjust(fixingRelativeTo.selectBaseDate(period));

            return(FxReset.of(obsFn(fixingDate), referenceCurrency));
        }
Exemplo n.º 14
0
 /// <summary>
 /// Generate rate start date from reset date.
 /// </summary>
 public double CalculateRateStartDate(double resetDate)
 {
     return(DateAdjuster.Add(resetDate, RateOffset, RateCalendar));
 }
Exemplo n.º 15
0
 /// <summary>
 /// Deal end date.
 /// </summary>
 public override double EndDate()
 {
     return(DateAdjuster.Add(Effective_Date, Investment_Horizon));
 }
Exemplo n.º 16
0
 /// <summary>
 /// Gets the next reset date.
 /// </summary>
 /// <remarks>Treat the horizon as truncating the lending.</remarks>
 public override TDate GetNextResetDate(TDate currentResetDate)
 {
     return(Reset_Frequency > 0.0 ?  Math.Min(DateAdjuster.Add(currentResetDate, Period.ValueToTerm(Reset_Frequency), GetHolidayCalendar()), EndDate()) : DateAdjuster.Add(Effective_Date, Investment_Horizon));
 }
        // creates an observation
        private FxIndexObservation create(LocalDate fixingDate, HolidayCalendar fixingCal, DateAdjuster maturityAdjuster)
        {
            LocalDate fixingBusinessDay = fixingCal.nextOrSame(fixingDate);
            LocalDate maturityDate      = maturityAdjuster.adjust(fixingBusinessDay);

            return(new FxIndexObservation(this, fixingDate, maturityDate));
        }
Exemplo n.º 18
0
        /// <summary>
        /// Calculate the dates and years fractions not specified on the deal and get any known rates.
        /// </summary>
        public void Prepare(double baseDate, RateFixingsProvider rateFixings)
        {
            // Calculates various date properties of the deal and cache them.
            var deal             = (FloatingInterestCashflowInterpolatedDeal)Deal;
            var accrualCalendars = deal.GetAccrualHolidayCalendars();
            var rateCalendars    = deal.GetRateHolidayCalendars();

            fPaymentDate = deal.Payment_Date;

            if (deal.Accrual_Year_Fraction > 0.0)
            {
                fAccrualYearFraction = deal.Accrual_Year_Fraction;
            }
            else
            {
                fAccrualYearFraction = CalcUtils.DayCountFraction(deal.Accrual_Start_Date, deal.Accrual_End_Date, deal.Accrual_Day_Count, accrualCalendars);
            }

            fKnownResetRate1 = null;
            fKnownResetRate2 = null;

            if (deal.HasRate1())
            {
                if (deal.Rate_1_End_Date > 0.0)
                {
                    fRate1EndDate = deal.Rate_1_End_Date;
                }
                else
                {
                    fRate1EndDate = DateAdjuster.Add(deal.Rate_Start_Date, deal.Rate_1_Tenor, 1, rateCalendars, true, deal.Rate_Adjustment_Method, deal.Rate_Sticky_Month_End == YesNo.Yes).ToOADate();
                }

                if (deal.Rate_1_Year_Fraction > 0.0)
                {
                    fRate1YearFraction = deal.Rate_1_Year_Fraction;
                }
                else
                {
                    fRate1YearFraction = CalcUtils.DayCountFraction(deal.Rate_Start_Date, fRate1EndDate, deal.Rate_Day_Count, rateCalendars, deal.Rate_1_Tenor);
                }

                fKnownResetRate1 = GetKnownResetRate(baseDate, deal.Reset_Date, fPaymentDate, deal.Use_Known_Rate_1, deal.Known_Rate_1, deal.Rate_1_Fixing, rateFixings, deal);
            }

            if (deal.HasRate2())
            {
                if (deal.Rate_2_End_Date > 0.0)
                {
                    fRate2EndDate = deal.Rate_2_End_Date;
                }
                else
                {
                    fRate2EndDate = DateAdjuster.Add(deal.Rate_Start_Date, deal.Rate_2_Tenor, 1, rateCalendars, true, deal.Rate_Adjustment_Method, deal.Rate_Sticky_Month_End == YesNo.Yes).ToOADate();
                }

                if (deal.Rate_2_Year_Fraction > 0.0)
                {
                    fRate2YearFraction = deal.Rate_2_Year_Fraction;
                }
                else
                {
                    fRate2YearFraction = CalcUtils.DayCountFraction(deal.Rate_Start_Date, fRate2EndDate, deal.Rate_Day_Count, rateCalendars, deal.Rate_2_Tenor);
                }

                fKnownResetRate2 = GetKnownResetRate(baseDate, deal.Reset_Date, fPaymentDate, deal.Use_Known_Rate_2, deal.Known_Rate_2, deal.Rate_2_Fixing, rateFixings, deal);
            }
        }