String snapshotName = "Sameday spread";
MarketDataSnapshotSearchRequest marketDataSnapshotSearchRequest = new MarketDataSnapshotSearchRequest();
marketDataSnapshotSearchRequest.setName(snapshotName);
MarketDataSnapshotSearchResult marketDataSnapshotSearchResult = getToolContext().getMarketDataSnapshotMaster().search(marketDataSnapshotSearchRequest);
ManageableMarketDataSnapshot snapshot = marketDataSnapshotSearchResult.getFirstSnapshot();
PortfolioSearchRequest portfolioSearchRequest = new PortfolioSearchRequest();
portfolioSearchRequest.setName("CDSOpts");
//portfolioSearchRequest.setName("Standard CDS Portfolio");
List<ManageablePortfolio> portfolios = getToolContext().getPortfolioMaster().search(portfolioSearchRequest).getPortfolios();
for (ManageablePortfolio portfolio : portfolios) {
List<ObjectId> positionIds = portfolio.getRootNode().getPositionIds();
List<UniqueId> positionUids = newArrayList();
for (ObjectId positionId : positionIds) {
positionUids.add(positionId.atLatestVersion());
}
Map<UniqueId, PositionDocument> positions = getToolContext().getPositionMaster().get(positionUids);
// Ticker, RedCode, Currency, Term, Seniority, RestructuringClause
final List<ExternalId> cdsArgs = newArrayList();
final SecureRandom random = new SecureRandom();
for (PositionDocument positionDocument : positions.values()) {
ManageablePosition position = positionDocument.getValue();
ManageableSecurityLink link = position.getSecurityLink();
SecuritySearchRequest ssr = new SecuritySearchRequest();
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)));
}