List of usage examples for java.time LocalTime plus
@Override public LocalTime plus(long amountToAdd, TemporalUnit unit)
From source file:Main.java
public static void main(String[] args) { LocalTime l = LocalTime.now(); LocalTime s = l.plus(14, ChronoUnit.MINUTES); System.out.println(s);//from w ww . j a v a2 s . c o m }
From source file:Main.java
public static void main(String[] args) { LocalTime localTime = LocalTime.of(11, 20, 50); System.out.println(localTime.plus(3, ChronoUnit.HOURS)); System.out.println(localTime.plus(Duration.ofDays(3))); //11:20:50 try {/* w w w . j a va 2 s .c o m*/ System.out.println(localTime.plus(Period.ofDays(3))); } catch (UnsupportedTemporalTypeException e) { e.printStackTrace(); } }
From source file:ch.algotrader.service.algo.VWAPOrderService.java
VWAPOrderStateVO createAlgoOrderState(final VWAPOrder algoOrder, final Date dateTime) throws OrderValidationException { Validate.notNull(algoOrder, "vwapOrder missing"); Security security = algoOrder.getSecurity(); SecurityFamily family = security.getSecurityFamily(); Exchange exchange = family.getExchange(); HistoricalDataService historicalDataService = this.applicationContext.getBean(HistoricalDataService.class); List<Bar> bars = historicalDataService.getHistoricalBars(security.getId(), // DateUtils.truncate(new Date(), Calendar.DATE), // algoOrder.getLookbackPeriod(), // TimePeriod.DAY, // algoOrder.getBucketSize(), // MarketDataEventType.TRADES, // Collections.emptyMap()); TreeMap<LocalTime, Long> buckets = new TreeMap<>(); Set<LocalDate> tradingDays = new HashSet<>(); for (Bar bar : bars) { int vol = bar.getVol(); LocalTime time = DateTimeLegacy.toLocalTime(bar.getDateTime()); tradingDays.add(DateTimeLegacy.toLocalDate(bar.getDateTime())); if (buckets.containsKey(time)) { buckets.put(time, buckets.get(time) + vol); } else {// w w w . j a va2 s.c o m buckets.put(time, (long) vol); } } // verify start and end time if (algoOrder.getStartTime() == null) { if (this.calendarService.isOpen(exchange.getId())) { algoOrder.setStartTime(dateTime); } else { Date nextOpenTime = this.calendarService.getNextOpenTime(exchange.getId()); algoOrder.setStartTime(nextOpenTime); } } Date closeTime = this.calendarService.getNextCloseTime(exchange.getId()); if (algoOrder.getEndTime() == null) { algoOrder.setEndTime(closeTime); } if (algoOrder.getStartTime().compareTo(dateTime) < 0) { throw new OrderValidationException("startTime needs to be in the future " + algoOrder); } else if (algoOrder.getEndTime().compareTo(dateTime) <= 0) { throw new OrderValidationException("endTime needs to be in the future " + algoOrder); } else if (algoOrder.getEndTime().compareTo(closeTime) > 0) { throw new OrderValidationException("endTime needs to be before next market closeTime for " + algoOrder); } else if (algoOrder.getEndTime().compareTo(algoOrder.getStartTime()) <= 0) { throw new OrderValidationException("endTime needs to be after startTime for " + algoOrder); } int historicalVolume = 0; LocalTime startTime = DateTimeLegacy.toLocalTime(algoOrder.getStartTime()); LocalTime endTime = DateTimeLegacy.toLocalTime(algoOrder.getEndTime()); LocalTime firstBucketStart = buckets.floorKey(startTime); LocalTime lastBucketStart = buckets.floorKey(endTime); SortedMap<LocalTime, Long> subBuckets = buckets.subMap(firstBucketStart, true, lastBucketStart, true); for (Map.Entry<LocalTime, Long> bucket : subBuckets.entrySet()) { long vol = bucket.getValue() / tradingDays.size(); bucket.setValue(vol); if (bucket.getKey().equals(firstBucketStart)) { LocalTime firstBucketEnd = firstBucketStart.plus(algoOrder.getBucketSize().getValue(), ChronoUnit.MILLIS); double fraction = (double) ChronoUnit.MILLIS.between(startTime, firstBucketEnd) / algoOrder.getBucketSize().getValue(); historicalVolume += vol * fraction; } else if (bucket.getKey().equals(lastBucketStart)) { double fraction = (double) ChronoUnit.MILLIS.between(lastBucketStart, endTime) / algoOrder.getBucketSize().getValue(); historicalVolume += vol * fraction; } else { historicalVolume += vol; } } double participation = algoOrder.getQuantity() / (double) historicalVolume; if (participation > MAX_PARTICIPATION) { throw new OrderValidationException("participation rate " + twoDigitFormat.format(participation * 100.0) + "% is above 50% of historical market volume for " + algoOrder); } if (LOGGER.isInfoEnabled()) { LOGGER.debug("participation of {} is {}%", algoOrder.getDescription(), twoDigitFormat.format(participation * 100.0)); } return new VWAPOrderStateVO(participation, buckets); }