Exemplo n.º 1
1
  @Test
  public void testAddDetachedTradeOrder() {
    try {
      String side = this.tradestrategy.getSide();
      String action = Action.BUY;
      if (Side.SLD.equals(side)) {
        action = Action.SELL;
      }

      TradeOrder tradeOrder =
          new TradeOrder(
              this.tradestrategy,
              action,
              OrderType.STPLMT,
              100,
              new BigDecimal(20.20),
              new BigDecimal(20.23),
              TradingCalendar.getDateTimeNowMarketTimeZone());
      tradeOrder.setOrderKey((new BigDecimal((Math.random() * 1000000))).intValue());
      // Save new order with detached trade
      tradeOrder = tradeOrderHome.persist(tradeOrder);
      Execution execution = new Execution();
      execution.m_side = side;
      execution.m_time =
          TradingCalendar.getFormattedDate(
              TradingCalendar.getDateTimeNowMarketTimeZone(), "yyyyMMdd HH:mm:ss");
      execution.m_exchange = "ISLAND";
      execution.m_shares = tradeOrder.getQuantity();
      execution.m_price = tradeOrder.getLimitPrice().doubleValue();
      execution.m_avgPrice = tradeOrder.getLimitPrice().doubleValue();
      execution.m_cumQty = tradeOrder.getQuantity();
      execution.m_execId = "1234";
      TradeOrderfill orderfill = new TradeOrderfill();
      TWSBrokerModel.populateTradeOrderfill(execution, orderfill);
      orderfill.setTradeOrder(tradeOrder);
      tradeOrder.addTradeOrderfill(orderfill);
      // Save a detached order with a new order fill
      tradeOrder = tradeOrderHome.persist(tradeOrder);
      if (action.equals(Action.BUY)) {
        action = Action.SELL;

      } else {
        action = Action.BUY;
      }
      TradeOrder tradeOrder1 =
          new TradeOrder(
              this.tradestrategy,
              action,
              OrderType.LMT,
              300,
              null,
              new BigDecimal(23.41),
              TradingCalendar.getDateTimeNowMarketTimeZone());
      tradeOrder1.setOrderKey((new BigDecimal((Math.random() * 1000000))).intValue());
      tradeOrder1 = tradeOrderHome.persist(tradeOrder1);

      Execution execution1 = new Execution();
      execution1.m_side = side;
      execution1.m_time =
          TradingCalendar.getFormattedDate(
              TradingCalendar.getDateTimeNowMarketTimeZone(), "yyyyMMdd HH:mm:ss");
      execution1.m_exchange = "ISLAND";
      execution1.m_shares = tradeOrder1.getQuantity();
      execution1.m_price = tradeOrder1.getLimitPrice().doubleValue();
      execution1.m_avgPrice = tradeOrder1.getLimitPrice().doubleValue();
      execution1.m_cumQty = tradeOrder1.getQuantity();
      execution1.m_execId = "1234";
      TradeOrderfill orderfill1 = new TradeOrderfill();
      TWSBrokerModel.populateTradeOrderfill(execution1, orderfill1);
      orderfill1.setTradeOrder(tradeOrder1);
      tradeOrder1.addTradeOrderfill(orderfill1);
      tradeOrder1 = tradeOrderHome.persist(tradeOrder1);
    } catch (Exception | AssertionError ex) {
      String msg = "Error running " + name.getMethodName() + " msg: " + ex.getMessage();
      _log.error(msg);
      fail(msg);
    }
  }
Exemplo n.º 2
1
  @Test
  public void testAddTradeOrderFill() {

    try {
      testAddTradeOrder();
      boolean stopped = true;
      int minute = 25;

      for (TradeOrder tradeOrder : this.tradestrategy.getTradeOrders()) {

        tradeOrder = tradeOrderHome.findTradeOrderByKey(tradeOrder.getOrderKey());
        minute = minute + 3;
        ZonedDateTime filledDate = this.tradestrategy.getTradingday().getOpen().plusMinutes(minute);
        if (tradeOrder.getIsOpenPosition()) {

          TradeOrderfill orderfill =
              new TradeOrderfill(
                  tradeOrder,
                  "Paper",
                  tradeOrder.getLimitPrice(),
                  tradeOrder.getQuantity() / 2,
                  "ISLAND",
                  "1234",
                  tradeOrder.getLimitPrice(),
                  tradeOrder.getQuantity() / 2,
                  this.tradestrategy.getSide(),
                  filledDate);

          tradeOrder.addTradeOrderfill(orderfill);

          TradeOrderfill orderfill1 =
              new TradeOrderfill(
                  tradeOrder,
                  "Paper",
                  tradeOrder.getLimitPrice(),
                  tradeOrder.getQuantity() / 2,
                  "ISLAND",
                  "12345",
                  tradeOrder.getLimitPrice(),
                  tradeOrder.getQuantity() / 2,
                  this.tradestrategy.getSide(),
                  filledDate.plusMinutes(3));
          tradeOrder.addTradeOrderfill(orderfill1);
          tradeOrder.setIsFilled(true);
          tradeOrder.setFilledQuantity(tradeOrder.getQuantity());
          tradeOrder.setStatus("FILLED");
          tradeOrder.setFilledDate(filledDate.plusMinutes(3));
          tradeOrder.setAverageFilledPrice(tradeOrder.getLimitPrice());
          tradeOrder.setCommission(new BigDecimal(tradeOrder.getQuantity() * 0.005));

        } else {
          if (stopped) {
            if (OrderType.STP.equals(tradeOrder.getOrderType())) {

              TradeOrderfill orderfill =
                  new TradeOrderfill(
                      tradeOrder,
                      "Paper",
                      tradeOrder.getAuxPrice(),
                      tradeOrder.getQuantity(),
                      "ISLAND",
                      "12345",
                      tradeOrder.getAuxPrice(),
                      tradeOrder.getQuantity(),
                      this.tradestrategy.getSide(),
                      filledDate.plusMinutes(5));
              tradeOrder.addTradeOrderfill(orderfill);
              tradeOrder.setIsFilled(true);
              tradeOrder.setStatus(OrderStatus.FILLED);
              tradeOrder.setAverageFilledPrice(tradeOrder.getAuxPrice());
              tradeOrder.setFilledDate(filledDate.plusMinutes(5));
              tradeOrder.setCommission(new BigDecimal(tradeOrder.getQuantity() * 0.005));
              tradeOrder.setFilledQuantity(tradeOrder.getQuantity());

            } else {
              tradeOrder.setStatus(OrderStatus.CANCELLED);
            }
          } else {
            if (OrderType.LMT.equals(tradeOrder.getOrderType())) {

              TradeOrderfill orderfill =
                  new TradeOrderfill(
                      tradeOrder,
                      "Paper",
                      tradeOrder.getLimitPrice(),
                      tradeOrder.getQuantity() / 2,
                      "ISLAND",
                      "12345",
                      tradeOrder.getLimitPrice(),
                      tradeOrder.getQuantity() / 2,
                      this.tradestrategy.getSide(),
                      filledDate.plusMinutes(5));
              tradeOrder.addTradeOrderfill(orderfill);
              TradeOrderfill orderfill1 =
                  new TradeOrderfill(
                      tradeOrder,
                      "Paper",
                      tradeOrder.getLimitPrice(),
                      tradeOrder.getQuantity() / 2,
                      "ISLAND",
                      "12345",
                      tradeOrder.getLimitPrice(),
                      tradeOrder.getQuantity() / 2,
                      this.tradestrategy.getSide(),
                      filledDate.plusMinutes(6));
              orderfill1.setTradeOrder(tradeOrder);
              tradeOrder.addTradeOrderfill(orderfill1);
              tradeOrder.setIsFilled(true);
              tradeOrder.setStatus(OrderStatus.FILLED);
              tradeOrder.setFilledQuantity(tradeOrder.getQuantity());
              tradeOrder.setFilledDate(filledDate.plusMinutes(15));
              tradeOrder.setAverageFilledPrice(tradeOrder.getLimitPrice());
              tradeOrder.setCommission(new BigDecimal(tradeOrder.getQuantity() * 0.005));

            } else {
              tradeOrder.setStatus(OrderStatus.CANCELLED);
            }
          }
        }

        tradeOrder = tradeOrderHome.persist(tradeOrder);
        _log.info(
            "IdOrder: "
                + tradeOrder.getIdTradeOrder()
                + " Action:"
                + tradeOrder.getAction()
                + " OrderType:"
                + tradeOrder.getOrderType()
                + " Status:"
                + tradeOrder.getStatus()
                + " filledDate:"
                + filledDate);
      }
    } catch (Exception | AssertionError ex) {
      String msg = "Error running " + name.getMethodName() + " msg: " + ex.getMessage();
      _log.error(msg);
      fail(msg);
    }
  }