コード例 #1
0
 private BigDecimal getTradingPL() {
   if (mTick == null) {
     return null;
   }
   BigDecimal trading = BigDecimal.ZERO;
   for (Trade<?> trade : mTrades) {
     BigDecimal single = mTick.subtract(trade.getPrice()).multiply(trade.getQuantity());
     trading = trading.add(single);
   }
   return trading;
 }
コード例 #2
0
 /** Helper method that updates two arrays of long and short position elements based on a trade. */
 private BigDecimal processTrade(
     Queue<PositionElement> longs, Queue<PositionElement> shorts, Trade<?> trade) {
   BigDecimal total = BigDecimal.ZERO;
   Queue<PositionElement> source, dest;
   BigDecimal remaining;
   BigDecimal quantity = trade.getQuantity();
   BigDecimal price = trade.getPrice();
   if (quantity.signum() == 1) {
     // buy
     remaining = quantity;
     source = shorts;
     dest = longs;
   } else {
     // sell
     remaining = quantity.negate();
     source = longs;
     dest = shorts;
   }
   while (remaining.signum() == 1 && !source.isEmpty()) {
     PositionElement element = source.peek();
     int compare = element.quantity.compareTo(remaining);
     BigDecimal priceDifference = price.subtract(element.price);
     if (source == shorts) {
       // negate the price difference for closing short positions
       // since realized gains happen when price has decreased
       priceDifference = priceDifference.negate();
     }
     if (compare == 0) {
       // position element is closed
       total = total.add(priceDifference.multiply(remaining));
       source.remove();
       remaining = BigDecimal.ZERO;
     } else if (compare > 0) {
       total = total.add(priceDifference.multiply(remaining));
       element.quantity = element.quantity.subtract(remaining);
       remaining = BigDecimal.ZERO;
     } else if (compare < 0) {
       total = total.add(priceDifference.multiply(element.quantity));
       source.remove();
       remaining = remaining.subtract(element.quantity);
     }
   }
   if (remaining.signum() == 1) {
     dest.add(new PositionElement(remaining, price));
   }
   return total;
 }