// helper function used to calculate Time-To-Discount for each stage when calculating discount factor stepwisely public static double getStepwiseDiscountTime(CashFlow cashFlow, DayCounter dc, Date npvDate, Date lastDate) { Date cashFlowDate = cashFlow.date(); Date refStartDate, refEndDate; Coupon coupon = cashFlow as Coupon; if (coupon != null) { refStartDate = coupon.referencePeriodStart; refEndDate = coupon.referencePeriodEnd; } else { if (lastDate == npvDate) { // we don't have a previous coupon date, // so we fake it refStartDate = cashFlowDate - new Period(1, TimeUnit.Years); } else { refStartDate = lastDate; } refEndDate = cashFlowDate; } if (coupon != null && lastDate != coupon.accrualStartDate()) { double couponPeriod = dc.yearFraction(coupon.accrualStartDate(), cashFlowDate, refStartDate, refEndDate); double accruedPeriod = dc.yearFraction(coupon.accrualStartDate(), lastDate, refStartDate, refEndDate); return(couponPeriod - accruedPeriod); } else { return(dc.yearFraction(lastDate, cashFlowDate, refStartDate, refEndDate)); } }
public static Date startDate(Leg leg) { Utils.QL_REQUIRE(!leg.empty(), () => "empty leg"); Date d = Date.maxDate(); for (int i = 0; i < leg.Count; ++i) { Coupon c = leg[i] as Coupon; if (c != null) { d = Date.Min(d, c.accrualStartDate()); } else { d = Date.Min(d, leg[i].date()); } } return(d); }
public static Date accrualStartDate(Leg leg, bool includeSettlementDateFlows, Date settlementDate = null) { CashFlow cf = nextCashFlow(leg, includeSettlementDateFlows, settlementDate); if (cf == null) { return(null); } Date paymentDate = cf.date(); foreach (CashFlow x in leg.Where(x => x.date() == paymentDate)) { Coupon cp = x as Coupon; if (cp != null) { return(cp.accrualStartDate()); } } return(null); }