private static double couponRate(List <CashFlow> leg, CashFlow cf) { if (cf == null) { return(0.0); } Date paymentDate = cf.date(); bool firstCouponFound = false; double nominal = 0; double accrualPeriod = 0; DayCounter dc = null; double result = 0.0; foreach (CashFlow x in leg.Where(x => x.date() == paymentDate)) { Coupon cp = x as Coupon; if (cp != null) { if (firstCouponFound) { if (!(nominal == cp.nominal() && accrualPeriod == cp.accrualPeriod() && dc == cp.dayCounter())) { throw new ApplicationException("cannot aggregate two different coupons on " + paymentDate); } } else { firstCouponFound = true; nominal = cp.nominal(); accrualPeriod = cp.accrualPeriod(); dc = cp.dayCounter(); } result += cp.rate(); } } if (!firstCouponFound) { throw new ApplicationException("next cashflow (" + paymentDate + ") is not a coupon"); } return(result); }
private static double aggregateRate(Leg leg, CashFlow cf) { if (cf == null) { return(0.0); } Date paymentDate = cf.date(); bool firstCouponFound = false; double nominal = 0.0; double accrualPeriod = 0.0; DayCounter dc = null; double result = 0.0; foreach (CashFlow x in leg.Where(x => x.date() == paymentDate)) { Coupon cp = x as Coupon; if (cp != null) { if (firstCouponFound) { Utils.QL_REQUIRE(nominal.IsEqual(cp.nominal()) && accrualPeriod.IsEqual(cp.accrualPeriod()) && dc == cp.dayCounter(), () => "cannot aggregate two different coupons on " + paymentDate); } else { firstCouponFound = true; nominal = cp.nominal(); accrualPeriod = cp.accrualPeriod(); dc = cp.dayCounter(); } result += cp.rate(); } } Utils.QL_REQUIRE(firstCouponFound, () => "no coupon paid at cashflow date " + paymentDate); return(result); }