Skip to content

Commit

Permalink
Merge remote-tracking branch 'master/Ver-1.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
amaggiulli committed Sep 22, 2014
2 parents 1775174 + a78c9ac commit 4224622
Show file tree
Hide file tree
Showing 69 changed files with 6,575 additions and 1,739 deletions.
2 changes: 1 addition & 1 deletion Examples/BermudanSwaption/BermudanSwaption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ static void Main(string[] args)
indexSixMonths.tenor(),
indexSixMonths.dayCounter(),
indexSixMonths.dayCounter(),
rhTermStructure, false));
rhTermStructure));
swaptions.Last().addTimesTo(times);
}

Expand Down
5 changes: 2 additions & 3 deletions Examples/Bonds/Bonds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,6 @@ static void Main(string[] args) {

Console.WriteLine("{0,18}{1,10}{2,10}{3,10}", "", "ZC", "Fixed", "Floating");

string separator = " | ";
int width = widths[0]
+ widths[1]
+ widths[2]
Expand Down Expand Up @@ -475,12 +474,12 @@ static void Main(string[] args) {

Console.WriteLine("Previous coupon".PadLeft(widths[0]) + "{0,10:0.00%}{1,10:0.00%}{2,10:0.00%}",
"N/A",
fixedRateBond.previousCouponRate(),
fixedRateBond.previousCouponRate(),
floatingRateBond.previousCouponRate());

Console.WriteLine("Next coupon".PadLeft(widths[0]) + "{0,10:0.00%}{1,10:0.00%}{2,10:0.00%}",
"N/A",
fixedRateBond.nextCouponRate(),
fixedRateBond.nextCouponRate(),
floatingRateBond.nextCouponRate());

Console.WriteLine("Yield".PadLeft(widths[0]) + "{0,10:0.00%}{1,10:0.00%}{2,10:0.00%}",
Expand Down
Binary file modified QLNet/Cashflow.cs
Binary file not shown.
31 changes: 21 additions & 10 deletions QLNet/Cashflows/CPICoupon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,10 @@ public CPICoupon(double baseCPI, // user provided, could be arbitrary
double fixedRate, // aka gearing
double spread = 0.0,
Date refPeriodStart = null,
Date refPeriodEnd = null)
:base(paymentDate, nominal, startDate, endDate, fixingDays, index, observationLag, dayCounter, refPeriodStart, refPeriodEnd)
Date refPeriodEnd = null,
Date exCouponDate = null)
:base(paymentDate, nominal, startDate, endDate, fixingDays, index,
observationLag, dayCounter, refPeriodStart, refPeriodEnd, exCouponDate)
{

baseCPI_ = baseCPI;
Expand Down Expand Up @@ -253,7 +255,8 @@ public CPILeg(Schedule schedule,
baseCPI_ = baseCPI;
observationLag_ = observationLag;
paymentDayCounter_ = new Thirty360();
paymentAdjustment_ = BusinessDayConvention.ModifiedFollowing;
paymentAdjustment_ = BusinessDayConvention.ModifiedFollowing;
paymentCalendar_ = schedule.calendar();
fixingDays_ = new List<int>() { 0 };
observationInterpolation_ = InterpolationType.AsIndex;
subtractInflationNominal_ = true;
Expand All @@ -265,8 +268,7 @@ public override List<CashFlow> value()
if (notionals_.empty())
throw new ApplicationException("no notional given");

int n = schedule_.Count;
Calendar calendar = schedule_.calendar();
int n = schedule_.Count - 1;
List<CashFlow> leg = new List<CashFlow>(n + 1);

if (n > 0)
Expand All @@ -275,13 +277,22 @@ public override List<CashFlow> value()
throw new ApplicationException("no fixedRates or spreads given");

Date refStart, start, refEnd, end;
Date lastPaymentDate = calendar.adjust(schedule_.date(n), paymentAdjustment_);

for (int i = 0; i < n; ++i)
{
refStart = start = schedule_.date(i);
refEnd = end = schedule_.date(i + 1);
Date paymentDate = calendar.adjust(end, paymentAdjustment_);
Date paymentDate = paymentCalendar_.adjust(end, paymentAdjustment_);

Date exCouponDate = null;
if (exCouponPeriod_ != null)
{
exCouponDate = exCouponCalendar_.advance(paymentDate,
-exCouponPeriod_,
exCouponAdjustment_,
exCouponEndOfMonth_);
}

if (i == 0 && !schedule_.isRegular(i + 1))
{
BusinessDayConvention bdc = schedule_.businessDayConvention();
Expand All @@ -298,7 +309,7 @@ public override List<CashFlow> value()
leg.Add(new FixedRateCoupon(Utils.Get(notionals_, i, 0.0),
paymentDate,
Utils.effectiveFixedRate(spreads_, caps_, floors_, i),
paymentDayCounter_, start, end, refStart, refEnd));
paymentDayCounter_, start, end, refStart, refEnd, exCouponDate));
}
else
{
Expand All @@ -318,7 +329,7 @@ public override List<CashFlow> value()
paymentDayCounter_,
Utils.Get(fixedRates_, i, 0.0),
Utils.Get(spreads_, i, 0.0),
refStart, refEnd);
refStart, refEnd, exCouponDate);

// in this case you can set a pricer
// straight away because it only provides computation - not data
Expand All @@ -337,7 +348,7 @@ public override List<CashFlow> value()
}

// in CPI legs you always have a notional flow of some sort
Date pDate = calendar.adjust(schedule_.date(n), paymentAdjustment_);
Date pDate = paymentCalendar_.adjust(schedule_.date(n), paymentAdjustment_);
Date fixingDate = pDate - observationLag_;
CashFlow xnl = new CPICashFlow
(Utils.Get(notionals_, n, 0.0), index_,
Expand Down
56 changes: 41 additions & 15 deletions QLNet/Cashflows/CashFlows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ public static double simpleDuration(Leg leg,InterestRate y, bool includeSettleme
if (leg[i].hasOccurred(settlementDate, includeSettlementDateFlows))
continue;

double c = leg[i].amount();
double c = leg[i].amount();
if (leg[i].tradingExCoupon(settlementDate))
{
c = 0.0;
}
Date couponDate = leg[i].date();
Coupon coupon = leg[i] as Coupon;
if (coupon != null)
Expand Down Expand Up @@ -157,7 +161,11 @@ public static double modifiedDuration(Leg leg,InterestRate y, bool includeSettle
if (leg[i].hasOccurred(settlementDate, includeSettlementDateFlows))
continue;

double c = leg[i].amount();
double c = leg[i].amount();
if (leg[i].tradingExCoupon(settlementDate))
{
c = 0.0;
}
Date couponDate = leg[i].date();
Coupon coupon = leg[i] as Coupon;
if (coupon != null)
Expand Down Expand Up @@ -278,8 +286,9 @@ private void checkSign()

int lastSign = Math.Sign(-npv_), signChanges = 0;
for (int i = 0; i < leg_.Count; ++i)
{
if (!leg_[i].hasOccurred(settlementDate_,includeSettlementDateFlows_))
{
if (!leg_[i].hasOccurred(settlementDate_, includeSettlementDateFlows_) &&
!leg_[i].tradingExCoupon(settlementDate_))
{
int thisSign = Math.Sign(leg_[i].amount());
if (lastSign * thisSign < 0) // sign change
Expand Down Expand Up @@ -610,7 +619,10 @@ public static double accruedPeriod(Leg leg, bool includeSettlementDateFlows, Dat
return 0;
}
public static int accruedDays(Leg leg, bool includeSettlementDateFlows,Date settlementDate = null)
{
{
if ( settlementDate == null )
settlementDate = Settings.evaluationDate();

CashFlow cf = nextCashFlow(leg, includeSettlementDateFlows, settlementDate);
if (cf == null) return 0;
Date paymentDate = cf.date();
Expand All @@ -624,7 +636,10 @@ public static int accruedDays(Leg leg, bool includeSettlementDateFlows,Date sett
return 0;
}
public static double accruedAmount(Leg leg, bool includeSettlementDateFlows,Date settlementDate = null)
{
{
if ( settlementDate == null )
settlementDate = Settings.evaluationDate();

CashFlow cf = nextCashFlow(leg, includeSettlementDateFlows, settlementDate);
if (cf == null) return 0;
Date paymentDate = cf.date();
Expand Down Expand Up @@ -675,7 +690,7 @@ public static double npv(Leg leg,YieldTermStructure discountCurve, bool includeS
double totalNPV = 0.0;
for (int i=0; i<leg.Count; ++i)
{
if (!leg[i].hasOccurred(settlementDate, includeSettlementDateFlows))
if (!leg[i].hasOccurred(settlementDate, includeSettlementDateFlows) && !leg[i].tradingExCoupon(settlementDate))
totalNPV += leg[i].amount() * discountCurve.discount(leg[i].date());
}

Expand All @@ -698,8 +713,9 @@ public static double bps(Leg leg, YieldTermStructure discountCurve, bool include

BPSCalculator calc = new BPSCalculator(discountCurve);
for (int i = 0; i < leg.Count; ++i)
{
if (!leg[i].hasOccurred(settlementDate, includeSettlementDateFlows))
{
if (!leg[i].hasOccurred(settlementDate, includeSettlementDateFlows) &&
!leg[i].tradingExCoupon(settlementDate))
leg[i].accept(calc);
}
return basisPoint_ * calc.bps() / discountCurve.discount(npvDate);
Expand All @@ -719,8 +735,9 @@ public static void npvbps(Leg leg,YieldTermStructure discountCurve, bool include
BPSCalculator calc = new BPSCalculator(discountCurve);
for (int i=0; i<leg.Count; ++i)
{
CashFlow cf = leg[i];
if (!cf.hasOccurred(settlementDate, includeSettlementDateFlows))
CashFlow cf = leg[i];
if (!cf.hasOccurred(settlementDate, includeSettlementDateFlows) &&
!cf.tradingExCoupon(settlementDate))
{
npv += cf.amount() * discountCurve.discount(cf.date());
cf.accept(calc);
Expand Down Expand Up @@ -748,8 +765,9 @@ public static double atmRate(Leg leg,YieldTermStructure discountCurve, bool incl
BPSCalculator calc = new BPSCalculator(discountCurve);
for (int i=0; i<leg.Count; ++i)
{
CashFlow cf = leg[i];
if (!cf.hasOccurred(settlementDate, includeSettlementDateFlows))
CashFlow cf = leg[i];
if (!cf.hasOccurred(settlementDate, includeSettlementDateFlows) &&
!cf.tradingExCoupon(settlementDate))
{
npv += cf.amount() * discountCurve.discount(cf.date());
cf.accept(calc);
Expand Down Expand Up @@ -801,7 +819,11 @@ public static double npv(Leg leg, InterestRate yield, bool includeSettlementDate
continue;

Date couponDate = leg[i].date();
double amount = leg[i].amount();
double amount = leg[i].amount();
if (leg[i].tradingExCoupon(settlementDate))
{
amount = 0.0;
}
Coupon coupon = leg[i] as Coupon;
if (coupon != null )
{
Expand Down Expand Up @@ -1017,7 +1039,11 @@ public static double convexity(Leg leg, InterestRate yield, bool includeSettleme
if (leg[i].hasOccurred(settlementDate,includeSettlementDateFlows))
continue;

double c = leg[i].amount();
double c = leg[i].amount();
if (leg[i].tradingExCoupon(settlementDate))
{
c = 0.0;
}
Date couponDate = leg[i].date();
Coupon coupon = leg[i] as Coupon;
if (coupon != null )
Expand Down
26 changes: 13 additions & 13 deletions QLNet/Cashflows/Cashflowvectors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,27 +268,27 @@ public static List<CashFlow> yoyInflationLeg(List<double> notionals_,
List<int> fixingDays_,
Period observationLag_)
{
int n = schedule_.Count;
int n = schedule_.Count -1 ;

if (notionals_.empty())
throw new ApplicationException("no notional given");

if (notionals_.Count > n)
throw new ApplicationException("too many nominals (" + notionals_.Count +
"), only " + n + " required");
if (gearings_.Count > n)
"), only " + n + " required");
if ( gearings_ != null && gearings_.Count > n )
throw new ApplicationException("too many gearings (" + gearings_.Count +
"), only " + n + " required");

if (spreads_.Count > n)
"), only " + n + " required");

if ( spreads_ != null && spreads_.Count > n )
throw new ApplicationException("too many spreads (" + spreads_.Count +
"), only " + n + " required");

if (caps_.Count > n)
"), only " + n + " required");

if ( caps_ != null && caps_.Count > n )
throw new ApplicationException("too many caps (" + caps_.Count +
"), only " + n + " required");

if (floors_.Count > n)
"), only " + n + " required");

if ( floors_ != null && floors_.Count > n )
throw new ApplicationException("too many floors (" + floors_.Count +
"), only " + n + " required");

Expand All @@ -298,7 +298,7 @@ public static List<CashFlow> yoyInflationLeg(List<double> notionals_,
Calendar calendar = paymentCalendar_;

Date refStart, start, refEnd, end;
Date lastPaymentDate = calendar.adjust(schedule_.date(n), paymentAdjustment_);
//Date lastPaymentDate = calendar.adjust(schedule_.date(n), paymentAdjustment_);

for (int i = 0; i < n; ++i)
{
Expand Down
Binary file modified QLNet/Cashflows/Coupon.cs
Binary file not shown.
Loading

0 comments on commit 4224622

Please sign in to comment.