@Test
  public void testAddRemoveTradePosition() {

    try {
      TradePosition instance =
          new TradePosition(
              this.tradestrategy.getContract(),
              TradingCalendar.getDateTimeNowMarketTimeZone(),
              Side.BOT);

      TradePosition tradePosition = aspectHome.persist(instance);

      assertNotNull("1", tradePosition.getIdTradePosition());
      _log.info(
          "testAddTradePosition IdTradeStrategy: "
              + this.tradestrategy.getIdTradeStrategy()
              + "IdTradePosition: "
              + tradePosition.getIdTradePosition());

      tradePositionHome.remove(tradePosition);
      _log.info("testDeleteTradePosition IdTradeStrategy: " + tradestrategy.getIdTradeStrategy());
      tradePosition = tradePositionHome.findById(tradePosition.getIdTradePosition());
      assertNull("2", tradePosition);
    } catch (Exception | AssertionError ex) {
      String msg = "Error running " + name.getMethodName() + " msg: " + ex.getMessage();
      _log.error(msg);
      fail(msg);
    }
  }
  @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);
    }
  }
  @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);
    }
  }
Beispiel #4
0
 /**
  * Constructor for Candle.
  *
  * @param tradingday Tradingday
  * @param contract Contract
  */
 public Candle(
     Contract contract,
     Tradingday tradingday,
     RegularTimePeriod period,
     ZonedDateTime lastUpdateDate) {
   this.setTradingday(tradingday);
   this.setContract(contract);
   this.setPeriod(period.toString());
   this.setStartPeriod(period.getStart());
   this.setEndPeriod(period.getEnd());
   int barSize =
       (int) (TradingCalendar.getDurationInSeconds(period.getStart(), period.getEnd()) + 1);
   this.setBarSize(barSize);
   this.setLastUpdateDate(lastUpdateDate);
 }
  public void addRow() {
    final Tradestrategy tradestrategy = getData();

    String side = tradestrategy.getSide();
    if (null == side) side = Side.BOT;
    String orderType = OrderType.STPLMT;
    String action = Action.BUY;
    if (Side.SLD.equals(side)) {
      action = Action.SELL;
    }
    double risk = tradestrategy.getRiskAmount().doubleValue();
    double stop = 1.0d;

    Money price =
        new Money(
            tradestrategy.getStrategyData().getBaseCandleSeries().getContract().getLastPrice());

    final Entrylimit entrylimit = DAOEntryLimit.newInstance().getValue(price);

    Money limitPrice =
        Action.BUY.equals(action)
            ? price.add(new Money(entrylimit.getLimitAmount().doubleValue()))
            : price.subtract(new Money(entrylimit.getLimitAmount().doubleValue()));

    int quantity = (int) ((int) risk / stop);
    if (tradestrategy.isThereOpenTradePosition()) {
      for (TradeOrder item : tradestrategy.getTradeOrders()) {
        if (item.hasTradePosition()) {
          TradePosition position = item.getTradePosition();
          if (position.isOpen()) {
            quantity = Math.abs(position.getOpenQuantity());
            side = position.getSide();
            action = Action.BUY;
            if (Side.BOT.equals(side)) {
              action = Action.SELL;
            }
            orderType = OrderType.LMT;
            limitPrice = price;
          }
        }
      }
    }

    final TradeOrder tradeOrder =
        new TradeOrder(
            tradestrategy,
            action,
            orderType,
            quantity,
            price.getBigDecimalValue(),
            limitPrice.getBigDecimalValue(),
            TradingCalendar.getDate());
    tradeOrder.setOcaGroupName("");
    tradeOrder.setTransmit(true);
    tradeOrder.setStatus(OrderStatus.UNSUBMIT);
    tradestrategy.addTradeOrder(tradeOrder);

    final Vector<Object> newRow = new Vector<Object>();
    getNewRow(newRow, tradeOrder);
    rows.add(newRow);

    // Tell the listeners a new table has arrived.

    fireTableChanged(new TableModelEvent(this));
  }