예제 #1
0
  public Order getBuyOrder(int[] spotPrices, int[] futurePrices, int pos, long money, int restDay) {

    /* Information available for trading:
      spotPrices[]: Time series of spot prices. It provides 120 elements from spotPrices[0]
      to spotPrices[119].  The element spotPrices[119] is the latest.
      futurePrices[]: Time series of futures price. It provides 60 elements from futurePrices[0]
      to futurePrices[59]. The element futurePrices[59] is the latest.
      Before opening the market, same values with spot prices are given.
      If no contract is made in the market, value -1 is given.
    pos: Position of the agent. Positive is buying position. Negative is selling.
      money: Available cash. Note that type 'long' is used because of needed precision.
    restDay: Number of remaining transaction to the closing of the market.  */

    Order order = new Order(); // Object to return values.

    order.buysell = Order.BUY;
    // Cancel decision if it may increase absolute value of the position
    if (pos > fMaxPosition) {
      order.buysell = Order.NONE;
      return order;
    }

    int latestFuturePrice = futurePrices[futurePrices.length - 1];
    if (latestFuturePrice < 0) {
      order.buysell = Order.NONE;
      return order;
    } // If previous price is invalid, it makes no order.
    order.price = (int) ((double) latestFuturePrice * (1.0 - fSpreadRatio));

    if (order.price < 1) {
      order.price = 1;
    }
    order.quant = fMinQuant + fRandom.nextInt(fMaxQuant - fMinQuant + 1);

    // Message
    message("Buy" + ", price = " + order.price + ", volume = " + order.quant + "  )");

    return order;
  }
예제 #2
0
  public Order getOrder(int[] spotPrices, int[] futurePrices, int pos, long money, int restDay) {

    Order order = new Order();
    // Decide buy or sell based on difference of the last two futures prices.
    int price1 = futurePrices[futurePrices.length - 1];
    int price2 = futurePrices[futurePrices.length - 2];
    if (price1 > 0 && price2 > 0) {
      if (price1 < price2) {
        order.buysell = Order.BUY; // buy if downward trend
      } else if (price1 > price2) {
        order.buysell = Order.SELL; // sell if upward trend
      } else {
        order.buysell = Order.NONE; // no order if price is constant
        return order;
      }
    } else {
      order.buysell = fRandom.nextInt(2) + 1; // randomly by or sell
      // if price is not well defined.
    }
    // Cancel decision if it may increase absolute value of the position
    if (order.buysell == Order.BUY) {
      if (pos > fMaxPosition) {
        order.buysell = Order.NONE;
        return order;
      }
    } else if (order.buysell == Order.SELL) {
      if (pos < -fMaxPosition) {
        order.buysell = Order.NONE;
        return order;
      }
    }
    int prevPrice = getLatestPrice(futurePrices);
    if (prevPrice == -1) {
      prevPrice = getLatestPrice(spotPrices); // use spot price instead
    }
    if (prevPrice == -1) {
      prevPrice = fNominalPrice; // use nominal value instead
    }
    while (true) {
      order.price = prevPrice + (int) (fWidthOfPrice * fRandom.nextGaussian());
      if (order.price > 0) {
        break;
      }
    }
    order.quant = fMinQuant + fRandom.nextInt(fMaxQuant - fMinQuant + 1);
    // Message
    message(
        Order.buySellToString(order.buysell)
            + ", price = "
            + order.price
            + ", volume = "
            + order.quant
            + " (trend = "
            + (price1 - price2)
            + "  )");
    return order;
  }