/// <summary> /// /// </summary> /// <param name="payerIsBase"></param> /// <param name="calculation"></param> /// <param name="coupon"></param> /// <param name="fOCalculationMethod"></param> /// <param name="fixingCalendar"></param> /// <param name="paymentCalendar"></param> /// <returns></returns> public static PriceableRateCoupon CreatePriceableCoupon(bool payerIsBase, Calculation calculation, PaymentCalculationPeriod coupon, bool fOCalculationMethod , IBusinessCalendar fixingCalendar, IBusinessCalendar paymentCalendar) { var notional = (Notional)calculation.Item; var currency = notional.notionalStepSchedule.currency; var dayCountFraction = calculation.dayCountFraction; var paymentDate = coupon.adjustedPaymentDateSpecified ? coupon.adjustedPaymentDate : coupon.unadjustedPaymentDate; var calculationPeriods = XsdClassesFieldResolver.GetPaymentCalculationPeriodCalculationPeriodArray(coupon); var discountFactorSpecified = coupon.discountFactorSpecified; if (calculationPeriods.Length == 1) { var calculationPeriod = calculationPeriods[0]; var notionalAmount = XsdClassesFieldResolver.CalculationPeriodGetNotionalAmount(calculationPeriod); var money = payerIsBase ? MoneyHelper.GetAmount(-1 * notionalAmount, currency) : MoneyHelper.GetAmount(notionalAmount, currency); var accrualStartDate = calculationPeriod.adjustedStartDateSpecified ? calculationPeriod.adjustedStartDate : calculationPeriod.unadjustedStartDate; var accrualEndDate = calculationPeriod.adjustedEndDateSpecified ? calculationPeriod.adjustedEndDate : calculationPeriod.unadjustedEndDate; // If has a fixed rate (fixed rate coupon) var isThereDiscounting = XsdClassesFieldResolver.CalculationHasDiscounting(calculation); PriceableRateCoupon rateCoupon; if (XsdClassesFieldResolver.CalculationPeriodHasFixedRate(calculationPeriod)) { decimal finalRate = XsdClassesFieldResolver.CalculationPeriodGetFixedRate(calculationPeriod); //The discount rate must be set. It would normally be the final rate. The assumption is that, it the discounting rate is zero then the final rate should be used. if (isThereDiscounting) { var discounting = XsdClassesFieldResolver.CalculationGetDiscounting(calculation); //This test works because if the rate is zero, then the coupon is not discounted and discounting Type should be null. var discountRate = discounting.discountRate == 0.0m ? finalRate : discounting.discountRate; rateCoupon = new PriceableFixedRateCoupon(coupon.id, payerIsBase, accrualStartDate, accrualEndDate, dayCountFraction, finalRate, money, null, paymentDate, discounting.discountingType, discountRate, null, paymentCalendar); if (discountFactorSpecified) { rateCoupon.PaymentDiscountFactor = coupon.discountFactor; } return(rateCoupon); } rateCoupon = new PriceableFixedRateCoupon(coupon.id, payerIsBase, accrualStartDate, accrualEndDate, dayCountFraction, finalRate, money, null, paymentDate, null, null, null, paymentCalendar); if (discountFactorSpecified) { rateCoupon.PaymentDiscountFactor = coupon.discountFactor; } return(rateCoupon); } if (XsdClassesFieldResolver.CalculationPeriodHasFloatingRateDefinition(calculationPeriod)) { //The floating rate definition. FloatingRateDefinition floatingRateDefinition = XsdClassesFieldResolver.CalculationPeriodGetFloatingRateDefinition(calculationPeriod); //The floating rate Calculation. Debug.Assert(calculation.Items != null); Debug.Assert(calculation.Items.Length > 0); Debug.Assert(calculation.Items[0] is FloatingRateCalculation); var floatingRateCalculation = (FloatingRateCalculation)calculation.Items[0]; //The forecast rate index. var floatingRateIndex = floatingRateCalculation.floatingRateIndex; var indexTenor = floatingRateCalculation.indexTenor.ToString(); var forecastRate = ForecastRateIndexHelper.Parse(floatingRateIndex.Value, indexTenor); //The rate observation // Apply spread from schedule if it hasn't been specified yet. var margin = 0m; if (floatingRateDefinition.spreadSpecified) { margin = floatingRateDefinition.spread; } //The observed rate. Decimal?observedRate = null; Decimal?capStrike = null; Decimal?floorStrike = null; if (floatingRateDefinition.capRate != null) { capStrike = floatingRateDefinition.capRate[0].strikeRate; } if (floatingRateDefinition.floorRate != null) { floorStrike = floatingRateDefinition.floorRate[0].strikeRate; } if (floatingRateDefinition.rateObservation != null)//TODO This is a big problem. Need to handle the case of no fixing date! { var rateObservation = floatingRateDefinition.rateObservation[0]; if (rateObservation.observedRateSpecified) { observedRate = rateObservation.observedRate; } //Removed because Igor's old code populates these fields when the trade is created. This means the coupon is not recalculated! //Now the coupon will ignore any previous calculations and only treat as a fixed coupon if the observed rate has been specified. if (isThereDiscounting) { var discounting = XsdClassesFieldResolver.CalculationGetDiscounting(calculation); if (capStrike != null || floorStrike != null) { rateCoupon = new PriceableCapFloorCoupon(coupon.id, !payerIsBase, capStrike, floorStrike, accrualStartDate, accrualEndDate, rateObservation.adjustedFixingDate, dayCountFraction, margin, observedRate, money, paymentDate, forecastRate, discounting.discountingType, observedRate, null, fixingCalendar, paymentCalendar); } else { rateCoupon = new PriceableFloatingRateCoupon(coupon.id, !payerIsBase, accrualStartDate, accrualEndDate, rateObservation.adjustedFixingDate, dayCountFraction, margin, observedRate, money, paymentDate, forecastRate, discounting.discountingType, observedRate, null, fixingCalendar, paymentCalendar); } } else { if (capStrike != null || floorStrike != null) { rateCoupon = new PriceableCapFloorCoupon(coupon.id, !payerIsBase, capStrike, floorStrike, accrualStartDate, accrualEndDate, rateObservation.adjustedFixingDate, dayCountFraction, margin, observedRate, money, paymentDate, forecastRate, null, null, null, fixingCalendar, paymentCalendar); } else { rateCoupon = new PriceableFloatingRateCoupon(coupon.id, payerIsBase, accrualStartDate, accrualEndDate, rateObservation.adjustedFixingDate, dayCountFraction, margin, observedRate, money, paymentDate, forecastRate, null, null, null, fixingCalendar, paymentCalendar); } } if (fOCalculationMethod) { ((PriceableFloatingRateCoupon)rateCoupon).ForecastRateInterpolation = true; } if (discountFactorSpecified) { rateCoupon.PaymentDiscountFactor = coupon.discountFactor; } return(rateCoupon); } throw new NotImplementedException("Need to return a rate coupon, Alex!"); } throw new System.Exception("CalculationPeriod has neither fixedRate nor floatingRateDefinition."); } throw new System.Exception("PaymentCalculationPeriod has zero, or multiple CalculationPeriods."); }
private static void CalculateForecastPaymentAmount(Calculation calculation, FloatingRateCalculation floatingRateCalculation, PaymentCalculationPeriod paymentCalculationPeriod, IRateCurve forecastCurve, IRateCurve discountCurve, DateTime valuationDate) { var amountAccruedPerPaymentPeriod = new List <Money>(); decimal interestFromPreviousPeriods = 0; Notional notionalSchedule = XsdClassesFieldResolver.CalculationGetNotionalSchedule(calculation); Currency notionalCurrency = notionalSchedule.notionalStepSchedule.currency; // Cashflows // foreach (CalculationPeriod calculationPeriod in XsdClassesFieldResolver.GetPaymentCalculationPeriodCalculationPeriodArray(paymentCalculationPeriod)) { decimal notional = XsdClassesFieldResolver.CalculationPeriodGetNotionalAmount(calculationPeriod); decimal finalRate = 0.0m; // If has a fixed rate (fixed rate coupon) // if (XsdClassesFieldResolver.CalculationPeriodHasFixedRate(calculationPeriod)) { finalRate = XsdClassesFieldResolver.CalculationPeriodGetFixedRate(calculationPeriod); } else if (XsdClassesFieldResolver.CalculationPeriodHasFloatingRateDefinition(calculationPeriod)) { if (null != forecastCurve) { FloatingRateDefinition floatingRateDefinition = XsdClassesFieldResolver.CalculationPeriodGetFloatingRateDefinition(calculationPeriod); // Apply spread from schedule if it hasn't been specified yet. // if (!floatingRateDefinition.spreadSpecified) { floatingRateDefinition.spread = floatingRateCalculation.spreadSchedule[0].initialValue; floatingRateDefinition.spreadSpecified = true; } ForecastRateHelper.UpdateFloatingRateDefinition(floatingRateDefinition, floatingRateCalculation, calculation.dayCountFraction, calculationPeriod, forecastCurve); calculationPeriod.Item1 = floatingRateDefinition; decimal calculatedRate = floatingRateDefinition.calculatedRate; // final rate after application of Cap/Floor rates // finalRate = calculatedRate; // If has a Cap rate, finalRate = MAX(0, FinalRate - CapRate) // if (null != floatingRateDefinition.capRate) { Strike strike = floatingRateDefinition.capRate[0]; finalRate = System.Math.Max(0, finalRate - strike.strikeRate); } // If has a Floor rate, finalRate = MAX(0, FloorRate - FinalRate) // if (null != floatingRateDefinition.floorRate) { Strike strike = floatingRateDefinition.floorRate[0]; finalRate = System.Math.Max(0, strike.strikeRate - finalRate); } } } else { throw new System.Exception("CalculationPeriod has neither fixedRate nor floatngRateDefinition."); } // Compound interest accrued during previos calculation periods in this payment period. // decimal notionalAdjustedForInterestFromPreviousPeriods = notional + interestFromPreviousPeriods; if (calculation.discounting == null) { interestFromPreviousPeriods = notionalAdjustedForInterestFromPreviousPeriods * finalRate * calculationPeriod.dayCountYearFraction; } else if (calculation.discounting.discountingType == DiscountingTypeEnum.FRA || calculation.discounting.discountingType == DiscountingTypeEnum.Standard) { interestFromPreviousPeriods = notionalAdjustedForInterestFromPreviousPeriods * (1.0m - 1.0m / (1.0m + finalRate * calculationPeriod.dayCountYearFraction)); } else { throw new NotSupportedException("The specified discountingType is not supported."); } Money amountAccruedPerCalculationPeriod = MoneyHelper.GetAmount(interestFromPreviousPeriods, notionalCurrency); amountAccruedPerPaymentPeriod.Add(amountAccruedPerCalculationPeriod); } paymentCalculationPeriod.forecastPaymentAmount = MoneyHelper.Sum(amountAccruedPerPaymentPeriod); paymentCalculationPeriod.discountFactor = (decimal)discountCurve.GetDiscountFactor(valuationDate, paymentCalculationPeriod.adjustedPaymentDate); paymentCalculationPeriod.discountFactorSpecified = true; paymentCalculationPeriod.presentValueAmount = MoneyHelper.Mul(paymentCalculationPeriod.forecastPaymentAmount, paymentCalculationPeriod.discountFactor); }