private static void UpdateCashflowsWithDetailedCashflows(Cashflows cashflows, IEnumerable <DetailedCashflowRangeItem> listDetailedCashflows /*, bool fixedLeg*/) { var paymentCalculationPeriods = new List <PaymentCalculationPeriod>(); foreach (DetailedCashflowRangeItem detailedCashflowRangeItem in listDetailedCashflows) { var paymentCalculationPeriod = new PaymentCalculationPeriod(); var calculationPeriod = new CalculationPeriod(); paymentCalculationPeriod.Items = new object[] { calculationPeriod }; paymentCalculationPeriod.adjustedPaymentDate = detailedCashflowRangeItem.PaymentDate; paymentCalculationPeriod.adjustedPaymentDateSpecified = true; PaymentCalculationPeriodHelper.SetCalculationPeriodStartDate(paymentCalculationPeriod, detailedCashflowRangeItem.StartDate); PaymentCalculationPeriodHelper.SetCalculationPeriodEndDate(paymentCalculationPeriod, detailedCashflowRangeItem.EndDate); // Update notional amount // PaymentCalculationPeriodHelper.SetNotionalAmount(paymentCalculationPeriod, (decimal)detailedCashflowRangeItem.NotionalAmount); if (detailedCashflowRangeItem.CouponType == "Fixed") { //CalculationPeriod calculationPeriod = PaymentCalculationPeriodHelper.GetCalculationPeriods(paymentCalculationPeriod)[0]; //if (XsdClassesFieldResolver.CalculationPeriod_HasFixedRate(calculationPeriod)) //{ // Fixed->Fixed // //PaymentCalculationPeriodHelper.SetRate(paymentCalculationPeriod, (decimal)detailedCashflowRangeItem.Rate); XsdClassesFieldResolver.SetCalculationPeriodFixedRate(calculationPeriod, (decimal)detailedCashflowRangeItem.Rate); //} //else if (XsdClassesFieldResolver.CalculationPeriod_HasFloatingRateDefinition(calculationPeriod)) // { // // Float->Fixed // // // PaymentCalculationPeriodHelper.ReplaceFloatingRateWithFixedRate(paymentCalculationPeriod, (decimal)detailedCashflowRangeItem.Rate); // } // else // { // throw new NotImplementedException(); // } } else if (detailedCashflowRangeItem.CouponType == "Float") { // Create floating rate definiton... // var floatingRateDefinition = new FloatingRateDefinition(); //XsdClassesFieldResolver.CalculationPeriod_SetFloatingRateDefinition(calculationPeriod, floatingRateDefinition); calculationPeriod.Item1 = floatingRateDefinition; // After the spread is reset - we need to update calculated rate. // PaymentCalculationPeriodHelper.SetSpread(paymentCalculationPeriod, (decimal)detailedCashflowRangeItem.Spread); } else { string message = $"Unsupported coupon type '{detailedCashflowRangeItem.CouponType}"; throw new System.Exception(message); } paymentCalculationPeriods.Add(paymentCalculationPeriod); } cashflows.cashflowsMatchParameters = false; cashflows.paymentCalculationPeriod = paymentCalculationPeriods.ToArray(); }
public void TestGetCapPremiumAUD_6M100M_5YExpiry05Vol20Pct() { var valuationDate = new DateTime(1994, 12, 14); var curveId = BuildAndCacheRateCurve(valuationDate); CapFloorLegParametersRange_Old capLeg = GetCapFloorInputParameters(valuationDate, valuationDate.AddMonths(6), valuationDate.AddYears(5), CapFloorType.Cap, "Standard", curveId, curveId); InterestRateStream floatStream = InterestRateStreamParametricDefinitionGenerator.GenerateStreamDefinition(capLeg); floatStream.cashflows = FixedAndFloatingRateStreamCashflowGenerator.GetCashflows(floatStream, FixingCalendar, PaymentCalendar); double sumOfCapletPremiums = 0; var rateCurve = (RateCurve)Engine.GetCurve(curveId, false); foreach (PaymentCalculationPeriod paymentCalculationPeriod in floatStream.cashflows.paymentCalculationPeriod) { DateTime startDate = PaymentCalculationPeriodHelper.GetCalculationPeriodStartDate(paymentCalculationPeriod); DateTime endDate = PaymentCalculationPeriodHelper.GetCalculationPeriodEndDate(paymentCalculationPeriod); double accrualFactor = (endDate - startDate).TotalDays / 365.0; var discountFactor = (double)paymentCalculationPeriod.discountFactor; var rate = (double)PaymentCalculationPeriodHelper.GetRate(paymentCalculationPeriod); double rate2 = rateCurve.GetForwardRate(startDate, endDate, "ACT/365.FIXED"); double diff = rate - rate2; Debug.Print("Diff in forward rate: {0}", diff); var strikeRate = (double)capLeg.StrikeRate; //fixed - replace with a schedule const double volatility = 0.2; //fixed - replace with a schedule double timeToExpiry = (startDate - valuationDate).TotalDays / 365.0; double optionValue = accrualFactor * BlackModel.GetSwaptionValue(rate, strikeRate, volatility, timeToExpiry) * discountFactor; Debug.Print("Expiry:\t{0},\tPremium:\t{1}'", timeToExpiry, optionValue); sumOfCapletPremiums += optionValue; } Debug.Print("Premium : '{0}'", sumOfCapletPremiums * (double)capLeg.NotionalAmount); }
public List <DetailedCashflowRangeItem> GetDetailedCashflowsWithNotionalSchedule( ILogger logger, ICoreCache cache, String nameSpace, IBusinessCalendar fixingCalendar, IBusinessCalendar paymentCalendar, SwapLegParametersRange_Old legParametersRange, List <DateTimeDoubleRangeItem> notionalValueItems, ValuationRange valuationRange) { var tempList = notionalValueItems.Select(item => new Pair <DateTime, decimal>(item.DateTime, Convert.ToDecimal(item.Value))).ToList(); NonNegativeSchedule notionalScheduleFpML = NonNegativeScheduleHelper.Create(tempList); Currency currency = CurrencyHelper.Parse(legParametersRange.Currency); NonNegativeAmountSchedule amountSchedule = NonNegativeAmountScheduleHelper.Create(notionalScheduleFpML, currency); InterestRateStream interestRateStream = GetCashflowsScheduleWithNotionalSchedule(fixingCalendar, paymentCalendar, legParametersRange, amountSchedule); UpdateCashflowsWithAmounts(logger, cache, nameSpace, interestRateStream, legParametersRange, valuationRange); var list = new List <DetailedCashflowRangeItem>(); //int periodNumber = 1; foreach (PaymentCalculationPeriod paymentCalculationPeriod in interestRateStream.cashflows.paymentCalculationPeriod) { var detailedCashflowRangeItem = new DetailedCashflowRangeItem(); list.Add(detailedCashflowRangeItem); detailedCashflowRangeItem.PaymentDate = paymentCalculationPeriod.adjustedPaymentDate; detailedCashflowRangeItem.StartDate = PaymentCalculationPeriodHelper.GetCalculationPeriodStartDate(paymentCalculationPeriod); detailedCashflowRangeItem.EndDate = PaymentCalculationPeriodHelper.GetCalculationPeriodEndDate(paymentCalculationPeriod); //detailedCashflowRangeItem.NumberOfDays = PaymentCalculationPeriodHelper.GetNumberOfDays(paymentCalculationPeriod); //detailedCashflowRangeItem.FutureValue = MoneyHelper.ToDouble(paymentCalculationPeriod.forecastPaymentAmount); //detailedCashflowRangeItem.PresentValue = MoneyHelper.ToDouble(paymentCalculationPeriod.presentValueAmount); //detailedCashflowRangeItem.DiscountFactor = (double)paymentCalculationPeriod.discountFactor; detailedCashflowRangeItem.NotionalAmount = (double)PaymentCalculationPeriodHelper.GetNotionalAmount(paymentCalculationPeriod); detailedCashflowRangeItem.CouponType = GetCouponType(paymentCalculationPeriod); detailedCashflowRangeItem.Rate = (double)PaymentCalculationPeriodHelper.GetRate(paymentCalculationPeriod); // If floating rate - retrieve the spread. // if (legParametersRange.IsFloatingLegType()) { detailedCashflowRangeItem.Spread = (double)PaymentCalculationPeriodHelper.GetSpread(paymentCalculationPeriod); } } return(list); }
private static string GetCouponType(PaymentCalculationPeriod pcalculationPeriod) { CalculationPeriod calculationPeriod = PaymentCalculationPeriodHelper.GetCalculationPeriods(pcalculationPeriod)[0]; FloatingRateDefinition floatingRateDefinition = XsdClassesFieldResolver.CalculationPeriodGetFloatingRateDefinition(calculationPeriod); // If has a Cap rate, finalRate = MAX(0, FinalRate - CapRate) // if (null != floatingRateDefinition.capRate) { return("Cap"); } // If has a Floor rate, finalRate = MAX(0, FloorRate - FinalRate) // if (null != floatingRateDefinition.floorRate) { return("Floor"); } throw new System.Exception("Invalid coupond type. Only Cap & Floor coupons are expected here."); }
public static List <DetailedCashflowRangeItem> GetDetailedCashflowsTestOnly( ILogger logger, ICoreCache cache, String nameSpace, IBusinessCalendar fixingCalendar, IBusinessCalendar paymentCalendar, SwapLegParametersRange_Old legParametersRange, ValuationRange valuationRange) { InterestRateStream interestRateStream = GetCashflowsSchedule(fixingCalendar, paymentCalendar, legParametersRange); UpdateCashflowsWithAmounts(logger, cache, nameSpace, interestRateStream, legParametersRange, valuationRange); var list = new List <DetailedCashflowRangeItem>(); foreach (PaymentCalculationPeriod paymentCalculationPeriod in interestRateStream.cashflows.paymentCalculationPeriod) { var detailedCashflowRangeItem = new DetailedCashflowRangeItem(); list.Add(detailedCashflowRangeItem); detailedCashflowRangeItem.PaymentDate = paymentCalculationPeriod.adjustedPaymentDate; detailedCashflowRangeItem.StartDate = PaymentCalculationPeriodHelper.GetCalculationPeriodStartDate(paymentCalculationPeriod); detailedCashflowRangeItem.EndDate = PaymentCalculationPeriodHelper.GetCalculationPeriodEndDate(paymentCalculationPeriod); //detailedCashflowRangeItem.NumberOfDays = PaymentCalculationPeriodHelper.GetNumberOfDays(paymentCalculationPeriod); //detailedCashflowRangeItem.FutureValue = MoneyHelper.ToDouble(paymentCalculationPeriod.forecastPaymentAmount); //detailedCashflowRangeItem.PresentValue = MoneyHelper.ToDouble(paymentCalculationPeriod.presentValueAmount); //detailedCashflowRangeItem.DiscountFactor = (double)paymentCalculationPeriod.discountFactor; detailedCashflowRangeItem.NotionalAmount = (double)PaymentCalculationPeriodHelper.GetNotionalAmount(paymentCalculationPeriod); detailedCashflowRangeItem.CouponType = GetCouponType(paymentCalculationPeriod); detailedCashflowRangeItem.Rate = (double)PaymentCalculationPeriodHelper.GetRate(paymentCalculationPeriod); // If floating rate - retrieve a spread. // if (legParametersRange.IsFloatingLegType()) { detailedCashflowRangeItem.Spread = (double)PaymentCalculationPeriodHelper.GetSpread(paymentCalculationPeriod); } } return(list); }
private static string GetCouponType(PaymentCalculationPeriod pcalculationPeriod) { CalculationPeriod calculationPeriod = PaymentCalculationPeriodHelper.GetCalculationPeriods(pcalculationPeriod)[0]; return(XsdClassesFieldResolver.CalculationPeriodHasFloatingRateDefinition(calculationPeriod) ? "Float" : "Fixed"); }
private static Cashflows UpdateCashflowsWithDetailedCashflows(IEnumerable <InputCashflowRangeItem> listDetailedCashflows /*, bool fixedLeg*/) { var cashflows = new Cashflows(); var paymentCalculationPeriods = new List <PaymentCalculationPeriod>(); foreach (var detailedCashflowRangeItem in listDetailedCashflows) { var paymentCalculationPeriod = new PaymentCalculationPeriod(); var calculationPeriod = new CalculationPeriod(); paymentCalculationPeriod.Items = new object[] { calculationPeriod }; paymentCalculationPeriod.adjustedPaymentDate = detailedCashflowRangeItem.PaymentDate; paymentCalculationPeriod.adjustedPaymentDateSpecified = true; PaymentCalculationPeriodHelper.SetCalculationPeriodStartDate(paymentCalculationPeriod, detailedCashflowRangeItem.StartDate); PaymentCalculationPeriodHelper.SetCalculationPeriodEndDate(paymentCalculationPeriod, detailedCashflowRangeItem.EndDate); // Update notional amount // PaymentCalculationPeriodHelper.SetNotionalAmount(paymentCalculationPeriod, (decimal)detailedCashflowRangeItem.NotionalAmount); switch (detailedCashflowRangeItem.CouponType.ToLower()) { case "cap": { var floatingRateDefinition = new FloatingRateDefinition(); calculationPeriod.Item1 = floatingRateDefinition; // After the spread is reset - we need to update calculated rate. // PaymentCalculationPeriodHelper.SetSpread(paymentCalculationPeriod, (decimal)detailedCashflowRangeItem.Spread); floatingRateDefinition.capRate = new[] { new Strike() }; floatingRateDefinition.capRate[0].strikeRate = (decimal)detailedCashflowRangeItem.StrikeRate; //tODO There is no fixing date. { var rateObservation = new RateObservation { adjustedFixingDate = detailedCashflowRangeItem.FixingDate, adjustedFixingDateSpecified = true }; floatingRateDefinition.rateObservation = new[] { rateObservation }; } break; } case "floor": { var floatingRateDefinition = new FloatingRateDefinition(); //XsdClassesFieldResolver.CalculationPeriod_SetFloatingRateDefinition(calculationPeriod, floatingRateDefinition); calculationPeriod.Item1 = floatingRateDefinition; // After the spread is reset - we need to update calculated rate. // PaymentCalculationPeriodHelper.SetSpread(paymentCalculationPeriod, (decimal)detailedCashflowRangeItem.Spread); floatingRateDefinition.capRate = new[] { new Strike() }; floatingRateDefinition.capRate[0].strikeRate = (decimal)detailedCashflowRangeItem.StrikeRate; { var rateObservation = new RateObservation { adjustedFixingDate = detailedCashflowRangeItem.FixingDate, adjustedFixingDateSpecified = true }; floatingRateDefinition.rateObservation = new[] { rateObservation }; } break; } default: { string message = String.Format("Specified coupon type : '{0}' is not supported. Please use one of these: 'cap, floor'", detailedCashflowRangeItem.CouponType.ToLower()); throw new NotSupportedException(message); } } paymentCalculationPeriods.Add(paymentCalculationPeriod); } cashflows.cashflowsMatchParameters = true; cashflows.paymentCalculationPeriod = paymentCalculationPeriods.ToArray(); return(cashflows); }
public List <DetailedCashflowRangeItem> GetDetailedCashflowsWithNotionalSchedule( ILogger logger, ICoreCache cache, String nameSpace, IBusinessCalendar fixingCalendar, IBusinessCalendar paymentCalendar, CapFloorLegParametersRange_Old legParametersRange, List <DateTimeDoubleRangeItem> notionalValueItems, ValuationRange valuationRange) { //Check if the calendars are null. If not build them! var list1 = notionalValueItems.Select(item => new Pair <DateTime, decimal>(item.DateTime, Convert.ToDecimal(item.Value))).ToList(); NonNegativeSchedule notionalScheduleFpML = NonNegativeScheduleHelper.Create(list1); Currency currency = CurrencyHelper.Parse(legParametersRange.Currency); NonNegativeAmountSchedule amountSchedule = NonNegativeAmountScheduleHelper.Create(notionalScheduleFpML, currency); InterestRateStream interestRateStream = GetCashflowsScheduleWithNotionalSchedule(fixingCalendar, paymentCalendar, legParametersRange, amountSchedule); //Add the principal exchanges to the cashflows. var principalExchangeList = list1.Select(cashflow => new PrincipalExchange { adjustedPrincipalExchangeDate = cashflow.First, adjustedPrincipalExchangeDateSpecified = true, principalExchangeAmount = cashflow.Second, principalExchangeAmountSpecified = true }).ToArray(); interestRateStream.cashflows.principalExchange = principalExchangeList; UpdateCashflowsWithAmounts(logger, cache, nameSpace, interestRateStream, legParametersRange, valuationRange); var list = new List <DetailedCashflowRangeItem>(); foreach (PaymentCalculationPeriod paymentCalculationPeriod in interestRateStream.cashflows.paymentCalculationPeriod) { var detailedCashflowRangeItem = new DetailedCashflowRangeItem(); detailedCashflowRangeItem.PaymentDate = paymentCalculationPeriod.adjustedPaymentDate; detailedCashflowRangeItem.StartDate = PaymentCalculationPeriodHelper.GetCalculationPeriodStartDate(paymentCalculationPeriod); detailedCashflowRangeItem.EndDate = PaymentCalculationPeriodHelper.GetCalculationPeriodEndDate(paymentCalculationPeriod); detailedCashflowRangeItem.NumberOfDays = PaymentCalculationPeriodHelper.GetNumberOfDays(paymentCalculationPeriod); detailedCashflowRangeItem.FutureValue = MoneyHelper.ToDouble(paymentCalculationPeriod.forecastPaymentAmount); detailedCashflowRangeItem.PresentValue = MoneyHelper.ToDouble(paymentCalculationPeriod.presentValueAmount); detailedCashflowRangeItem.DiscountFactor = (double)paymentCalculationPeriod.discountFactor; detailedCashflowRangeItem.NotionalAmount = (double)PaymentCalculationPeriodHelper.GetNotionalAmount(paymentCalculationPeriod); detailedCashflowRangeItem.CouponType = GetCouponType(paymentCalculationPeriod); detailedCashflowRangeItem.Rate = (double)PaymentCalculationPeriodHelper.GetRate(paymentCalculationPeriod); CalculationPeriod calculationPeriod = PaymentCalculationPeriodHelper.GetCalculationPeriods(paymentCalculationPeriod)[0]; FloatingRateDefinition floatingRateDefinition = XsdClassesFieldResolver.CalculationPeriodGetFloatingRateDefinition(calculationPeriod); switch (detailedCashflowRangeItem.CouponType.ToLower()) { case "cap": { Strike strike = floatingRateDefinition.capRate[0]; detailedCashflowRangeItem.StrikeRate = (double)strike.strikeRate; break; } case "floor": { Strike strike = floatingRateDefinition.floorRate[0]; detailedCashflowRangeItem.StrikeRate = (double)strike.strikeRate; break; } default: { string message = String.Format("Specified coupon type : '{0}' is not supported. Please use one of these: 'cap, floor'", detailedCashflowRangeItem.CouponType.ToLower()); throw new NotSupportedException(message); } } // If floating rate - retrieve the spread. // detailedCashflowRangeItem.Spread = (double)PaymentCalculationPeriodHelper.GetSpread(paymentCalculationPeriod); var fixingDate = new DateTime(); var tempDate = PaymentCalculationPeriodHelper.GetFirstFloatingFixingDate(paymentCalculationPeriod); if (tempDate != null) { fixingDate = (DateTime)tempDate; } detailedCashflowRangeItem.FixingDate = fixingDate; detailedCashflowRangeItem.Currency = "Not Specified"; if (currency != null) { detailedCashflowRangeItem.Currency = currency.Value; } list.Add(detailedCashflowRangeItem); } return(list); }