-
Notifications
You must be signed in to change notification settings - Fork 58
/
xll_fixed_income.cpp
280 lines (243 loc) · 8.6 KB
/
xll_fixed_income.cpp
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
// xll_fixed_income.cpp - Fixed Income instruments
#include "fms_fixed_income.h"
#include "fms_fixed_income_cash_deposit.h"
#include "fms_fixed_income_forward_rate_agreement.h"
#include "fms_fixed_income_swap.h"
#include "G5260.h"
using namespace fms;
using namespace xll;
#ifdef _DEBUG
xll::test xll_test_fixed_income_intrument([](){
//_crtBreakAlloc = 3255;
fixed_income::test_fms_fixed_income_instrument();
fixed_income::test_fms_fixed_income_cash_deposit();
});
#endif // _DEBUG
#define PREFIX L"FIXED.INCOME."
static AddIn xai_fixed_income_instrument(
Function(XLL_HANDLE, L"?xll_fixed_income_instrument", PREFIX L"INSTRUMENT")
.Arg(XLL_FP, L"time", L"is and array of times.")
.Arg(XLL_FP, L"cash", L"is an array of cash flows.")
.Uncalced()
.Category(CATEGORY)
.FunctionHelp(L"Return a handle to a fixed income instrument.")
);
HANDLEX WINAPI xll_fixed_income_instrument(_FP12* pu, _FP12* pc)
{
#pragma XLLEXPORT
handlex h;
try {
ensure (size(*pu) == size(*pc));
xll::handle<fixed_income::instrument<>> h_(new fixed_income::instrument<>(size(*pu), pu->array, pc->array));
h = h_.get();
}
catch(const std::exception& ex) {
XLL_ERROR(ex.what());
}
return h;
}
static AddIn xai_fixed_income_instrument_size(
Function(XLL_DOUBLE, L"?xll_fixed_income_instrument_size", PREFIX L"INSTRUMENT.SIZE")
.Arg(XLL_HANDLE, L"handle", L"is a handle to an instrument.")
.Category(CATEGORY)
.FunctionHelp(L"Return the number of times/cash flows of a fixed income instrument.")
);
double WINAPI xll_fixed_income_instrument_size(HANDLEX h)
{
#pragma XLLEXPORT
double size;
try {
xll::handle<fixed_income::instrument<>> h_(h);
ensure (h_);
size = static_cast<double>(h_->size());
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
size = std::numeric_limits<double>::quiet_NaN();
}
return size;
}
static AddIn xai_fixed_income_instrument_time(
Function(XLL_FP, L"?xll_fixed_income_instrument_time", PREFIX L"INSTRUMENT.TIME")
.Arg(XLL_HANDLE, L"handle", L"is handle to an instrument.")
.Category(CATEGORY)
.FunctionHelp(L"Return a one row array of cash flow times of a fixed income instrument.")
);
_FP12* WINAPI xll_fixed_income_instrument_time(HANDLEX h)
{
#pragma XLLEXPORT
static xll::FP12 t;
try {
xll::handle<fixed_income::instrument<>> h_(h);
ensure (h_);
t.resize(1, static_cast<COL>(h_->size()));
std::copy(h_->time(), h_->time() + h_->size(), begin(t));
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
return 0;
}
return t.get();
}
static AddIn xai_fixed_income_instrument_cash(
Function(XLL_FP, L"?xll_fixed_income_instrument_cash", PREFIX L"INSTRUMENT.CASH")
.Arg(XLL_HANDLE, L"handle", L"is handle to an instrument.")
.Category(CATEGORY)
.FunctionHelp(L"Return a one row array of cash flows of a fixed income instrument.")
);
_FP12* WINAPI xll_fixed_income_instrument_cash(HANDLEX h)
{
#pragma XLLEXPORT
static xll::FP12 c;
try {
xll::handle<fixed_income::instrument<>> h_(h);
ensure (h_);
c.resize(1, static_cast<COL>(h_->size()));
std::copy(h_->cash(), h_->cash() + h_->size(), begin(c));
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
return 0;
}
return c.get();
}
static AddIn xai_fixed_income_cash_deposit(
Function(XLL_HANDLE, L"?xll_fixed_income_cash_deposit", PREFIX L"CASH.DEPOSIT")
.Arg(XLL_SHORT, L"settlement", L"is the number of days until the cash deposit settles.")
.Arg(XLL_SHORT, L"tenor", L"is the number of months until maturity.")
.Arg(XLL_LONG, L"dcb", L"is the day count basis.")
.Arg(XLL_LONG, L"roll", L"is the business day roll convention.")
.Uncalced()
.Category(CATEGORY)
.FunctionHelp(L"Return a handle to a cash deposit.")
);
HANDLEX WINAPI xll_fixed_income_cash_deposit(short settlement, short tenor,
fms::date::DAY_COUNT_BASIS dcb, fms::date::BUSINESS_DAY_ROLL roll)
{
#pragma XLLEXPORT
handlex h;
try {
xll::handle<fixed_income::instrument<>> h_(new fixed_income::cash_deposit<>(::date::days{ settlement }, ::date::months{ tenor }, dcb, roll));
h = h_.get();
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
}
return h;
}
static AddIn xai_fixed_income_instrument_cash_deposit_fix(
Function(XLL_DOUBLE, L"?xll_fixed_income_instrument_cash_deposit_fix", PREFIX L"CASH.DEPOSIT.FIX")
.Arg(XLL_HANDLE, L"handle", L"is a handle to a cash deposit.")
.Arg(XLL_DOUBLE, L"valuation", L"date at which rate is quoted.")
.Arg(XLL_DOUBLE, L"rate", L"is the cash deposit rate on valuation date.")
.Category(CATEGORY)
.FunctionHelp(L"Fix times and cash flows of the cash deposit.")
);
HANDLEX WINAPI xll_fixed_income_instrument_cash_deposit_fix(HANDLEX h, double valuation, double rate)
{
#pragma XLLEXPORT
try {
xll::handle<fixed_income::instrument<>> h_(h);
ensure(h_);
h_->fix(fms::date::excel_date(valuation), rate);
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
return handlex{};
}
return h;
}
static AddIn xai_fixed_income_forward_rate_agreement(
Function(XLL_HANDLE, L"?xll_fixed_income_forward_rate_agreement", PREFIX L"FORWARD.RATE.AGREEMENT")
.Arg(XLL_DOUBLE, L"effective", L"is the date of the first cash flow.")
.Arg(XLL_SHORT, L"tenor", L"is the number of months until maturity.")
.Arg(XLL_LONG, L"dcb", L"is the day count basis.")
.Arg(XLL_LONG, L"roll", L"is the business day roll convention.")
.Uncalced()
.Category(CATEGORY)
.FunctionHelp(L"Return a handle to a cash deposit.")
);
HANDLEX WINAPI xll_fixed_income_forward_rate_agreement(double effective, short tenor,
fms::date::DAY_COUNT_BASIS dcb, fms::date::BUSINESS_DAY_ROLL roll)
{
#pragma XLLEXPORT
handlex h;
try {
xll::handle<fixed_income::instrument<>> h_(new fixed_income::forward_rate_agreement<>(fms::date::excel_date(effective), ::date::months{ tenor }, dcb, roll));
h = h_.get();
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
}
return h;
}
static AddIn xai_fixed_income_instrument_forward_rate_agreement_fix(
Function(XLL_DOUBLE, L"?xll_fixed_income_instrument_forward_rate_agreement_fix", PREFIX L"FORWARD.RATE.AGREEMENT.FIX")
.Arg(XLL_HANDLE, L"handle", L"is a handle to a forward rate agreement.")
.Arg(XLL_DOUBLE, L"valuation", L"date at which rate is quoted.")
.Arg(XLL_DOUBLE, L"rate", L"is the cash deposit rate on valuation date.")
.Category(CATEGORY)
.FunctionHelp(L"Fix times and cash flows of the cash deposit.")
);
HANDLEX WINAPI xll_fixed_income_instrument_forward_rate_agreement_fix(HANDLEX h, double valuation, double rate)
{
#pragma XLLEXPORT
try {
xll::handle<fixed_income::instrument<>> h_(h);
ensure(h_);
h_->fix(fms::date::excel_date(valuation), rate);
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
return handlex{};
}
return h;
}
static AddIn xai_fixed_income_swap(
Function(XLL_HANDLE, L"?xll_fixed_income_swap", PREFIX L"SWAP")
.Arg(XLL_SHORT, L"settlement", L"is the number of days until the swap settles.")
.Arg(XLL_SHORT, L"tenor", L"is the number of months until maturity.")
.Arg(XLL_SHORT, L"freq", L"is the payment frequency of the fixed leg.")
.Arg(XLL_SHORT, L"dcb", L"is the day count basis.")
.Arg(XLL_SHORT, L"roll", L"is the business day roll convention.")
.Uncalced()
.Category(CATEGORY)
.FunctionHelp(L"Return a handle to a swap.")
);
HANDLEX WINAPI xll_fixed_income_swap(short settlement, short tenor,
fms::date::PAYMENT_FREQUENCY freq,
fms::date::DAY_COUNT_BASIS dcb, fms::date::BUSINESS_DAY_ROLL roll)
{
#pragma XLLEXPORT
handlex h;
try {
xll::handle<fixed_income::instrument<>> h_(new fixed_income::swap<>(::date::days(settlement), ::date::years{ tenor }, freq, dcb, roll));
h = h_.get();
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
}
return h;
}
static AddIn xai_fixed_income_instrument_swap_fix(
Function(XLL_DOUBLE, L"?xll_fixed_income_instrument_swap_fix", PREFIX L"SWAP.FIX")
.Arg(XLL_HANDLE, L"handle", L"is a handle to a forward rate agreement.")
.Arg(XLL_DOUBLE, L"valuation", L"date at which rate is quoted.")
.Arg(XLL_DOUBLE, L"rate", L"is the cash deposit rate on valuation date.")
.Category(CATEGORY)
.FunctionHelp(L"Fix times and cash flows of the cash deposit.")
);
HANDLEX WINAPI xll_fixed_income_instrument_swap_fix(HANDLEX h, double valuation, double rate)
{
#pragma XLLEXPORT
try {
xll::handle<fixed_income::instrument<>> h_(h);
ensure(h_);
h_->fix(fms::date::excel_date(valuation), rate);
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
return handlex{};
}
return h;
}