final SecuritySource securitySource = OpenGammaExecutionContext.getSecuritySource(executionContext);
final CreditDefaultSwapSecurityConverter converter = new CreditDefaultSwapSecurityConverter(holidaySource, regionSource, organizationSource);
final ZonedDateTime valuationTime = ZonedDateTime.now(executionContext.getValuationClock());
final CreditDefaultSwapSecurity security = (CreditDefaultSwapSecurity) target.getSecurity();
final Calendar calendar = new HolidaySourceCalendarAdapter(holidaySource, FinancialSecurityUtils.getCurrency(security));
LegacyVanillaCreditDefaultSwapDefinition cds = (LegacyVanillaCreditDefaultSwapDefinition) security.accept(converter);
cds = cds.withEffectiveDate(FOLLOWING.adjustDate(calendar, valuationTime.withHour(0).withMinute(0).withSecond(0).withNano(0).plusDays(1)));
final CdsRecoveryRateIdentifier recoveryRateIdentifier = security.accept(new CreditSecurityToRecoveryRateVisitor(securitySource));
Object recoveryRateObject = inputs.getValue(new ValueRequirement(MarketDataRequirementNames.MARKET_VALUE, ComputationTargetType.PRIMITIVE, recoveryRateIdentifier.getExternalId()));
if (recoveryRateObject == null) {
//throw new OpenGammaRuntimeException("Could not get recovery rate");
recoveryRateObject = 0.4;
}
cds = cds.withRecoveryRate((Double) recoveryRateObject);
final Object yieldCurveObject = inputs.getValue(ValueRequirementNames.YIELD_CURVE);
if (yieldCurveObject == null) {
throw new OpenGammaRuntimeException("Could not get yield curve");
}
final Object spreadCurveObject = inputs.getValue(ValueRequirementNames.CREDIT_SPREAD_CURVE);
if (spreadCurveObject == null) {
throw new OpenGammaRuntimeException("Could not get credit spread curve");
}
final ISDACompliantYieldCurve yieldCurve = (ISDACompliantYieldCurve) yieldCurveObject;
final NodalObjectsCurve<?, ?> spreadCurve = (NodalObjectsCurve<?, ?>) spreadCurveObject;
final Tenor[] tenors = CreditFunctionUtils.getTenors(spreadCurve.getXData());
final Double[] marketSpreadObjects = CreditFunctionUtils.getSpreads(spreadCurve.getYData());
ParallelArrayBinarySort.parallelBinarySort(tenors, marketSpreadObjects);
final int n = tenors.length;
// assume new style IMM maturities
final CDSAnalyticFactory analyticFactory = new CDSAnalyticFactory(cds.getRecoveryRate(), cds.getCouponFrequency().getPeriod())
.with(cds.getBusinessDayAdjustmentConvention())
.with(calendar).with(cds.getStubType())
.withAccualDCC(cds.getDayCountFractionConvention());
final CDSAnalytic pricingCDS = analyticFactory.makeCDS(valuationTime.toLocalDate(), cds.getEffectiveDate().toLocalDate(), cds.getMaturityDate().toLocalDate());
double spread = 0;
final ZonedDateTime[] times = new ZonedDateTime[n];
final CDSAnalytic[] creditAnalytics = new CDSAnalytic[n];
final double[] marketSpreads = new double[n];
for (int i = 0; i < n; i++) {
ZonedDateTime nextIMM = IMMDateGenerator.getNextIMMDate(valuationTime, tenors[i]).withHour(0).withMinute(0).withSecond(0).withNano(0);
creditAnalytics[i] = analyticFactory.makeCDS(valuationTime.toLocalDate(), cds.getEffectiveDate().toLocalDate(), nextIMM.toLocalDate());
marketSpreads[i] = marketSpreadObjects[i] * 1e-4;
if (!nextIMM.isAfter(cds.getMaturityDate().withHour(0).withMinute(0).withSecond(0).withNano(0))) {
spread = marketSpreads[i];
}
}
final ValueProperties properties = Iterables.getOnlyElement(desiredValues).getConstraints().copy()
.with(ValuePropertyNames.FUNCTION, getUniqueId())
.get();
//final ISDAYieldCurveAndSpreadsProvider data = new ISDAYieldCurveAndSpreadsProvider(times, marketSpreads, yieldCurve);
//final HazardRateCurve curve = CALCULATOR.calibrateHazardRateCurve(cds, data, valuationTime);
final ISDACompliantCreditCurve curve;
if (IMMDateGenerator.isIMMDate(cds.getMaturityDate())) {
curve = CREDIT_CURVE_BUILDER.calibrateCreditCurve(pricingCDS, spread, yieldCurve);
} else {
curve = CREDIT_CURVE_BUILDER.calibrateCreditCurve(creditAnalytics, marketSpreads, yieldCurve);
}
final ValueSpecification spec = new ValueSpecification(ValueRequirementNames.HAZARD_RATE_CURVE, target.toSpecification(), properties);