forked from eddelbuettel/rquantlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscount.cpp
More file actions
131 lines (112 loc) · 5.8 KB
/
Copy pathdiscount.cpp
File metadata and controls
131 lines (112 loc) · 5.8 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
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
//
// RQuantLib function DiscountCurve
//
// Copyright (C) 2005 - 2007 Dominick Samperi
// Copyright (C) 2007 - 2009 Dirk Eddelbuettel
// Copyright (C) 2009 - 2011 Dirk Eddelbuettel and Khanh Nguyen
// Copyright (C) 2012 - 2014 Dirk Eddelbuettel
//
// This file is part of RQuantLib.
//
// 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::export]]
Rcpp::List discountCurveEngine(Rcpp::List rparams,
Rcpp::List tslist,
Rcpp::NumericVector times) {
std::vector<std::string> tsNames = tslist.names();
int i;
QuantLib::Date todaysDate(Rcpp::as<QuantLib::Date>(rparams["tradeDate"]));
QuantLib::Date settlementDate(Rcpp::as<QuantLib::Date>(rparams["settleDate"]));
RQLContext::instance().settleDate = settlementDate;
QuantLib::Date evalDate = QuantLib::Settings::instance().evaluationDate();
QuantLib::Settings::instance().evaluationDate() = todaysDate;
std::string firstQuoteName = tsNames[0];
double dt = Rcpp::as<double>(rparams["dt"]);
std::string interpWhat, interpHow;
bool flatQuotes = true;
if (firstQuoteName.compare("flat") != 0) {
// Get interpolation method (not needed for "flat" case)
interpWhat = Rcpp::as<std::string>(rparams["interpWhat"]);
interpHow = Rcpp::as<std::string>(rparams["interpHow"]);
flatQuotes = false;
}
// 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-8;
boost::shared_ptr<QuantLib::YieldTermStructure> curve;
if (firstQuoteName.compare("flat") == 0) { // Create a flat term structure.
double rateQuote = Rcpp::as<double>(tslist[0]);
//boost::shared_ptr<Quote> flatRate(new SimpleQuote(rateQuote));
//boost::shared_ptr<FlatForward> ts(new FlatForward(settlementDate,
// Handle<Quote>(flatRate),
// ActualActual()));
boost::shared_ptr<QuantLib::SimpleQuote> rRate(new QuantLib::SimpleQuote(rateQuote));
curve = flatRate(settlementDate,rRate,QuantLib::ActualActual());
} else { // Build curve based on a set of observed rates and/or prices.
std::vector<boost::shared_ptr<QuantLib::RateHelper> > curveInput;
for(i = 0; i < tslist.size(); i++) {
std::string name = 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 discount, forward rate, and zero coupon curves
int ntimes = times.size();
Rcpp::NumericVector disc(ntimes), fwds(ntimes), zero(ntimes);
QuantLib::Date current = settlementDate;
for (i = 0; i < ntimes; i++) {
double t = times[i];
disc[i] = curve->discount(t);
fwds[i] = curve->forwardRate(t, t+dt, QuantLib::Continuous);
zero[i] = curve->zeroRate(t, QuantLib::Continuous);
}
QuantLib::Settings::instance().evaluationDate() = evalDate;
std::vector<QuantLib::Date> dates;
std::vector<double> zeroRates;
QuantLib::Date d = current;
QuantLib::Date maxDate(31, QuantLib::December, 2099);
while (d < curve->maxDate() && d < maxDate) { // TODO set a max of, say, 5 or 10 years for flat curve
double z = curve->zeroRate(d, QuantLib::ActualActual(), QuantLib::Continuous);
dates.push_back(d);
zeroRates.push_back(z);
d = advanceDate(d, 21); // TODO: make the increment a parameter
}
//Rcpp::DataFrame frame = Rcpp::DataFrame::create(Rcpp::Named("date") = dates,
// Rcpp::Named("zeroRates") = zeroRates);
Rcpp::List frame = Rcpp::List::create(Rcpp::Named("date") = dates,
Rcpp::Named("zeroRates") = zeroRates);
Rcpp::List rl = Rcpp::List::create(Rcpp::Named("times") = times,
Rcpp::Named("discounts") = disc,
Rcpp::Named("forwards") = fwds,
Rcpp::Named("zerorates") = zero,
Rcpp::Named("flatQuotes") = flatQuotes,
Rcpp::Named("params") = rparams,
Rcpp::Named("table") = frame);
return rl;
}