private void addFilteredPeriod( List<PeriodRatingDTO> periods, PeriodRatingDTO period, int sizeConstraint) { if ((periods.size() == 0) && EventType.valueOf(period.getTrend()).equals(EventType.BULLISH)) { LOGGER.info("First bullish period discarded : " + period); return; } if (sizeConstraint != -1 && period.getPeriodLenght() < sizeConstraint) { String invFlasePositiveTrend = (EventType.valueOf(period.getTrend()).equals(EventType.BULLISH)) ? EventType.NONE.toString() : EventType.BULLISH.toString(); LOGGER.info( "Period is too short (false positive) : " + period + ". Trend will be set as " + invFlasePositiveTrend); period.setTrend(invFlasePositiveTrend); if ((periods.size() == 0) && EventType.valueOf(period.getTrend()).equals(EventType.BULLISH)) { LOGGER.info("First bullish period discarded : " + period); return; } } PeriodRatingDTO previousPeriod; if (periods.size() > 0 && (previousPeriod = periods.get(periods.size() - 1)) .getTrend() .equals(period.getTrend())) { previousPeriod.setTo(period.getTo()); previousPeriod.setPriceAtTo(period.getPriceAtTo()); previousPeriod.setRealised(period.isRealised()); } else { periods.add(period); } }
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; }