58 {
59 DLOG(
"BondBasket::build() called");
60
61 QuantLib::ext::shared_ptr<Pool>
pool(
new Pool());
62 vector<Issuer> issuerPair;
63 set<Currency> currencies_unique;
64
65 map<string, QuantLib::ext::shared_ptr<QuantLib::Bond>> qlBonds;
66 map<string, double> recoveries;
67 map<string, double> multipliers;
68 map<string, Handle<YieldTermStructure>> yieldTermStructures;
69 map<string, Currency> currencies;
70
71 const QuantLib::ext::shared_ptr<Market> market = engineFactory->market();
72
73 for (
size_t i = 0; i <
bonds_.size(); i++) {
74 DLOG(
"BondBasket::build() -- processing issuer number " << i);
75
76 bonds_[i]->build(engineFactory);
77 string creditId =
bonds_[i]->bondData().creditCurveId();
78 string securityId =
bonds_[i]->bondData().securityId();
79
80 Handle<DefaultProbabilityTermStructure> defaultOriginal =
82 Handle<DefaultProbabilityTermStructure> defaultTS;
83 Handle<Quote> recovery;
84 try {
85 recovery = market->recoveryRate(securityId, Market::defaultConfiguration);
86 } catch (...) {
87 }
88
89 try {
90
91 Real rr = recovery.empty() ? 0.0 : recovery->value();
92 auto m = [rr](Real x) { return x / (1.0 - rr); };
93 Handle<Quote> scaledSpread(QuantLib::ext::make_shared<DerivedQuote<decltype(m)>>(
94 market->securitySpread(securityId, Market::defaultConfiguration), m));
95
96 defaultTS = Handle<DefaultProbabilityTermStructure>(
97 QuantLib::ext::make_shared<QuantExt::HazardSpreadedDefaultTermStructure>(defaultOriginal, scaledSpread));
98
99 bonds_[i]->instrument()->qlInstrument()->registerWith(scaledSpread);
100 recoveries[
bonds_[i]->id()] = rr;
101
102 } catch (...) {
103 defaultTS = defaultOriginal;
104 recoveries[
bonds_[i]->id()] = 0.0;
105 }
106
107
108 issuerPair.push_back(dummyIssuer(defaultTS));
109 pool->add(bonds_[i]->
id(), issuerPair[i]);
112
113 QuantLib::ext::shared_ptr<QuantLib::Bond> qlBond = QuantLib::ext::dynamic_pointer_cast<QuantLib::Bond>(bonds_[i]->instrument()->qlInstrument());
114 QL_REQUIRE(qlBond, "ql bond expected " << bonds_[i]->id());
115 qlBonds[
bonds_[i]->id()]= qlBond;
116
117 multipliers[bonds_[i]->id()] = bonds_[i]->instrument()->multiplier();
118 yieldTermStructures[bonds_[i]->id()] = market->discountCurve(bonds_[i]->bondData().
currency());
120
121 }
122
124 for (
size_t i = 0; i <
pool->names().
size(); i++) {
125 DLOG(
"name: " <<
pool->names()[i] <<
", included: " <<
pool->has(bonds_[i]->id()));
126 }
127
128
129 for (auto it = currencies_unique.begin(); it!=currencies_unique.end(); ++it){
130 if(it->code() != baseCcy.code()){
131 string source = it->code();
132 string target = baseCcy.code();
133 Handle<YieldTermStructure> sorTS = market->discountCurve(source);
134 Handle<YieldTermStructure> tarTS = market->discountCurve(target);
135 Handle<Quote> spot = market->fxSpot(source + target);
136 Calendar cal = NullCalendar();
137 Natural fixingDays = 0;
138
139 string name = source + target +
"Index";
140
141 QuantLib::ext::shared_ptr<QuantExt::FxIndex> fxi = QuantLib::ext::make_shared<QuantExt::FxIndex>(
143
145
146 DLOG(
"BondBasket::build() -- created FX Index for " << source + target);
147 }
148 }
149
151 if(reinvestmentEndDate != "")
153
155
156 QuantLib::ext::shared_ptr<QuantExt::BondBasket> basket(
159
160 DLOG(
"BondBasket::build() -- completed");
161
162 return basket;
163}
const std::map< std::string, boost::shared_ptr< QuantExt::FxIndex > > fxIndexMap_
const std::map< std::string, std::vector< double > > reinvestmentScalar_
const Currency currency(const std::string &name) const
const boost::shared_ptr< QuantLib::Pool > & pool() const
const std::map< std::string, std::vector< std::string > > flowType_
const RequiredFixings & requiredFixings() const
void setReinvestmentScalar()
RequiredFixings requiredFixings_
QuantLib::Date reinvestment_
void addData(const RequiredFixings &requiredFixings)
Date parseDate(const string &s)
Convert std::string to QuantLib::Date.
Currency parseCurrency(const string &s)
Convert text to QuantLib::Currency.
#define DLOG(text)
Logging Macro (Level = Debug)
Size size(const ValueType &v)
QuantLib::Handle< QuantExt::CreditCurve > securitySpecificCreditCurve(const QuantLib::ext::shared_ptr< Market > &market, const std::string &securityId, const std::string &creditCurveId, const std::string &configuration)