/* *************************************************************************** ** Clears all objects from cache. *************************************************************************** */ void FreeObjects() { TObject *next = cache; TObject *node; while (next) { node = next; next = (TObject*) node->next; FREE(node->name); JpmcdsFreeTCurve((TCurve*)(node->data)); FREE(node); } }
/* *************************************************************************** ** Build zero curve from money market, and swap instruments. *************************************************************************** */ EXPORT TCurve* JpmcdsBuildIRZeroCurve( TDate valueDate, /* (I) Value date */ char *instrNames, /* (I) Array of 'M' or 'S' */ TDate *dates, /* (I) Array of swaps dates */ double *rates, /* (I) Array of swap rates */ long nInstr, /* (I) Number of benchmark instruments */ long mmDCC, /* (I) DCC of MM instruments */ long fixedSwapFreq, /* (I) Fixed leg freqency */ long floatSwapFreq, /* (I) Floating leg freqency */ long fixedSwapDCC, /* (I) DCC of fixed leg */ long floatSwapDCC, /* (I) DCC of floating leg */ long badDayConv, /* (I) Bad day convention */ char *holidayFile) /* (I) Holiday file */ { static char routine[] = "BuildIRZeroCurve"; int status = FAILURE; int i; int nCash = 0; int nSwap = 0; char fwdLength = '3'; /* not used */ char instr; TDate *cashDates = NULL; TDate *swapDates = NULL; double *cashRates = NULL; double *swapRates = NULL; TCurve *zcurveIni = NULL; TCurve *zcurveCash = NULL; TCurve *zcurveSwap = NULL; /* Allocate enough spaces for cash and swap dates/rates */ cashDates = NEW_ARRAY(TDate, nInstr); swapDates = NEW_ARRAY(TDate, nInstr); cashRates = NEW_ARRAY(double, nInstr); swapRates = NEW_ARRAY(double, nInstr); if (cashDates == NULL || swapDates == NULL || cashRates == NULL || swapRates == NULL) goto done; /* Sort out cash and swap separately */ for(i = 0; i < nInstr; i++) { instr = toupper(instrNames[i]); if (instr != JpmcdsMONEYNAME && instr != JpmcdsSWAPNAME) { JpmcdsErrMsg("%s: unknown instrument type (%c)." " Only (M)oney market or (S)wap is allowed.\n", routine, instrNames[i]); goto done; } if (instr == JpmcdsMONEYNAME) { /* MM Rate */ cashDates[nCash] = dates[i]; cashRates[nCash] = rates[i]; nCash++; } else /* Swap Rate */ { swapDates[nSwap] = dates[i]; swapRates[nSwap] = rates[i]; nSwap++; } } /* Initialize the zero curve */ zcurveIni = JpmcdsNewTCurve(valueDate, 0, (double) 1L, JPMCDS_ACT_365F); if (zcurveIni == NULL){ goto done; printf("zcurveIni...\n"); } /* Cash instruments */ zcurveCash = JpmcdsZCCash(zcurveIni, cashDates, cashRates, nCash, mmDCC); if (zcurveCash == NULL){ JpmcdsErrMsg("Cash Curve not available ... \n"); goto done; } /* Swap instruments */ zcurveSwap = JpmcdsZCSwaps(zcurveCash, NULL, /* discZC */ swapDates, swapRates, nSwap, fixedSwapFreq, floatSwapFreq, fixedSwapDCC, floatSwapDCC, fwdLength, badDayConv, holidayFile); if (zcurveSwap == NULL){ JpmcdsErrMsg("IR curve not available ... \n"); // printf("g...\n"); goto done; } status = SUCCESS; done: FREE(cashDates); FREE(cashRates); FREE(swapDates); FREE(swapRates); JpmcdsFreeTCurve(zcurveIni); JpmcdsFreeTCurve(zcurveCash); if (status != SUCCESS) { JpmcdsFreeTCurve(zcurveSwap); zcurveSwap = NULL; JpmcdsErrMsgFailure(routine); } return (zcurveSwap); }
/* *************************************************************************** ** Adds cash points to a given zero curve. *************************************************************************** */ TCurve* JpmcdsZCCash( TCurve *zeroCurve, /* (I) Zero curve to add to */ TDate *dates, /* (I) Array of cash dates */ double *rates, /* (I) Array of cash rates */ int numRates, /* (I) Number of cash rates to add */ long dayCountConv) /* (I) See JpmcdsDayCountConvention */ { static char routine[] = "JpmcdsZCCash"; int status = FAILURE; TCurve *tcCash = NULL; ZCurve *zcCash = NULL; ZCurve *zcStub = NULL; if (numRates == 0) /* nothing to add */ { tcCash = JpmcdsCopyCurve (zeroCurve); return tcCash; } if (CheckZCCashInputs(zeroCurve, dates, rates, numRates, dayCountConv) == FAILURE) goto done; /* convert the input TCurve into a ZCurve so we can call all the usual zero curve routines */ if (zeroCurve->fNumItems > 0) /* only want it if we need it */ { zcStub = JpmcdsZCFromTCurve(zeroCurve); if (zcStub == NULL) { JpmcdsErrMsg("%s: failed to make ZCurve structure from input TCurve.\n", routine); goto done; } } zcCash = JpmcdsZCMake(zeroCurve->fBaseDate, numRates + 2, ZC_DEFAULT_BASIS, ZC_DEFAULT_DAYCNT); if (zcCash == NULL) { JpmcdsErrMsg("%s: couldn't make Zero Curve for cash.\n", routine); goto done; } if (JpmcdsZCAddMoneyMarket(zcCash, dates, rates, numRates, dayCountConv) == FAILURE) { JpmcdsErrMsg("%s: Adding cash instruments failed.\n", routine); goto done; } /* combine the stub curve with the cash part */ if (zcStub != NULL) { if (JpmcdsZCAddPrefixCurve(zcCash, zcStub) == FAILURE) { JpmcdsErrMsg("%s: Error adding Cash and stub zero curve together.\n", routine); goto done; } if (JpmcdsZCAddSuffixCurve(zcCash, zcStub) == FAILURE) { JpmcdsErrMsg("%s: Error adding cash and stub zero curve together(2).\n", routine); goto done; } } tcCash = JpmcdsZCToTCurve(zcCash); if (tcCash == NULL) { JpmcdsErrMsg("%s: couldn't make a TCurve copy of Cash zero curve.\n", routine); goto done; } status = SUCCESS; goto done; done: if (status == FAILURE) { JpmcdsFreeTCurve(tcCash); tcCash = NULL; JpmcdsErrMsg("%s: Failed.\n", routine); } JpmcdsZCFree(zcCash); JpmcdsZCFree(zcStub); return tcCash; }
/* *************************************************************************** ** Adds swap points to a given zero curve. *************************************************************************** */ TCurve* JpmcdsZCSwaps( TCurve *zeroCurve, /* (I) Zero curve to add swap points to*/ TCurve *discZC, /* (I) Zero curve used for discounting */ TDate *dates, /* (I) Unadjusted swap maturity dates */ double *rates, /* (I) Swap par fixed rates (0.06=6%) */ int numSwaps, /* (I) Len of dates,rates,prices */ int fixedSwapFreq, /* (I) Fixed leg coupon frequency */ int floatSwapFreq, /* (I) Floating leg coupon frequency */ long fixDayCountConv, /* (I) See JpmcdsDayCountConvention */ long floatDayCountConv, /* (I) See JpmcdsDayCountConvention */ char fwdLength, /* (I) For fwd smoothing, length of fwds*/ TBadDayAndStubPos badDayAndStubPos, /* (I) Bad day conv + stub pos */ char *holidayFile) /* (I) See JpmcdsBusinessDay */ { static char routine[] = "JpmcdsZCSwaps"; int status=FAILURE; /* until proven successful */ int offset; /* offset in array of swap rates */ TInterpData *interpData=NULL; /* interpolation data for zero rates */ TDate lastStubDate; TCurve *tcSwaps = NULL; ZCurve *zcSwaps = NULL; TBadDayList *badDayList = NULL; long badDayConv; TStubPos stubPos; if (numSwaps == 0) /* nothing to add */ { tcSwaps = JpmcdsCopyCurve (zeroCurve); return tcSwaps; } if (JpmcdsBadDayAndStubPosSplit(badDayAndStubPos, &badDayConv, &stubPos) != SUCCESS) goto done; /* Check inputs */ if (CheckZCSwapsInputs( zeroCurve, discZC, dates, rates, numSwaps, fixedSwapFreq, floatSwapFreq, fixDayCountConv, floatDayCountConv, fwdLength, badDayConv, holidayFile)==FAILURE) { goto done; } /* convert the input TCurve into a ZCurve so we can call all the usual zero curve routines */ zcSwaps = JpmcdsZCFromTCurve(zeroCurve); if (zcSwaps == NULL) { JpmcdsErrMsg("%s: failed to make ZCurve structure from input TCurve.\n", routine); goto done; } /* only want to add swap points not already covered by stub curve, don't want to replace anything in the existing curve */ if (zeroCurve->fNumItems < 1) { lastStubDate = zeroCurve->fBaseDate; } else { lastStubDate = zeroCurve->fArray[zeroCurve->fNumItems-1].fDate; } offset = 0; while (numSwaps > 0 && dates[offset] < lastStubDate) { offset++; numSwaps--; } if (numSwaps > 0) { if (JpmcdsZCAddSwaps(zcSwaps, discZC, &dates[offset], &rates[offset], numSwaps, fixedSwapFreq, floatSwapFreq, fixDayCountConv, floatDayCountConv, JPMCDS_FLAT_FORWARDS, interpData, badDayList, badDayAndStubPos, holidayFile)==FAILURE) { goto done; } } tcSwaps = JpmcdsZCToTCurve(zcSwaps); if (tcSwaps == NULL) { JpmcdsErrMsg("%s: couldn't make a TCurve copy of swaps zero curve.\n", routine); goto done; } status = SUCCESS; goto done; done: FREE(interpData); if (status == FAILURE) { JpmcdsFreeTCurve(tcSwaps); tcSwaps = NULL; JpmcdsErrMsg("%s: Failed.\n", routine); } JpmcdsZCFree(zcSwaps); return tcSwaps; }
/* *************************************************************************** ** Store data for addin object. Returns handle (NULL if error). *************************************************************************** */ char* StoreObject(char* name, void* data) { static char *routine = "StoreObject"; int status = FAILURE; char *handle = NULL; TObject *node; static char buffer[255]; if (data == NULL) { JpmcdsErrMsg("%s: No data to store provided.\n", routine); goto done; } if (name == NULL) { JpmcdsErrMsg("%s: No object name provided.\n", routine); goto done; } if (strlen(name) > 200) { JpmcdsErrMsg("%s: Object name cannot exceed 200 characters.\n", routine); goto done; } node = FindNode(name); if (node == NULL) { node = NEW(TObject); if (node == NULL) goto done; node->name = strdup(name); node->version = 1; node->data = data; node->next = cache; cache = node; } else { node->version += 1; JpmcdsFreeTCurve((TCurve*)(node->data)); node->data = data; } sprintf(buffer, "%s%c%d\0", node->name, SEPARATOR, node->version); handle = strdup(buffer); if (handle == NULL) goto done; strcpy(handle, buffer); status = SUCCESS; done: if (status != SUCCESS) { JpmcdsErrMsg("%s: Failed!\n", routine); FREE(handle); handle = NULL; } return handle; }