Esempio n. 1
0
  /**
   * find trade in history using the orderId
   *
   * @param orderId
   * @return
   */
  protected Trade findTradeInHistory(long orderId) {
    Trade trade = null;
    ListIterator<Trade> list =
        ((LinkedList<Trade>) _recentTrades).listIterator(_recentTrades.size());

    while (list.hasPrevious()) {
      trade = (Trade) list.previous();

      if (trade.getTradeMessage().OrderID == orderId) {
        break;
      }
    }

    return (trade);
  }
Esempio n. 2
0
  /**
   * process trade message
   *
   * @param tradeMessage
   */
  public void processTrade(Trade trade) {
    if (logger.isTraceEnabled()) {
      logger.trace("processTrade:" + trade);
    }

    if (_recentTrades.size() >= MarketInterface._maxRecentTrades) {
      ((LinkedList<Trade>) _recentTrades).removeFirst();
    }

    ((LinkedList<Trade>) _recentTrades).addLast(trade);

    if (MDFUtil.canProcessTrade(trade)) {
      if (_statistics == null) {
        _statistics = new MarketStatistics();
      }

      if (!trade.isAdjusted()) {
        _statistics.setLastTradeQuantity(trade.getTradeMessage().Quantity);
        _statistics.setLastTradePrice(trade.getTradeMessage().Price);
        _statistics.setLastTradeDateTime(trade.getTradeMessage().DateTime);
      } else {
        ListIterator<Trade> list =
            ((LinkedList<Trade>) _recentTrades).listIterator(_recentTrades.size());
        Trade lastTradeEligibleForLastDeal = null;
        while (list.hasPrevious()) {
          lastTradeEligibleForLastDeal = (Trade) list.previous();
          if (!lastTradeEligibleForLastDeal.isCancelled()
              && (lastTradeEligibleForLastDeal.getInvestigationStatus() == null
                  || lastTradeEligibleForLastDeal.getInvestigationStatus().getStatus()
                      != InvestigationStatus.STATUS_UNDER_INVESTIGATION)) {
            break;
          }
        }
        int qty = trade.getTradeMessage().Quantity;
        long price = trade.getTradeMessage().Price;
        long dateTime = trade.getTradeMessage().DateTime;

        if (lastTradeEligibleForLastDeal != null
            && lastTradeEligibleForLastDeal.getTradeMessage().DateTime
                > trade.getTradeMessage().DateTime) {
          qty = lastTradeEligibleForLastDeal.getTradeMessage().Quantity;
          price = lastTradeEligibleForLastDeal.getTradeMessage().Price;
          dateTime = lastTradeEligibleForLastDeal.getTradeMessage().DateTime;
        }
        _statistics.setLastTradeQuantity(qty);
        _statistics.setLastTradePrice(price);
        _statistics.setLastTradeDateTime(dateTime);
      }

      // ((LinkedList<Trade>)_recentTrades).addLast(trade);
    }

    _orderUpdated.compareAndSet(false, true);

    _tradeHistoryUpdated.compareAndSet(false, true);

    return;
  }
Esempio n. 3
0
  /**
   * Process cancel trade
   *
   * @param orderId
   */
  public void processCancelTrade(long orderId) {
    if (logger.isTraceEnabled()) {
      logger.trace("processCancelTrade:" + orderId);
    }

    Trade trade = findTradeInHistory(orderId);

    if (trade == null) {
      if (logger.isTraceEnabled()) {
        logger.trace("Trade not found while trying to cancel:" + orderId);
      }

      return;
    }

    trade.setCancelled(true);

    /** Update the statistics with the last good trade */
    if (logger.isTraceEnabled()) {
      logger.trace("processCancelTrade: Updating the statistics");
    }

    ListIterator<Trade> list =
        ((LinkedList<Trade>) _recentTrades).listIterator(_recentTrades.size());

    Trade lastGoodTrade = null;

    while (list.hasPrevious()) {
      lastGoodTrade = (Trade) list.previous();

      if (lastGoodTrade.isCancelled()) {
        lastGoodTrade = null;
        continue;
      }

      if (MDFUtil.canProcessTrade(lastGoodTrade)) {
        break;
      } else {
        // set lastGoodTrade to null, otherwise it might be pointing to the previous trade that is
        // not eligible for being "last trade"
        lastGoodTrade = null;
      }
    }

    if (lastGoodTrade != null) {
      if (_statistics == null) {
        _statistics = new MarketStatistics();
      }

      _statistics.setLastTradeQuantity(lastGoodTrade.getTradeMessage().Quantity);
      _statistics.setLastTradePrice(lastGoodTrade.getTradeMessage().Price);
      _statistics.setLastTradeDateTime(lastGoodTrade.getTradeMessage().DateTime);
    } else {
      if (_statistics != null) {
        _statistics.setLastTradeQuantity(0);
        _statistics.setLastTradePrice(0);
        _statistics.setLastTradeDateTime(0L);
      }
    }

    _orderUpdated.compareAndSet(false, true);
    _tradeHistoryUpdated.compareAndSet(false, true);

    return;
  }