示例#1
0
  /**
   * This method is called whenever two agents agree on an exchange. It can be called by the market
   * or the agents themselves.
   *
   * @param buyer the buyer
   * @param seller the seller
   * @param good the good being exchanged
   * @param price the price
   */
  public PurchaseResult trade(
      EconomicAgent buyer,
      EconomicAgent seller,
      Good good,
      int price,
      Quote buyerQuote,
      Quote sellerQuote) {
    Preconditions.checkArgument(buyer != seller, "buyer and seller are the same person!");
    assert getBuyers().contains(buyer) : buyer.toString() + " ----- " + buyers;
    assert getSellers().contains(seller);
    double sellerCost =
        good.getLastValidPrice(); // record the price of the good BEFORE it is traded, so we can
    // learn its markup

    PurchaseResult result = policy.trade(buyer, seller, good, price, buyerQuote, sellerQuote, this);

    if (result == PurchaseResult.SUCCESS) {

      // record
      lastPrice = price;
      lastMarkup = (price - sellerCost) / sellerCost;
      todaySumOfClosingPrices += price;
      lastFilledAsk = sellerQuote.getPriceQuoted();
      lastFilledBid = buyerQuote.getPriceQuoted();
      weeklyVolume++;
      todayVolume++;

      // tell the listeners!
      for (TradeListener tl : tradeListeners)
        tl.tradeEvent(buyer, seller, good, price, sellerQuote, buyerQuote);

      // if there is GUI tell the records
      if (MacroII.hasGUI()) {
        // record it on the timeline
        // todo logtodo

        // register it on the network!
        registerTradeOnNetwork(seller, buyer, good.getType(), 1);
      }
    }

    return result;
  }