Ejemplo n.º 1
0
/*f
***************************************************************************
** Calculates a date being at a business or calendar days offset from the 
** input date.
***************************************************************************
*/
int JpmcdsDtFwdAdj
   (TDate          startDate,    /* (I) input date       */
    TDateAdjIntvl *adjIvl,       /* (I) in business or calendar days  */
    TDate         *result)       /* (O) resulting date   */
{
    int         status = FAILURE;
    static char routine[]="JpmcdsDtFwdAdj";

    switch (adjIvl->isBusDays)
    {
    case JPMCDS_DATE_ADJ_TYPE_BUSINESS:  /* offset by business days */
        if (adjIvl->interval.prd_typ == 'D')
        {
            if (JpmcdsDateFromBusDaysOffset
                (startDate,
                 adjIvl->interval.prd, /* Number days */
                 adjIvl->holidayFile,
                 result) != SUCCESS)
            {
                goto done;
            }
        }
        else
        {
            /*
            ** Non-daily interval and offset by business days.
            ** This means that if the start date is the end of the business
            ** month, then the end date must also be the end of the month.
            ** Actually we do adjust the end date by the bad day convention,
            ** but in the context of generating a set of cash flow dates,
            ** this is often not done when computing the maturity date.
            */
            TDate businessEOM;
            TDate endDate;
            
            if (JpmcdsDateToBusinessEOM (startDate,
                                      adjIvl->holidayFile,
                                      &businessEOM) != SUCCESS)
            {
                goto done;
            }

            if (JpmcdsDtFwdAny (startDate,
                             &adjIvl->interval,
                             &endDate) != SUCCESS)
            {
                goto done;
            }

            if (startDate == businessEOM)
            {
                if (JpmcdsDateToEOM (endDate, &endDate) != SUCCESS)
                    goto done;
            }

            if (JpmcdsBusinessDay (endDate, adjIvl->badDayConv,
                                adjIvl->holidayFile, result) != SUCCESS)
            {
                goto done;
            }
        }
        break;

    case JPMCDS_DATE_ADJ_TYPE_CALENDAR:
        /* offset by calendar period */
        if (JpmcdsDateFwdThenAdjust
            (startDate, &adjIvl->interval,
             adjIvl->badDayConv, adjIvl->holidayFile, result) != SUCCESS)
        {
            goto done;
        }
        break;

    case JPMCDS_DATE_ADJ_TYPE_WEEKDAY:
        /* Adjust interval as weekdays, and then bad day adjust the result. */
        if (adjIvl->interval.prd_typ == 'D')
        {
            if (JpmcdsDateFromBusDaysOffset
                (startDate,
                 adjIvl->interval.prd, /* Number days */
                 "NONE", /* A holiday file with week-ends only */
                 result) != SUCCESS)
            {
                goto done;
            }
            if (JpmcdsBusinessDay (*result, 
                                adjIvl->badDayConv, 
                                adjIvl->holidayFile,
                                result) != SUCCESS)
            {
                goto done;
            }
        }
        else
        {
            JpmcdsErrMsg ("%s: Weekday adjustment requires date interval "
                       "%s to be in days.\n",
                       routine, JpmcdsFormatDateInterval (&adjIvl->interval));
            goto done;
        }
        break;

    default:
        JpmcdsErrMsg ("%s: Invalid date adjustment type %d.\n", routine, adjIvl->isBusDays);
        goto done;
    }
   
    status = SUCCESS;

done:
    
    if (status != SUCCESS)
        JpmcdsErrMsg("%s: Failed.\n", routine);

    return status;
}  
Ejemplo n.º 2
0
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;
}
Ejemplo n.º 3
0
//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;
}
Ejemplo n.º 4
0
/*
***************************************************************************
** 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;
}