//EXPORT int JpmcdsCdsoneUpfrontCharge(cdsone.c) SEXP calcUpfrontTest (SEXP baseDate_input, /* (I) Value date for zero curve */ SEXP types, /* "MMMMMSSSSSSSSS"*/ SEXP rates, /* rates[14] = {1e-9, 1e-9, 1e-9, 1e-9, 1e-9, 1e-9, 1e-9, 1e-9, 1e-9, 1e-9, 1e-9, 1e-9, 1e-9, 1e-9};/\* (I) Array of swap rates *\/ */ SEXP expiries, SEXP mmDCC, /* (I) DCC of MM instruments */ SEXP fixedSwapFreq, /* (I) Fixed leg freqency/interval */ SEXP floatSwapFreq, /* (I) Floating leg freqency/interval */ SEXP fixedSwapDCC, /* (I) DCC of fixed leg */ SEXP floatSwapDCC, /* (I) DCC of floating leg */ SEXP badDayConvZC, //'M' badDayConv for zero curve SEXP holidays,//'None' // input for upfront charge calculation SEXP todayDate_input, /*today: T (Where T = trade date)*/ SEXP valueDate_input, /* value date: T+3 Business Days*/ SEXP benchmarkDate_input,/* start date of benchmark CDS for internal ** clean spread bootstrapping; ** accrual Begin Date */ SEXP startDate_input,/* Accrual Begin Date */ SEXP endDate_input,/* Maturity (Fixed) */ SEXP stepinDate_input, /* T + 1*/ SEXP dccCDS, /* accruedDcc */ SEXP ivlCDS, SEXP stubCDS, SEXP badDayConvCDS, SEXP calendar, SEXP parSpread, SEXP couponRate, SEXP recoveryRate, SEXP isPriceClean_input, SEXP payAccruedOnDefault_input, SEXP notional) { // static char routine[] = "JpmcdsCdsoneUpfrontCharge"; // my vars int n; TDate baseDate, today, benchmarkDate, startDate, endDate, stepinDate,valueDate; int isPriceClean, payAccruedOnDefault; SEXP upfrontPayment; TCurve *discCurve = NULL; char* pt_types; char* pt_holidays; char* pt_mmDCC; char* pt_fixedSwapDCC; char* pt_floatSwapDCC; char* pt_fixedSwapFreq; char* pt_floatSwapFreq; char* pt_dccCDS; char* pt_ivlCDS; char* pt_stubCDS; char* pt_calendar; char* pt_badDayConvCDS; // new char *pt_badDayConvZC; double parSpread_for_upf, couponRate_for_upf, recoveryRate_for_upf, notional_for_upf; // function to consolidate R input to TDate baseDate_input = coerceVector(baseDate_input,INTSXP); baseDate = JpmcdsDate((long)INTEGER(baseDate_input)[0], (long)INTEGER(baseDate_input)[1], (long)INTEGER(baseDate_input)[2]); todayDate_input = coerceVector(todayDate_input,INTSXP); today = JpmcdsDate((long)INTEGER(todayDate_input)[0], (long)INTEGER(todayDate_input)[1], (long)INTEGER(todayDate_input)[2]); valueDate_input = coerceVector(valueDate_input,INTSXP); valueDate = JpmcdsDate((long)INTEGER(valueDate_input)[0], (long)INTEGER(valueDate_input)[1], (long)INTEGER(valueDate_input)[2]); benchmarkDate_input = coerceVector(benchmarkDate_input,INTSXP); benchmarkDate = JpmcdsDate((long)INTEGER(benchmarkDate_input)[0], (long)INTEGER(benchmarkDate_input)[1], (long)INTEGER(benchmarkDate_input)[2]); startDate_input = coerceVector(startDate_input,INTSXP); startDate = JpmcdsDate((long)INTEGER(startDate_input)[0], (long)INTEGER(startDate_input)[1], (long)INTEGER(startDate_input)[2]); endDate_input = coerceVector(endDate_input,INTSXP); endDate = JpmcdsDate((long)INTEGER(endDate_input)[0], (long)INTEGER(endDate_input)[1], (long)INTEGER(endDate_input)[2]); stepinDate_input = coerceVector(stepinDate_input,INTSXP); stepinDate = JpmcdsDate((long)INTEGER(stepinDate_input)[0], (long)INTEGER(stepinDate_input)[1], (long)INTEGER(stepinDate_input)[2]); types = coerceVector(types, STRSXP); pt_types = (char *) CHAR(STRING_ELT(types,0)); holidays = coerceVector(holidays, STRSXP); pt_holidays = (char *) CHAR(STRING_ELT(holidays, 0)); n = strlen(CHAR(STRING_ELT(types, 0))); // for zerocurve rates = coerceVector(rates,REALSXP); mmDCC = coerceVector(mmDCC, STRSXP); pt_mmDCC = (char *) CHAR(STRING_ELT(mmDCC,0)); fixedSwapFreq = coerceVector(fixedSwapFreq, STRSXP); pt_fixedSwapFreq = (char *) CHAR(STRING_ELT(fixedSwapFreq,0)); floatSwapFreq = coerceVector(floatSwapFreq, STRSXP); pt_floatSwapFreq = (char *) CHAR(STRING_ELT(floatSwapFreq,0)); fixedSwapDCC = coerceVector(fixedSwapDCC, STRSXP); pt_fixedSwapDCC = (char *) CHAR(STRING_ELT(fixedSwapDCC,0)); floatSwapDCC = coerceVector(floatSwapDCC, STRSXP); pt_floatSwapDCC = (char *) CHAR(STRING_ELT(floatSwapDCC,0)); calendar = coerceVector(calendar, STRSXP); pt_calendar = (char *) CHAR(STRING_ELT(calendar,0)); parSpread_for_upf = *REAL(parSpread); couponRate_for_upf = *REAL(couponRate); recoveryRate_for_upf = *REAL(recoveryRate); isPriceClean = *INTEGER(isPriceClean_input); payAccruedOnDefault = *INTEGER(payAccruedOnDefault_input); notional_for_upf = *REAL(notional); badDayConvZC = coerceVector(badDayConvZC, STRSXP); pt_badDayConvZC = (char *) CHAR(STRING_ELT(badDayConvZC,0)); badDayConvCDS = coerceVector(badDayConvCDS, STRSXP); pt_badDayConvCDS = (char *) CHAR(STRING_ELT(badDayConvCDS,0)); TDateInterval fixedSwapIvl_curve; TDateInterval floatSwapIvl_curve; long fixedSwapDCC_curve; long floatSwapDCC_curve; double fixedSwapFreq_curve; double floatSwapFreq_curve; long mmDCC_zc_main; static char *routine_zc_main = "BuildExampleZeroCurve"; if (JpmcdsStringToDayCountConv(pt_mmDCC, &mmDCC_zc_main) != SUCCESS) goto done; if (JpmcdsStringToDayCountConv(pt_fixedSwapDCC, &fixedSwapDCC_curve) != SUCCESS) goto done; if (JpmcdsStringToDayCountConv(pt_floatSwapDCC, &floatSwapDCC_curve) != SUCCESS) goto done; if (JpmcdsStringToDateInterval(pt_fixedSwapFreq, routine_zc_main, &fixedSwapIvl_curve) != SUCCESS) goto done; if (JpmcdsStringToDateInterval(pt_floatSwapFreq, routine_zc_main, &floatSwapIvl_curve) != SUCCESS) goto done; if (JpmcdsDateIntervalToFreq(&fixedSwapIvl_curve, &fixedSwapFreq_curve) != SUCCESS) goto done; if (JpmcdsDateIntervalToFreq(&floatSwapIvl_curve, &floatSwapFreq_curve) != SUCCESS) goto done; expiries = coerceVector(expiries, VECSXP); TDate *dates_main = NULL; dates_main = NEW_ARRAY1(TDate, n); int i; for (i = 0; i < n; i++) { TDateInterval tmp; if (JpmcdsStringToDateInterval(strdup(CHAR(asChar(VECTOR_ELT(expiries, i)))), routine_zc_main, &tmp) != SUCCESS) { JpmcdsErrMsg ("%s: invalid interval for element[%d].\n", routine_zc_main, i); goto done; } if (JpmcdsDateFwdThenAdjust(baseDate, &tmp, JPMCDS_BAD_DAY_NONE, "None", dates_main+i) != SUCCESS) { JpmcdsErrMsg ("%s: invalid interval for element[%d].\n", routine_zc_main, i); goto done; } } discCurve = JpmcdsBuildIRZeroCurve(baseDate, pt_types, dates_main, REAL(rates), (long)n, (long) mmDCC_zc_main, (long) fixedSwapFreq_curve, (long) floatSwapFreq_curve, fixedSwapDCC_curve, floatSwapDCC_curve, (char) *pt_badDayConvZC, pt_holidays); if (discCurve == NULL) JpmcdsErrMsg("IR curve not available ... \n"); dccCDS = coerceVector(dccCDS, STRSXP); pt_dccCDS = (char *) CHAR(STRING_ELT(dccCDS,0)); ivlCDS = coerceVector(ivlCDS, STRSXP); pt_ivlCDS = (char *) CHAR(STRING_ELT(ivlCDS,0)); stubCDS = coerceVector(stubCDS, STRSXP); pt_stubCDS = (char *) CHAR(STRING_ELT(stubCDS,0)); static char *routine = "CalcUpfrontCharge"; TDateInterval ivl; TStubMethod stub; long dcc; if (JpmcdsStringToDayCountConv(pt_dccCDS, &dcc) != SUCCESS) goto done; if (JpmcdsStringToDateInterval(pt_ivlCDS, routine, &ivl) != SUCCESS) goto done; if (JpmcdsStringToStubMethod(pt_stubCDS, &stub) != SUCCESS) goto done; double result = -1.0; PROTECT(upfrontPayment = allocVector(REALSXP, 1)); if (JpmcdsCdsoneUpfrontCharge(today, valueDate, benchmarkDate, stepinDate, startDate, endDate, couponRate_for_upf / 10000.0, payAccruedOnDefault, //TRUE, &ivl, &stub, dcc, (char) *pt_badDayConvCDS, pt_calendar, discCurve, parSpread_for_upf/10000.0, recoveryRate_for_upf, isPriceClean, &result) != SUCCESS) goto done; done: REAL(upfrontPayment)[0] = result * notional_for_upf; UNPROTECT(1); FREE(dates_main); return upfrontPayment; }
SEXP calcCdsoneSpread (// variables for the zero curve SEXP baseDate_input, /* (I) Value date for zero curve */ SEXP types, //"MMMMMSSSSSSSSS" SEXP rates, /* rates[14] = {1e-9, 1e-9, 1e-9, 1e-9, 1e-9, 1e-9, 1e-9, 1e-9, 1e-9, 1e-9, 1e-9, 1e-9, 1e-9, 1e-9};/\* (I) Array of swap rates *\/ */ SEXP expiries, SEXP mmDCC, /* (I) DCC of MM instruments */ SEXP fixedSwapFreq, /* (I) Fixed leg freqency */ SEXP floatSwapFreq, /* (I) Floating leg freqency */ SEXP fixedSwapDCC, /* (I) DCC of fixed leg */ SEXP floatSwapDCC, /* (I) DCC of floating leg */ SEXP badDayConvZC, //'M' badDayConv for zero curve SEXP holidays,//'None' SEXP todayDate_input, /*today: T (Where T = trade date)*/ SEXP valueDate_input, /* value date: T+3 Business Days*/ SEXP benchmarkDate_input, /* start date of benchmark CDS for ** internal clean spread bootstrapping; ** accrual Begin Date */ SEXP startDate_input, /* Accrual Begin Date */ SEXP endDate_input, /* Maturity (Fixed) */ SEXP stepinDate_input, /* T + 1*/ SEXP couponRate_input, /* Fixed Coupon Amt */ SEXP payAccruedOnDefault_input, /* TRUE in new contract */ SEXP dccCDS, //accrueDCC_input, /* ACT/360 */ SEXP dateInterval, /* Q - 3 months in new contract */ SEXP stubType, /* F/S */ SEXP badDayConv_input, /* (F) Following */ SEXP calendar_input, /* None (no holiday calendar) in new contract */ SEXP upfrontCharge_input, SEXP recoveryRate_input, SEXP payAccruedAtStart_input /* (True/False), True: Clean Upfront supplied */ ) { static char routine[] = "JpmcdsCdsoneSpread"; SEXP spread; TDate baseDate, todayDate, benchmarkDate, startDate, endDate, stepinDate,valueDate; int n; /* for zero curve */ TCurve *discCurve = NULL; char* pt_types; char* pt_holidays; char* pt_mmDCC; char* pt_fixedSwapDCC; char* pt_floatSwapDCC; char* pt_fixedSwapFreq; char* pt_floatSwapFreq; TBoolean payAccruedOnDefault, payAccruedAtStart; double couponRate, upfrontCharge, recoveryRate; char* pt_dccCDS; char* pt_stubType; char* pt_dateInterval; char* pt_calendar; char* pt_badDayConvCDS; const char* badDayConvZC_char; double* pt_onespread; pt_onespread = malloc(sizeof(double)); CDSONE_SPREAD_CONTEXT context; baseDate_input = coerceVector(baseDate_input,INTSXP); baseDate = JpmcdsDate((long)INTEGER(baseDate_input)[0], (long)INTEGER(baseDate_input)[1], (long)INTEGER(baseDate_input)[2]); todayDate_input = coerceVector(todayDate_input,INTSXP); todayDate = JpmcdsDate((long)INTEGER(todayDate_input)[0], (long)INTEGER(todayDate_input)[1], (long)INTEGER(todayDate_input)[2]); valueDate_input = coerceVector(valueDate_input,INTSXP); valueDate = JpmcdsDate((long)INTEGER(valueDate_input)[0], (long)INTEGER(valueDate_input)[1], (long)INTEGER(valueDate_input)[2]); benchmarkDate_input = coerceVector(benchmarkDate_input,INTSXP); benchmarkDate = JpmcdsDate((long)INTEGER(benchmarkDate_input)[0], (long)INTEGER(benchmarkDate_input)[1], (long)INTEGER(benchmarkDate_input)[2]); stepinDate_input = coerceVector(stepinDate_input,INTSXP); stepinDate = JpmcdsDate((long)INTEGER(stepinDate_input)[0], (long)INTEGER(stepinDate_input)[1], (long)INTEGER(stepinDate_input)[2]); startDate_input = coerceVector(startDate_input,INTSXP); startDate = JpmcdsDate((long)INTEGER(startDate_input)[0], (long)INTEGER(startDate_input)[1], (long)INTEGER(startDate_input)[2]); endDate_input = coerceVector(endDate_input,INTSXP); endDate = JpmcdsDate((long)INTEGER(endDate_input)[0], (long)INTEGER(endDate_input)[1], (long)INTEGER(endDate_input)[2]); types = coerceVector(types, STRSXP); pt_types = (char *) CHAR(STRING_ELT(types, 0)); holidays = coerceVector(holidays, STRSXP); pt_holidays = (char *) CHAR(STRING_ELT(holidays, 0)); n = strlen(CHAR(STRING_ELT(types, 0))); // for zerocurve rates = coerceVector(rates,REALSXP); mmDCC = coerceVector(mmDCC, STRSXP); pt_mmDCC = (char *) CHAR(STRING_ELT(mmDCC,0)); fixedSwapFreq = coerceVector(fixedSwapFreq, STRSXP); pt_fixedSwapFreq = (char *) CHAR(STRING_ELT(fixedSwapFreq,0)); floatSwapFreq = coerceVector(floatSwapFreq, STRSXP); pt_floatSwapFreq = (char *) CHAR(STRING_ELT(floatSwapFreq,0)); fixedSwapDCC = coerceVector(fixedSwapDCC, STRSXP); pt_fixedSwapDCC = (char *) CHAR(STRING_ELT(fixedSwapDCC,0)); floatSwapDCC = coerceVector(floatSwapDCC, STRSXP); pt_floatSwapDCC = (char *) CHAR(STRING_ELT(floatSwapDCC,0)); /* fixedSwapFreq = coerceVector(fixedSwapFreq,REALSXP); */ /* floatSwapFreq = coerceVector(floatSwapFreq,REALSXP); */ /* fixedSwapDCC = coerceVector(fixedSwapDCC,REALSXP); */ /* floatSwapDCC = coerceVector(floatSwapDCC,REALSXP); */ /* badDayConvZC = AS_CHARACTER(badDayConvZC); */ /* pt_badDayConvZC = CHAR(asChar(STRING_ELT(badDayConvZC, 0))); */ badDayConvZC = AS_CHARACTER(badDayConvZC); badDayConvZC_char = CHAR(asChar(STRING_ELT(badDayConvZC, 0))); // main.c dates /* TDateInterval ivl; */ /* long dcc; */ /* double freq; */ TDateInterval fixedSwapIvl_curve; TDateInterval floatSwapIvl_curve; long fixedSwapDCC_curve; long floatSwapDCC_curve; double fixedSwapFreq_curve; double floatSwapFreq_curve; long mmDCC_zc_main; static char *routine_zc_main = "BuildExampleZeroCurve"; /* if (JpmcdsStringToDayCountConv("Act/360", &mmDCC_zc_main) != SUCCESS) */ /* goto done; */ if (JpmcdsStringToDayCountConv(pt_mmDCC, &mmDCC_zc_main) != SUCCESS) goto done; /* if (JpmcdsStringToDayCountConv("30/360", &dcc) != SUCCESS) */ /* goto done; */ if (JpmcdsStringToDayCountConv(pt_fixedSwapDCC, &fixedSwapDCC_curve) != SUCCESS) goto done; if (JpmcdsStringToDayCountConv(pt_floatSwapDCC, &floatSwapDCC_curve) != SUCCESS) goto done; /* if (JpmcdsStringToDateInterval("6M", routine_zc_main, &ivl) != SUCCESS) */ /* goto done; */ if (JpmcdsStringToDateInterval(pt_fixedSwapFreq, routine_zc_main, &fixedSwapIvl_curve) != SUCCESS) goto done; if (JpmcdsStringToDateInterval(pt_floatSwapFreq, routine_zc_main, &floatSwapIvl_curve) != SUCCESS) goto done; /* if (JpmcdsDateIntervalToFreq(&ivl, &freq) != SUCCESS) */ /* goto done; */ if (JpmcdsDateIntervalToFreq(&fixedSwapIvl_curve, &fixedSwapFreq_curve) != SUCCESS) goto done; if (JpmcdsDateIntervalToFreq(&floatSwapIvl_curve, &floatSwapFreq_curve) != SUCCESS) goto done; expiries = coerceVector(expiries, VECSXP); TDate *dates_main = NULL; dates_main = NEW_ARRAY1(TDate, n); int i; for (i = 0; i < n; i++) { TDateInterval tmp; // if (JpmcdsStringToDateInterval(expiries[i], routine_zc_main, &tmp) != SUCCESS) /* if (JpmcdsStringToDateInterval(CHAR(asChar(VECTOR_ELT(expiries, i))), routine_zc_main, &tmp) != SUCCESS) { */ if (JpmcdsStringToDateInterval(strdup(CHAR(asChar(VECTOR_ELT(expiries, i)))), routine_zc_main, &tmp) != SUCCESS) { JpmcdsErrMsg ("%s: invalid interval for element[%d].\n", routine_zc_main, i); goto done; } if (JpmcdsDateFwdThenAdjust(baseDate, &tmp, JPMCDS_BAD_DAY_NONE, "None", dates_main+i) != SUCCESS) { JpmcdsErrMsg ("%s: invalid interval for element[%d].\n", routine_zc_main, i); goto done; } } // discCurve = (TCurve*)malloc(sizeof(TCurve)); discCurve = JpmcdsBuildIRZeroCurve(baseDate, pt_types, dates_main, REAL(rates), (long) n, (long) mmDCC_zc_main, (long) fixedSwapFreq_curve, (long) floatSwapFreq_curve, fixedSwapDCC_curve, floatSwapDCC_curve, (char) *badDayConvZC_char, pt_holidays); if (discCurve == NULL) JpmcdsErrMsg("IR curve not available ... \n"); couponRate = *REAL(couponRate_input); payAccruedOnDefault = *LOGICAL(payAccruedOnDefault_input); /* pt_dateInterval = (TDateInterval*)malloc(sizeof(TDateInterval)); */ /* pt_dateInterval->prd_typ = *CHAR(STRING_ELT(coerceVector(dateInterval, STRSXP), 0)); */ /* pt_dateInterval->prd = 6; */ /* pt_dateInterval->flag = 0; */ dateInterval = coerceVector(dateInterval, STRSXP); pt_dateInterval = (char *) CHAR(STRING_ELT(dateInterval,0)); /* TBoolean stubAtEnd = FALSE; */ /* TBoolean longStub = FALSE; */ /* pt_stubType = (TStubMethod*)malloc(sizeof(TStubMethod)); */ /* pt_stubType->stubAtEnd = stubAtEnd; */ /* pt_stubType->longStub = longStub; */ stubType = coerceVector(stubType, STRSXP); pt_stubType = (char *) CHAR(STRING_ELT(stubType,0)); /* accrueDCC = *INTEGER(dccCDS); */ /* badDayConv = *INTEGER(badDayConv_input); */ dccCDS = coerceVector(dccCDS, STRSXP); pt_dccCDS = (char *) CHAR(STRING_ELT(dccCDS,0)); badDayConv_input = coerceVector(badDayConv_input, STRSXP); pt_badDayConvCDS = (char *) CHAR(STRING_ELT(badDayConv_input,0)); TDateInterval ivl; TStubMethod stub; long dcc; if (JpmcdsStringToDayCountConv(pt_dccCDS, &dcc) != SUCCESS) goto done; if (JpmcdsStringToDateInterval(pt_dateInterval, routine, &ivl) != SUCCESS) goto done; if (JpmcdsStringToStubMethod(pt_stubType, &stub) != SUCCESS) goto done; pt_calendar = (char *) CHAR(STRING_ELT(coerceVector(calendar_input, STRSXP), 0)); upfrontCharge = *REAL(upfrontCharge_input); recoveryRate = *REAL(recoveryRate_input); payAccruedAtStart = *LOGICAL(payAccruedAtStart_input); context.today = todayDate; context.valueDate = valueDate; context.benchmarkDate = benchmarkDate; context.stepinDate = stepinDate; context.startDate = startDate; context.endDate = endDate; context.couponRate = couponRate; context.payAccruedOnDefault = payAccruedOnDefault; context.dateInterval = &ivl; context.stubType = &stub; /* context.accrueDCC = (long) accrueDCC; */ context.accrueDCC = dcc; // context.badDayConv = (long) badDayConv; context.badDayConv = *pt_badDayConvCDS; context.calendar = pt_calendar; context.discCurve = discCurve; context.upfrontCharge = upfrontCharge; context.recoveryRate = recoveryRate; context.payAccruedAtStart = payAccruedAtStart; if (JpmcdsRootFindBrent ((TObjectFunc)cdsoneSpreadSolverFunction, &context, 0.0, /* boundLo */ 100.0, /* boundHi */ 100, /* numIterations */ 0.01, /* guess */ 0.0001, /* initialXStep */ 0.0, /* initialFDeriv */ 1e-8, /* xacc */ 1e-8, /* facc */ pt_onespread) != SUCCESS) goto done; done: PROTECT(spread = allocVector(REALSXP, 1)); REAL(spread)[0] = (*pt_onespread) * 1e4; UNPROTECT(1); // if (status != SUCCESS) // JpmcdsErrMsgFailure (routine); return spread; }
/* *************************************************************************** ** Main function. *************************************************************************** */ int main(int argc, char** argv) { int status = 1; char version[256]; char **lines = NULL; int i; TCurve *zerocurve = NULL; if (JpmcdsVersionString(version) != SUCCESS) goto done; /* print library version */ printf("starting...\n"); printf("%s\n", version); /* enable logging */ printf("enabling logging...\n"); if (JpmcdsErrMsgEnableRecord(20, 128) != SUCCESS) /* ie. 20 lines, each of max length 128 */ goto done; /* construct IR zero curve */ printf("building zero curve...\n"); zerocurve = BuildExampleZeroCurve(); if (zerocurve == NULL) goto done; /* get discount factor */ printf("\n"); printf("Discount factor on 3rd Jan 08 = %f\n", JpmcdsZeroPrice(zerocurve, JpmcdsDate(2008,1,3))); printf("Discount factor on 3rd Jan 09 = %f\n", JpmcdsZeroPrice(zerocurve, JpmcdsDate(2009,1,3))); printf("Discount factor on 3rd Jan 17 = %f\n", JpmcdsZeroPrice(zerocurve, JpmcdsDate(2017,1,3))); /* get upfront charge */ printf("\n"); printf("Upfront charge @ cpn = 0bps = %f\n", CalcUpfrontCharge(zerocurve, 0)); printf("Upfront charge @ cpn = 3600bps = %f\n", CalcUpfrontCharge(zerocurve, 3600)); printf("Upfront charge @ cpn = 7200bps = %f\n", CalcUpfrontCharge(zerocurve, 7200)); /* return 'no error' */ status = 0; done: if (status != 0) printf("\n*** ERROR ***\n"); /* print error log contents */ printf("\n"); printf("Error log contains:\n"); printf("------------------:\n"); lines = JpmcdsErrGetMsgRecord(); if (lines == NULL) printf("(no log contents)\n"); else { for(i = 0; lines[i] != NULL; i++) { if (strcmp(lines[i],"") != 0) printf("%s\n", lines[i]); } } FREE(zerocurve); return status; }
/* *************************************************************************** ** Build IR zero curve. *************************************************************************** */ TCurve* BuildExampleZeroCurve() { static char *routine = "BuildExampleZeroCurve"; TCurve *zc = NULL; char *types = "MMMMMSSSSSSSSS"; char *expiries[14] = {"1M", "2M", "3M", "6M", "9M", "1Y", "2Y", "3Y", "4Y", "5Y", "6Y", "7Y", "8Y", "9Y"}; TDate *dates = NULL; double rates[14] = {1e-9, 1e-9, 1e-9, 1e-9, 1e-9, 1e-9, 1e-9, 1e-9, 1e-9, 1e-9, 1e-9, 1e-9, 1e-9, 1e-9}; TDate baseDate; long mmDCC; TDateInterval ivl; long dcc; double freq; char badDayConv = 'M'; char *holidays = "None"; int i, n; baseDate = JpmcdsDate(2008, 1, 3); if (JpmcdsStringToDayCountConv("Act/360", &mmDCC) != SUCCESS) goto done; if (JpmcdsStringToDayCountConv("30/360", &dcc) != SUCCESS) goto done; if (JpmcdsStringToDateInterval("6M", routine, &ivl) != SUCCESS) goto done; if (JpmcdsDateIntervalToFreq(&ivl, &freq) != SUCCESS) goto done; n = strlen(types); dates = NEW_ARRAY(TDate, n); for (i = 0; i < n; i++) { TDateInterval tmp; if (JpmcdsStringToDateInterval(expiries[i], routine, &tmp) != SUCCESS) { JpmcdsErrMsg ("%s: invalid interval for element[%d].\n", routine, i); goto done; } if (JpmcdsDateFwdThenAdjust(baseDate, &tmp, JPMCDS_BAD_DAY_NONE, "None", dates+i) != SUCCESS) { JpmcdsErrMsg ("%s: invalid interval for element[%d].\n", routine, i); goto done; } } printf("calling JpmcdsBuildIRZeroCurve...\n"); zc = JpmcdsBuildIRZeroCurve( baseDate, types, dates, rates, n, mmDCC, (long) freq, (long) freq, dcc, dcc, badDayConv, holidays); done: FREE(dates); return zc; }
/* *************************************************************************** ** Calculate upfront charge. *************************************************************************** */ double CalcUpfrontCharge(TCurve* curve, double couponRate) { static char *routine = "CalcUpfrontCharge"; TDate today; TDate valueDate; TDate startDate; TDate benchmarkStart; TDate stepinDate; TDate endDate; TBoolean payAccOnDefault = TRUE; TDateInterval ivl; TStubMethod stub; long dcc; double parSpread = 3600; double recoveryRate = 0.4; TBoolean isPriceClean = FALSE; double notional = 1e7; double result = -1.0; if (curve == NULL) { JpmcdsErrMsg("CalcUpfrontCharge: NULL IR zero curve passed\n"); goto done; } today = JpmcdsDate(2008, 2, 1); valueDate = JpmcdsDate(2008, 2, 1); benchmarkStart = JpmcdsDate(2008, 2, 2); startDate = JpmcdsDate(2008, 2, 8); endDate = JpmcdsDate(2008, 2, 12); stepinDate = JpmcdsDate(2008, 2, 9); if (JpmcdsStringToDayCountConv("Act/360", &dcc) != SUCCESS) goto done; if (JpmcdsStringToDateInterval("1S", routine, &ivl) != SUCCESS) goto done; if (JpmcdsStringToStubMethod("f/s", &stub) != SUCCESS) goto done; if (JpmcdsCdsoneUpfrontCharge(today, valueDate, benchmarkStart, stepinDate, startDate, endDate, couponRate / 10000.0, payAccOnDefault, &ivl, &stub, dcc, 'F', "None", curve, parSpread / 10000.0, recoveryRate, isPriceClean, &result) != SUCCESS) goto done; done: return result * notional; }