Package com.opengamma.master.position

Examples of com.opengamma.master.position.ManageablePosition


   
    ArgumentChecker.notNull(row, "row");
    ArgumentChecker.notNull(security, "security");
   
    if (row.containsKey(QUANTITY)) {     
      return new ManageablePosition(
          BigDecimal.valueOf(Integer.parseInt(getWithException(row, QUANTITY))),
          security.getExternalIdBundle()
      );
    } else {
      return new ManageablePosition(
          BigDecimal.ONE,
          security.getExternalIdBundle()
      );
    }
  }
View Full Code Here


  public ManageablePosition constructPosition(Map<String, String> row, ManageableSecurity security) {
   
    ArgumentChecker.notNull(row, "row");
    ArgumentChecker.notNull(security, "security");
   
    ManageablePosition result = (ManageablePosition) recursiveConstructBean(row, ManageablePosition.class, "position:");
    if (result != null) {
      result.setSecurityLink(new ManageableSecurityLink(security.getExternalIdBundle()));
    }
    return result;
  }
View Full Code Here

      PortfolioDocument portfolioDocument = getPortfolioMaster().get(_portfolioId, VersionCorrection.LATEST);
      s_logger.debug("Updating portfolio {} with {}", portfolioDocument.getUniqueId(), _trade);
      ManageablePortfolio portfolio = portfolioDocument.getPortfolio();
      ManageablePortfolioNode root = portfolio.getRootNode();

      ManageablePosition position = new ManageablePosition();
      position.getSecurityLink().setExternalId(_trade.getSecurityLink().getExternalId());
      position.setQuantity(_trade.getQuantity());
      String providerIdStr = _trade.getAttributes().get(getProviderIdName());
      ManageableTrade manageableTrade = new ManageableTrade(_trade);
      if (providerIdStr != null) {
        ExternalId providerId = ExternalId.parse(providerIdStr);
        position.setProviderId(providerId);
        manageableTrade.setProviderId(providerId);
      }
      position.addTrade(manageableTrade);
      PositionDocument addedPosition = getPositionMaster().add(new PositionDocument(position));
      root.addPosition(addedPosition.getUniqueId());

      PortfolioDocument currentPortfolio = getPortfolioMaster().update(new PortfolioDocument(portfolio));
      s_logger.info("Portfolio ID {} updated", currentPortfolio.getUniqueId());
View Full Code Here

    final ManageablePortfolioNode root = portfolio.getRootNode();
    final Random rnd = new Random();
    root.setName("Root");
    for (final Tutorial1Security security : loadSecurities()) {
      securities.add(new SecurityDocument(security.toRawSecurity()));
      final ManageablePosition position = new ManageablePosition(new BigDecimal(100 + rnd.nextInt(900)), security.getExternalIdBundle());
      root.addPosition(positions.add(new PositionDocument(position)));
    }
    portfolios.add(new PortfolioDocument(portfolio));
  }
View Full Code Here

   
    try (Timer.Context context = _insertTimer.time()) {
      final long positionId = nextId("pos_master_seq");
      final long positionOid = (document.getUniqueId() != null ? extractOid(document.getUniqueId()) : positionId);
      final UniqueId positionUid = createUniqueId(positionOid, positionId);
      final ManageablePosition position = document.getPosition();
     
      // the arguments for inserting into the position table
      final DbMapSqlParameterSource docArgs = new DbMapSqlParameterSource().addValue("position_id", positionId).addValue("position_oid", positionOid)
          .addTimestamp("ver_from_instant", document.getVersionFromInstant()).addTimestampNullFuture("ver_to_instant", document.getVersionToInstant())
          .addTimestamp("corr_from_instant", document.getCorrectionFromInstant())
          .addTimestampNullFuture("corr_to_instant", document.getCorrectionToInstant())
          .addValue("quantity", position.getQuantity(), Types.DECIMAL)
          .addValue("provider_scheme",
              position.getProviderId() != null ? position.getProviderId().getScheme().getName() : null, Types.VARCHAR)
          .addValue("provider_value",
              position.getProviderId() != null ? position.getProviderId().getValue() : null, Types.VARCHAR);
     
      // the arguments for inserting into the pos_attribute table
      final List<DbMapSqlParameterSource> posAttrList = Lists.newArrayList();
      for (final Entry<String, String> entry : position.getAttributes().entrySet()) {
        final long posAttrId = nextId("pos_trade_attr_seq");
        final DbMapSqlParameterSource posAttrArgs = new DbMapSqlParameterSource().addValue("attr_id", posAttrId)
            .addValue("pos_id", positionId)
            .addValue("pos_oid", positionOid)
            .addValue("key", entry.getKey())
            .addValue("value", entry.getValue());
        posAttrList.add(posAttrArgs);
      }
     
      // the arguments for inserting into the idkey tables
      final List<DbMapSqlParameterSource> posAssocList = new ArrayList<DbMapSqlParameterSource>();
      final Set<Pair<String, String>> schemeValueSet = Sets.newHashSet();
      for (final ExternalId id : position.getSecurityLink().getAllExternalIds()) {
        final DbMapSqlParameterSource assocArgs = new DbMapSqlParameterSource().addValue("position_id", positionId)
            .addValue("key_scheme", id.getScheme().getName())
            .addValue("key_value", id.getValue());
        posAssocList.add(assocArgs);
        schemeValueSet.add(Pair.of(id.getScheme().getName(), id.getValue()));
      }
     
      // the arguments for inserting into the trade table
      final List<DbMapSqlParameterSource> tradeList = Lists.newArrayList();
      final List<DbMapSqlParameterSource> tradeAssocList = Lists.newArrayList();
      final List<DbMapSqlParameterSource> tradeAttributeList = Lists.newArrayList();
      for (final ManageableTrade trade : position.getTrades()) {
        final long tradeId = nextId("pos_master_seq");
        final long tradeOid = (trade.getUniqueId() != null ? extractOid(trade.getUniqueId()) : tradeId);
        final ExternalId counterpartyId = trade.getCounterpartyExternalId();
       
        final DbMapSqlParameterSource tradeArgs = new DbMapSqlParameterSource().addValue("trade_id", tradeId)
            .addValue("trade_oid", tradeOid)
            .addValue("position_id", positionId)
            .addValue("position_oid", positionOid)
            .addValue("quantity", trade.getQuantity())
            .addDate("trade_date", trade.getTradeDate())
            .addTimeAllowNull("trade_time", trade.getTradeTime() != null ? trade.getTradeTime().toLocalTime() : null)
            .addValue("zone_offset",
                trade.getTradeTime() != null ? trade.getTradeTime().getOffset().getTotalSeconds() : null, Types.INTEGER)
            .addValue("cparty_scheme", counterpartyId.getScheme().getName())
            .addValue("cparty_value", counterpartyId.getValue())
            .addValue("provider_scheme",
                position.getProviderId() != null ? position.getProviderId().getScheme().getName() : null, Types.VARCHAR)
            .addValue("provider_value",
                position.getProviderId() != null ? position.getProviderId().getValue() : null, Types.VARCHAR)
            .addValue("premium_value", trade.getPremium(), Types.DOUBLE)
            .addValue("premium_currency",
                trade.getPremiumCurrency() != null ? trade.getPremiumCurrency().getCode() : null, Types.VARCHAR)
            .addDateAllowNull("premium_date", trade.getPremiumDate())
            .addTimeAllowNull("premium_time", (trade.getPremiumTime() != null ? trade.getPremiumTime().toLocalTime() : null))
            .addValue("premium_zone_offset",
                trade.getPremiumTime() != null ? trade.getPremiumTime().getOffset().getTotalSeconds() : null, Types.INTEGER);
        tradeList.add(tradeArgs);
       
        // trade attributes
        final Map<String, String> attributes = new HashMap<String, String>(trade.getAttributes());
        for (final Entry<String, String> entry : attributes.entrySet()) {
          final long tradeAttrId = nextId("pos_trade_attr_seq");
          final DbMapSqlParameterSource tradeAttributeArgs = new DbMapSqlParameterSource().addValue("attr_id", tradeAttrId)
              .addValue("trade_id", tradeId)
              .addValue("trade_oid", tradeOid)
              .addValue("key", entry.getKey())
              .addValue("value", entry.getValue());
          tradeAttributeList.add(tradeAttributeArgs);
        }
       
        // set the trade uniqueId
        final UniqueId tradeUid = createUniqueId(tradeOid, tradeId);
        IdUtils.setInto(trade, tradeUid);
        trade.setParentPositionId(positionUid);
        for (final ExternalId id : trade.getSecurityLink().getAllExternalIds()) {
          final DbMapSqlParameterSource assocArgs = new DbMapSqlParameterSource().addValue("trade_id", tradeId)
              .addValue("key_scheme", id.getScheme().getName())
              .addValue("key_value", id.getValue());
          tradeAssocList.add(assocArgs);
          schemeValueSet.add(Pair.of(id.getScheme().getName(), id.getValue()));
        }
      }
     
      final List<DbMapSqlParameterSource> idKeyList = new ArrayList<DbMapSqlParameterSource>();
      final String sqlSelectIdKey = getElSqlBundle().getSql("SelectIdKey");
      for (final Pair<String, String> pair : schemeValueSet) {
        final DbMapSqlParameterSource idkeyArgs = new DbMapSqlParameterSource().addValue("key_scheme", pair.getFirst())
            .addValue("key_value", pair.getSecond());
        if (getJdbcTemplate().queryForList(sqlSelectIdKey, idkeyArgs).isEmpty()) {
          // select avoids creating unecessary id, but id may still not be used
          final long idKeyId = nextId("pos_idkey_seq");
          idkeyArgs.addValue("idkey_id", idKeyId);
          idKeyList.add(idkeyArgs);
        }
      }
     
      final String sqlDoc = getElSqlBundle().getSql("Insert", docArgs);
      final String sqlIdKey = getElSqlBundle().getSql("InsertIdKey");
      final String sqlPosition2IdKey = getElSqlBundle().getSql("InsertPosition2IdKey");
      final String sqlTrade = getElSqlBundle().getSql("InsertTrade");
      final String sqlTrade2IdKey = getElSqlBundle().getSql("InsertTrade2IdKey");
      final String sqlPositionAttributes = getElSqlBundle().getSql("InsertPositionAttributes");
      final String sqlTradeAttributes = getElSqlBundle().getSql("InsertTradeAttributes");
      getJdbcTemplate().update(sqlDoc, docArgs);
      getJdbcTemplate().batchUpdate(sqlIdKey, idKeyList.toArray(new DbMapSqlParameterSource[idKeyList.size()]));
      getJdbcTemplate().batchUpdate(sqlPosition2IdKey, posAssocList.toArray(new DbMapSqlParameterSource[posAssocList.size()]));
      getJdbcTemplate().batchUpdate(sqlTrade, tradeList.toArray(new DbMapSqlParameterSource[tradeList.size()]));
      getJdbcTemplate().batchUpdate(sqlTrade2IdKey, tradeAssocList.toArray(new DbMapSqlParameterSource[tradeAssocList.size()]));
      getJdbcTemplate().batchUpdate(sqlPositionAttributes, posAttrList.toArray(new DbMapSqlParameterSource[posAttrList.size()]));
      getJdbcTemplate().batchUpdate(sqlTradeAttributes, tradeAttributeList.toArray(new DbMapSqlParameterSource[tradeAttributeList.size()]));
     
      // set the uniqueId
      position.setUniqueId(positionUid);
      document.setUniqueId(positionUid);
      return document;
    }
  }
View Full Code Here

      final Timestamp versionTo = rs.getTimestamp("VER_TO_INSTANT");
      final Timestamp correctionFrom = rs.getTimestamp("CORR_FROM_INSTANT");
      final Timestamp correctionTo = rs.getTimestamp("CORR_TO_INSTANT");
      final String providerScheme = rs.getString("POS_PROVIDER_SCHEME");
      final String providerValue = rs.getString("POS_PROVIDER_VALUE");
      _position = new ManageablePosition(quantity, ExternalIdBundle.EMPTY);
      _position.setUniqueId(createUniqueId(positionOid, positionId));
      if (providerScheme != null && providerValue != null) {
        _position.setProviderId(ExternalId.of(providerScheme, providerValue));
      }
      final PositionDocument doc = new PositionDocument(_position);
View Full Code Here

        s_logger.debug("Creating node for sector {}", sector);
        sectorNode = new ManageablePortfolioNode(sector);
        rootNode.addChildNode(sectorNode);
      }
      // 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
      sectorNode.addPosition(addedPosition.getUniqueId());
    }
View Full Code Here

    s_logger.debug("Creating position {}", security);
    int shares = (RandomUtils.nextInt(490) + 10) * 10;

    ExternalIdBundle bundle = security.getExternalIdBundle(); // we could add an identifier pointing back to the original source database if we're doing an ETL.

    ManageablePosition position = new ManageablePosition(BigDecimal.valueOf(shares), bundle);

    // create random trades that add up in shares to the position they're under (this is not enforced by the system)
    if (shares <= 2000) {
      ManageableTrade trade = new ManageableTrade(BigDecimal.valueOf(shares), bundle, _tradeDate3, null, ExternalId.of("CPARTY", "BACS"));
      position.addTrade(trade);
    } else {
      ManageableTrade trade1 = new ManageableTrade(BigDecimal.valueOf(2000), bundle, _tradeDate1, null, ExternalId.of("CPARTY", "BACS"));
      position.addTrade(trade1);
      ManageableTrade trade2 = new ManageableTrade(BigDecimal.valueOf(shares - 2000), bundle, _tradeDate2, null, ExternalId.of("CPARTY", "BACS"));
      position.addTrade(trade2);
    }
    return position;
  }
View Full Code Here

  private void addPortfolioNode(final ManageablePortfolioNode rootNode, final Collection<FinancialSecurity> finSecurities, final String portfolioNodeName, BigDecimal quantity) {
    PositionMaster positionMaster = getToolContext().getPositionMaster();
    final ManageablePortfolioNode portfolioNode = new ManageablePortfolioNode(portfolioNodeName);
    for (final FinancialSecurity security : finSecurities) {
      storeFinancialSecurity(security);
      ManageablePosition position = new ManageablePosition(quantity, security.getExternalIdBundle());
      PositionDocument addedDoc = positionMaster.add(new PositionDocument(position));
      portfolioNode.addPosition(addedDoc.getUniqueId());
    }
    rootNode.addChildNode(portfolioNode);
  }
View Full Code Here

   
    final PositionMaster posMaster = getToolContext().getPositionMaster();
    final PortfolioMaster portMaster = getToolContext().getPortfolioMaster();
   
    final ManageablePosition position = makePositionAndTrade(security);
    final PositionDocument positionDoc = new PositionDocument(position);
    posMaster.add(positionDoc);
   
    ManageablePortfolio portfolio = new ManageablePortfolio(portfolioName);
    ManageablePortfolioNode rootNode = portfolio.getRootNode();
    rootNode.setName("Root");
    rootNode.addPosition(position.getUniqueId());
   
    PortfolioDocument portfolioDoc = new PortfolioDocument(portfolio);
    portMaster.add(portfolioDoc);

  }
View Full Code Here

TOP

Related Classes of com.opengamma.master.position.ManageablePosition

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.