Skip to content
Navigation Menu
{{ message }}
forked from eddelbuettel/rquantlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
670 lines (610 loc) · 28.7 KB
/
Copy pathutils.cpp
File metadata and controls
670 lines (610 loc) · 28.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
//
// RQuantLib -- R interface to the QuantLib libraries
//
// Copyright (C) 2002 - 2015 Dirk Eddelbuettel
// Copyright (C) 2005 - 2006 Dominick Samperi
// Copyright (C) 2009 - 2012 Dirk Eddelbuettel and Khanh Nguyen
//
//
// RQuantLib is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// RQuantLib is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with RQuantLib. If not, see <http://www.gnu.org/licenses/>.
#include <rquantlib.h>
// [[Rcpp::interfaces(r, cpp)]]
QuantLib::Option::Type getOptionType(const std::string &type) {
QuantLib::Option::Type optionType;
if (type=="call") {
optionType = QuantLib::Option::Call;
} else if (type=="put") {
optionType = QuantLib::Option::Put;
} else {
throw std::range_error("Unknown option " + type);
}
return optionType;
}
// cf QuantLib-0.9.0/test-suite/europeanoption.cpp
boost::shared_ptr<QuantLib::VanillaOption>
makeOption(const boost::shared_ptr<QuantLib::StrikedTypePayoff>& payoff,
const boost::shared_ptr<QuantLib::Exercise>& exercise,
const boost::shared_ptr<QuantLib::Quote>& u,
const boost::shared_ptr<QuantLib::YieldTermStructure>& q,
const boost::shared_ptr<QuantLib::YieldTermStructure>& r,
const boost::shared_ptr<QuantLib::BlackVolTermStructure>& vol,
EngineType engineType,
QuantLib::Size binomialSteps,
QuantLib::Size samples) {
boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess> stochProcess = makeProcess(u,q,r,vol);
boost::shared_ptr<QuantLib::PricingEngine> engine;
typedef boost::shared_ptr<QuantLib::PricingEngine> spPE; // shorthand used below
switch (engineType) {
case Analytic:
engine = spPE(new QuantLib::AnalyticEuropeanEngine(stochProcess));
break;
case JR:
engine = spPE(new QuantLib::BinomialVanillaEngine<QuantLib::JarrowRudd>(stochProcess, binomialSteps));
break;
case CRR:
engine = spPE(new QuantLib::BinomialVanillaEngine<QuantLib::CoxRossRubinstein>(stochProcess, binomialSteps));
break;
case EQP:
engine = spPE(new QuantLib::BinomialVanillaEngine<QuantLib::AdditiveEQPBinomialTree>(stochProcess, binomialSteps));
break;
case TGEO:
engine = spPE(new QuantLib::BinomialVanillaEngine<QuantLib::Trigeorgis>(stochProcess, binomialSteps));
break;
case TIAN:
engine = spPE(new QuantLib::BinomialVanillaEngine<QuantLib::Tian>(stochProcess, binomialSteps));
break;
case LR:
engine = spPE(new QuantLib::BinomialVanillaEngine<QuantLib::LeisenReimer>(stochProcess, binomialSteps));
break;
case JOSHI:
engine = spPE(new QuantLib::BinomialVanillaEngine<QuantLib::Joshi4>(stochProcess, binomialSteps));
break;
case FiniteDifferences:
engine = spPE(new QuantLib::FDEuropeanEngine<QuantLib::CrankNicolson>(stochProcess, binomialSteps, samples));
break;
case Integral:
engine = spPE(new QuantLib::IntegralEngine(stochProcess));
break;
case PseudoMonteCarlo:
engine = QuantLib::MakeMCEuropeanEngine<QuantLib::PseudoRandom>(stochProcess)
.withStepsPerYear(1)
.withSamples(samples)
.withSeed(42);
break;
case QuasiMonteCarlo:
engine = QuantLib::MakeMCEuropeanEngine<QuantLib::LowDiscrepancy>(stochProcess)
.withStepsPerYear(1)
.withSamples(samples);
break;
default:
QL_FAIL("Unknown engine type");
}
boost::shared_ptr<QuantLib::VanillaOption> option(new QuantLib::EuropeanOption(payoff, exercise));
option->setPricingEngine(engine);
return option;
}
// QuantLib option setup utils, copied from the test-suite sources
boost::shared_ptr<QuantLib::YieldTermStructure> buildTermStructure(Rcpp::List rparam, Rcpp::List tslist) {
boost::shared_ptr<QuantLib::YieldTermStructure> curve;
try {
Rcpp::CharacterVector tsnames = tslist.names();
QuantLib::Date todaysDate(Rcpp::as<QuantLib::Date>(rparam["tradeDate"]));
QuantLib::Date settlementDate(Rcpp::as<QuantLib::Date>(rparam["settleDate"]));
// cout << "TradeDate: " << todaysDate << endl << "Settle: " << settlementDate << endl;
RQLContext::instance().settleDate = settlementDate;
QuantLib::Settings::instance().evaluationDate() = todaysDate;
std::string firstQuoteName = Rcpp::as<std::string>(tsnames[0]);
//double dt = rparam.getDoubleValue("dt");
std::string interpWhat, interpHow;
if (firstQuoteName.compare("flat") != 0) { // Get interpolation method (not needed for "flat" case)
interpWhat = Rcpp::as<std::string>(rparam["interpWhat"]);
interpHow = Rcpp::as<std::string>(rparam["interpHow"]);
}
// initialise from the singleton instance
QuantLib::Calendar calendar = RQLContext::instance().calendar;
//Integer fixingDays = RQLContext::instance().fixingDays;
// Any DayCounter would be fine; ActualActual::ISDA ensures that 30 years is 30.0
QuantLib::DayCounter termStructureDayCounter = QuantLib::ActualActual(QuantLib::ActualActual::ISDA);
double tolerance = 1.0e-15;
if (firstQuoteName.compare("flat") == 0) { // Create a flat term structure.
double rateQuote = Rcpp::as<double>(tslist[0]);
boost::shared_ptr<QuantLib::Quote> flatRate(new QuantLib::SimpleQuote(rateQuote));
boost::shared_ptr<QuantLib::FlatForward>
ts(new QuantLib::FlatForward(settlementDate, QuantLib::Handle<QuantLib::Quote>(flatRate), QuantLib::Actual365Fixed()));
curve = ts;
} else { // Build curve based on a set of observed rates and/or prices.
std::vector<boost::shared_ptr<QuantLib::RateHelper> > curveInput;
for (int i = 0; i < tslist.size(); i++) {
std::string name = Rcpp::as<std::string>(tsnames[i]);
double val = Rcpp::as<double>(tslist[i]);
boost::shared_ptr<QuantLib::RateHelper> rh = ObservableDB::instance().getRateHelper(name, val);
// edd 2009-11-01 FIXME NULL_RateHelper no longer builds under 0.9.9
// if (rh == NULL_RateHelper)
if (rh.get() == NULL)
throw std::range_error("Unknown rate in getRateHelper");
curveInput.push_back(rh);
}
boost::shared_ptr<QuantLib::YieldTermStructure> ts =
getTermStructure(interpWhat, interpHow, settlementDate, curveInput,
termStructureDayCounter, tolerance);
curve = ts;
}
return curve;
} catch(std::exception &ex) {
forward_exception_to_r(ex);
} catch(...) {
::Rf_error("c++ exception (unknown reason)");
}
return curve;
}
QuantLib::Schedule getSchedule(Rcpp::List rparam) {
QuantLib::Date effectiveDate(Rcpp::as<QuantLib::Date>(rparam["effectiveDate"]));
QuantLib::Date maturityDate(Rcpp::as<QuantLib::Date>(rparam["maturityDate"]));
QuantLib::Period period = QuantLib::Period(getFrequency(Rcpp::as<double>(rparam["period"])));
std::string cal = Rcpp::as<std::string>(rparam["calendar"]);
QuantLib::Calendar calendar;
if(!cal.empty()) {
boost::shared_ptr<QuantLib::Calendar> p = getCalendar(cal);
calendar = *p;
}
QuantLib::BusinessDayConvention businessDayConvention =
getBusinessDayConvention(Rcpp::as<double>(rparam["businessDayConvention"]));
QuantLib::BusinessDayConvention terminationDateConvention =
getBusinessDayConvention(Rcpp::as<double>(rparam["terminationDateConvention"]));
// keep these default values for the last two parameters for backward compatibility
// (although in QuantLib::schedule they have no default values)
QuantLib::DateGeneration::Rule dateGeneration = QuantLib::DateGeneration::Backward;
if(rparam.containsElementNamed("dateGeneration") ) {
dateGeneration = getDateGenerationRule(Rcpp::as<double>(rparam["dateGeneration"]));
}
bool endOfMonth = false;
if(rparam.containsElementNamed("endOfMonth") ) {
endOfMonth = (Rcpp::as<double>(rparam["endOfMonth"]) == 1) ? true : false;
}
QuantLib::Schedule schedule(effectiveDate,
maturityDate,
period,
calendar,
businessDayConvention,
terminationDateConvention,
dateGeneration,
endOfMonth);
return schedule;
}
boost::shared_ptr<QuantLib::FixedRateBond> getFixedRateBond(
Rcpp::List bondparam, std::vector<double> ratesVec, Rcpp::List scheduleparam) {
// get bond parameters
double settlementDays = Rcpp::as<double>(bondparam["settlementDays"]);
double faceAmount = Rcpp::as<double>(bondparam["faceAmount"]);
QuantLib::DayCounter accrualDayCounter =
getDayCounter(Rcpp::as<double>(bondparam["dayCounter"]));
QuantLib::BusinessDayConvention paymentConvention = QuantLib::Following;
if(bondparam.containsElementNamed("paymentConvention") ) {
paymentConvention = getBusinessDayConvention(Rcpp::as<double>(bondparam["paymentConvention"]));
}
double redemption = 100.0;
if(bondparam.containsElementNamed("redemption") ) {
redemption = Rcpp::as<double>(bondparam["redemption"]);
}
QuantLib::Date issueDate;
if(bondparam.containsElementNamed("issueDate") ) {
issueDate = Rcpp::as<QuantLib::Date>(bondparam["issueDate"]);
}
QuantLib::Calendar paymentCalendar;
if(bondparam.containsElementNamed("paymentCalendar") ) {
boost::shared_ptr<QuantLib::Calendar> p = getCalendar(Rcpp::as<std::string>(bondparam["paymentCalendar"]));
paymentCalendar = *p;
}
QuantLib::Period exCouponPeriod;
if(bondparam.containsElementNamed("exCouponPeriod") ) {
exCouponPeriod = QuantLib::Period(Rcpp::as<double>(bondparam["exCouponPeriod"]), QuantLib::Days);
}
QuantLib::Calendar exCouponCalendar;
if(bondparam.containsElementNamed("exCouponCalendar") ) {
boost::shared_ptr<QuantLib::Calendar> p = getCalendar(Rcpp::as<std::string>(bondparam["exCouponCalendar"]));
exCouponCalendar = *p;
}
QuantLib::BusinessDayConvention exCouponConvention = QuantLib::Unadjusted;
if(bondparam.containsElementNamed("exCouponConvention") ) {
exCouponConvention = getBusinessDayConvention(Rcpp::as<double>(bondparam["exCouponConvention"]));
}
bool exCouponEndOfMonth = false;
if(bondparam.containsElementNamed("exCouponEndOfMonth") ) {
exCouponEndOfMonth = (Rcpp::as<double>(bondparam["exCouponEndOfMonth"]) == 1) ? true : false;
}
QuantLib::Schedule schedule = getSchedule(scheduleparam);
boost::shared_ptr<QuantLib::FixedRateBond> result(
new QuantLib::FixedRateBond(settlementDays,
faceAmount,
schedule,
ratesVec,
accrualDayCounter,
paymentConvention,
redemption,
issueDate,
paymentCalendar,
exCouponPeriod,
exCouponCalendar,
exCouponConvention,
exCouponEndOfMonth));
return result;
}
boost::shared_ptr<QuantLib::YieldTermStructure>
rebuildCurveFromZeroRates(std::vector<QuantLib::Date> dates,
std::vector<double> zeros) {
boost::shared_ptr<QuantLib::YieldTermStructure>
rebuilt_curve(new QuantLib::InterpolatedZeroCurve<QuantLib::LogLinear>(dates,
zeros,
QuantLib::ActualActual()));
return rebuilt_curve;
}
boost::shared_ptr<QuantLib::YieldTermStructure> getFlatCurve(Rcpp::List curve) {
QuantLib::Rate riskFreeRate = Rcpp::as<double>(curve["riskFreeRate"]);
QuantLib::Date today(Rcpp::as<QuantLib::Date>(curve["todayDate"]));
boost::shared_ptr<QuantLib::SimpleQuote> rRate(new QuantLib::SimpleQuote(riskFreeRate));
QuantLib::Settings::instance().evaluationDate() = today;
return flatRate(today, rRate, QuantLib::Actual360());
}
boost::shared_ptr<QuantLib::IborIndex> getIborIndex(Rcpp::List rparam, const QuantLib::Date today) {
std::string type = Rcpp::as<std::string>(rparam["type"]);
if (type == "USDLibor"){
double riskFreeRate = Rcpp::as<double>(rparam["riskFreeRate"]);
double period = Rcpp::as<double>(rparam["period"]);
boost::shared_ptr<QuantLib::SimpleQuote> rRate(new QuantLib::SimpleQuote(riskFreeRate));
QuantLib::Settings::instance().evaluationDate() = today;
QuantLib::Handle<QuantLib::YieldTermStructure> curve(flatRate(today, rRate, QuantLib::Actual360()));
boost::shared_ptr<QuantLib::IborIndex> iindex(new QuantLib::USDLibor(period * QuantLib::Months, curve));
return iindex;
}
else return boost::shared_ptr<QuantLib::IborIndex>();
}
// std::vector<double> getDoubleVector(SEXP vecSexp) {
// if (::Rf_length(vecSexp) == 0) {
// return(std::vector<double>());
// } else {
// return std::vector<double>( Rcpp::as<std::vector< double> >( Rcpp::NumericVector(vecSexp) ) );
// }
// }
boost::shared_ptr<QuantLib::YieldTermStructure>
makeFlatCurve(const QuantLib::Date& today,
const boost::shared_ptr<QuantLib::Quote>& forward,
const QuantLib::DayCounter& dc) {
return boost::shared_ptr<QuantLib::YieldTermStructure>(new QuantLib::FlatForward(today, QuantLib::Handle<QuantLib::Quote>(forward), dc));
}
boost::shared_ptr<QuantLib::YieldTermStructure>
flatRate(const QuantLib::Date& today,
const boost::shared_ptr<QuantLib::Quote>& forward,
const QuantLib::DayCounter& dc) {
return boost::shared_ptr<QuantLib::YieldTermStructure>(new QuantLib::FlatForward(today, QuantLib::Handle<QuantLib::Quote>(forward), dc));
}
boost::shared_ptr<QuantLib::BlackVolTermStructure>
makeFlatVolatility(const QuantLib::Date& today,
const boost::shared_ptr<QuantLib::Quote>& vol,
const QuantLib::DayCounter dc) {
return boost::shared_ptr<QuantLib::BlackVolTermStructure>(new QuantLib::BlackConstantVol(today,
QuantLib::NullCalendar(),
QuantLib::Handle<QuantLib::Quote>(vol), dc));
}
boost::shared_ptr<QuantLib::BlackVolTermStructure>
flatVol(const QuantLib::Date& today,
const boost::shared_ptr<QuantLib::Quote>& vol,
const QuantLib::DayCounter& dc) {
return boost::shared_ptr<QuantLib::BlackVolTermStructure>(
new QuantLib::BlackConstantVol(today,
QuantLib::NullCalendar(),
QuantLib::Handle<QuantLib::Quote>(vol), dc));
}
typedef QuantLib::BlackScholesMertonProcess BSMProcess; // shortcut
boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>
makeProcess(const boost::shared_ptr<QuantLib::Quote>& u,
const boost::shared_ptr<QuantLib::YieldTermStructure>& q,
const boost::shared_ptr<QuantLib::YieldTermStructure>& r,
const boost::shared_ptr<QuantLib::BlackVolTermStructure>& vol) {
return boost::shared_ptr<BSMProcess>(new BSMProcess(QuantLib::Handle<QuantLib::Quote>(u),
QuantLib::Handle<QuantLib::YieldTermStructure>(q),
QuantLib::Handle<QuantLib::YieldTermStructure>(r),
QuantLib::Handle<QuantLib::BlackVolTermStructure>(vol)));
}
// R uses dates indexed to Jan 1, 1970. RcppDate uses an internal Julian Date representation,
// but Quantlib uses the 'spreadsheet' format indexed to 1905 so we need to adjust
// int dateFromR(const RcppDate &d) {
// return(d.getJDN() - RcppDate::Jan1970Offset + RcppDate::QLtoJan1970Offset);
// }
//static const unsigned int QLtoJan1970Offset = 25569; // Offset between R / Unix epoch
// // R and Rcpp::Date use the same 'days since epoch' representation; QL uses Excel style
// int dateFromR(const Rcpp::Date &d) {
// static const unsigned int QLtoJan1970Offset = 25569; // Offset to R / Unix epoch
// return(d.getDate() + QLtoJan1970Offset);
// }
QuantLib::DayCounter getDayCounter(const double n){
if (n==0)
return QuantLib::Actual360();
else if (n==1)
return QuantLib::Actual365Fixed();
else if (n==2)
return QuantLib::ActualActual();
else if (n==3)
return QuantLib::Business252();
else if (n==4)
return QuantLib::OneDayCounter();
else if (n==5)
return QuantLib::SimpleDayCounter();
else if (n==6)
return QuantLib::Thirty360();
else if (n==7)
return QuantLib::Actual365NoLeap();
else if (n==8)
return QuantLib::ActualActual(QuantLib::ActualActual::ISMA);
else if (n==9)
return QuantLib::ActualActual(QuantLib::ActualActual::Bond);
else if (n==10)
return QuantLib::ActualActual(QuantLib::ActualActual::ISDA);
else if (n==11)
return QuantLib::ActualActual(QuantLib::ActualActual::Historical);
else if (n==12)
return QuantLib::ActualActual(QuantLib::ActualActual::AFB);
else // if (n==13)
return QuantLib::ActualActual(QuantLib::ActualActual::Euro);
}
QuantLib::BusinessDayConvention getBusinessDayConvention(const double n){
if (n==0)
return QuantLib::Following;
else if (n==1)
return QuantLib::ModifiedFollowing;
else if (n==2)
return QuantLib::Preceding;
else if (n==3)
return QuantLib::ModifiedPreceding;
else if (n==4)
return QuantLib::Unadjusted;
else if (n==5)
return QuantLib::HalfMonthModifiedFollowing;
else if (n==6)
return QuantLib::Nearest;
else
return QuantLib::Unadjusted;
}
QuantLib::Compounding getCompounding(const double n){
if (n==0)
return QuantLib::Simple;
else if (n==1)
return QuantLib::Compounded;
else if (n==2)
return QuantLib::Continuous;
else
return QuantLib::SimpleThenCompounded;
}
QuantLib::Frequency getFrequency(const double n){
if (n==-1)
return QuantLib::NoFrequency;
else if (n==0)
return QuantLib::Once;
else if (n==1)
return QuantLib::Annual;
else if (n==2)
return QuantLib::Semiannual;
else if (n==3)
return QuantLib::EveryFourthMonth;
else if (n==4)
return QuantLib::Quarterly;
else if (n==6)
return QuantLib::Bimonthly;
else if (n==12)
return QuantLib::Monthly;
else if (n==13)
return QuantLib::EveryFourthWeek;
else if (n==26)
return QuantLib::Biweekly;
else if (n==52)
return QuantLib::Weekly;
else if (n==365)
return QuantLib::Daily;
else
return QuantLib::OtherFrequency;
}
QuantLib::Period periodByTimeUnit(int length, std::string unit){
QuantLib::TimeUnit tu = QuantLib::Years;
if (unit=="Days")
tu = QuantLib::Days;
if (unit=="Weeks")
tu = QuantLib::Weeks;
if (unit=="Months")
tu = QuantLib::Months;
return QuantLib::Period(length, tu);
}
QuantLib::TimeUnit getTimeUnit(const double n){
if (n==0)
return QuantLib::Days;
else if (n==1)
return QuantLib::Weeks;
else if (n==2)
return QuantLib::Months;
else
return QuantLib::Years;
}
QuantLib::DateGeneration::Rule getDateGenerationRule(const double n){
if (n==0)
return QuantLib::DateGeneration::Backward;
else if (n==1)
return QuantLib::DateGeneration::Forward;
else if (n==2)
return QuantLib::DateGeneration::Zero;
else if (n==3)
return QuantLib::DateGeneration::ThirdWednesday;
else if (n==4)
return QuantLib::DateGeneration::Twentieth;
else if (n==5)
return QuantLib::DateGeneration::TwentiethIMM;
else if (n==6)
return QuantLib::DateGeneration::OldCDS;
else if (n==7)
return QuantLib::DateGeneration::CDS;
else
return QuantLib::DateGeneration::TwentiethIMM;
}
boost::shared_ptr<QuantLib::IborIndex> buildIborIndex(std::string type,
const QuantLib::Handle<QuantLib::YieldTermStructure>& iborStrc){
if (type == "Euribor10M")
return boost::shared_ptr<QuantLib::IborIndex>(new QuantLib::Euribor10M(iborStrc));
if (type == "Euribor11M")
return boost::shared_ptr<QuantLib::IborIndex>(new QuantLib::Euribor11M(iborStrc));
if (type == "Euribor1M")
return boost::shared_ptr<QuantLib::IborIndex>(new QuantLib::Euribor1M(iborStrc));
if (type == "Euribor1Y")
return boost::shared_ptr<QuantLib::IborIndex>(new QuantLib::Euribor1Y(iborStrc));
if (type == "Euribor2M")
return boost::shared_ptr<QuantLib::IborIndex>(new QuantLib::Euribor2M(iborStrc));
if (type == "Euribor2W")
return boost::shared_ptr<QuantLib::IborIndex>(new QuantLib::Euribor2W(iborStrc));
if (type == "Euribor3M")
return boost::shared_ptr<QuantLib::IborIndex>(new QuantLib::Euribor3M(iborStrc));
if (type == "Euribor3W")
return boost::shared_ptr<QuantLib::IborIndex>(new QuantLib::Euribor3W(iborStrc));
if (type == "Euribor4M")
return boost::shared_ptr<QuantLib::IborIndex>(new QuantLib::Euribor4M(iborStrc));
if (type == "Euribor5M")
return boost::shared_ptr<QuantLib::IborIndex>(new QuantLib::Euribor5M(iborStrc));
if (type == "Euribor6M")
return boost::shared_ptr<QuantLib::IborIndex>(new QuantLib::Euribor6M(iborStrc));
if (type == "Euribor7M")
return boost::shared_ptr<QuantLib::IborIndex>(new QuantLib::Euribor7M(iborStrc));
if (type == "Euribor8M")
return boost::shared_ptr<QuantLib::IborIndex>(new QuantLib::Euribor8M(iborStrc));
if (type == "Euribor9M")
return boost::shared_ptr<QuantLib::IborIndex>(new QuantLib::Euribor9M(iborStrc));
if (type == "EuriborSW")
return boost::shared_ptr<QuantLib::IborIndex>(new QuantLib::EuriborSW(iborStrc));
return boost::shared_ptr<QuantLib::IborIndex>();
}
Rcpp::DataFrame getCashFlowDataFrame(const QuantLib::Leg &bondCashFlow) {
Rcpp::DateVector dates(bondCashFlow.size());
Rcpp::NumericVector amount(bondCashFlow.size());
for (unsigned int i = 0; i< bondCashFlow.size(); i++){
QuantLib::Date d = bondCashFlow[i]->date();
dates[i] = Rcpp::Date(d.month(), d.dayOfMonth(), d.year());
amount[i] = bondCashFlow[i]->amount();
}
return Rcpp::DataFrame::create(Rcpp::Named("Date") = dates,
Rcpp::Named("Amount") = amount);
}
QuantLib::DividendSchedule getDividendSchedule(Rcpp::DataFrame divScheDF) {
QuantLib::DividendSchedule dividendSchedule;
try {
Rcpp::CharacterVector s0v = divScheDF[0];
Rcpp::NumericVector n1v = divScheDF[1];
Rcpp::NumericVector n2v = divScheDF[2];
Rcpp::NumericVector n3v = divScheDF[3];
int nrow = s0v.size();
for (int row=0; row<nrow; row++){
int type = (s0v[row] == "Fixed") ? 1 : 0; // (table[row][0].getStringValue()=="Fixed") ? 1 : 0;
double amount = n1v[row]; // table[row][1].getDoubleValue();
double rate = n2v[row]; // table[row][2].getDoubleValue();
Rcpp::Date rd = Rcpp::Date(n3v[row]);
QuantLib::Date d(Rcpp::as<QuantLib::Date>(Rcpp::wrap(rd))); //table[row][3].getDateValue()));
if (type==1) {
dividendSchedule.push_back(boost::shared_ptr<QuantLib::Dividend>(new QuantLib::FixedDividend(amount, d)));
} else {
dividendSchedule.push_back(boost::shared_ptr<QuantLib::Dividend>(new QuantLib::FractionalDividend(rate, amount, d)));
}
}
} catch (std::exception& ex) {
forward_exception_to_r(ex);
}
return dividendSchedule;
}
QuantLib::CallabilitySchedule getCallabilitySchedule(Rcpp::DataFrame callScheDF) {
QuantLib::CallabilitySchedule callabilitySchedule;
try {
// RcppFrame rcppCallabilitySchedule(callabilityScheduleFrame);
// std::vector<std::vector<ColDatum> > table = rcppCallabilitySchedule.getTableData();
// int nrow = table.size();
Rcpp::NumericVector n0v = callScheDF[0];
Rcpp::CharacterVector s1v = callScheDF[1];
Rcpp::NumericVector n2v = callScheDF[2];
int nrow = n0v.size();
for (int row=0; row<nrow; row++) {
double price = n0v[row]; //table[row][0].getDoubleValue();
int type = (s1v[row]=="P") ? 1 : 0;
Rcpp::Date rd = Rcpp::Date(n2v[row]);
QuantLib::Date d(Rcpp::as<QuantLib::Date>(Rcpp::wrap(rd)));
if (type==1){
callabilitySchedule.push_back(boost::shared_ptr<QuantLib::Callability>
(new QuantLib::Callability(QuantLib::Callability::Price(price,
QuantLib::Callability::Price::Clean),
QuantLib::Callability::Put,d )));
} else {
callabilitySchedule.push_back(boost::shared_ptr<QuantLib::Callability>
(new QuantLib::Callability(QuantLib::Callability::Price(price,
QuantLib::Callability::Price::Clean),
QuantLib::Callability::Call,d )));
}
}
} catch (std::exception& ex){
forward_exception_to_r(ex);
}
return callabilitySchedule;
}
QuantLib::Duration::Type getDurationType(const double n) {
if (n==0)
return QuantLib::Duration::Simple;
else if (n==1)
return QuantLib::Duration::Macaulay;
else if (n==2)
return QuantLib::Duration::Modified;
else {
throw std::range_error("Invalid duration type " + boost::lexical_cast<std::string>(n));
}
}
//' This function returns the QuantLib version string as encoded in the header
//' file \code{config.hpp} and determined at compilation time of the QuantLib library.
//'
//' @title Return the QuantLib version number
//' @return A character variable
//' @references \url{http://quantlib.org} for details on \code{QuantLib}.
//' @author Dirk Eddelbuettel
//' @examples
//' getQuantLibVersion()
// [[Rcpp::export]]
std::string getQuantLibVersion() {
return std::string(QL_PACKAGE_VERSION);
}
//' This function returns a named vector of boolean variables describing several
//' configuration options determined at compilation time of the QuantLib library.
//'
//' Not all of these features are used (yet) by RQuantLib.
//' @title Return configuration options of the QuantLib library
//' @return A named vector of logical variables
//' @references \url{http://quantlib.org} for details on \code{QuantLib}.
//' @author Dirk Eddelbuettel
//' @examples
//' getQuantLibCapabilities()
// [[Rcpp::export]]
Rcpp::LogicalVector getQuantLibCapabilities() {
bool hasSessions = false, // not (yet?) used by RQuantLib
hasHighResolutionDate=false, // supported as of RQuantLib 0.4.2
hasNegativeRates=false; // not explicitly supported or denied
#ifdef QL_ENABLE_SESSIONS
hasSessions = true;
#endif
#ifdef QL_HIGH_RESOLUTION_DATE
hasHighResolutionDate=true;
#endif
#ifdef QL_NEGATIVE_RATES
hasNegativeRates=true;
#endif
return Rcpp::LogicalVector::create(Rcpp::Named("sessions") = hasSessions,
Rcpp::Named("intradayDate") = hasHighResolutionDate,
Rcpp::Named("negativeRates") = hasNegativeRates);
}
You can’t perform that action at this time.
