Exemplo n.º 1
0
  @Override
  public void onPositionOpened(Position newPosition) {
    double entryPrice = getPosition().getUnitaryEntryPrice();
    long quantity = getPosition().getQuantity();

    barCount = 0;

    cancelOrderIfNecessary(stopSellOrder);
    cancelOrderIfNecessary(limitSellOrder);

    if (getPosition().isLong()) {
      double targetProfitExitPrice = entryPrice * (1 + limitSellExitTargetProfitPct);
      limitSellOrder = sellLimit(quantity, targetProfitExitPrice, "Exit @ Limit");

      double stopLossExitPrice = entryPrice * (1 - stopSellExitTargetProfitPct);
      stopSellOrder = sellStop(quantity, stopLossExitPrice, "Exit @ Stop");
    } else {
      double targetProfitExitPrice = entryPrice * (1 - limitSellExitTargetProfitPct);
      limitSellOrder = buyLimit(quantity, targetProfitExitPrice, "Exit @ Limit");

      double stopLossExitPrice = entryPrice * (1 + stopSellExitTargetProfitPct);
      stopSellOrder = buyStop(quantity, stopLossExitPrice, "Exit @ Stop");
    }

    limitSellOrder.setOCAGroup(getInstrument().getSymbol() + " OCA " + ocaCount);
    stopSellOrder.setOCAGroup(getInstrument().getSymbol() + " OCA " + ocaCount);
    ocaCount++;
  }
Exemplo n.º 2
0
 void cancelOrderIfNecessary(Order order) {
   if ((order != null) && !order.isCancelled() && !order.isFilled()) {
     cancelOrder(order);
   }
 }