public Set<ComputedValue> execute(final FunctionExecutionContext executionContext, final FunctionInputs inputs, final ComputationTarget target,
final Set<ValueRequirement> desiredValues) throws AsynchronousExecution {
final ZonedDateTime now = ZonedDateTime.now(executionContext.getValuationClock());
final ValueRequirement desiredValue = Iterables.getOnlyElement(desiredValues);
//TODO
CurveSpecification curveSpecification;
final ConfigSource configSource = OpenGammaExecutionContext.getConfigSource(executionContext);
final String idName = desiredValue.getConstraint(ValuePropertyNames.CURVE);
String curveName;
try {
curveName = "SAMEDAY_" + idName;
curveSpecification = CurveUtils.getCurveSpecification(now.toInstant(), configSource, now.toLocalDate(), curveName);
} catch (final Exception e) {
curveName = idName;
curveSpecification = CurveUtils.getCurveSpecification(now.toInstant(), configSource, now.toLocalDate(), idName);
}
final List<Tenor> tenors = new ArrayList<>();
final List<Double> marketSpreads = new ArrayList<>();
for (final CurveNodeWithIdentifier strip : curveSpecification.getNodes()) {
final Object marketSpreadObject = inputs.getValue(new ValueRequirement(MarketDataRequirementNames.MARKET_VALUE, ComputationTargetType.PRIMITIVE, strip.getIdentifier()));
if (marketSpreadObject != null) {
tenors.add(strip.getCurveNode().getResolvedMaturity());
marketSpreads.add(10000 * (Double) marketSpreadObject);
} else {
s_logger.warn("Could not get spread data for {}, defaulting", strip.getIdentifier());
tenors.add(strip.getCurveNode().getResolvedMaturity());
if (!marketSpreads.isEmpty()) {
//TODO: Find out why some tails are missing - for now set flat spread
marketSpreads.add(marketSpreads.get(marketSpreads.size() - 1));
} else {
marketSpreads.add(105.0);
//throw new OpenGammaRuntimeException("Couldnt get spreads for " + strip.getIdentifier());
}
}
}
if (tenors.size() == 0) {
throw new OpenGammaRuntimeException("Could not get any credit spread data for curve called " + curveName);
}
final NodalObjectsCurve<Tenor, Double> curve = NodalObjectsCurve.from(tenors, marketSpreads);
final ValueProperties properties = createValueProperties()
.with(ValuePropertyNames.CURVE, idName)
.get();
final ValueSpecification spec = new ValueSpecification(ValueRequirementNames.CREDIT_SPREAD_CURVE, target.toSpecification(), properties);
return Collections.singleton(new ComputedValue(spec, curve));
}
@Override
public ComputationTargetType getTargetType() {
return ComputationTargetType.NULL;
}
@Override
public Set<ValueSpecification> getResults(final FunctionCompilationContext context, final ComputationTarget target) {
@SuppressWarnings("synthetic-access")
final ValueProperties properties = createValueProperties()
.withAny(ValuePropertyNames.CURVE)
.get();
return Collections.singleton(new ValueSpecification(ValueRequirementNames.CREDIT_SPREAD_CURVE, target.toSpecification(), properties));
}
@Override
public Set<ValueRequirement> getRequirements(final FunctionCompilationContext context, final ComputationTarget target, final ValueRequirement desiredValue) {
final ValueProperties constraints = desiredValue.getConstraints();
final Set<String> curveNames = constraints.getValues(ValuePropertyNames.CURVE);
if (curveNames == null || curveNames.size() != 1) {
return null;
}
//TODO
String curveName = "SAMEDAY_" + Iterables.getOnlyElement(curveNames);
final Set<ValueRequirement> requirements = new HashSet<>();
final ConfigSource configSource = OpenGammaCompilationContext.getConfigSource(context);
try {
final CurveSpecification specification = CurveUtils.getCurveSpecification(atInstant, configSource, atZDT.toLocalDate(), curveName);
for (final CurveNodeWithIdentifier strip : specification.getNodes()) {
if (strip.getCurveNode() instanceof CreditSpreadNode) {
requirements.add(new ValueRequirement(MarketDataRequirementNames.MARKET_VALUE, ComputationTargetType.PRIMITIVE, strip.getIdentifier()));
}
}
return requirements;
} catch (final Exception e) {
s_logger.error(e.getMessage());
//TODO backwards compatibility - remove when upstream functions select the correct prefix
curveName = Iterables.getOnlyElement(curveNames);
try {
final CurveSpecification specification = CurveUtils.getCurveSpecification(atInstant, configSource, atZDT.toLocalDate(), curveName);
for (final CurveNodeWithIdentifier strip : specification.getNodes()) {
if (strip.getCurveNode() instanceof CreditSpreadNode) {
requirements.add(new ValueRequirement(MarketDataRequirementNames.MARKET_VALUE, ComputationTargetType.PRIMITIVE, strip.getIdentifier()));
}
}
return requirements;