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);
    }
  }
Exemplo n.º 3
1
  @Test
  public void testAddOpenStopTargetTradeOrder() {

    try {
      String side = this.tradestrategy.getSide();
      String action = Action.BUY;
      if (Side.SLD.equals(side)) {
        action = Action.SELL;
      }

      double risk = this.tradestrategy.getRiskAmount().doubleValue();

      double stop = 0.20;
      BigDecimal price = new BigDecimal(20);
      int quantity = (int) ((int) risk / stop);
      ZonedDateTime createDate = this.tradestrategy.getTradingday().getOpen().plusMinutes(5);

      TradeOrder tradeOrder1 =
          new TradeOrder(
              this.tradestrategy, action, OrderType.STPLMT, quantity, price, price, createDate);
      tradeOrder1.setOrderKey((new BigDecimal((Math.random() * 1000000))).intValue());
      tradeOrder1.setClientId(clientId);
      tradeOrder1.setOcaGroupName("");
      tradeOrder1.setTransmit(true);
      tradeOrder1.setStatus("SUBMITTED");
      tradeOrder1.validate();
      tradeOrder1 = tradeOrderHome.persist(tradeOrder1);
      int buySellMultiplier = 1;
      if (action.equals(Action.BUY)) {
        action = Action.SELL;

      } else {
        action = Action.BUY;
        buySellMultiplier = -1;
      }

      TradeOrder tradeOrder2 =
          new TradeOrder(
              this.tradestrategy,
              action,
              OrderType.LMT,
              quantity / 2,
              null,
              price.add(new BigDecimal((stop * 3) * buySellMultiplier)),
              createDate);

      tradeOrder2.setClientId(clientId);
      tradeOrder2.setOrderKey((new BigDecimal((Math.random() * 1000000))).intValue());
      tradeOrder2.setOcaType(2);
      tradeOrder2.setOcaGroupName(this.tradestrategy.getIdTradeStrategy() + "q1w2e3");
      tradeOrder2.setTransmit(true);
      tradeOrder2.setStatus("SUBMITTED");
      tradeOrder2.validate();
      tradeOrder2 = tradeOrderHome.persist(tradeOrder2);

      TradeOrder tradeOrder3 =
          new TradeOrder(
              this.tradestrategy,
              action,
              OrderType.LMT,
              quantity / 2,
              null,
              price.add(new BigDecimal((stop * 4) * buySellMultiplier)),
              createDate);

      tradeOrder3.setClientId(clientId);
      tradeOrder3.setOrderKey((new BigDecimal((Math.random() * 1000000))).intValue());
      tradeOrder3.setOcaType(2);
      tradeOrder3.setOcaGroupName(this.tradestrategy.getIdTradeStrategy() + "q1w2e3");
      tradeOrder3.setTransmit(true);
      tradeOrder3.setStatus("SUBMITTED");
      tradeOrder3.validate();
      tradeOrder3 = tradeOrderHome.persist(tradeOrder3);

      TradeOrder tradeOrder4 =
          new TradeOrder(
              this.tradestrategy,
              action,
              OrderType.STP,
              quantity,
              price.add(new BigDecimal(stop * buySellMultiplier * -1)),
              null,
              createDate);
      tradeOrder4.setLimitPrice(new BigDecimal(0));
      tradeOrder4.setAuxPrice(price.add(new BigDecimal(stop * buySellMultiplier * -1)));
      tradeOrder4.setClientId(clientId);
      tradeOrder4.setOrderKey((new BigDecimal((Math.random() * 1000000))).intValue());
      tradeOrder4.setOcaType(2);
      tradeOrder4.setOcaGroupName(this.tradestrategy.getIdTradeStrategy() + "q1w2e3");
      tradeOrder4.setTransmit(true);
      tradeOrder4.setStatus("SUBMITTED");
      tradeOrder4.validate();
      tradeOrder4 = tradeOrderHome.persist(tradeOrder4);

      _log.info("IdOrder: " + tradeOrder1.getIdTradeOrder());
    } catch (Exception | AssertionError ex) {
      String msg = "Error running " + name.getMethodName() + " msg: " + ex.getMessage();
      _log.error(msg);
      fail(msg);
    }
  }
Exemplo n.º 4
1
  @Test
  public void testAddTradeOrder() {

    try {
      String side = this.tradestrategy.getSide();
      String action = Action.BUY;
      if (Side.SLD.equals(side)) {
        action = Action.SELL;
      }

      double risk = this.tradestrategy.getRiskAmount().doubleValue();

      double stop = 0.20;
      BigDecimal price = new BigDecimal(20);
      int quantity = (int) ((int) risk / stop);
      ZonedDateTime createDate = this.tradestrategy.getTradingday().getOpen().plusMinutes(5);

      TradeOrder tradeOrder =
          new TradeOrder(
              this.tradestrategy,
              action,
              OrderType.STPLMT,
              quantity,
              price,
              price.add(new BigDecimal(0.004)),
              TradingCalendar.getDateTimeNowMarketTimeZone());
      tradeOrder.setOrderKey((new BigDecimal((Math.random() * 1000000))).intValue());
      tradeOrder.setClientId(clientId);
      tradeOrder.setTransmit(true);
      tradeOrder.setStatus("SUBMITTED");
      tradeOrder.validate();
      tradeOrder = tradeOrderHome.persist(tradeOrder);
      assertNotNull("1", tradeOrder);
      _log.info("IdOrder: " + tradeOrder.getIdTradeOrder());

      TradeOrder tradeOrder1 =
          new TradeOrder(
              this.tradestrategy,
              Action.SELL,
              OrderType.STP,
              quantity,
              price.subtract(new BigDecimal(1)),
              null,
              createDate);

      tradeOrder1.setAuxPrice(price.subtract(new BigDecimal(1)));
      tradeOrder1.setOrderKey((new BigDecimal((Math.random() * 1000000))).intValue());
      tradeOrder1.setClientId(clientId);
      tradeOrder1.setTransmit(true);
      tradeOrder1.setStatus("SUBMITTED");
      tradeOrder1.validate();
      tradeOrder1 = tradeOrderHome.persist(tradeOrder1);
      assertNotNull("2", tradeOrder1);
    } catch (Exception | AssertionError ex) {
      String msg = "Error running " + name.getMethodName() + " msg: " + ex.getMessage();
      _log.error(msg);
      fail(msg);
    }
  }
Exemplo n.º 5
0
  /** Write [DATE] Buy [SYMBOL] [NUMBER]sh @[PRICE] = [VALUE] --> [NUMBER]sh held * */
  public void orderBought(
      TradeOrder order, Date date, StockPosition position, TradingAccount account) {
    double profitOrLoss =
        (order.shares * (order.getExecutedPrice() - position.getCostBasis())
            - account.getTradeFees(order.shares));

    double percent = profitOrLoss / (position.getCostBasis() * order.shares);

    this.writer.println(
        DATE_FORMAT.format(date)
            + " "
            + (position.getShares() != 0 ? "Buy " : "Cover ")
            + order.symbol
            + " "
            + order.shares
            + "sh "
            + "@"
            + DOLLAR_FORMAT.format(order.getExecutedPrice())
            + " = "
            + DOLLAR_FORMAT.format(order.getExecutedValue())
            + " --> "
            + position.getShares()
            + "sh "
            + (position.getShares() <= 0
                ? "left, "
                    + DOLLAR_FORMAT.format(profitOrLoss)
                    + (profitOrLoss > 0
                        ? " (" + PERCENT_FORMAT.format(percent) + " profit)"
                        : profitOrLoss < 0
                            ? " (" + PERCENT_FORMAT.format(percent) + "loss)"
                            : " (even)")
                : "long"));

    this.lastTraceDate = date;
  }
Exemplo n.º 6
0
 /** Write [DATE] Buy/Sell [SYMBOL] [NUMBER]sh @[PRICE] CANCELLED (no price on date) * */
 public void orderCancelled(
     TradeOrder order, Date date, StockPosition position, TradingAccount account) {
   String priceString;
   switch (order.getPriceTiming()) {
     case OPEN:
     case CLOSE:
     default:
       priceString = DOLLAR_FORMAT.format(order.projectedPrice);
       break;
     case LIMIT_OR_BELOW:
       priceString = "<=" + DOLLAR_FORMAT.format(order.limit);
       break;
     case LIMIT_OR_ABOVE:
       priceString = ">=" + DOLLAR_FORMAT.format(order.limit);
       break;
   }
   // ATR removed "cancelled order" log. Doesn't make sense when
   // using lots of stop and limit orders that don't fill.
 }
Exemplo n.º 7
0
  public void processOrder(TradeOrder order) throws OrderProcessingException {
    Figures figures = figuresFactory.buildFrom(order, calculateEffectiveDateFor(order));

    Trade trade = tradeFactory.createTrade(figures, order.getAsset());
    trade.save();
  }