From c19277e39050a7789719db767df26bef240856cd Mon Sep 17 00:00:00 2001 From: vpckiet Date: Thu, 10 Oct 2024 10:50:01 +0700 Subject: [PATCH] update --- .../stock/market/web/MoneyApiController.java | 62 ++++++++++++++++--- 1 file changed, 53 insertions(+), 9 deletions(-) diff --git a/src/main/java/cn/stock/market/web/MoneyApiController.java b/src/main/java/cn/stock/market/web/MoneyApiController.java index 6204e7f..adfc339 100644 --- a/src/main/java/cn/stock/market/web/MoneyApiController.java +++ b/src/main/java/cn/stock/market/web/MoneyApiController.java @@ -55,6 +55,10 @@ import org.springframework.web.util.UriUtils; import java.io.IOException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; +import java.time.Instant; +import java.time.YearMonth; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; import java.util.*; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; @@ -722,17 +726,17 @@ public class MoneyApiController { }else if(StringUtils.equals("D",resolution)){ to = (long) (System.currentTimeMillis() / 1000); from = to - (2 * 30 * 24 * 60 * 60 ); - countback = 329; + countback = 730; request.setResolution("1D"); } else if (StringUtils.equals("W", resolution)) { to = (long) (System.currentTimeMillis() / 1000); from = to - (7 * 24 * 60 * 60); - countback = 471; + countback = 730; request.setResolution("1W"); } else if (StringUtils.equals("M", resolution)) { to = (long) (System.currentTimeMillis() / 1000); - from = to - (2 * 30 * 24 * 60 * 60 ); - countback = 329; + from = to - (15 * 30 * 24 * 60 * 60); + countback = 730; request.setResolution("1D"); } @@ -772,10 +776,53 @@ public class MoneyApiController { double low = lowNode.get(i).asDouble(); double high = highNode.get(i).asDouble(); - stocks.add(new StockChartDto(timestamp, openPrice, closePrice, volume, low, high)); + stocks.add(new StockChartDto(timestamp, openPrice, high, low, closePrice, volume)); } - response = restTemplate.getForObject(apiUrl, StockHistoryResponse.class); + Map> groupedByMonth = stocks.stream() + .collect(Collectors.groupingBy( + sp -> Instant.ofEpochSecond(sp.getTimestamp()) + .atZone(ZoneId.systemDefault()) + .toLocalDate() + .format(DateTimeFormatter.ofPattern("yyyy-MM")), + LinkedHashMap::new, + Collectors.toList() + )); + + List timestamps = new ArrayList<>(); + List opens = new ArrayList<>(); + List closes = new ArrayList<>(); + List highs = new ArrayList<>(); + List lows = new ArrayList<>(); + List volumes = new ArrayList<>(); + + response = new StockHistoryResponse(); + groupedByMonth.forEach((month, prices) -> { + double open = prices.get(0).getOpen(); + double close = prices.get(prices.size() - 1).getClose(); + double high = prices.stream().mapToDouble(StockChartDto::getHigh).max().orElse(0); + double low = prices.stream().mapToDouble(StockChartDto::getLow).min().orElse(0); + double volume = prices.stream().mapToDouble(StockChartDto::getVolume).sum(); + + long timestamp = YearMonth.parse(month, DateTimeFormatter.ofPattern("yyyy-MM")).atDay(1).atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli() / 1000; + timestamps.add(timestamp); + opens.add(open); + closes.add(close); + highs.add(high); + lows.add(low); + volumes.add((long) volume); + + System.out.println("Month: " + month); + System.out.println("Open: " + open + ", Close: " + close + ", High: " + high + ", Low: " + low); + }); + + response.setS("ok"); + response.setT(timestamps); + response.setL(lows); + response.setH(highs); + response.setO(opens); + response.setC(closes); + response.setV(volumes); } else { response = restTemplate.getForObject(apiUrl, StockHistoryResponse.class); @@ -800,9 +847,6 @@ public class MoneyApiController { } if (response != null && !response.getS().equals("error")) { - if (StringUtils.equals("M", resolution)) { - - } // setResponse(response, resolution); // API request successful, return the response return ResponseEntity.ok(response);