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)); }
/** * Method runStrategy. Note the current candle is just forming Enter a tier 1-3 gap in first 5min * bar direction, with a 3R target and stop @ 5min high/low. * * <p>TradeOrders create TradePositions that are associated to Contracts. TradeOrders are * associated to TradePositions and the Tradestrategy that created them. A TradePosition may have * TradeOrders from multiple Tradestrategies. * * <p>TradePositions are created when there is no open TradePosition and a TradeOrder is either * filled or partially filled. * * <p>Note TradePositions are closed when the open quantity is zero. The new TradePosition is * associated to the Contract with the 1 to 1 relationship from Contract to TradePosition. The * TradeOrder that opened the TradePosition is marked as the open order @see * org.trade.persistent.dao.TradeOrder.getIsOpenPosition() * * <p>TradePosition will have the Side set to either BOT/SLD i.e. Long/Short. If an open position * changes from Long to Short dues to an over Sell/Buy order the side will switch. * * @param candleSeries CandleSeries the series of candles that has been updated. * @param newBar boolean has a new bar just started. * @see org.trade.strategy.AbstractStrategyRule#runStrategy(CandleSeries, boolean) */ @SuppressWarnings("unused") public void runStrategy(CandleSeries candleSeries, boolean newBar) { _log.info( "Inside BreakEvenStrategy.runStrategy::" + this.getSymbol() + "_at_" + this.getCurrentCandle().getPeriod().getStart()); try { // Get the current candle CandleItem currentCandleItem = this.getCurrentCandle(); ZonedDateTime startPeriod = currentCandleItem.getPeriod().getStart(); // QuantityShares = this.getTradestrategy().getCodeValues().get(0).getCodeValue(); // QuantityShares = // this.getTradestrategy().getCodeValues().get(0).getCodeAttribute().getDefaultValue(); QuantityShares = ((Long) this.getTradestrategy().getValueCode("stockSharesQuantity")).intValue(); /* * Position is open kill this Strategy as its job is done. In this * example we would manage the position with a strategy manager. * This strategy is just used to create the order that would open * the position. */ if (this.isThereOpenPosition()) { _log.info( "Strategy complete open position filled symbol: " + getSymbol() + " startPeriod: " + startPeriod); /* * If the order is partial filled check if the risk goes beyond * 1 risk unit. If it does cancel the openPositionOrder this * will cause it to be marked as filled. */ if (OrderStatus.PARTIALFILLED.equals(this.getOpenPositionOrder().getStatus())) { if (isRiskViolated( currentCandleItem.getClose(), this.getTradestrategy().getRiskAmount(), this.getOpenPositionOrder().getQuantity(), this.getOpenPositionOrder().getAverageFilledPrice())) { this.cancelOrder(this.getOpenPositionOrder()); } } // this.cancel(); return; } else /* * Open position order was cancelled kill this Strategy as its job * is done. */ if (null != openPositionOrderKey && !this.getTradeOrder(openPositionOrderKey).isActive()) { _log.info( "Strategy complete open position cancelled symbol: " + getSymbol() + " startPeriod: " + startPeriod); updateTradestrategyStatus(TradestrategyStatus.CANCELLED); // this.cancel(); return; } else { /* * Create code here to create orders based on your conditions/rules. * * * Validamos que la estrategia solo se ejecute dentro del periodo de 9:30am a 15:00pm */ /* if (startPeriod.isAfter(this.getTradestrategy().getTradingday().getOpen().minusSeconds(1)) && startPeriod.isBefore(this.getTradestrategy().getTradingday().getClose().plusSeconds(1)) ) { // && newBar */ /* * Example On start of the second (9:35) candle check the 9:30 * candle and buy over under in the direction of the bar. * * * Validamos que no hayan transcurrido 25min (despues de la apertura) y que la estrategia * no se haya cumplido **/ if (startPeriod.isBefore(this.getTradestrategy().getTradingday().getOpen().plusMinutes(25)) && !StrategyOK) { Candle currentCandle = currentCandleItem.getCandle(); Candle prevDayCandle = getPreviousDayCandleFromDb( candleSeries, startPeriod); // Obtenemos el punto P, es decir el punto de apertura del día // anterior DecimalFormat df = new DecimalFormat("#.00"); BigDecimal currentClose = new BigDecimal(currentCandle.getClose().doubleValue()) .setScale(2, RoundingMode.HALF_EVEN); BigDecimal preDayClose = new BigDecimal(prevDayCandle.getClose().doubleValue()) .setScale(2, RoundingMode.HALF_EVEN); if (currentClose.doubleValue() == preDayClose .doubleValue()) { // Validamos que el valor actual de LOW sea igual al de P StrategyOK = Boolean.TRUE; if (!this.isThereOpenPosition()) { // Siempre que no haya una orden abierta ... // Money auxStopPrice = new Money(prevDayCandle.getLow()); Money auxStopPrice = new Money(prevDayCandle.getClose()).subtract(new Money(0.04)); Money limitPrice = new Money(prevDayCandle.getClose()); LastAuxStopPrice = auxStopPrice.doubleValue(); TradeOrder tradeOrder = this.createOrder( this.getTradestrategy().getContract(), Action.BUY, OrderType.STPLMT, limitPrice, auxStopPrice, QuantityShares, false, true); // Creamos y transmitimos una orden BUY, STPLMT = LOW - 4c } _log.info( "StrategyOK::" + StrategyOK + ", LastAuxStopPrice::" + LastAuxStopPrice + ", Symbol::" + this.getSymbol()); /* TradeOrder tradeOrder = new TradeOrder(this.getTradestrategy(), Action.BUY, OrderType.STPLMT, 100, price, price.add(new BigDecimal(0.02)), TradingCalendar.getDateTimeNowMarketTimeZone()); //tradeOrder.setClientId(clientId); tradeOrder.setTransmit(new Boolean(true)); //tradeOrder.setStatus(OrderStatus.UNSUBMIT); this.submitOrder(this.getTradestrategy().getContract(), tradeOrder); */ } } else if (startPeriod.isAfter( this.getTradestrategy().getTradingday().getOpen().plusMinutes(25)) && !StrategyOK) { // Si han pasado 25min y no se cumplio la estrategia, cancelamos la // ejecución // this.cancel(); return; } else if (StrategyOK) { // Si se ejecuto la estrategia... // Candle prevDayCandle = getPreviousDayCandleFromDb(candleSeries, startPeriod); /* * Is the candle in the direction of the Tradestrategy side i.e. * a long play should have a green 5min candle */ CandleItem prevCandleItem = null; if (getCurrentCandleCount() > 0) { prevCandleItem = (CandleItem) candleSeries.getDataItem(getCurrentCandleCount() - 1); // AbstractStrategyRule // .logCandle(this, prevCandleItem.getCandle()); } this.reFreshPositionOrders(); // double lastAuxStopPrice = this.getOpenPositionOrder().getAuxPrice().doubleValue(); if (currentCandleItem.getClose() > prevCandleItem.getClose() && currentCandleItem.getClose() >= addAPercentToANumber(LastAuxStopPrice, 50)) { /* * Validamos que haya un incremento en LOW entre la * posicion actual y la posicion anterior del dia; y * además, que el incremento de LOW del dia sea mayor o * igual a un 50% de LOW del dia anterior */ /* Money stopPrice = addPennyAndRoundStop(this .getOpenPositionOrder().getAverageFilledPrice() .doubleValue(), getOpenTradePosition() .getSide(), Action.BUY, 0.04); moveStopOCAPrice(stopPrice, true); */ Money auxStopPrice = new Money(addAPercentToANumber(LastAuxStopPrice, 50)).subtract(new Money(0.04)); Money limitPrice = new Money(addAPercentToANumber(LastAuxStopPrice, 50)); LastAuxStopPrice = auxStopPrice.doubleValue(); TradeOrder tradeOrder = this.updateOrder( this.getOpenPositionOrder().getOrderKey(), Action.BUY, OrderType.STPLMT, limitPrice, auxStopPrice, QuantityShares, false, true); // Creamos y transmitimos una orden BUY, STPLMT = (LOW + 50%) - 4c this.reFreshPositionOrders(); } _log.info( "StrategyOK::" + StrategyOK + ", LastAuxStopPrice::" + LastAuxStopPrice + ", Symbol::" + this.getSymbol()); } } /* * Close any opened positions with a market order at day end minus * one bar. * if (!currentCandleItem.getLastUpdateDate().isBefore( this.getTradestrategy() .getTradingday() .getClose() .minusMinutes( this.getTradestrategy().getBarSize() / 60))) { cancelOrdersClosePosition(true); _log.info("Rule 15:55:00 close all open positions: " + getSymbol() + " Time: " + startPeriod); this.cancel(); } */ } catch (StrategyRuleException | PersistentModelException | ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException | IOException ex) { _log.error("Error runRule exception: " + ex.getMessage(), ex); error(1, 10, "Error runRule exception: " + ex.getMessage()); } catch (Exception ex) { _log.error("Error runRule exception: " + ex.getMessage(), ex); error(1, 10, "Error runRule exception: " + ex.getMessage()); } }