21#include <ql/cashflows/simplecashflow.hpp>
31 if(bond.first == name){
32 const QuantLib::Leg& leg = bond.second->cashflows();
34 for(
size_t d = 1; d < leg.size(); d++){
35 if(leg[d-1]->date() < currentDate && leg[d]->date() >= currentDate)
52 std::string name = bond.first;
55 Leg leg = bond.second->cashflows();
57 std::vector<ext::shared_ptr<QuantLib::CashFlow> > cashflows;
58 std::vector<ext::shared_ptr<QuantLib::CashFlow> > interestFlows;
59 std::vector<ext::shared_ptr<QuantLib::CashFlow> > notionalFlows;
60 std::vector<ext::shared_ptr<QuantLib::CashFlow> > feeFlows;
62 for (
size_t j = 0; j < leg.size(); j++) {
64 QuantLib::ext::shared_ptr<QuantLib::CashFlow> ptrFlow = leg[j];
68 QuantLib::ext::shared_ptr<QuantLib::Coupon> ptrCoupon =
69 QuantLib::ext::dynamic_pointer_cast<QuantLib::Coupon>(ptrFlow);
70 QL_REQUIRE(ptrCoupon,
"expected couopn type");
72 QuantLib::ext::shared_ptr<QuantExt::ScaledCoupon> newPtr(
new QuantExt::ScaledCoupon(multi * scalar[j], ptrCoupon ));
73 interestFlows.push_back(newPtr);
74 cashflows.push_back(newPtr);
76 }
else if(
flowType(name,j) ==
"xnl"){
82 ext::shared_ptr<CashFlow>
83 flow(
new SimpleCashFlow(ptrFlow->amount() * multi * scalar[j] * amort, ptrFlow->date()));
84 notionalFlows.push_back(flow);
85 cashflows.push_back(flow);
87 }
else if (
flowType(name,j) ==
"fee"){
89 ext::shared_ptr<CashFlow>
90 flow(
new SimpleCashFlow(ptrFlow->amount() * multi * scalar[j], ptrFlow->date()));
91 feeFlows.push_back(flow);
92 cashflows.push_back(flow);
118 date = Settings::instance().evaluationDate();
123 std::string code = ccy.code();
124 double fxRate =
fxIndex(code)->fixing(date,
false);
125 double inBase = fxRate * amount;
131 const std::map<std::string, double>& recoveries,
132 const std::map<std::string, double>& multipliers,
133 const std::map<std::string, QuantLib::Handle<QuantLib::YieldTermStructure>>& yieldTermStructures,
134 const std::map<std::string, Currency>& currencies,
135 const QuantLib::ext::shared_ptr<Pool> pool,
137 const std::map <
string, QuantLib::ext::shared_ptr<QuantExt::FxIndex>>& fxIndexMap,
138 const QuantLib::Date & reinvestmentEndDate,
139 const std::map<std::string, std::vector<double>>& reinvestmentScalar,
140 const std::map<std::string, std::vector<std::string>>& flowType)
142 recoveries_(recoveries),
143 multipliers_(multipliers),
144 yieldTermStructures_(yieldTermStructures),
145 currencies_(currencies),
148 fxIndexMap_(fxIndexMap),
149 reinvestmentEndDate_(reinvestmentEndDate),
150 reinvestmentScalar_(reinvestmentScalar),
154 QL_REQUIRE(!
qlBonds_.empty(),
"no bonds given");
163 grid_ = vector<Date>();
175 QL_FAIL(
"no recovery value for bond " << name);
182 QL_FAIL(
"no multiplier for bond " << name);
185 const QuantLib::Handle<QuantLib::YieldTermStructure>
BondBasket::yts(
const std::string& name)
const{
189 QL_FAIL(
"no yieldTermStructure for bond " << name);
196 QL_FAIL(
"no currency for bond " << name);
203 QL_FAIL(
"no reinvestmentScalar for bond " << name);
209 return it->second[idx];
210 QL_FAIL(
"no flowType for bond " << name);
217 QL_FAIL(
"no fxIndex for bond " << name);
223 string name = bond.first;
225 for (Size j = 0; j <
cashflows_[name].size(); j++) {
227 for (Size k = 1; k < dates.size(); k++) {
228 if (d > dates[k-1] && d <= dates[k]) {
237 for (Size k = 1; k < dates.size(); k++) {
238 if (d > dates[k-1] && d <= dates[k]) {
247 for (Size k = 1; k < dates.size(); k++) {
248 if (d > dates[k-1] && d <= dates[k]) {
255 for (Size j = 0; j <
feeFlows_[name].size(); j++) {
257 for (Size k = 1; k < dates.size(); k++) {
258 if (d > dates[k-1] && d <= dates[k]) {
267 std::map<Currency, vector<Cash> >
269 QL_REQUIRE(
grid_.size() > 0,
"grid not set");
270 Date today = Settings::instance().evaluationDate();
271 map<Currency,vector<Cash> > cf;
273 cf[ccy].resize(dates.size(),
Cash());
274 for (
const auto & bond :
qlBonds_) {
275 string name = bond.first;
276 DayCounter dc =
yts(name)->dayCounter();
278 Real defaultTime =
pool_->getTime(name);
279 Real maturityTime = dc.yearFraction(today, bond.second->maturityDate());
280 for (Size j = 0; j <
cashflows_[name].size(); j++) {
282 Real t = dc.yearFraction(today, d);
284 if (t < defaultTime) {
287 if (k <
grid_.size()) {
288 cf[ccy][k].flow_ +=
cashflows_[name][j]->amount();
289 cf[ccy][k].discountedFlow_
291 *
yts(name)->discount(t);
296 if (defaultTime < maturityTime) {
297 for (Size k = 1; k < dates.size(); k++) {
298 Real t1 = dc.yearFraction(today, dates[k-1]);
299 Real t2 = dc.yearFraction(today, dates[k]);
300 if (defaultTime >= t1 && defaultTime < t2) {
303 cf[ccy][k].discountedFlow_
305 *
yts(name)->discount(defaultTime);
313 std::map<Currency, vector<Cash> >
315 QL_REQUIRE(
grid_.size() > 0,
"grid not set");
316 Date today = Settings::instance().evaluationDate();
317 map<Currency,vector<Cash> > cf;
319 cf[ccy].resize(dates.size(),
Cash());
320 for (
const auto & bond :
qlBonds_) {
321 string name = bond.first;
322 DayCounter dc =
yts(name)->dayCounter();
324 Real defaultTime =
pool_->getTime(name);
327 Real t = dc.yearFraction(today, d);
328 if (t < defaultTime) {
331 if (k <
grid_.size()) {
333 cf[ccy][k].discountedFlow_
335 *
yts(name)->discount(t);
344 std::map<Currency, vector<Cash> >
346 QL_REQUIRE(
grid_.size() > 0,
"grid not set");
347 Date today = Settings::instance().evaluationDate();
348 map<Currency,vector<Cash> > cf;
350 cf[ccy].resize(dates.size(),
Cash());
351 for (
const auto & bond :
qlBonds_) {
352 string name = bond.first;
353 DayCounter dc =
yts(name)->dayCounter();
355 Real defaultTime =
pool_->getTime(name);
356 Real maturityTime = dc.yearFraction(today, bond.second->maturityDate());
359 Real t = dc.yearFraction(today, d);
360 if (t < defaultTime) {
363 if (k <
grid_.size()) {
365 cf[ccy][k].discountedFlow_
367 *
yts(name)->discount(t);
372 if (defaultTime < maturityTime) {
373 for (Size k = 1; k < dates.size(); k++) {
374 Real t1 = dc.yearFraction(today, dates[k-1]);
375 Real t2 = dc.yearFraction(today, dates[k]);
376 if (defaultTime >= t1 &&
380 cf[ccy][k].discountedFlow_
382 *
yts(name)->discount(defaultTime);
391 QL_REQUIRE(
grid_.size() > 0,
"grid not set");
392 Date today = Settings::instance().evaluationDate();
393 map<Currency,vector<Cash> > cf;
395 cf[ccy].resize(dates.size(),
Cash());
396 for (
const auto & bond :
qlBonds_) {
397 string name = bond.first;
398 DayCounter dc =
yts(name)->dayCounter();
400 Real defaultTime =
pool_->getTime(name);
401 for (Size j = 0; j <
feeFlows_[name].size(); j++) {
403 Real t = dc.yearFraction(today, d);
404 if (t < defaultTime) {
406 if (k < dates.size()) {
407 cf[ccy][k].flow_ +=
feeFlows_[name][j]->amount();
408 cf[ccy][k].discountedFlow_
409 +=
feeFlows_[name][j]->amount() *
yts(name)->discount(t);
419 std::map<Currency, vector<Cash> >
421 QL_REQUIRE(
grid_.size() > 0,
"grid not set");
422 Date today = Settings::instance().evaluationDate();
423 map<Currency,vector<Cash> > cf;
425 cf[ccy].resize(dates.size(),
Cash());
426 for (
const auto & bond :
qlBonds_) {
427 string name = bond.first;
428 DayCounter dc =
yts(name)->dayCounter();
430 Real defaultTime =
pool_->getTime(name);
431 Real maturityTime = dc.yearFraction(today, bond.second->maturityDate());
432 if (defaultTime < maturityTime) {
433 for (Size k = 1; k < dates.size(); k++) {
434 Real t1 = dc.yearFraction(today, dates[k-1]);
435 Real t2 = dc.yearFraction(today, dates[k]);
436 if (defaultTime >= t1 &&
440 cf[ccy][k].discountedFlow_
442 *
yts(name)->discount(defaultTime);
450 std::map<Currency, vector<Real> >
452 QL_REQUIRE(
grid_.size() > 0,
"grid not set");
453 Date today = Settings::instance().evaluationDate();
454 map<Currency,vector<Real> > cf;
456 cf[ccy].resize(dates.size(), 0.0);
457 for (
const auto & bond :
qlBonds_) {
458 string name = bond.first;
459 DayCounter dc =
yts(name)->dayCounter();
461 Real defaultTime =
pool_->getTime(name);
462 for (Size k = 1; k < dates.size(); k++) {
463 Real t1 = dc.yearFraction(today, dates[k]);
464 if (defaultTime >= t1) {
465 cf[ccy][k] += bond.second->notional(dates[k]) *
multiplier(name) *
getScalar(name, dates[k]);
Basket of defaultable bonds.
const QuantLib::ext::shared_ptr< QuantLib::Pool > pool_
const QuantLib::ext::shared_ptr< QuantExt::FxIndex > fxIndex(const std::string &name) const
std::map< std::string, std::vector< int > > interestflow2grid_
const Date reinvestmentEndDate_
std::map< std::string, std::vector< ext::shared_ptr< QuantLib::CashFlow > > > feeFlows_
std::map< Currency, std::vector< Cash > > scenarioLossflow(std::vector< Date > dates)
std::vector< Date > grid_
const std::map< std::string, std::vector< double > > reinvestmentScalar_
std::map< Currency, std::vector< Cash > > scenarioCashflow(std::vector< Date > dates)
const double multiplier(const std::string &name) const
std::map< Currency, std::vector< Cash > > scenarioInterestflow(std::vector< Date > dates)
const Currency currency(const std::string &name) const
const std::map< std::string, double > recoveries_
Real convert(Real amount, Currency ccy, Date date=Date())
std::map< std::string, std::vector< ext::shared_ptr< QuantLib::CashFlow > > > notionalFlows_
void setGrid(std::vector< Date > dates)
std::map< std::string, std::vector< int > > cashflow2grid_
const double recoveryRate(const std::string &name) const
std::map< Currency, std::vector< Real > > scenarioRemainingNotional(std::vector< Date > dates)
const std::vector< double > reinvestmentScalar(const std::string &name) const
const double getScalar(const std::string &name, const QuantLib::Date ¤tDate) const
std::map< std::string, std::vector< ext::shared_ptr< QuantLib::CashFlow > > > interestFlows_
const std::map< std::string, QuantLib::ext::shared_ptr< QuantExt::FxIndex > > fxIndexMap_
const std::map< std::string, QuantLib::ext::shared_ptr< QuantLib::Bond > > qlBonds_
const QuantLib::Handle< QuantLib::YieldTermStructure > yts(const std::string &name) const
std::set< QuantLib::Currency > unique_currencies_
std::map< Currency, std::vector< Cash > > scenarioPrincipalflow(std::vector< Date > dates)
const std::map< std::string, QuantLib::Currency > currencies_
std::map< Currency, std::vector< Cash > > scenarioFeeflow(const std::vector< QuantLib::Date > &dates)
std::map< std::string, std::vector< int > > notionalflow2grid_
const std::map< std::string, std::vector< std::string > > flowType_
std::map< std::string, std::vector< int > > feeflow2grid_
const std::string flowType(const std::string &name, int idx) const
const QuantLib::ext::shared_ptr< QuantLib::Pool > & pool() const
std::map< std::string, std::vector< ext::shared_ptr< QuantLib::CashFlow > > > cashflows_
const std::map< std::string, QuantLib::Handle< QuantLib::YieldTermStructure > > yieldTermStructures_
BondBasket(const std::map< std::string, QuantLib::ext::shared_ptr< QuantLib::Bond > > &qlBonds, const std::map< std::string, double > &recoveries, const std::map< std::string, double > &multipliers, const std::map< std::string, QuantLib::Handle< QuantLib::YieldTermStructure > > &yieldTermStructures, const std::map< std::string, Currency > ¤cies, const QuantLib::ext::shared_ptr< QuantLib::Pool > pool, Currency baseCcy, const std::map< std::string, QuantLib::ext::shared_ptr< QuantExt::FxIndex > > &fxIndexMap, const QuantLib::Date &reinvestmentEndDate, const std::map< std::string, std::vector< double > > &reinvestmentScalar, const std::map< std::string, std::vector< std::string > > &flowType)
const std::map< std::string, double > multipliers_
Real sumDiscounted(const Cash &c, const Cash &d)
Real sum(const Cash &c, const Cash &d)
Coupon / Cashflow paying scaled amounts.