예제 #1
0
  @Override
  protected void onBarClosed(BarHistory history, Bar bar) throws Exception {

    double close = bar.getClose();

    if (close <= 0) {
      throw new RuntimeException(
          String.format(
              "Negative close for [%s %2$tY-%2$tm-%2$td]: %3$f",
              bar.getSymbol(), bar.getDateTime(), bar.getClose()));
    }

    Status status = statuses_.get(bar.getSymbol());
    status.history = history;
    status.lastClose = bar.getClose();
    status.ts = bar.getDateTime();
    // Feed the indicators
    double roc = status.roc.add(close);
    status.returns.add(roc);

    LocalDateTime dateTime = bar.getDateTime();

    status.position = broker.getPosition(status.instrument).quantity;

    if (dateTime.isAfter(getTradingStart())) {

      status.longScores.clear();
      status.shortScores.clear();

      if (history.size() > minLen) {
        status.longScores.add(status.roc.last());
      }

      if (dateTime.isAfter(getTradingStop())) {
        Position pos = broker.getPosition(status.instrument);
        if (pos.quantity > 0) exitLong(bar.getSymbol());
        else if (pos.quantity < 0) exitShort(bar.getSymbol());
      }
    }
  }