protected void createPortfolio(final Collection<ExternalId> tickers) {
// create shell portfolio
final ManageablePortfolio portfolio = createEmptyPortfolio();
final ManageablePortfolioNode rootNode = portfolio.getRootNode();
final Collection<UniqueId> loadSecurities = loadSecurities(tickers);
final SecurityMaster secMaster = getToolContext().getSecurityMaster();
for (final UniqueId uniqueId : loadSecurities) {
final SecurityDocument securityDocument = secMaster.get(uniqueId);
final EquitySecurity security = (EquitySecurity) securityDocument.getSecurity();
final GICSCode gics = security.getGicsCode();
if (gics == null || gics.isPartial()) {
continue;
}
final String sector = SECTORS.get(gics.getSectorCode());
final String industryGroup = gics.getIndustryGroupCode();
final String industry = gics.getIndustryCode();
final String subIndustry = gics.getSubIndustryCode();
// create portfolio structure
ManageablePortfolioNode sectorNode = rootNode.findNodeByName(sector);
if (sectorNode == null) {
s_logger.debug("Creating node for sector {}", sector);
sectorNode = new ManageablePortfolioNode(sector);
rootNode.addChildNode(sectorNode);
}
ManageablePortfolioNode groupNode = sectorNode.findNodeByName("Group " + industryGroup);
if (groupNode == null) {
s_logger.debug("Creating node for industry group {}", industryGroup);
groupNode = new ManageablePortfolioNode("Group " + industryGroup);
sectorNode.addChildNode(groupNode);
}
ManageablePortfolioNode industryNode = groupNode.findNodeByName("Industry " + industry);
if (industryNode == null) {
s_logger.debug("Creating node for industry {}", industry);
industryNode = new ManageablePortfolioNode("Industry " + industry);
groupNode.addChildNode(industryNode);
}
ManageablePortfolioNode subIndustryNode = industryNode.findNodeByName("Sub industry " + subIndustry);
if (subIndustryNode == null) {
s_logger.debug("Creating node for sub industry {}", subIndustry);
subIndustryNode = new ManageablePortfolioNode("Sub industry " + subIndustry);
industryNode.addChildNode(subIndustryNode);
}
// create the position and add it to the master
final ManageablePosition position = createPositionAndTrade(security);
final PositionDocument addedPosition = addPosition(position);
// add the position reference (the unique identifier) to portfolio
subIndustryNode.addPosition(addedPosition.getUniqueId());
}
// adds the complete tree structure to the master
addPortfolio(portfolio);
}