private List<SearchRequestBuilder> makeRequestBuilders() {
   List<SearchRequestBuilder> builders = new ArrayList<>();
   ZonedDateTime currentDate = config.getFrom();
   ZonedDateTime maxDate = up(TimeUnit.HOURS, config.getTo());
   do {
     builders.add(
         client
             .prepareSearch(
                 appendForEach(INDEXES_PREFIXES, ELASTIC_INDEX_DATE_FORMAT.format(currentDate)))
             .setTypes(TYPES)
             .setSize(config.getBucketSize())
             .setPostFilter(FilterBuilders.andFilter(makeFilters(currentDate))));
     currentDate = currentDate.plusDays(1);
   } while (currentDate.isBefore(maxDate));
   return builders;
 }
  /**
   * 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());
    }
  }
Ejemplo n.º 3
0
  @Scheduled(
      initialDelayString = "${schedule.analytic.service.initial.delay}",
      fixedDelayString = "${schedule.analytic.service.fixed.delay}")
  public void updateAllWeatherData() {
    log.debug("Start hourly analytics");

    ZonedDateTime startHour = getStartHour();
    ZonedDateTime currentHour = ZonedDateTime.now();
    ZonedDateTime nextHour = currentHour.plusHours(1);

    ZonedDateTime temp;
    Integer lastRainTic = null;

    Hour currentLastHour = hourDataRepository.findTopByOrderByTimestampHourDesc();

    while (startHour.isBefore(nextHour)) {

      ZonedDateTime endOfHour = startHour.plusHours(1);

      Date dateFrom = Date.from(startHour.toInstant());
      Date dateTo = Date.from(endOfHour.toInstant());

      Hour findByTimestampHour = hourDataRepository.findByTimestampHour(dateFrom);

      if (findByTimestampHour != null) { // hour already exists

        log.debug("findByTimestampHour.getId() == currentLastHour.getId()");
        log.debug(findByTimestampHour.getId() + "");
        log.debug(currentLastHour.getId() + "");
        log.debug("if true: " + (findByTimestampHour.getId().equals(currentLastHour.getId())));
        ;
        log.debug("");

        if (findByTimestampHour.getId()
            == currentLastHour.getId()) { // if the current hour == the last hour in the DB
          log.info("++++++ Aktuelle Stunde loeschen und neu Berechnen ++++++");
          hourDataRepository.delete(currentLastHour.getId());
          findByTimestampHour = null;
        }
      }

      if (findByTimestampHour == null) {
        Hour newHour = new Hour(this.stationId, dateFrom, 0, 0.0F);

        List<StationRawData> allForPeriod =
            rawDataService.findAllForPeriod(this.stationId, dateFrom, dateTo);

        int counter = 0;
        int minute;
        Float currentPrecipMM;
        Float sumPrecipMM = 0.0F;
        Map<Integer, Float> valueMap = new HashMap<>();
        for (StationRawData srd : allForPeriod) {

          try {
            temp = ZonedDateTime.ofInstant(srd.getTimeOfRecording().toInstant(), ZoneOffset.UTC);
            minute = temp.getMinute();

            String[] dataValues = srd.getRawData().split(";");

            // RAIN
            Integer currentTics = Integer.parseInt(dataValues[22]);
            Integer ticsSinceLast = 0;
            // first entry of the day or no rain since last entry
            /**
             * TODO Calculation of the first value of the day need the last value from day before...
             */
            if (lastRainTic == null || lastRainTic.equals(currentTics)) {
              currentPrecipMM = new Float("0.0");
            }
            // rain
            else {
              ticsSinceLast = WeatherMapper.getCurrentPrecipCount(lastRainTic, currentTics);
              currentPrecipMM = (ticsSinceLast * 295 / new Float("1000.0"));
            }

            valueMap.put(minute, currentPrecipMM);
            sumPrecipMM += currentPrecipMM;

            lastRainTic = currentTics;
            counter++;

          } catch (Exception e) {
            log.debug("Error parsing temperature: " + e.getMessage());
          }
        }

        if (valueMap != null && !valueMap.isEmpty()) {
          newHour.setValues(valueMap);
          newHour.setPrecipTotalMM(sumPrecipMM);
          newHour.setCount(counter);

          hourDataRepository.save(newHour);
        }

        log.debug("Hour: " + newHour.toString());
      }

      startHour = endOfHour;
    }

    log.info("End hourly analytics");
  }