final ComputationTarget target,
final Set<ValueRequirement> desiredValues) throws AsynchronousExecution {
ValueRequirement requirement = desiredValues.iterator().next();
final Clock snapshotClock = executionContext.getValuationClock();
final ZonedDateTime now = ZonedDateTime.now(snapshotClock);
final LegacyVanillaCDSSecurity security = (LegacyVanillaCDSSecurity) target.getSecurity();
final CdsRecoveryRateIdentifier recoveryRateIdentifier = security.accept(new CreditSecurityToRecoveryRateVisitor(
executionContext.getSecuritySource()));
Object recoveryRateObject = inputs.getValue(new ValueRequirement("PX_LAST",
ComputationTargetType.PRIMITIVE,
recoveryRateIdentifier.getExternalId()));
if (recoveryRateObject == null) {
throw new OpenGammaRuntimeException("Could not get recovery rate");
//s_logger.warn("Could not get recovery rate, defaulting to 0.4: " + recoveryRateIdentifier);
//recoveryRateObject = 0.4;
}
final double recoveryRate = (Double) recoveryRateObject;
CreditDefaultSwapSecurityConverterDeprecated converter = new CreditDefaultSwapSecurityConverterDeprecated(
_holidaySource,
_regionSource,
recoveryRate);
LegacyVanillaCreditDefaultSwapDefinition cds = converter.visitLegacyVanillaCDSSecurity(security);
final StandardCDSQuotingConvention quoteConvention = StandardCDSQuotingConvention.parse(requirement.getConstraint(
ISDAFunctionConstants.CDS_QUOTE_CONVENTION));
final NodalTenorDoubleCurve spreadCurve = (NodalTenorDoubleCurve) inputs.getValue(ValueRequirementNames.BUCKETED_SPREADS);
if (spreadCurve == null) {
throw new OpenGammaRuntimeException("Bucketed spreads not available for " + getSpreadCurveIdentifier(security));
}
// get the isda curve
final ISDACompliantYieldCurve yieldCurve = (ISDACompliantYieldCurve) inputs.getValue(ValueRequirementNames.YIELD_CURVE);
if (yieldCurve == null) {
throw new OpenGammaRuntimeException("Couldn't get isda curve");
}
final Double cdsQuoteDouble = (Double) inputs.getValue(MarketDataRequirementNames.MARKET_VALUE);
if (cdsQuoteDouble == null) {
throw new OpenGammaRuntimeException("Couldn't get spread for " + security);
}
final CDSAnalyticVisitor pricingVisitor = new CDSAnalyticVisitor(now.toLocalDate(),
_holidaySource,
_regionSource,
recoveryRate);
final CDSAnalytic pricingCDS = security.accept(pricingVisitor);
final CDSQuoteConvention quote = SpreadCurveFunctions.getQuotes(security.getMaturityDate(),
new double[]{cdsQuoteDouble},
security.getParSpread(),
quoteConvention,
true)[0];
final QuotedSpread quotedSpread = getQuotedSpread(quote,
security.isBuy() ? BuySellProtection.BUY : BuySellProtection.SELL,
yieldCurve,
pricingCDS,
security.getParSpread());
ISDACompliantCreditCurve creditCurve;
NodalTenorDoubleCurve modifiedSpreadCurve;
NodalTenorDoubleCurve modifiedPillarCurve;
if (IMMDateGenerator.isIMMDate(security.getMaturityDate())) {
final String pillarString = requirement.getConstraint(ISDAFunctionConstants.ISDA_BUCKET_TENORS);
final ZonedDateTime[] bucketDates = SpreadCurveFunctions.getPillarDates(now, pillarString);
final ZonedDateTime[] pillarDates = bucketDates;
double[] spreads = SpreadCurveFunctions.getSpreadCurveNew(spreadCurve,
bucketDates,
security.getStartDate(),
quoteConvention);
Tenor[] tenors = SpreadCurveFunctions.getBuckets(pillarString);
modifiedSpreadCurve = new NodalTenorDoubleCurve(tenors, ArrayUtils.toObject(spreads), true);
modifiedPillarCurve = modifiedSpreadCurve; // for IMM buckets and spreads are the same
//final CDSQuoteConvention[] quotes = SpreadCurveFunctions.getQuotes(security.getMaturityDate(), spreads, security.getParSpread(), quoteConvention, false);
// CDS analytics for credit curve
final CDSAnalytic[] creditAnalytics = new CDSAnalytic[pillarDates.length];
for (int i = 0; i < creditAnalytics.length; i++) {
final CDSAnalyticVisitor curveVisitor = new CDSAnalyticVisitor(now.toLocalDate(),
_holidaySource,
_regionSource,
security.getStartDate().toLocalDate(),
pillarDates[i].toLocalDate(),
recoveryRate);
creditAnalytics[i] = security.accept(curveVisitor);
}
creditCurve = CREDIT_CURVE_BUILDER.calibrateCreditCurve(pricingCDS, quotedSpread, yieldCurve);
} else {
// non IMM date - pillars set to fixed set
final String pillarString = NON_IMM_PILLAR_TENORS;
final String bucketString = requirement.getConstraint(ISDAFunctionConstants.ISDA_BUCKET_TENORS);
final ZonedDateTime[] bucketDates = SpreadCurveFunctions.getPillarDates(now, bucketString);
final ZonedDateTime[] pillarDates = SpreadCurveFunctions.getPillarDates(now, pillarString);
double[] bucketSpreads = SpreadCurveFunctions.getSpreadCurveNew(spreadCurve,
bucketDates,
security.getStartDate(),
quoteConvention);
double[] pillarSpreads = SpreadCurveFunctions.getSpreadCurveNew(spreadCurve,
pillarDates,
security.getStartDate(),
quoteConvention);
Tenor[] bucketTenors = SpreadCurveFunctions.getBuckets(bucketString);
Tenor[] pillarTenors = SpreadCurveFunctions.getBuckets(pillarString);
modifiedSpreadCurve = new NodalTenorDoubleCurve(bucketTenors, ArrayUtils.toObject(bucketSpreads), true);
modifiedPillarCurve = new NodalTenorDoubleCurve(pillarTenors, ArrayUtils.toObject(pillarSpreads), true);
final CDSQuoteConvention[] quotes = SpreadCurveFunctions.getQuotes(security.getMaturityDate(),
pillarSpreads,
security.getParSpread(),
quoteConvention,
false);
// CDS analytics for credit curve
final CDSAnalytic[] creditAnalytics = new CDSAnalytic[pillarDates.length];
for (int i = 0; i < creditAnalytics.length; i++) {
final CDSAnalyticVisitor curveVisitor = new CDSAnalyticVisitor(now.toLocalDate(),
_holidaySource,
_regionSource,
security.getStartDate().toLocalDate(),
pillarDates[i].toLocalDate(), recoveryRate);
creditAnalytics[i] = security.accept(curveVisitor);
}
creditCurve = CREDIT_CURVE_BUILDER.calibrateCreditCurve(creditAnalytics, quotes, yieldCurve);
}