Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
testportfolio.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
42#include <ql/time/calendars/all.hpp>
44#include <ql/instruments/creditdefaultswap.hpp>
45
46namespace testsuite {
47
48QuantLib::ext::shared_ptr<Trade> buildSwap(string id, string ccy, bool isPayer, Real notional, int start, Size term, Real rate,
49 Real spread, string fixedFreq, string fixedDC, string floatFreq, string floatDC,
50 string index, Calendar calendar, Natural spotDays, bool spotStartLag) {
51 Date today = Settings::instance().evaluationDate();
52 ostringstream o;
53 o << calendar;
54 string cal = o.str();
55 string conv = "MF";
56 string rule = "Forward";
57
58 vector<Real> notionals(1, notional);
59 vector<Real> rates(1, rate);
60 vector<Real> spreads(1, spread);
61
62 Period spotStartLagTenor = spotStartLag ? spotDays * Days : 0 * Days;
63
64 Date qlStartDate = calendar.adjust(today + spotStartLagTenor + start * Years);
65 Date qlEndDate = calendar.adjust(qlStartDate + term * Years);
66 string startDate = ore::data::to_string(qlStartDate);
67 string endDate = ore::data::to_string(qlEndDate);
68
69 // envelope
70 Envelope env("CP");
71 // schedules
72 ScheduleData floatSchedule(ScheduleRules(startDate, endDate, floatFreq, cal, conv, conv, rule));
73 ScheduleData fixedSchedule(ScheduleRules(startDate, endDate, fixedFreq, cal, conv, conv, rule));
74 // fixed leg
75 LegData fixedLeg(QuantLib::ext::make_shared<FixedLegData>(rates), isPayer, ccy, fixedSchedule, fixedDC, notionals);
76 // float leg
77 LegData floatingLeg(QuantLib::ext::make_shared<FloatingLegData>(index, spotDays, false, spreads), !isPayer, ccy,
78 floatSchedule, floatDC, notionals);
79 // trade
80 QuantLib::ext::shared_ptr<Trade> trade(new ore::data::Swap(env, floatingLeg, fixedLeg));
81 trade->id() = id;
82
83 return trade;
84
85}
86
87QuantLib::ext::shared_ptr<Trade> buildEuropeanSwaption(string id, string longShort, string ccy, bool isPayer, Real notional,
88 int start, Size term, Real rate, Real spread, string fixedFreq,
89 string fixedDC, string floatFreq, string floatDC, string index,
90 string cashPhysical, Real premium, string premiumCcy,
91 string premiumDate) {
92
93 Date today = Settings::instance().evaluationDate();
94 Calendar calendar = TARGET();
95 int days = 2;
96 string cal = "TARGET";
97 string conv = "MF";
98 string rule = "Forward";
99
100 vector<Real> notionals(1, notional);
101 vector<Real> rates(1, rate);
102 vector<Real> spreads(1, spread);
103
104 Date qlStartDate = calendar.adjust(today + start * Years);
105 Date qlEndDate = calendar.adjust(qlStartDate + term * Years);
106 string startDate = ore::data::to_string(qlStartDate);
107 string endDate = ore::data::to_string(qlEndDate);
108
109 // envelope
110 Envelope env("CP");
111 // schedules
112 ScheduleData floatSchedule(ScheduleRules(startDate, endDate, floatFreq, cal, conv, conv, rule));
113 ScheduleData fixedSchedule(ScheduleRules(startDate, endDate, fixedFreq, cal, conv, conv, rule));
114 // fixed leg
115 LegData fixedLeg(QuantLib::ext::make_shared<FixedLegData>(rates), isPayer, ccy, fixedSchedule, fixedDC, notionals);
116 // float leg
117 LegData floatingLeg(QuantLib::ext::make_shared<FloatingLegData>(index, days, false, spreads), !isPayer, ccy, floatSchedule,
118 floatDC, notionals);
119 // leg vector
120 vector<LegData> legs;
121 legs.push_back(fixedLeg);
122 legs.push_back(floatingLeg);
123 // option data
124 OptionData option(longShort, "Call", "European", false, vector<string>(1, startDate), cashPhysical, "",
125 premiumDate.empty() ? PremiumData() : PremiumData(premium, premiumCcy, parseDate(premiumDate)));
126 // trade
127 QuantLib::ext::shared_ptr<Trade> trade(new ore::data::Swaption(env, option, legs));
128 trade->id() = id;
129
130 return trade;
131}
132
133QuantLib::ext::shared_ptr<Trade> buildBermudanSwaption(string id, string longShort, string ccy, bool isPayer, Real notional,
134 Size exercises, int start, Size term, Real rate, Real spread,
135 string fixedFreq, string fixedDC, string floatFreq, string floatDC,
136 string index, string cashPhysical, Real premium, string premiumCcy,
137 string premiumDate) {
138
139 Date today = Settings::instance().evaluationDate();
140 Calendar calendar = TARGET();
141 int days = 2;
142 string cal = "TARGET";
143 string conv = "MF";
144 string rule = "Forward";
145
146 vector<Real> notionals(1, notional);
147 vector<Real> rates(1, rate);
148 vector<Real> spreads(1, spread);
149
150 Date qlStartDate = calendar.adjust(today + start * Years);
151 Date qlEndDate = calendar.adjust(qlStartDate + term * Years);
152 string startDate = ore::data::to_string(qlStartDate);
153 string endDate = ore::data::to_string(qlEndDate);
154
155 vector<string> exerciseDates;
156 for (Size i = 0; i < exercises; ++i) {
157 Date exerciseDate = qlStartDate + i * Years;
158 exerciseDates.push_back(ore::data::to_string(exerciseDate));
159 }
160
161 // envelope
162 Envelope env("CP");
163 // schedules
164 ScheduleData floatSchedule(ScheduleRules(startDate, endDate, floatFreq, cal, conv, conv, rule));
165 ScheduleData fixedSchedule(ScheduleRules(startDate, endDate, fixedFreq, cal, conv, conv, rule));
166 // fixed leg
167 LegData fixedLeg(QuantLib::ext::make_shared<FixedLegData>(rates), isPayer, ccy, fixedSchedule, fixedDC, notionals);
168 // float leg
169 LegData floatingLeg(QuantLib::ext::make_shared<FloatingLegData>(index, days, false, spreads), !isPayer, ccy, floatSchedule,
170 floatDC, notionals);
171 // leg vector
172 vector<LegData> legs;
173 legs.push_back(fixedLeg);
174 legs.push_back(floatingLeg);
175 // option data
176 OptionData option(longShort, "Call", "Bermudan", false, exerciseDates, cashPhysical, "",
177 premiumDate.empty() ? PremiumData() : PremiumData(premium, premiumCcy, parseDate(premiumDate)));
178 // trade
179 QuantLib::ext::shared_ptr<Trade> trade(new ore::data::Swaption(env, option, legs));
180 trade->id() = id;
181
182 return trade;
183}
184
185QuantLib::ext::shared_ptr<Trade> buildFxOption(string id, string longShort, string putCall, Size expiry, string boughtCcy,
186 Real boughtAmount, string soldCcy, Real soldAmount, Real premium,
187 string premiumCcy, string premiumDate) {
188 Date today = Settings::instance().evaluationDate();
189 Calendar calendar = TARGET();
190 string cal = "TARGET";
191 string conv = "MF";
192 string rule = "Forward";
193
194 Date qlExpiry = calendar.adjust(today + expiry * Years);
195 string expiryDate = ore::data::to_string(qlExpiry);
196
197 // envelope
198 Envelope env("CP");
199 // option data
200 OptionData option(longShort, putCall, "European", false, vector<string>(1, expiryDate), "Cash", "",
201 premiumDate.empty() ? PremiumData() : PremiumData(premium, premiumCcy, parseDate(premiumDate)));
202 // trade
203 QuantLib::ext::shared_ptr<Trade> trade(new ore::data::FxOption(env, option, boughtCcy, boughtAmount, soldCcy, soldAmount));
204 trade->id() = id;
205
206 return trade;
207}
208
209QuantLib::ext::shared_ptr<Trade> buildEquityOption(string id, string longShort, string putCall, Size expiry, string equityName,
210 string currency, Real strike, Real quantity, Real premium, string premiumCcy,
211 string premiumDate) {
212 Date today = Settings::instance().evaluationDate();
213 Calendar calendar = TARGET();
214 string cal = "TARGET";
215 string conv = "MF";
216 string rule = "Forward";
217
218 Date qlExpiry = calendar.adjust(today + expiry * Years);
219 string expiryDate = ore::data::to_string(qlExpiry);
220
221 TradeStrike tradeStrike(strike, currency);
222
223 // envelope
224 Envelope env("CP");
225 // option data
226 OptionData option(longShort, putCall, "European", false, vector<string>(1, expiryDate), "Cash", "",
227 premiumDate.empty() ? PremiumData() : PremiumData(premium, premiumCcy, parseDate(premiumDate)));
228 // trade
229 QuantLib::ext::shared_ptr<Trade> trade(
230 new ore::data::EquityOption(env, option, EquityUnderlying(equityName), currency, quantity, tradeStrike));
231 trade->id() = id;
232
233 return trade;
234}
235
236QuantLib::ext::shared_ptr<Trade> buildEquityForward(string id, string longShort, Size expiry, string equityName,
237 string currency, Real strike, Real quantity) {
238 Date today = Settings::instance().evaluationDate();
239 Calendar calendar = TARGET();
240
241 Date qlExpiry = calendar.adjust(today + expiry * Years);
242 string expiryDate = ore::data::to_string(qlExpiry);
243
244 // envelope
245 Envelope env("CP");
246 // trade
247 QuantLib::ext::shared_ptr<Trade> trade(new ore::data::EquityForward(env, longShort, EquityUnderlying(equityName), currency,
248 quantity, expiryDate, strike));
249 trade->id() = id;
250
251 return trade;
252}
253
254QuantLib::ext::shared_ptr<Trade> buildCap(string id, string ccy, string longShort, Real capRate, Real notional, int start,
255 Size term, string floatFreq, string floatDC, string index,
256 Calendar calendar, Natural spotDays, bool spotStartLag) {
257 return buildCapFloor(id, ccy, longShort, vector<Real>(1, capRate), vector<Real>(), notional, start, term, floatFreq,
258 floatDC, index, calendar, spotDays, spotStartLag);
259}
260
261QuantLib::ext::shared_ptr<Trade> buildFloor(string id, string ccy, string longShort, Real floorRate, Real notional, int start,
262 Size term, string floatFreq, string floatDC, string index,
263 Calendar calendar, Natural spotDays, bool spotStartLag) {
264 return buildCapFloor(id, ccy, longShort, vector<Real>(), vector<Real>(1, floorRate), notional, start, term,
265 floatFreq, floatDC, index, calendar, spotDays, spotStartLag);
266}
267
268QuantLib::ext::shared_ptr<Trade> buildCapFloor(string id, string ccy, string longShort, vector<Real> capRates,
269 vector<Real> floorRates, Real notional, int start, Size term, string floatFreq,
270 string floatDC, string index, Calendar calendar, Natural spotDays,
271 bool spotStartLag) {
272 Date today = Settings::instance().evaluationDate();
273 // Calendar calendar = TARGET();
274 // Size days = 2;
275 // string cal = "TARGET";
276 ostringstream o;
277 o << calendar;
278 string cal = o.str();
279 string conv = "MF";
280 string rule = "Forward";
281
282 vector<Real> notionals(1, notional);
283 vector<Real> spreads(1, 0.0);
284
285 Period spotStartLagTenor = spotStartLag ? spotDays * Days : 0 * Days;
286
287 Date qlStartDate = calendar.adjust(today + spotStartLagTenor + start * Years);
288 Date qlEndDate = calendar.adjust(qlStartDate + term * Years);
289 string startDate = ore::data::to_string(qlStartDate);
290 string endDate = ore::data::to_string(qlEndDate);
291
292 // envelope
293 Envelope env("CP");
294 // schedules
295 ScheduleData floatSchedule(ScheduleRules(startDate, endDate, floatFreq, cal, conv, conv, rule));
296 // float leg
297 LegData floatingLeg(QuantLib::ext::make_shared<FloatingLegData>(index, spotDays, false, spreads), false, ccy, floatSchedule,
298 floatDC, notionals);
299 // trade
300 QuantLib::ext::shared_ptr<Trade> trade(new ore::data::CapFloor(env, longShort, floatingLeg, capRates, floorRates));
301 trade->id() = id;
302
303 return trade;
304}
305
306QuantLib::ext::shared_ptr<Trade> buildCrossCcyBasisSwap(
307 string id, string recCcy, Real recNotional, string payCcy, Real payNotional, int start, Size term,
308 Real recLegSpread, Real payLegSpread, string recFreq, string recDC, string recIndex, Calendar recCalendar,
309 string payFreq, string payDC, string payIndex, Calendar payCalendar, Natural spotDays, bool spotStartLag,
310 bool notionalInitialExchange, bool notionalFinalExchange, bool notionalAmortizingExchange,
311 bool isRecLegFXResettable, bool isPayLegFXResettable) {
312 Date today = Settings::instance().evaluationDate();
313
314 string payCal = to_string(payCalendar);
315 string recCal = to_string(recCalendar);
316 string conv = "MF";
317 string rule = "Forward";
318
319 vector<Real> recNotionals(1, recNotional);
320 vector<Real> recSpreads(1, recLegSpread);
321 vector<Real> payNotionals(1, payNotional);
322 vector<Real> paySpreads(1, payLegSpread);
323
324 Period spotStartLagTenor = spotStartLag ? spotDays * Days : 0 * Days;
325
326 Date qlStartDate = recCalendar.adjust(today + spotStartLagTenor + start * Years);
327 Date qlEndDate = recCalendar.adjust(qlStartDate + term * Years);
328 string startDate = ore::data::to_string(qlStartDate);
329 string endDate = ore::data::to_string(qlEndDate);
330
331 // envelope
332 Envelope env("CP");
333 // schedules
334 ScheduleData recSchedule(ScheduleRules(startDate, endDate, recFreq, recCal, conv, conv, rule));
335 ScheduleData paySchedule(ScheduleRules(startDate, endDate, payFreq, payCal, conv, conv, rule));
336 // rec float leg
337 auto recFloatingLegData = QuantLib::ext::make_shared<FloatingLegData>(recIndex, spotDays, false, recSpreads);
338 LegData recFloatingLeg;
339 if (isRecLegFXResettable) {
340 string fxIndex = "FX-ECB-" + recCcy + "-" + payCcy;
341 recFloatingLeg = LegData(recFloatingLegData, false, recCcy, recSchedule, recDC, recNotionals, vector<string>(),
342 conv, notionalInitialExchange, notionalFinalExchange, notionalAmortizingExchange,
343 isRecLegFXResettable, payCcy, payNotional, fxIndex);
344 } else {
345 recFloatingLeg = LegData(recFloatingLegData, false, recCcy, recSchedule, recDC, recNotionals, vector<string>(),
346 conv, notionalInitialExchange, notionalFinalExchange, notionalAmortizingExchange);
347 }
348 // pay float leg
349 auto payFloatingLegData = QuantLib::ext::make_shared<FloatingLegData>(payIndex, spotDays, false, recSpreads);
350 LegData payFloatingLeg;
351 if (isPayLegFXResettable) {
352 string fxIndex = "FX-ECB-" + payCcy + "-" + recCcy;
353 payFloatingLeg = LegData(payFloatingLegData, true, payCcy, paySchedule, payDC, payNotionals, vector<string>(),
354 conv, notionalInitialExchange, notionalFinalExchange, notionalAmortizingExchange,
355 !isPayLegFXResettable, recCcy, recNotional, fxIndex);
356 } else {
357 payFloatingLeg = LegData(payFloatingLegData, true, payCcy, paySchedule, payDC, payNotionals, vector<string>(),
358 conv, notionalInitialExchange, notionalFinalExchange, notionalAmortizingExchange);
359 }
360 // trade
361 QuantLib::ext::shared_ptr<Trade> trade(new ore::data::Swap(env, recFloatingLeg, payFloatingLeg));
362 trade->id() = id;
363
364 return trade;
365}
366
367QuantLib::ext::shared_ptr<Trade> buildZeroBond(string id, string ccy, Real notional, Size term, string suffix) {
368 Date today = Settings::instance().evaluationDate();
369 Date qlEndDate = today + term * Years;
370 string maturityDate = ore::data::to_string(qlEndDate);
371 string issueDate = ore::data::to_string(today);
372
373 string settlementDays = "2";
374 string calendar = "TARGET";
375 string issuerId = "BondIssuer" + suffix;
376 string creditCurveId = "BondIssuer" + suffix;
377 string securityId = "Bond" + suffix;
378 string referenceCurveId = "BondCurve" + suffix;
379 // envelope
380 Envelope env("CP");
381 QuantLib::ext::shared_ptr<Trade> trade(
382 new ore::data::Bond(env, BondData(issuerId, creditCurveId, securityId, referenceCurveId, settlementDays,
383 calendar, notional, maturityDate, ccy, issueDate)));
384 trade->id() = id;
385
386 return trade;
387}
388
389QuantLib::ext::shared_ptr<Trade> buildCreditDefaultSwap(string id, string ccy, string issuerId,
390 string creditCurveId, bool isPayer, Real notional,
391 int start, Size term, Real rate, Real spread,
392 string fixedFreq, string fixedDC) {
393 Date today = Settings::instance().evaluationDate();
394
395 string settlementDays = "1";
396 Calendar calendar = WeekendsOnly();
397 string cal = "WeekendsOnly";
398 string conv = "F";
399 string convEnd = "U";
400 string rule = "CDS2015";
401
402 vector<Real> notionals(1, notional);
403 vector<Real> rates(1, rate);
404 vector<Real> spreads(1, spread);
405
406 Date qlStartDate = calendar.adjust(today + start * Years);
407 Date qlEndDate = calendar.adjust(qlStartDate + term * Years);
408 string startDate = ore::data::to_string(qlStartDate);
409 string endDate = ore::data::to_string(qlEndDate);
410
411 // envelope
412 Envelope env("CP");
413 ScheduleData fixedSchedule(ScheduleRules(startDate, endDate, fixedFreq, cal, conv, convEnd, rule));
414 // fixed leg
415 LegData fixedLeg(QuantLib::ext::make_shared<FixedLegData>(spreads), isPayer, ccy, fixedSchedule, fixedDC, notionals);
416
417 ore::data::CreditDefaultSwapData swap(issuerId, creditCurveId, fixedLeg, true,
418 QuantExt::CreditDefaultSwap::ProtectionPaymentTime::atDefault, today + 1);
419 QuantLib::ext::shared_ptr<Trade> trade(new ore::data::CreditDefaultSwap(env, swap));
420
421 trade->id() = id;
422
423 return trade;
424}
425
426QuantLib::ext::shared_ptr<Trade> buildSyntheticCDO(string id, string name, vector<string> names,
427 string longShort, string ccy, vector<string> ccys,
428 bool isPayer, vector<Real> notionals, Real notional,
429 int start, Size term, Real rate, Real spread,
430 string fixedFreq, string fixedDC) {
431 Date today = Settings::instance().evaluationDate();
432 Calendar calendar = WeekendsOnly();
433 string cal = "WeekendsOnly";
434 string conv = "F";
435 string rule = "CDS2015";
436 string issuerId = name;
437 string creditCurveId = name;
438 string qualifier = "Tranch1";
439
440 Real attachmentPoint = 0.0;
441 Real detachmentPoint = 0.1;
442 bool settlesAccrual = true;
443 QuantExt::CreditDefaultSwap::ProtectionPaymentTime protectionPaymentTime =
444 QuantExt::CreditDefaultSwap::ProtectionPaymentTime::atDefault;
445
446 Real upfrontFee = 0.0;
447
448 vector<Real> notionalTotal(names.size(), notional);
449 vector<Real> rates(names.size(), rate);
450 vector<Real> spreads(names.size(), spread);
451
452 Date qlStartDate = calendar.adjust(today + start * Years);
453 Date qlProtectionStartDate = calendar.advance(qlStartDate, 1 * Days);
454 Date qlEndDate = calendar.adjust(qlStartDate + term * Years);
455 Date qlUpfrontDate = calendar.advance(qlStartDate, 3 * Days);
456 string startDate = ore::data::to_string(qlStartDate);
457 string protectionStart = ore::data::to_string(qlProtectionStartDate);
458 string endDate = ore::data::to_string(qlEndDate);
459 string upfrontDate = ore::data::to_string(qlUpfrontDate);
460
461 // envelope
462 Envelope env("CP");
463 // schedules
464 ScheduleData fixedSchedule(ScheduleRules(startDate, endDate, fixedFreq, cal, conv, conv, rule));
465 // fixed leg
466 LegData fixedLeg(QuantLib::ext::make_shared<FixedLegData>(rates), isPayer, ccy, fixedSchedule, fixedDC, notionalTotal);
467 // basket data
469 vector<BasketConstituent> constituents;
470 for (Size i = 0; i < names.size(); ++i) {
471 constituents.emplace_back(names[i], names[i], notionals[i], ccys[i], qualifier);
472 }
473 ore::data::BasketData basket(constituents);
474 // swap data
475 ore::data::IndexCreditDefaultSwapData swap(creditCurveId, basket, fixedLeg);
476
477 QuantLib::ext::shared_ptr<Trade> trade(new ore::data::SyntheticCDO(
478 env, fixedLeg, qualifier, basket, attachmentPoint, detachmentPoint, settlesAccrual, protectionPaymentTime,
479 protectionStart, upfrontDate, upfrontFee));
480 if (trade == NULL)
481 std::cout << "failed to build" << std::endl;
482
483 trade->id() = id;
484
485 return trade;
486}
487
488QuantLib::ext::shared_ptr<Trade> buildCmsCapFloor(string id, string ccy, string indexId, bool isPayer,
489 Real notional, int start, Size term, Real capRate,
490 Real floorRate, Real spread, string freq, string dc) {
491 Date today = Settings::instance().evaluationDate();
492
493 string settlementDays = "2";
494 Calendar calendar = TARGET();
495 string cal = "TARGET";
496 string qualifier = "";
497 string conv = "MF";
498 string rule = "Forward";
499
500 Date qlStartDate = calendar.adjust(today + start * Years);
501 Date qlEndDate = calendar.adjust(qlStartDate + term * Years);
502 string startDate = ore::data::to_string(qlStartDate);
503 string endDate = ore::data::to_string(qlEndDate);
504
505 string longShort = "Long";
506
507 vector<Real> notionals(1, notional);
508 vector<Real> caps(1, capRate);
509 vector<Real> floors(1, floorRate);
510 vector<Real> spreads(1, spread);
511 bool isInArrears = false;
512
513 // envelope
514 Envelope env("CP");
515 ScheduleData schedule(ScheduleRules(startDate, endDate, freq, cal, conv, conv, rule));
516 // fixed leg
517
518 LegData cmsLeg(QuantLib::ext::make_shared<CMSLegData>(indexId, 0, isInArrears, spreads, vector<string>(1, startDate)),
519 isPayer, ccy, schedule, dc, notionals, vector<string>(1, startDate));
520
521 QuantLib::ext::shared_ptr<Trade> trade(new ore::data::CapFloor(env, longShort, cmsLeg, vector<double>(), floors));
522
523 trade->id() = id;
524
525 return trade;
526}
527
528QuantLib::ext::shared_ptr<Trade> buildCPIInflationSwap(string id, string ccy, bool isPayer, Real notional, int start, Size term,
529 Real spread, string floatFreq, string floatDC, string index,
530 string cpiFreq, string cpiDC, string cpiIndex, Real baseRate,
531 string observationLag, bool interpolated, Real cpiRate) {
532
533 Date today = Settings::instance().evaluationDate();
534 Calendar calendar = TARGET();
535 int days = 2;
536 string cal = "TARGET";
537 string conv = "MF";
538 string rule = "Forward";
539
540 vector<Real> notionals(1, notional);
541 vector<Real> cpiRates(1, cpiRate);
542 vector<Real> spreads(1, spread);
543
544 Date qlStartDate = calendar.adjust(today + start * Years);
545 Date qlEndDate = calendar.adjust(qlStartDate + term * Years);
546 string startDate = ore::data::to_string(qlStartDate);
547 string endDate = ore::data::to_string(qlEndDate);
548
549 // envelope
550 Envelope env("CP");
551 // schedules
552 ScheduleData floatSchedule(ScheduleRules(startDate, endDate, floatFreq, cal, conv, conv, rule));
553 ScheduleData cpiSchedule(ScheduleRules(startDate, endDate, cpiFreq, cal, conv, conv, rule));
554 // float leg
555 LegData floatingLeg(QuantLib::ext::make_shared<FloatingLegData>(index, days, false, spreads), !isPayer, ccy, floatSchedule,
556 floatDC, notionals);
557 // fixed leg
558 LegData cpiLeg(QuantLib::ext::make_shared<CPILegData>(cpiIndex, startDate, baseRate, observationLag,
559 (interpolated ? "Linear" : "Flat"), cpiRates),
560 isPayer, ccy, cpiSchedule, cpiDC, notionals, vector<string>(), "F", false, true);
561
562 // trade
563 QuantLib::ext::shared_ptr<Trade> trade(new ore::data::Swap(env, floatingLeg, cpiLeg));
564 trade->id() = id;
565
566 return trade;
567}
568
569QuantLib::ext::shared_ptr<Trade> buildYYInflationSwap(string id, string ccy, bool isPayer, Real notional, int start, Size term,
570 Real spread, string floatFreq, string floatDC, string index,
571 string yyFreq, string yyDC, string yyIndex, string observationLag,
572 Size fixDays) {
573
574 Date today = Settings::instance().evaluationDate();
575 Calendar calendar = TARGET();
576 int days = 2;
577 string cal = "TARGET";
578 string conv = "MF";
579 string rule = "Forward";
580
581 vector<Real> notionals(1, notional);
582 vector<Real> spreads(1, spread);
583
584 Date qlStartDate = calendar.adjust(today + start * Years);
585 Date qlEndDate = calendar.adjust(qlStartDate + term * Years);
586 string startDate = ore::data::to_string(qlStartDate);
587 string endDate = ore::data::to_string(qlEndDate);
588
589 // envelope
590 Envelope env("CP");
591 // schedules
592 ScheduleData floatSchedule(ScheduleRules(startDate, endDate, floatFreq, cal, conv, conv, rule));
593 ScheduleData yySchedule(ScheduleRules(startDate, endDate, yyFreq, cal, conv, conv, rule));
594 // float leg
595 LegData floatingLeg(QuantLib::ext::make_shared<FloatingLegData>(index, days, false, spreads), !isPayer, ccy, floatSchedule,
596 floatDC, notionals);
597 // fixed leg
598 LegData yyLeg(QuantLib::ext::make_shared<YoYLegData>(yyIndex, observationLag, fixDays), isPayer, ccy, yySchedule, yyDC,
599 notionals);
600
601 // trade
602 QuantLib::ext::shared_ptr<Trade> trade(new ore::data::Swap(env, floatingLeg, yyLeg));
603 trade->id() = id;
604
605 return trade;
606}
607
608QuantLib::ext::shared_ptr<Trade> buildYYInflationCapFloor(string id, string ccy, Real notional, bool isCap,
609 bool isLong, Real capFloorRate, int start,
610 Size term, string yyFreq, string yyDC,
611 string yyIndex, string observationLag,
612 Size fixDays) {
613
614 Date today = Settings::instance().evaluationDate();
615 Calendar calendar = TARGET();
616 string cal = "TARGET";
617 string conv = "MF";
618 string rule = "Forward";
619
620 vector<Real> notionals(1, notional);
621 vector<Real> caps, floors;
622 if (isCap)
623 caps.resize(1, capFloorRate);
624 else
625 floors.resize(1, capFloorRate);
626
627 Date qlStartDate = calendar.adjust(today + start * Years);
628 Date qlEndDate = calendar.adjust(qlStartDate + term * Years);
629 string startDate = ore::data::to_string(qlStartDate);
630 string endDate = ore::data::to_string(qlEndDate);
631
632 ScheduleData yySchedule(ScheduleRules(startDate, endDate, yyFreq, cal, conv, conv, rule));
633
634 LegData yyLeg(QuantLib::ext::make_shared<YoYLegData>(yyIndex, observationLag, fixDays), true, ccy, yySchedule, yyDC,
635 notionals);
636
637 Envelope env("CP");
638
639 auto trade = QuantLib::ext::make_shared<ore::data::CapFloor>(env, isLong ? "Long" : "Short", yyLeg, caps, floors);
640 trade->id() = id;
641 return trade;
642}
643
644QuantLib::ext::shared_ptr<Trade> buildCommodityForward(const std::string& id, const std::string& position, Size term,
645 const std::string& commodityName, const std::string& currency,
646 Real strike, Real quantity) {
647
648 Date today = Settings::instance().evaluationDate();
649 string maturity = ore::data::to_string(today + term * Years);
650
651 Envelope env("CP");
652 QuantLib::ext::shared_ptr<Trade> trade = QuantLib::ext::make_shared<ore::data::CommodityForward>(
653 env, position, commodityName, currency, quantity, maturity, strike);
654 trade->id() = id;
655
656 return trade;
657}
658
659QuantLib::ext::shared_ptr<Trade> buildCommodityOption(const string& id, const string& longShort, const string& putCall,
660 Size term, const string& commodityName, const string& currency,
661 Real strike, Real quantity, Real premium, const string& premiumCcy,
662 const string& premiumDate) {
663
664 Date today = Settings::instance().evaluationDate();
665 vector<string> expiryDate{ore::data::to_string(today + term * Years)};
666
667 Envelope env("CP");
668 OptionData option(longShort, putCall, "European", false, expiryDate, "Cash", "",
669 premiumDate.empty() ? PremiumData() : PremiumData(premium, premiumCcy, parseDate(premiumDate)));
670 TradeStrike trStrike(TradeStrike::Type::Price, strike);
671 QuantLib::ext::shared_ptr<Trade> trade =
672 QuantLib::ext::make_shared<ore::data::CommodityOption>(env, option, commodityName, currency, quantity, trStrike);
673 trade->id() = id;
674
675 return trade;
676}
677
678} // namespace testsuite
Date parseDate(const string &s)
Time maturity
Calendar calendar
std::string to_string(const LocationInfo &l)
QuantLib::ext::shared_ptr< Trade > buildCreditDefaultSwap(string id, string ccy, string issuerId, string creditCurveId, bool isPayer, Real notional, int start, Size term, Real rate, Real spread, string fixedFreq, string fixedDC)
QuantLib::ext::shared_ptr< Trade > buildSyntheticCDO(string id, string name, vector< string > names, string longShort, string ccy, vector< string > ccys, bool isPayer, vector< Real > notionals, Real notional, int start, Size term, Real rate, Real spread, string fixedFreq, string fixedDC)
QuantLib::ext::shared_ptr< Trade > buildCap(string id, string ccy, string longShort, Real capRate, Real notional, int start, Size term, string floatFreq, string floatDC, string index, Calendar calendar, Natural spotDays, bool spotStartLag)
QuantLib::ext::shared_ptr< Trade > buildFxOption(string id, string longShort, string putCall, Size expiry, string boughtCcy, Real boughtAmount, string soldCcy, Real soldAmount, Real premium, string premiumCcy, string premiumDate)
QuantLib::ext::shared_ptr< Trade > buildCmsCapFloor(string id, string ccy, string indexId, bool isPayer, Real notional, int start, Size term, Real capRate, Real floorRate, Real spread, string freq, string dc)
QuantLib::ext::shared_ptr< Trade > buildSwap(string id, string ccy, bool isPayer, Real notional, int start, Size term, Real rate, Real spread, string fixedFreq, string fixedDC, string floatFreq, string floatDC, string index, Calendar calendar, Natural spotDays, bool spotStartLag)
QuantLib::ext::shared_ptr< Trade > buildCPIInflationSwap(string id, string ccy, bool isPayer, Real notional, int start, Size term, Real spread, string floatFreq, string floatDC, string index, string cpiFreq, string cpiDC, string cpiIndex, Real baseRate, string observationLag, bool interpolated, Real cpiRate)
QuantLib::ext::shared_ptr< Trade > buildZeroBond(string id, string ccy, Real notional, Size term, string suffix)
QuantLib::ext::shared_ptr< Trade > buildBermudanSwaption(string id, string longShort, string ccy, bool isPayer, Real notional, Size exercises, int start, Size term, Real rate, Real spread, string fixedFreq, string fixedDC, string floatFreq, string floatDC, string index, string cashPhysical, Real premium, string premiumCcy, string premiumDate)
QuantLib::ext::shared_ptr< Trade > buildEquityOption(string id, string longShort, string putCall, Size expiry, string equityName, string currency, Real strike, Real quantity, Real premium, string premiumCcy, string premiumDate)
QuantLib::ext::shared_ptr< Trade > buildFloor(string id, string ccy, string longShort, Real floorRate, Real notional, int start, Size term, string floatFreq, string floatDC, string index, Calendar calendar, Natural spotDays, bool spotStartLag)
QuantLib::ext::shared_ptr< Trade > buildCrossCcyBasisSwap(string id, string recCcy, Real recNotional, string payCcy, Real payNotional, int start, Size term, Real recLegSpread, Real payLegSpread, string recFreq, string recDC, string recIndex, Calendar recCalendar, string payFreq, string payDC, string payIndex, Calendar payCalendar, Natural spotDays, bool spotStartLag, bool notionalInitialExchange, bool notionalFinalExchange, bool notionalAmortizingExchange, bool isRecLegFXResettable, bool isPayLegFXResettable)
QuantLib::ext::shared_ptr< Trade > buildYYInflationSwap(string id, string ccy, bool isPayer, Real notional, int start, Size term, Real spread, string floatFreq, string floatDC, string index, string yyFreq, string yyDC, string yyIndex, string observationLag, Size fixDays)
QuantLib::ext::shared_ptr< Trade > buildEquityForward(string id, string longShort, Size expiry, string equityName, string currency, Real strike, Real quantity)
QuantLib::ext::shared_ptr< Trade > buildCapFloor(string id, string ccy, string longShort, vector< Real > capRates, vector< Real > floorRates, Real notional, int start, Size term, string floatFreq, string floatDC, string index, Calendar calendar, Natural spotDays, bool spotStartLag)
QuantLib::ext::shared_ptr< Trade > buildCommodityForward(const std::string &id, const std::string &position, Size term, const std::string &commodityName, const std::string &currency, Real strike, Real quantity)
QuantLib::ext::shared_ptr< Trade > buildEuropeanSwaption(string id, string longShort, string ccy, bool isPayer, Real notional, int start, Size term, Real rate, Real spread, string fixedFreq, string fixedDC, string floatFreq, string floatDC, string index, string cashPhysical, Real premium, string premiumCcy, string premiumDate)
QuantLib::ext::shared_ptr< Trade > buildYYInflationCapFloor(string id, string ccy, Real notional, bool isCap, bool isLong, Real capFloorRate, int start, Size term, string yyFreq, string yyDC, string yyIndex, string observationLag, Size fixDays)
QuantLib::ext::shared_ptr< Trade > buildCommodityOption(const string &id, const string &longShort, const string &putCall, Size term, const string &commodityName, const string &currency, Real strike, Real quantity, Real premium, const string &premiumCcy, const string &premiumDate)
string name