Package com.quantcomponents.algo

Examples of com.quantcomponents.algo.PositionBean


 
  @Override
  public synchronized void onTrade(ITrade trade) {
    IContract contract = trade.getOrder().getContract();
    IContract currency = ContractBean.cash(contract.getCurrency());
    PositionBean contractPosition = positions.get(contract);
    if (contractPosition == null) {
      contractPosition = new PositionBean();
      positions.put(contract, contractPosition);
    }
    PositionBean cashPosition = positions.get(currency);
    if (cashPosition == null) {
      cashPosition = new PositionBean();
      cashPosition.setAveragePrice(1.0);
      cashPosition.setMarketPrice(1.0);
      positions.put(currency, cashPosition);
    }
    Integer contractMultiplier = contract.getMultiplier();
    if (contractMultiplier == null || contractMultiplier == 0) {
      contractMultiplier = 1;
    }
    contractPosition.setMarketPrice(trade.getExecutionPrice());
    contractPosition.setTimestamp(trade.getExecutionTime());
    cashPosition.setTimestamp(trade.getExecutionTime());
    double residueSignedPositionAmount = contractPosition.getSignedAmount();
    int residueTradeAmount = trade.getAmount();
    // reducing position
    if (residueSignedPositionAmount != 0) {
      if (residueSignedPositionAmount > 0 && trade.getOrder().getSide() == OrderSide.SELL) { // reducing long position
        double positionReduction = Math.min(residueSignedPositionAmount, residueTradeAmount);
        contractPosition.setRealizedPnl(contractPosition.getRealizedPnl() + (trade.getAveragePrice() - contractPosition.getAveragePrice()) * contractMultiplier * positionReduction);
        residueSignedPositionAmount -= positionReduction;
        residueTradeAmount -= positionReduction;
      } else if (residueSignedPositionAmount < 0 && trade.getOrder().getSide() == OrderSide.BUY) { // reducing short position
        double positionReduction = Math.min(-residueSignedPositionAmount, residueTradeAmount);
        contractPosition.setRealizedPnl(contractPosition.getRealizedPnl() + (contractPosition.getAveragePrice() - trade.getAveragePrice()) * contractMultiplier * positionReduction);
        residueSignedPositionAmount += positionReduction;
        residueTradeAmount -= positionReduction;
      }
    }
    double newSignedPositionAmount = residueSignedPositionAmount;
    if (newSignedPositionAmount == 0) {
      contractPosition.setAveragePrice(0.0);
    }
    // building position
    if (residueTradeAmount > 0) {
      if (trade.getOrder().getSide() == OrderSide.BUY) { // building long position from 0 or more
        newSignedPositionAmount = residueSignedPositionAmount + residueTradeAmount;
        contractPosition.setAveragePrice((contractPosition.getAveragePrice() * residueSignedPositionAmount + trade.getAveragePrice() * residueTradeAmount) / newSignedPositionAmount);
      } else { // building long position from 0 or less
        newSignedPositionAmount = residueSignedPositionAmount - residueTradeAmount;
        contractPosition.setAveragePrice((contractPosition.getAveragePrice() * residueSignedPositionAmount - trade.getAveragePrice() * residueTradeAmount) / newSignedPositionAmount);
      }
    }
    contractPosition.setSignedAmount(newSignedPositionAmount);
    double cashChange = trade.getAveragePrice() * trade.getAmount() * contractMultiplier * (trade.getOrder().getSide() == OrderSide.BUY ? -1 : 1);;
    cashPosition.setSignedAmount(cashPosition.getSignedAmount() + cashChange);
    recalculateMarketValueAndUPnl();
  }
View Full Code Here


  public void onPriceUpdate(IContract contract, ISeriesPoint<Date, Double> price) {
    updateMarketPrice(contract, price.getIndex(), price.getValue());
  }
 
  private void updateMarketPrice(IContract contract, Date timestamp, double price) {
    PositionBean position = positions.get(contract);
    if (position != null) {
      if (price != position.getMarketPrice()) {
        position.setMarketPrice(price);
        position.setTimestamp(timestamp);
        recalculateMarketValueAndUPnl();
      }
    }
  }
View Full Code Here

    for (Map.Entry<IContract, PositionBean> entry : positions.entrySet()) {
      Integer contractMultiplier = entry.getKey().getMultiplier();
      if (contractMultiplier == null || contractMultiplier == 0) {
        contractMultiplier = 1;
      }
      PositionBean position = entry.getValue();
      position.setMarketValue(position.getMarketPrice() * position.getSignedAmount() * contractMultiplier);
      position.setUnrealizedPnl(position.getMarketValue() - position.getAveragePrice() * position.getSignedAmount() * contractMultiplier);
    }
  }
 
View Full Code Here

 
  private void notifyPositionListeners(IContract contract) {
    IPosition position = positionCalculator.getPositions().get(contract);
    if (position != null) {
      for (IPositionListener listener : orderPositionListeners) {
        listener.onPositionUpdate(contract, new PositionBean(position));
      }
    }
  }
View Full Code Here

   
    @Override
    public void updatePortfolio(Contract iBContract, int positionAmt, double marketPrice, double marketValue, double averageCost, double unrealizedPNL, double realizedPNL, String accountName) {
      if (accountName.equals(accountId)) {
        final IContract contract = ContractBean.copyOf(new IBContract(iBContract, constantTranslator));
        final PositionBean position = new PositionBean(new Date(), positionAmt, marketPrice, marketValue, averageCost, unrealizedPNL, realizedPNL);
       
        if (!positionUpdaters.containsKey(contract)) { // setup price listener for that contract: since IB sends too few updates, we update automatically based on price changes
          PositionPriceUpdater positionPriceUpdater = new PositionPriceUpdater();
          positionPriceUpdater.position = position;
          final ITickListener tickListener = new ITickListener() {
View Full Code Here

TOP

Related Classes of com.quantcomponents.algo.PositionBean

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.