protected void calculateNotionalsFromCashflows()
{
notionalSchedule_.Clear();
notionals_.Clear();
Date lastPaymentDate = new Date();
//notionalSchedule_.Add((Coupon)(cashflows_[0])accrualStartDate());
for (int i = 0; i < cashflows_.Count; ++i)
{
Coupon coupon = cashflows_[i] as Coupon;
if (coupon == null)
{
continue;
}
if (i == 0)
{
notionalSchedule_.Add(coupon.accrualStartDate());
}
double notional = coupon.nominal();
// we add the notional only if it is the first one...
if (notionals_.empty())
{
notionals_.Add(coupon.nominal());
lastPaymentDate = coupon.date();
}
else if (!Utils.close(notional, notionals_.Last()))
{
// ...or if it has changed.
if (!(notional < notionals_.Last()))
{
throw new ApplicationException("increasing coupon notionals");
}
notionals_.Add(coupon.nominal());
// in this case, we also add the last valid date for
// the previous one...
notionalSchedule_.Add(lastPaymentDate);
// ...and store the candidate for this one.
lastPaymentDate = coupon.date();
}
else
{
// otherwise, we just extend the valid range of dates
// for the current notional.
lastPaymentDate = coupon.date();
}
}
if (notionals_.empty())
{
throw new ApplicationException("no coupons provided");
}
notionals_.Add(0.0);
notionalSchedule_.Add(lastPaymentDate);
}