protected Integer extractPreviousRank(
      Stock stock, String analysisName, Date endDate, EventType... eventTypes)
      throws IgnoredEventDateException {
    EventValue previousEventValue =
        DataSource.getInstance().getLastTrendEventFor(stock, analysisName, endDate, eventTypes);

    if (previousEventValue == null) return null;
    Calendar currentDateCal = Calendar.getInstance();
    currentDateCal.setTime(endDate);
    currentDateCal.set(Calendar.HOUR_OF_DAY, 0);
    currentDateCal.set(Calendar.MINUTE, 0);
    currentDateCal.set(Calendar.SECOND, 0);
    currentDateCal.set(Calendar.MILLISECOND, 0);
    if (previousEventValue.getDate().compareTo(currentDateCal.getTime()) == 0)
      throw new IgnoredEventDateException(previousEventValue);

    Integer previousRank = extractPreviousRankFromMessage(previousEventValue);
    return previousRank;
  }
  protected Integer extractPreviousRankFromMessage(EventValue previousEventValue) {

    Pattern pattern = Pattern.compile("Rank is : --([0-9]+)--");
    Matcher matcher = pattern.matcher(previousEventValue.getMessage());
    if (matcher.find()) {
      return new Integer(matcher.group(1));
    } else {
      LOGGER.error("Invalid trend event : " + previousEventValue, new Throwable());
      return 0;
    }
  }
Example #3
0
  private List<PeriodRatingDTO> validPeriods(
      SortedMap<Date, Number> mapFromQuotationsClose,
      Stock stock,
      Date startDate,
      Date endDate,
      Date endCalcRes,
      String analyseName,
      SortedMap<Date, double[]> calcOutput,
      Collection<EventValue> eventListForEvtDef,
      String noResMsg,
      String evtDefInfo)
      throws NotEnoughDataException {

    List<PeriodRatingDTO> periods = new ArrayList<PeriodRatingDTO>();

    EventType prevEventType = null;
    PeriodRatingDTO period = null;

    BigDecimal lastClose =
        (BigDecimal) mapFromQuotationsClose.get(mapFromQuotationsClose.lastKey());

    Calendar currentIterationDateCal = zeroTimeCal(startDate);

    // First event
    Iterator<EventValue> eventsIt = eventListForEvtDef.iterator();
    EventValue currentEvent = null;
    if (eventsIt.hasNext()) {
      currentEvent = eventsIt.next();
    }

    for (Date currentIterationDate : mapFromQuotationsClose.keySet()) {

      currentIterationDateCal.setTime(currentIterationDate);

      BigDecimal closeForDate = (BigDecimal) mapFromQuotationsClose.get(currentIterationDate);
      if (closeForDate.compareTo(BigDecimal.ZERO) == 0)
        LOGGER.error(
            "Close for date is zero for at "
                + currentIterationDate
                + " for "
                + stock.getFriendlyName()
                + " and "
                + evtDefInfo
                + " between "
                + startDate
                + " and "
                + endDate
                + ", end calculation res is "
                + endCalcRes);

      // Some events may have been skipped (ie WE events) and must be disregarded
      while (currentEvent != null
          && currentIterationDateCal.getTime().after(currentEvent.getDate())
          && eventsIt.hasNext()) {
        currentEvent = eventsIt.next();
      }

      // We process the event when reached
      if (currentEvent != null
          && currentIterationDateCal.getTime().compareTo(currentEvent.getDate())
              == 0) { // Event date reached

        EventType eventType = currentEvent.getEventType();
        if (prevEventType == null
            || (!eventType.equals(EventType.NONE)
                && !prevEventType.equals(eventType))) { // First trend or Valid trend change

          // TO
          if (prevEventType
              != null) { // Not the First trend : We close the current period and add it to the list

            period.setTo(currentEvent.getDate());
            period.setPriceAtTo(closeForDate.doubleValue());
            period.setRealised(true);
            addFilteredPeriod(periods, period, 15);

          } else { // First trend : Nothing to close

          }

          // FROM : First trend or new one start
          period =
              new PeriodRatingDTO(
                  currentEvent.getDate(), closeForDate.doubleValue(), eventType.toString());

          // Updating loop vars
          if (eventsIt.hasNext()) {
            currentEvent = eventsIt.next();
          }
          prevEventType = eventType;

        } else { // Same trend or invalid trend (ie NONE)

          // Updating loop vars
          if (eventsIt.hasNext()) {
            currentEvent = eventsIt.next();
          }
        }
      }
    } // End for quotations dates iteration

    // Not enough data were found : we get out of here
    if (prevEventType == null) {

      String message = "No trend forecast events were found after calculation.\n";

      LOGGER.warn(
          noResMsg
              + message
              + ".\n"
              + "Stock :"
              + stock
              + "\n"
              + "Available neural events  :"
              + ((eventListForEvtDef == null) ? "null" : eventListForEvtDef.size())
              + "\n"
              + "Call Params : start "
              + startDate
              + ", end "
              + endDate
              + ", analysis "
              + analyseName
              + ", event Def "
              + evtDefInfo
              + "\n"
              + "Output :  endCalc "
              + endCalcRes
              + ", Calculated output size "
              + ((calcOutput == null) ? "null" : calcOutput.size()));

      Date firstAvailDate = new Date(0);
      Date lastAvailDate = new Date(0);
      if (mapFromQuotationsClose.size() > 0) {
        firstAvailDate = mapFromQuotationsClose.firstKey();
        lastAvailDate = mapFromQuotationsClose.lastKey();
      }
      throw new NotEnoughDataException(
          stock, firstAvailDate, lastAvailDate, noResMsg + message, new Throwable());
    }

    // Finalising last trend
    period.setTo(endDate);
    period.setPriceAtTo(lastClose.doubleValue());
    period.setRealised(prevEventType.equals(EventType.BEARISH));
    addFilteredPeriod(periods, period, -1);

    return periods;
  }