-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathxll_bootstrap.cpp
More file actions
99 lines (85 loc) · 2.79 KB
/
xll_bootstrap.cpp
File metadata and controls
99 lines (85 loc) · 2.79 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
// xll_bootstrap.cpp - Bootstrap a curve.
#include "fms_bootstrap.h"
#include "G5260.h"
using namespace xll;
using namespace fms;
xll::test xll_pwflat_bootstrap_test([]() {
fms_pwflat_bootstrap_test();
});
AddIn xai_xll_bootstrap(
Function(XLL_FP, L"?xll_pwflat_bootstrap", L"PWFLAT.BOOTSTRAP")
.Arg(XLL_DOUBLE, L"price", L"is the price of the instrument.")
.Arg(XLL_FP, L"instrument", L"is a two row array of times and cash flows or a handle to a fixed income instrument.")
.Arg(XLL_FP, L"curve", L"is a two row array of times and forwards or a handle to a pwflat curve.")
.Arg(XLL_DOUBLE, L"initial", L"is an initial guess at the bootstrap value.")
.Category(CATEGORY)
.FunctionHelp(L"A two row array of bootstrap time and rate that prices the instrument.")
);
_FP12* WINAPI xll_pwflat_bootstrap(double p, _FP12* pi, _FP12* pc, double f_)
{
#pragma XLLEXPORT
static xll::FP12 tf(2,1);
try {
size_t m, n;
const double *u, *c, *t, *f;
if (size(*pi) == 1) {
xll::handle<fixed_income::instrument<>> i_(pi->array[0]);
ensure (i_);
m = i_->size();
u = i_->time();
c = i_->cash();
}
else {
ensure (pi->rows == 2);
m = pi->columns;
u = pi->array;
c = pi->array + m;
}
if (size(*pc) == 1) {
xll::handle<pwflat::curve<>> c_(pc->array[0]);
ensure (c_);
n = c_->size();
t = c_->time();
f = c_->rate();
}
else {
ensure (pc->rows == 2);
n = pc->columns;
t = pc->array;
f = pc->array + n;
}
std::tie(tf[0],tf[1]) = pwflat::bootstrap(p, m, u, c, n, t, f, f_);
//auto [tf[0],tf[1]] = pwflat::bootstrap(p, m, u, c, n, t, f, f_);
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
return 0;
}
return tf.get();
}
AddIn xai_pwflat_curve_bootstrap(
Function(XLL_HANDLE, L"?xll_pwflat_curve_bootstrap", L"PWFLAT.CURVE.BOOTSTRAP")
.Arg(XLL_FP, L"instruments", L"is an array fixed income instrument handles.")
.Uncalced()
.Category(CATEGORY)
.FunctionHelp(L"Return a handle to a piecewise flat forward curve pricing each instrument to zero.")
);
HANDLEX WINAPI xll_pwflat_curve_bootstrap(_FP12* pi)
{
#pragma XLLEXPORT
handlex h;
try {
xll::handle<pwflat::curve<>> h_(new pwflat::curve<>{});
ensure (h_);
for (int i = 0; i < size(*pi); ++i) {
xll::handle<fixed_income::instrument<>> hi_(pi->array[i]);
h_->push_back(pwflat::bootstrap<>(0., *hi_, *h_));
}
h = h_.get();
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
return 0;
}
return h;
}