Skip to content

Commit

Permalink
Merge pull request #112.
Browse files Browse the repository at this point in the history
  • Loading branch information
lballabio committed Sep 13, 2017
2 parents db35c6b + 8126b0a commit f9d8a9b
Show file tree
Hide file tree
Showing 28 changed files with 2,066 additions and 406 deletions.
811 changes: 615 additions & 196 deletions Examples/CDS/CDS.cpp

Large diffs are not rendered by default.

42 changes: 41 additions & 1 deletion QuantLib.dev
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[Project]
FileName=QuantLib.dev
Name=QuantLib
UnitCount=2158
UnitCount=2162
Type=2
Ver=1
ObjFiles=
Expand Down Expand Up @@ -21696,3 +21696,43 @@ Link=1
Priority=1000
OverrideBuildCmd=0
BuildCmd=

[Unit2159]
FileName=ql\instruments\makecds.cpp
CompileCpp=1
Folder=instruments
Compile=1
Link=1
Priority=1000
OverrideBuildCmd=0
BuildCmd=

[Unit2160]
FileName=ql\instruments\makecds.hpp
CompileCpp=1
Folder=instruments
Compile=1
Link=1
Priority=1000
OverrideBuildCmd=0
BuildCmd=

[Unit2161]
FileName=ql\pricingengines\credit\isdacdsengine.hpp
CompileCpp=1
Folder=pricingengines/credit
Compile=1
Link=1
Priority=1000
OverrideBuildCmd=0
BuildCmd=

[Unit2162]
FileName=ql\pricingengines\credit\isdacdsengine.cpp
CompileCpp=1
Folder=pricingengines/credit
Compile=1
Link=1
Priority=1000
OverrideBuildCmd=0
BuildCmd=
6 changes: 5 additions & 1 deletion QuantLib.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,7 @@
<ClInclude Include="ql\instruments\inflationcapfloor.hpp" />
<ClInclude Include="ql\instruments\lookbackoption.hpp" />
<ClInclude Include="ql\instruments\makecapfloor.hpp" />
<ClInclude Include="ql\instruments\makecds.hpp" />
<ClInclude Include="ql\instruments\makecms.hpp" />
<ClInclude Include="ql\instruments\makeois.hpp" />
<ClInclude Include="ql\instruments\makeswaption.hpp" />
Expand Down Expand Up @@ -1391,6 +1392,7 @@
<ClInclude Include="ql\pricingengines\swap\treeswapengine.hpp" />
<ClInclude Include="ql\pricingengines\credit\all.hpp" />
<ClInclude Include="ql\pricingengines\credit\integralcdsengine.hpp" />
<ClInclude Include="ql\pricingengines\credit\isdacdsengine.hpp" />
<ClInclude Include="ql\pricingengines\credit\midpointcdsengine.hpp" />
<ClInclude Include="ql\pricingengines\inflation\all.hpp" />
<ClInclude Include="ql\pricingengines\inflation\inflationcapfloorengines.hpp" />
Expand Down Expand Up @@ -1979,6 +1981,7 @@
<ClCompile Include="ql\instruments\inflationcapfloor.cpp" />
<ClCompile Include="ql\instruments\lookbackoption.cpp" />
<ClCompile Include="ql\instruments\makecapfloor.cpp" />
<ClCompile Include="ql\instruments\makecds.cpp" />
<ClCompile Include="ql\instruments\makecms.cpp" />
<ClCompile Include="ql\instruments\makeois.cpp" />
<ClCompile Include="ql\instruments\makeswaption.cpp" />
Expand Down Expand Up @@ -2373,6 +2376,7 @@
<ClCompile Include="ql\pricingengines\swap\discretizedswap.cpp" />
<ClCompile Include="ql\pricingengines\swap\treeswapengine.cpp" />
<ClCompile Include="ql\pricingengines\credit\integralcdsengine.cpp" />
<ClCompile Include="ql\pricingengines\credit\isdacdsengine.cpp" />
<ClCompile Include="ql\pricingengines\credit\midpointcdsengine.cpp" />
<ClCompile Include="ql\pricingengines\inflation\inflationcapfloorengines.cpp" />
<ClCompile Include="ql\quotes\eurodollarfuturesquote.cpp" />
Expand Down Expand Up @@ -2652,4 +2656,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>
14 changes: 13 additions & 1 deletion QuantLib.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,9 @@
<ClInclude Include="ql\instruments\makecapfloor.hpp">
<Filter>instruments</Filter>
</ClInclude>
<ClInclude Include="ql\instruments\makecds.hpp">
<Filter>instruments</Filter>
</ClInclude>
<ClInclude Include="ql\instruments\makecms.hpp">
<Filter>instruments</Filter>
</ClInclude>
Expand Down Expand Up @@ -2532,6 +2535,9 @@
<ClInclude Include="ql\pricingengines\credit\integralcdsengine.hpp">
<Filter>pricingengines\credit</Filter>
</ClInclude>
<ClInclude Include="ql\pricingengines\credit\isdacdsengine.hpp">
<Filter>pricingengines\credit</Filter>
</ClInclude>
<ClInclude Include="ql\pricingengines\credit\midpointcdsengine.hpp">
<Filter>pricingengines\credit</Filter>
</ClInclude>
Expand Down Expand Up @@ -4401,6 +4407,9 @@
<ClCompile Include="ql\instruments\makecapfloor.cpp">
<Filter>instruments</Filter>
</ClCompile>
<ClCompile Include="ql\instruments\makecds.cpp">
<Filter>instruments</Filter>
</ClCompile>
<ClCompile Include="ql\instruments\makecms.cpp">
<Filter>instruments</Filter>
</ClCompile>
Expand Down Expand Up @@ -5508,6 +5517,9 @@
<ClCompile Include="ql\pricingengines\credit\integralcdsengine.cpp">
<Filter>pricingengines\credit</Filter>
</ClCompile>
<ClCompile Include="ql\pricingengines\credit\isdacdsengine.cpp">
<Filter>pricingengines\credit</Filter>
</ClCompile>
<ClCompile Include="ql\pricingengines\credit\midpointcdsengine.cpp">
<Filter>pricingengines\credit</Filter>
</ClCompile>
Expand Down Expand Up @@ -6717,4 +6729,4 @@
<Filter>time\daycounters</Filter>
</ClCompile>
</ItemGroup>
</Project>
</Project>
16 changes: 16 additions & 0 deletions QuantLib_vc9.vcproj
Original file line number Diff line number Diff line change
Expand Up @@ -2282,6 +2282,14 @@
RelativePath=".\ql\instruments\makecapfloor.hpp"
>
</File>
<File
RelativePath=".\ql\instruments\makecds.cpp"
>
</File>
<File
RelativePath=".\ql\instruments\makecds.hpp"
>
</File>
<File
RelativePath=".\ql\instruments\makecms.cpp"
>
Expand Down Expand Up @@ -6757,6 +6765,14 @@
RelativePath=".\ql\pricingengines\credit\integralcdsengine.hpp"
>
</File>
<File
RelativePath=".\ql\pricingengines\credit\isdacdsengine.cpp"
>
</File>
<File
RelativePath=".\ql\pricingengines\credit\isdacdsengine.hpp"
>
</File>
<File
RelativePath=".\ql\pricingengines\credit\midpointcdsengine.cpp"
>
Expand Down
13 changes: 11 additions & 2 deletions ql/cashflows/fixedratecoupon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ namespace QuantLib {
return *this;
}

FixedRateLeg& FixedRateLeg::withLastPeriodDayCounter(
const DayCounter& dayCounter) {
lastPeriodDC_ = dayCounter;
return *this;
}

FixedRateLeg& FixedRateLeg::withPaymentCalendar(const Calendar& cal) {
paymentCalendar_ = cal;
return *this;
Expand Down Expand Up @@ -247,9 +253,12 @@ namespace QuantLib {
nominal = notionals_[N-2];
else
nominal = notionals_.back();
InterestRate r( rate.rate(), lastPeriodDC_.empty() ?
rate.dayCounter() :
lastPeriodDC_ , rate.compounding(), rate.frequency() );
if (schedule_.isRegular(N-1)) {
leg.push_back(shared_ptr<CashFlow>(new
FixedRateCoupon(paymentDate, nominal, rate,
FixedRateCoupon(paymentDate, nominal, r,
start, end, start, end, exCouponDate)));
} else {
Date ref = schedule_.calendar().advance(
Expand All @@ -258,7 +267,7 @@ namespace QuantLib {
schedule_.businessDayConvention(),
schedule_.endOfMonth());
leg.push_back(shared_ptr<CashFlow>(new
FixedRateCoupon(paymentDate, nominal, rate,
FixedRateCoupon(paymentDate, nominal, r,
start, end, start, ref, exCouponDate)));
}
}
Expand Down
3 changes: 2 additions & 1 deletion ql/cashflows/fixedratecoupon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ namespace QuantLib {
FixedRateLeg& withCouponRates(const std::vector<InterestRate>&);
FixedRateLeg& withPaymentAdjustment(BusinessDayConvention);
FixedRateLeg& withFirstPeriodDayCounter(const DayCounter&);
FixedRateLeg& withLastPeriodDayCounter(const DayCounter&);
FixedRateLeg& withPaymentCalendar(const Calendar&);
FixedRateLeg& withPaymentLag(Natural lag);
FixedRateLeg& withExCouponPeriod(const Period&,
Expand All @@ -110,7 +111,7 @@ namespace QuantLib {
Schedule schedule_;
std::vector<Real> notionals_;
std::vector<InterestRate> couponRates_;
DayCounter firstPeriodDC_;
DayCounter firstPeriodDC_ , lastPeriodDC_;
Calendar paymentCalendar_;
BusinessDayConvention paymentAdjustment_;
Natural paymentLag_;
Expand Down
45 changes: 35 additions & 10 deletions ql/experimental/credit/riskybond.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ using namespace std;

namespace QuantLib {

RiskyBond::RiskyBond(
const std::string& name,
const Currency& ccy,
Real recoveryRate,
const Handle<DefaultProbabilityTermStructure>& defaultTS,
const Handle<YieldTermStructure>& yieldTS,
Natural settlementDays, Calendar calendar)
: name_(name), ccy_(ccy), recoveryRate_(recoveryRate),
defaultTS_(defaultTS), yieldTS_(yieldTS),
settlementDays_(settlementDays), calendar_(calendar) {
registerWith (yieldTS_);
registerWith (defaultTS_);
//the two above might not be registered with evalDate
registerWith(Settings::instance().evaluationDate());
}

bool RiskyBond::isExpired() const {
return detail::simple_event(maturityDate()).hasOccurred();
}
Expand All @@ -41,12 +57,13 @@ namespace QuantLib {
void RiskyBond::performCalculations() const {
NPV_ = 0;
Date today = Settings::instance().evaluationDate();
Date npvDate = calendar_.advance(today, settlementDays_, Days);
std::vector<boost::shared_ptr<CashFlow> > cf = cashflows();
Date d1 = effectiveDate();
for (Size i = 0; i < cf.size(); i++) {
Date d2 = cf[i]->date();
if (d2 > today) {
d1 = max(today , d1);
if (d2 > npvDate) {
d1 = max(npvDate , d1);
Date defaultDate = d1 + (d2-d1)/2;

Real coupon = cf[i]->amount()
Expand All @@ -59,26 +76,29 @@ namespace QuantLib {
}
d1 = d2;
}
valuationDate_ = npvDate;
}

Real RiskyBond::riskfreeNPV() const {
Date today = Settings::instance().evaluationDate();
Date npvDate = calendar_.advance(today, settlementDays_, Days);
Real npv = 0;
std::vector<boost::shared_ptr<CashFlow> > cf = cashflows();
for (Size i = 0; i < cf.size(); i++) {
Date d2 = cf[i]->date();
if (d2 > today)
if (d2 > npvDate)
npv += cf[i]->amount() * yieldTS()->discount(d2);
}
return npv;
}

Real RiskyBond::totalFutureFlows() const {
Date today = Settings::instance().evaluationDate();
Date npvDate = calendar_.advance(today, settlementDays_, Days);
Real flow = 0;
std::vector<boost::shared_ptr<CashFlow> > cf = cashflows();
for (Size i = 0; i < cf.size(); i++) {
if (cf[i]->date() > today)
if (cf[i]->date() > npvDate)
flow += cf[i]->amount();
}
return flow;
Expand All @@ -88,11 +108,12 @@ namespace QuantLib {
std::vector<boost::shared_ptr<CashFlow> > expected;
std::vector<boost::shared_ptr<CashFlow> > cf = cashflows();
Date today = Settings::instance().evaluationDate();
Date npvDate = calendar_.advance(today, settlementDays_, Days);
Date d1 = effectiveDate();
for (Size i = 0; i < cf.size(); i++) {
Date d2 = cf[i]->date();
if (d2 > today) {
d1 = max(today , d1);
if (d2 > npvDate) {
d1 = max(npvDate, d1);
Date defaultDate = d1 + (d2-d1)/2;

Real coupon = cf[i]->amount()
Expand Down Expand Up @@ -124,8 +145,10 @@ namespace QuantLib {
const DayCounter& dayCounter,
BusinessDayConvention paymentConvention,
const std::vector<Real>& notionals,
const Handle<YieldTermStructure>& yieldTS)
: RiskyBond(name, ccy, recoveryRate, defaultTS, yieldTS),
const Handle<YieldTermStructure>& yieldTS,
Natural settlementDays)
: RiskyBond(name, ccy, recoveryRate, defaultTS, yieldTS,
settlementDays, schedule.calendar()),
schedule_(schedule),
rate_(rate),
dayCounter_(dayCounter),
Expand Down Expand Up @@ -201,8 +224,10 @@ namespace QuantLib {
Integer fixingDays,
Real spread,
std::vector<Real> notionals,
Handle<YieldTermStructure> yieldTS)
: RiskyBond(name, ccy, recoveryRate, defaultTS, yieldTS),
Handle<YieldTermStructure> yieldTS,
Natural settlementDays)
: RiskyBond(name, ccy, recoveryRate, defaultTS, yieldTS,
settlementDays, schedule.calendar()),
schedule_(schedule),
index_(index),
fixingDays_(fixingDays),
Expand Down
26 changes: 18 additions & 8 deletions ql/experimental/credit/riskybond.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,27 @@
#include <ql/termstructures/yieldtermstructure.hpp>
#include <ql/experimental/credit/pool.hpp>
#include <ql/termstructures/defaulttermstructure.hpp>
#include <ql/currency.hpp>

namespace QuantLib {

/*! Base class for default risky bonds
\ingroup credit
*/
class RiskyBond : public Instrument {
public:
/*! The value is contingent to survival, i.e., the knockout
probability is considered. To compute the npv given that
the issuer has survived, divide the npv by
\f[(1-P_{def}(T_{npv}))\f]
*/
RiskyBond(const std::string& name,
const Currency& ccy,
Real recoveryRate,
const Handle<DefaultProbabilityTermStructure>& defaultTS,
const Handle<YieldTermStructure>& yieldTS)
: name_(name), ccy_(ccy), recoveryRate_(recoveryRate),
defaultTS_(defaultTS), yieldTS_(yieldTS) {
registerWith (yieldTS_);
registerWith (defaultTS_);
}
const Handle<YieldTermStructure>& yieldTS,
Natural settlementDays = 0,
Calendar calendar = Calendar());
virtual ~RiskyBond() {}
virtual std::vector<boost::shared_ptr<CashFlow> > cashflows() const = 0;
std::vector<boost::shared_ptr<CashFlow> > expectedCashflows();
Expand Down Expand Up @@ -80,6 +84,10 @@ namespace QuantLib {
Real recoveryRate_;
Handle<DefaultProbabilityTermStructure> defaultTS_;
Handle<YieldTermStructure> yieldTS_;
protected:
// engines data
Natural settlementDays_;
Calendar calendar_;
};

inline std::string RiskyBond::name() const {
Expand Down Expand Up @@ -117,7 +125,8 @@ namespace QuantLib {
const DayCounter& dayCounter,
BusinessDayConvention paymentConvention,
const std::vector<Real>& notionals,
const Handle<YieldTermStructure>& yieldTS);
const Handle<YieldTermStructure>& yieldTS,
Natural settlementDays = 0);
std::vector<boost::shared_ptr<CashFlow> > cashflows() const;
Real notional(Date date = Date::minDate()) const;
Date effectiveDate() const;
Expand Down Expand Up @@ -150,7 +159,8 @@ namespace QuantLib {
Integer fixingDays,
Real spread,
std::vector<Real> notionals,
Handle<YieldTermStructure> yieldTS);
Handle<YieldTermStructure> yieldTS,
Natural settlementDays = 0);
std::vector<boost::shared_ptr<CashFlow> > cashflows() const;
Real notional(Date date = Date::minDate()) const;
Date effectiveDate() const;
Expand Down
Loading

0 comments on commit f9d8a9b

Please sign in to comment.