protected void exportLine( Boolean generateBuySellCsv, Boolean generateSmaCmpOutChart, Double csvDispFactor, BufferedWriter csvWriter, SortedMap<Date, Double> buySerie, SortedMap<Date, Double> sellSerie, Date periodInnerDate, Double closeForInnerDate, EventType periodTrend, double[] output) { DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss"); String line = simpleDateFormat.format(periodInnerDate); line = line + ", " + closeForInnerDate; String outputString = (output == null) ? "NaN" : output[0] + ""; if (periodTrend.equals(EventType.BULLISH)) { if (generateBuySellCsv) { line = line + ", , " + (closeForInnerDate * csvDispFactor) + ", " + outputString + "\n"; try { csvWriter.append(line); } catch (IOException e) { LOGGER.error("failed to export csv ", e); } } if (generateSmaCmpOutChart) buySerie.put(periodInnerDate, closeForInnerDate); } else if (periodTrend.equals(EventType.BEARISH)) { if (generateBuySellCsv) { line = line + ", " + (closeForInnerDate * csvDispFactor) + ", , " + outputString + "\n"; try { csvWriter.append(line); } catch (IOException e) { LOGGER.error("failed to export csv ", e); } } if (generateSmaCmpOutChart) sellSerie.put(periodInnerDate, closeForInnerDate); } else { if (generateBuySellCsv) { line = line + ", , , " + outputString + "\n"; try { csvWriter.append(line); } catch (IOException e) { LOGGER.error("failed to export csv ", e); } } } }
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; }