예제 #1
0
  public void addObservedResult(
      double min, double max, double closeVal, long currentTime, String passCode)
      throws IllegalStateValueDataModificationException {
    if (!this.passCode.equals(passCode)) {
      throw new IllegalStateValueDataModificationException();
    }

    int priority = 0;
    Set<Long> closedOrders = new HashSet<Long>();
    for (Map.Entry<Long, OpenOrder> entry : openOrders.entrySet()) {
      long orderNumber = entry.getKey();
      if (orderNumber != currentTime) {
        OpenOrder order = entry.getValue();
        double weight = Math.exp(-priority);
        Integer timeClass = (int) (Math.log(currentTime - orderNumber) / Math.log(2));
        int maxClass = (int) ((max - order.getPrice()) / res);
        int minClass = (int) ((min - order.getPrice()) / res);
        if (!dist.containsKey(timeClass)) {
          dist.put(timeClass, new TreeMap<Integer, Double>());
        }
        for (int i = minClass; i <= maxClass; i++) {
          if (!dist.get(timeClass).containsKey(i)) {
            dist.get(timeClass).put(i, 0.0);
          }

          if (!collapsedDist.containsKey(i)) {
            collapsedDist.put(i, 0.0);
          }
        }
        if (minClass != 0 && maxClass != 0) {
          for (int i = minClass; i <= maxClass; i++) {
            dist.get(timeClass).put(i, dist.get(timeClass).get(i) + weight);
            collapsedDist.put(i, collapsedDist.get(i) + weight);
          }
        }
        sumWeights += weight;
        boolean closed = false;
        if (!order.isClosed()) {
          if (order.getPosition() == 'L') {
            closed = order.newPrice(min);
            if (Math.random() > 0.5) // (!closed)
            {
              closed = order.newPrice(max);
            }
          } else {
            closed = order.newPrice(max);
            if (Math.random() > 0.5) // (!closed)
            {
              closed = order.newPrice(min);
            }
          }

          if (!closed && (currentTime - orderNumber) > maxAge) {
            order.close(closeVal);
            closed = true;
          }

          if (closed) {
            double profit = weight * (order.getPnL() / res);
            // System.out.println("Order Closed from " + state + ": " + orderNumber + ": Profit: " +
            // profit);

            pnl += profit;
            sum = pnl;
            sum2PnL += (profit * profit);
            sum2 = sum2PnL;
            correlator.pnl -= average;
            countClosed += weight;

            average = pnl / countClosed;
            correlator.pnl += average;
            correlator.stackPnL.addToStack(Math.signum(profit));

            double variance = Math.abs(sum2 / countClosed - average);

            sdev = Math.sqrt(variance);
            double average2PnL = average * average;
            sharpe = Math.sqrt(average2PnL / variance);
          }
        }

        if (closed && (currentTime - orderNumber) >= maxAge) {
          closedOrders.add(orderNumber);
        }

        priority++;
      }
    }
    for (long orderNumber : closedOrders) {
      openOrders.remove(orderNumber);
    }
    if (openOrders.isEmpty()) {
      correlator.removeLiveState(this);
    }
  }
예제 #2
0
  public void newOrder(long orderNumber, double price, String passCode)
      throws IllegalStateValueDataModificationException {
    if (!this.passCode.equals(passCode)) {
      throw new IllegalStateValueDataModificationException();
    }
    correlator.addLiveState(this);

    TreeMap<Integer, Double> neg = new TreeMap<Integer, Double>(collapsedDist.headMap(0));
    SortedMap<Integer, Double> pos = collapsedDist.tailMap(0);

    int shortProfit = 0;
    int shortLoss = 0;

    // System.out.println("Distribution: ");
    for (int returnClass : neg.descendingKeySet()) {
      double weight = neg.get(returnClass);
      if (weight / sumWeights > correlator.confidenceProfit) {
        shortProfit = returnClass;
        break;
      }
    }

    for (int returnClass : neg.descendingKeySet()) {
      double weight = neg.get(returnClass);
      if (weight / sumWeights > correlator.confidenceLoss) {
        shortLoss = returnClass;
        break;
      }
    }

    int longProfit = 0;
    int longLoss = 0;
    for (int returnClass : pos.keySet()) {
      double weight = pos.get(returnClass);
      if (weight / sumWeights > correlator.confidenceProfit) {
        longProfit = returnClass;
        break;
      }
    }

    for (int returnClass : pos.keySet()) {
      double weight = pos.get(returnClass);
      if (weight / sumWeights > correlator.confidenceLoss) {
        longLoss = returnClass;
        break;
      }
    }
    if (longProfit == 0) {
      longProfit = (int) (10 * Math.random());
    }
    if (shortProfit == 0) {
      shortProfit = (int) (-10 * Math.random());
    }

    double takeProfit = price + (longProfit * res);
    // double stopLoss = price - (shortLoss*res);
    double stopLoss = price - (longProfit * res);
    if (Math.abs(shortProfit) > longProfit) {
      takeProfit = price + (shortProfit * res);
      // stopLoss = price + (longLoss*res);
      stopLoss = price - (shortProfit * res);
    }
    openOrders.put(orderNumber, new OpenOrder("" + orderNumber, price, takeProfit, stopLoss));
    // System.out.println("New Order from " + state + ": " + orderNumber + ": " + price + " " +
    // takeProfit + " " + stopLoss);
  }