public static Date maturityDate(Leg leg) { Utils.QL_REQUIRE(!leg.empty(), () => "empty leg"); Date d = Date.minDate(); for (int i = 0; i < leg.Count; ++i) { Coupon c = leg[i] as Coupon; if (c != null) { d = Date.Max(d, c.accrualEndDate()); } else { d = Date.Max(d, leg[i].date()); } } return(d); }
public static Date accrualEndDate(List <CashFlow> 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.accrualEndDate()); } } return(null); }
public static double npv(List <CashFlow> leg, InterestRate y, bool includeSettlementDateFlows, Date settlementDate = null, Date npvDate = null) { if (leg == null || leg.empty()) { return(0.0); } if (settlementDate == null) { settlementDate = Settings.evaluationDate(); } if (npvDate == null) { npvDate = settlementDate; } double npv = 0.0; double discount = 1.0; Date lastDate = npvDate; Date refStartDate, refEndDate; for (int i = 0; i < leg.Count; ++i) { if (leg[i].hasOccurred(settlementDate, includeSettlementDateFlows)) { continue; } Date couponDate = leg[i].date(); double amount = leg[i].amount(); Coupon coupon = leg[i] as Coupon; if (coupon != null) { refStartDate = coupon.accrualStartDate(); refEndDate = coupon.accrualEndDate(); } else { if (lastDate == npvDate) { // we don't have a previous coupon date, // so we fake it refStartDate = couponDate - new Period(1, TimeUnit.Years); } else { refStartDate = lastDate; } refEndDate = couponDate; } double b = y.discountFactor(lastDate, couponDate, refStartDate, refEndDate); discount *= b; lastDate = couponDate; npv += amount * discount; } return(npv); }