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
|
#include "postgres.h"
#include "fmgr.h"
#include "executor/spi.h"
#include "utils/date.h"
#include "utils/builtins.h"
#include "isda/dateconv.h"
#include "isda/busday.h"
PG_MODULE_MAGIC;
static inline TDate next_business_day(TDate date, long method, const char* cal) {
TDate r;
if (JpmcdsBusinessDay(date, method, cal, &r) != SUCCESS) {
return -1;
} else {
return r;
}
}
TDate _previous_twentieth(TDate d, bool roll, const char* cal) {
TMonthDayYear mdy;
if (JpmcdsDateToMDY(d, &mdy) != SUCCESS) {
return -1;
}
if (mdy.day < 20) {
if(mdy.month == 1) {
mdy.month = 12;
mdy.year -= 1;
} else {
mdy.month -= 1;
}
}
mdy.day = 20;
int mod = mdy.month % 3;
if (mod != 0) {
mdy.month -= mod;
if (mdy.month <= 0) {
mdy.month += 12;
mdy.year -= 1;
}
}
TDate r;
if (JpmcdsMDYToDate(&mdy, &r) != SUCCESS) {
return -1;
}
if (roll) {
return next_business_day(r, JPMCDS_BAD_DAY_FOLLOW, cal);
} else {
return r;
}
}
// postgresql represents dates as number of days since 2000-01-01
// TDate are integers since 1601-01-01
static inline TDate TDate_from_DateADT(DateADT d) {
return d + 145731;
}
static inline const char* cal_from_currency(const char* curr) {
static const char default_cal[] = "NONE";
static const char us_cal[] = "/usr/share/cds/US";
if (strcmp(curr, "USD") == 0) {
return us_cal;
} else {
return default_cal;
}
}
PG_FUNCTION_INFO_V1(cds_accrued);
Datum cds_accrued(PG_FUNCTION_ARGS) {
DateADT d = PG_GETARG_DATEADT(0);
float8 coupon = PG_GETARG_FLOAT8(1);
bool include_cashflow = PG_GETARG_BOOL(2);
char* currency = text_to_cstring(PG_GETARG_TEXT_PP(3));
const char* cal = cal_from_currency(currency);
TDate date = TDate_from_DateADT(d) + 1;
TDate date1 = next_business_day(date, JPMCDS_BAD_DAY_PREVIOUS, cal);
if (date1 == -1)
elog(ERROR, "Please set up the US calendar in /usr/share/cds/US");
TDate date_prev = _previous_twentieth(date1, true, cal);
if ((date_prev == date) && include_cashflow) {
date_prev = _previous_twentieth(date - 1, true, cal);
}
pfree(currency);
PG_RETURN_FLOAT8((date - date_prev) / 360. * coupon);
}
PG_FUNCTION_INFO_V1(test);
Datum cds_enrich(PG_FUNCTION_ARGS) {
if (SPI_connect() == SPI_ERROR_CONNECT) {
elog(ERROR, "pomme");
}
const text* redindexcode = PG_GETARG_TEXT_PP(0);
DateADT maturity = PG_GETARG_DATEADT(1);
float8 traded_level = PG_GETARG_FLOAT8(2);
DateADT trade_datge = PG_GETARG_DATEADT(3);
char* sql_query = "SELECT index, series, tenor::text, coupon, issue_date, indexfactor/100, "
"version, cumulativeloss "
"FROM index_desc "
"WHERE redindexcode=$1 AND maturity=$2";
int ret;
uint64 proc;
int nargs = 2;
Oid argtypes[2] = {TEXTOID, DATEOID};
char nulls[2] = " ";
Datum values[2];
values[0] = PointerGetDatum(redindexcode);
values[1] = DateADTGetDatum(maturity);
ret = SPI_execute_with_args(sql_query, nargs, argtypes, values, nulls, true, 1);
proc = SPI_processed;
short series, version;
int coupon;
char *index, *tenor;
TDate issue_date;
double factor, cumulativeloss;
if (ret == SPI_OK_SELECT && SPI_tuptable != NULL) {
SPITupleTable *tuptable = SPI_tuptable;
TupleDesc tupdesc = tuptable->tupdesc;
bool isnull;
HeapTuple tuple = tuptable->vals[0];
index = text_to_cstring(DatumGetTextPP(SPI_getbinval(tuple, tupdesc, 1, &isnull)));
series = DatumGetInt16(SPI_getbinval(tuple, tupdesc, 2, &isnull));
tenor = text_to_cstring(DatumGetTextPP(SPI_getbinval(tuple, tupdesc, 3, &isnull)));
coupon = DatumGetInt32(SPI_getbinval(tuple, tupdesc, 4, &isnull));
issue_date = TDate_from_DateADT(DatumGetDateADT(SPI_getbinval(tuple, tupdesc, 5, &isnull)));
factor = DatumGetFloat8(SPI_getbinval(tuple, tupdesc, 6, &isnull));
version = DatumGetInt16(SPI_getbinval(tuple, tupdesc, 7, &isnull));
cumulativeloss = DatumGetFloat8(SPI_getbinval(tuple, tupdesc, 8, &isnull));
}
char* yc_query
SPI_finish();
pfree(index);
pfree(tenor);
PG_RETURN_NULL();
}
|