final RegionSource regionSource = OpenGammaExecutionContext.getRegionSource(executionContext);
final OrganizationSource organizationSource = OpenGammaExecutionContext.getOrganizationSource(executionContext);
final SecuritySource securitySource = OpenGammaExecutionContext.getSecuritySource(executionContext);
final CreditDefaultSwapOptionSecurityConverter converter = new CreditDefaultSwapOptionSecurityConverter(securitySource, holidaySource, regionSource, organizationSource);
final CreditDefaultSwapOptionSecurity security = (CreditDefaultSwapOptionSecurity) target.getSecurity();
final CreditDefaultSwapOptionDefinition cdsOption = security.accept(converter);
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 ISDADateCurve yieldCurve = (ISDADateCurve) 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;
final List<ZonedDateTime> calibrationTimes = new ArrayList<>();
final DoubleArrayList marketSpreads = new DoubleArrayList();
for (int i = 0; i < n; i++) {
final ZonedDateTime nextIMMDate = IMMDateGenerator.getNextIMMDate(valuationTime, tenors[i]).withHour(0).withMinute(0).withSecond(0).withNano(0);
if (nextIMMDate.isAfter(cdsOption.getOptionExerciseDate())) {
calibrationTimes.add(IMMDateGenerator.getNextIMMDate(valuationTime, tenors[i]).withHour(0).withMinute(0).withSecond(0).withNano(0));
marketSpreads.add(marketSpreadObjects[i]);
}
}
if (calibrationTimes.size() < 2) {
throw new OpenGammaRuntimeException("Need at least two credit spread points for pricing");
}
final ValueProperties properties = Iterables.getOnlyElement(desiredValues).getConstraints().copy()
.with(ValuePropertyNames.FUNCTION, getUniqueId())
.get();
final ISDAYieldCurveAndSpreadsProvider data = new ISDAYieldCurveAndSpreadsProvider(calibrationTimes.toArray(new ZonedDateTime[calibrationTimes.size()]),
marketSpreads.toDoubleArray(), yieldCurve);
final HazardRateCurve curve = CALCULATOR.calibrateHazardRateCurve(cdsOption.getUnderlyingCDS(), data, valuationTime);
final ValueSpecification spec = new ValueSpecification(ValueRequirementNames.HAZARD_RATE_CURVE, target.toSpecification(), properties);
return Collections.singleton(new ComputedValue(spec, curve));
}