@Override
 public TradeRow apply(Trade input) {
   TradeRow tradeRow = new TradeRow();
   tradeRow.setBuyingBroker(input.getBuyBroker());
   tradeRow.setPrice(input.getPrice());
   tradeRow.setQuantity(input.getQuantity());
   tradeRow.setSellingBroker(input.getSellBroker());
   return tradeRow;
 }
  public void add(Trade trade) throws TradeException {
    // Is a valid trade?
    if (trade.getDate() == null) throw new TradeException("Trade date must be informed!");
    if (trade.getPrice() <= 0) throw new TradeException("Trade price must be greater than zero!");
    if (trade.getQuantity() <= 0)
      throw new TradeException("Trade quantity must be greater than zero!");

    tradesRecordTable.add(trade);
    Collections.sort(tradesRecordTable);
  }
  public double calculateVolumeWeightedStockPrice(String stockSymbol, int lastMinutes) {
    // Get end date
    Calendar startDate = Calendar.getInstance();
    startDate.add(Calendar.MINUTE, lastMinutes * -1);

    double totalTradedPrice = 0, totalQuantity = 0;

    // Acumulate quantity and price
    for (Trade trade : tradesRecordTable) {
      // Is the stock requested?
      if (!trade.getStock().getStockSymbol().equals(stockSymbol)) continue;

      // Check date range of trade
      if (lastMinutes > 0 && trade.getDate().before(startDate.getTime())) continue;

      totalTradedPrice += (trade.getPrice() * trade.getQuantity());
      totalQuantity += trade.getQuantity();
    }

    // Prevent zero divide
    if (totalQuantity == 0) return 0;

    return totalTradedPrice / totalQuantity;
  }