// -------------------------------------------------------------------------
  @Test(enabled = false)
  public void test_equity() throws Exception {
    EquitySecurity sec = new EquitySecurity("London", "LON", "OpenGamma Ltd", Currency.GBP);
    sec.setName("OpenGamma");
    sec.setGicsCode(GICSCode.of("20102010"));
    sec.setShortName("OG");
    sec.setExternalIdBundle(ExternalIdBundle.of("Test", "OG"));
    SecurityDocument addDoc = new SecurityDocument(sec);
    SecurityDocument added = _secMaster.add(addDoc);

    SecurityDocument loaded = _secMaster.get(added.getUniqueId());
    assertEquals(added, loaded);
  }
 /**
  * Returns AAPL equity security for testing
  *
  * @return the equity security
  */
 public static EquitySecurity getEquitySecurity() {
   EquitySecurity equitySecurity =
       new EquitySecurity("NASDAQ/NGS (GLOBAL SELECT MARKET)", "XNGS", "APPLE INC", Currency.USD);
   equitySecurity.addExternalId(ExternalSchemes.bloombergTickerSecurityId("AAPL US Equity"));
   equitySecurity.addExternalId(ExternalSchemes.bloombergBuidSecurityId("EQ0010169500001000"));
   equitySecurity.addExternalId(ExternalSchemes.cusipSecurityId("037833100"));
   equitySecurity.addExternalId(ExternalSchemes.isinSecurityId("US0378331005"));
   equitySecurity.addExternalId(ExternalSchemes.sedol1SecurityId("2046251"));
   equitySecurity.setShortName("AAPL");
   equitySecurity.setName("APPLE INC");
   equitySecurity.setGicsCode(GICSCode.of("45202010"));
   return equitySecurity;
 }
  // -------------------------------------------------------------------------
  protected ManageablePortfolio generatePortfolio(final String portfolioName) {
    final ReferenceDataProvider referenceDataProvider =
        getToolContext().getBloombergReferenceDataProvider();

    final ManageablePortfolio portfolio = new ManageablePortfolio(portfolioName);

    // Is this a hack?
    final ManageablePortfolioNode rootNode = portfolio.getRootNode();
    portfolio.setRootNode(rootNode);

    //    String indexTickerSuffix = " Index";

    final Set<String> memberEquities = new HashSet<String>();

    for (final Entry<String, String> entry : INDEXES_TO_EXCHANGE.entrySet()) {

      final String indexTickerSuffix = " Index";

      final String underlying = entry.getKey();
      final String ticker = underlying + indexTickerSuffix;

      // don't add index (delete at some point)
      //      addNodes(rootNode, ticker, false, INDEX_OPTION_PERIODS);

      final Set<String> indexMembers =
          BloombergDataUtils.getIndexMembers(referenceDataProvider, ticker);
      for (final String member : indexMembers) {
        final String symbol = getBloombergEquitySymbol(entry.getValue(), member);
        // time series errors for Walmart
        // Todo: investegate & fix
        if ("WMT US Equity".equals(symbol)) {
          continue;
        }
        memberEquities.add(symbol);
      }
    }

    // Sort the symbols for the current index by market cap (highest to lowest), skipping any in the
    // list of EXCLUDED_SECTORS
    final TreeMap<Double, String> equityByMarketCap = new TreeMap<Double, String>();
    final Map<String, FudgeMsg> refDataMap =
        referenceDataProvider.getReferenceData(
            memberEquities,
            Sets.newHashSet(
                BloombergFields.CURRENT_MARKET_CAP_FIELD,
                BloombergConstants.FIELD_GICS_SUB_INDUSTRY));
    for (final String equity : memberEquities) {
      final FudgeMsg fieldData = refDataMap.get(equity);
      if (fieldData == null) {
        throw new OpenGammaRuntimeException("Information not found for equity: " + equity);
      }
      final String gicsCodeString = fieldData.getString(BloombergConstants.FIELD_GICS_SUB_INDUSTRY);
      if (gicsCodeString == null) {
        continue;
      }
      final GICSCode gicsCode = GICSCode.of(gicsCodeString);
      if (EXCLUDED_SECTORS.contains(gicsCode.getSectorDescription())) {
        continue;
      }
      final Double marketCap = fieldData.getDouble(BloombergFields.CURRENT_MARKET_CAP_FIELD);
      if (marketCap != null) {
        equityByMarketCap.put(marketCap, equity);
      }
    }

    // Add a given number of symbols (MEMBERS_DEPTH) to the portfolio and store in a List
    // When adding to the portfolio, add a collar of options with PVs distributed equally +/- around
    // 0
    int count = 0;
    final List<String> chosenEquities = new ArrayList<String>();
    for (final Entry<Double, String> entry : equityByMarketCap.descendingMap().entrySet()) {
      try {
        addNodes(rootNode, entry.getValue(), true, MEMBER_OPTION_PERIODS);
        chosenEquities.add(entry.getValue());
        if (++count >= _numMembers) {
          break;
        }
      } catch (final RuntimeException e) {
        s_logger.warn("Caught exception", e);
      }
    }
    s_logger.info("Generated collar portfolio for {}", chosenEquities);
    return portfolio;
  }
  // -------------------------------------------------------------------------
  protected ManageablePortfolio generatePortfolio(String portfolioName) {
    ReferenceDataProvider referenceDataProvider =
        ((BloombergToolContext) getToolContext()).getBloombergReferenceDataProvider();

    ManageablePortfolio portfolio = new ManageablePortfolio(portfolioName);

    // Is this a hack?
    ManageablePortfolioNode rootNode = portfolio.getRootNode();
    portfolio.setRootNode(rootNode);

    //    String indexTickerSuffix = " Index";

    Set<String> memberEquities = new HashSet<String>();

    for (Entry<String, String> entry : INDEXES_TO_EXCHANGE.entrySet()) {

      String indexTickerSuffix = " Index";

      String underlying = entry.getKey();
      String ticker = underlying + indexTickerSuffix;

      // don't add index (delete at some point)
      //      addNodes(rootNode, ticker, false, INDEX_OPTION_PERIODS);

      Set<String> indexMembers = BloombergDataUtils.getIndexMembers(referenceDataProvider, ticker);
      for (String member : indexMembers) {
        String symbol = getBloombergEquitySymbol(entry.getValue(), member);
        // time series errors for Walmart
        // Todo: investegate & fix
        if ("WMT US Equity".equals(symbol)) {
          continue;
        }
        memberEquities.add(symbol);
      }
    }

    // Sort the symbols for the current index by market cap (highest to lowest), skipping any in the
    // list of EXCLUDED_SECTORS
    TreeMap<Double, String> equityByMarketCap = new TreeMap<Double, String>();
    ReferenceDataResult fields =
        referenceDataProvider.getFields(
            memberEquities,
            Sets.newHashSet(
                BloombergFields.CURRENT_MARKET_CAP_FIELD,
                BloombergConstants.FIELD_GICS_SUB_INDUSTRY));
    for (String equity : memberEquities) {
      PerSecurityReferenceDataResult result = fields.getResult(equity);
      String gicsCodeString =
          result.getFieldData().getString(BloombergConstants.FIELD_GICS_SUB_INDUSTRY);
      GICSCode gicsCode = GICSCode.of(gicsCodeString);
      if (EXCLUDED_SECTORS.contains(gicsCode.getSectorDescription())) {
        continue;
      }
      Double marketCap = result.getFieldData().getDouble(BloombergFields.CURRENT_MARKET_CAP_FIELD);
      equityByMarketCap.put(marketCap, equity);
    }

    // Add a given number of symbols (MEMBERS_DEPTH) to the portfolio and store in a List
    // When adding to the portfolio, add a collar of options with PVs distributed equally +/- around
    // 0
    int count = 0;
    List<String> chosenEquities = new ArrayList<String>();
    for (Entry<Double, String> entry : equityByMarketCap.descendingMap().entrySet()) {
      if (count++ >= _numMembers) {
        break;
      }
      addNodes(rootNode, entry.getValue(), true, MEMBER_OPTION_PERIODS);
      chosenEquities.add(entry.getValue());
    }
    s_logger.info("Generated collar portfolio for {}", chosenEquities);
    return portfolio;
  }