ssr.addExternalIds(link.getExternalIds());
SecuritySearchResult securitySearchResult = getToolContext().getSecurityMaster().search(ssr);
Security security = securitySearchResult.getFirstSecurity();
//Security security = position.getSecurity();
if (security != null && security instanceof CreditDefaultSwapOptionSecurity) {
CreditDefaultSwapOptionSecurity cdsOption = (CreditDefaultSwapOptionSecurity) security;
CreditDefaultSwapSecurity cds = (CreditDefaultSwapSecurity) this.getToolContext().getSecuritySource().getSingle(
cdsOption.getUnderlyingId().toBundle());
String curveDefinitionID = "SAMEDAY_" + cds.getReferenceEntity().getValue() + "_" + cds.getNotional().getCurrency() + "_" +
cds.getDebtSeniority().toString() + "_" + cds.getRestructuringClause();
ConfigSearchRequest<CurveDefinition> curveDefinitionConfigSearchRequest = new ConfigSearchRequest<CurveDefinition>(CurveDefinition.class);
curveDefinitionConfigSearchRequest.setName(curveDefinitionID);
CurveDefinition curveDefinition = getToolContext().getConfigMaster().search(
curveDefinitionConfigSearchRequest).getFirstValue().getValue();
/*final CurveDefinition curveDefinition = configSource.getSingle(CurveDefinition.class,
curveName,
VersionCorrection.LATEST);
if (curveDefinition == null) {
throw new OpenGammaRuntimeException("No curve definition for " + curveName);
}
//Map<Tenor, CurveNode> curveNodesByTenors = new HashMap<Tenor, CurveNode>();
//for (CurveNode curveNode : curveDefinition.getNodes()) {
// curveNodesByTenors.put(curveNode.getResolvedMaturity(), curveNode);
//}
Map<Tenor, CurveNode> curveNodesByTenors = functional(curveDefinition.getNodes()).groupBy(new Function1<CurveNode, Tenor>() {
@Override
public Tenor execute(CurveNode curveNode) {
return curveNode.getResolvedMaturity();
}
});*/
ZonedDateTime start = cds.getStartDate();
ZonedDateTime maturity = cds.getMaturityDate();
Period period = Period.between(start.toLocalDate(), maturity.toLocalDate());
Tenor tenor = Tenor.of(period);
final CurveNodeIdMapper curveNodeIdMapper = configSource.getSingle(CurveNodeIdMapper.class,
curveDefinitionID,
VersionCorrection.LATEST);
try {
tenor = Tenor.of(Period.ofYears(5));
ExternalId timeSeriesId = curveNodeIdMapper.getCreditSpreadNodeId(null /* magic null - ask Elaine */, tenor);
Object strikeObj = snapshot.getGlobalValues().getValue(timeSeriesId, "PX_LAST").getMarketValue();
if ((strikeObj instanceof Double)) {
cdsOption.setStrike((Double) strikeObj);
} else {
throw new OpenGammaRuntimeException(format("Double expected for strike but '%s' found instead.", String.valueOf(strikeObj)));
}
//else throw?
//snapshot.getGlobalValues().getValue()