From 425754134215b9cdf2d19b8ee1cad0d530b46fab Mon Sep 17 00:00:00 2001 From: vpckiet Date: Wed, 9 Oct 2024 17:18:26 +0700 Subject: [PATCH] fix bug kline --- pom.xml | 6 ++ .../stock/market/dto/query/StockChartDto.java | 48 +++++++++++ .../stock/market/web/MoneyApiController.java | 80 +++++++++++++++---- 3 files changed, 117 insertions(+), 17 deletions(-) create mode 100644 src/main/java/cn/stock/market/dto/query/StockChartDto.java diff --git a/pom.xml b/pom.xml index 870d991..bd70d8f 100644 --- a/pom.xml +++ b/pom.xml @@ -185,6 +185,12 @@ 30.1-jre + + org.apache.httpcomponents + httpclient + 4.5.13 + + diff --git a/src/main/java/cn/stock/market/dto/query/StockChartDto.java b/src/main/java/cn/stock/market/dto/query/StockChartDto.java new file mode 100644 index 0000000..08f2263 --- /dev/null +++ b/src/main/java/cn/stock/market/dto/query/StockChartDto.java @@ -0,0 +1,48 @@ +package cn.stock.market.dto.query; + +import lombok.Data; + +import java.time.LocalDate; + +@Data +public class StockChartDto { + private Long timestamp; + private double open; + private double high; + private double low; + private double close; + private double volume; + + public StockChartDto(Long date, double open, double high, double low, double close, double volume) { + this.timestamp = date; + this.open = open; + this.high = high; + this.low = low; + this.close = close; + this.volume = volume; + } + + public double getVolume() { + return volume; + } + + public Long getTimestamp() { + return timestamp; + } + + public double getOpen() { + return open; + } + + public double getHigh() { + return high; + } + + public double getLow() { + return low; + } + + public double getClose() { + return close; + } +} diff --git a/src/main/java/cn/stock/market/web/MoneyApiController.java b/src/main/java/cn/stock/market/web/MoneyApiController.java index c4dcee6..6204e7f 100644 --- a/src/main/java/cn/stock/market/web/MoneyApiController.java +++ b/src/main/java/cn/stock/market/web/MoneyApiController.java @@ -9,12 +9,14 @@ import cn.stock.market.domain.basic.repository.OptionalStockRepository; import cn.stock.market.dto.OptionalStockResponse; import cn.stock.market.dto.StockHistoryRequest; import cn.stock.market.dto.StockHistoryResponse; +import cn.stock.market.dto.query.StockChartDto; import cn.stock.market.infrastructure.db.po.QMoneyStockPO; import cn.stock.market.utils.HttpRequest; import cn.stock.market.utils.NseIndiaRequest; import cn.stock.market.utils.ServerResponse; import cn.stock.market.web.annotations.EncryptFilter; import com.alibaba.fastjson.JSONObject; +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; @@ -28,6 +30,12 @@ import io.swagger.annotations.ApiResponses; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.http.HttpResponse; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; @@ -706,26 +714,26 @@ public class MoneyApiController { Long to = null; Long from = null; int countback = 5; - if (StringUtils.equals("H", resolution)) { - to = (long) (System.currentTimeMillis() / 1000); - from = to - (60 * 60); - countback = 60; - request.setResolution("1"); - } else if (StringUtils.equals("D", resolution)) { - to = (long) (System.currentTimeMillis() / 1000); - from = to - (24 * 60 * 60); - countback = 390; - request.setResolution("1"); + if(StringUtils.equals("H",resolution)){ + to = (long) (System.currentTimeMillis() / 1000); + from = to - (10 * 60 * 60 ); + countback = 328; + request.setResolution("60"); + }else if(StringUtils.equals("D",resolution)){ + to = (long) (System.currentTimeMillis() / 1000); + from = to - (2 * 30 * 24 * 60 * 60 ); + countback = 329; + request.setResolution("1D"); } else if (StringUtils.equals("W", resolution)) { to = (long) (System.currentTimeMillis() / 1000); from = to - (7 * 24 * 60 * 60); countback = 471; - request.setResolution("5"); + request.setResolution("1W"); } else if (StringUtils.equals("M", resolution)) { - to = (long) (System.currentTimeMillis() / 1000); - from = to - (35 * 24 * 60 * 60); - countback = 328; - request.setResolution("30"); + to = (long) (System.currentTimeMillis() / 1000); + from = to - (2 * 30 * 24 * 60 * 60 ); + countback = 329; + request.setResolution("1D"); } request.setFrom(from); @@ -740,7 +748,38 @@ public class MoneyApiController { while (response == null && retryCount < maxRetries) { try { - response = restTemplate.getForObject(apiUrl, StockHistoryResponse.class); + if (StringUtils.equals("M", resolution)) { + CloseableHttpClient client = HttpClients.createDefault(); + HttpGet req = new HttpGet(apiUrl); + HttpResponse resp = client.execute(req); + String jsonResponse = EntityUtils.toString(resp.getEntity(), "UTF-8"); + ObjectMapper mapper = new ObjectMapper(); + JsonNode rootNode = mapper.readTree(jsonResponse); + + JsonNode timeNode = rootNode.get("t"); + JsonNode lowNode = rootNode.get("l"); + JsonNode highNode = rootNode.get("h"); + JsonNode openNode = rootNode.get("o"); + JsonNode closeNode = rootNode.get("c"); + JsonNode volumeNote = rootNode.get("v"); + + List stocks = new ArrayList<>(); + for (int i = 0; i < timeNode.size(); i++) { + long timestamp = timeNode.get(i).asLong(); + double closePrice = closeNode.get(i).asDouble(); + double openPrice = openNode.get(i).asDouble(); + double volume = volumeNote.get(i).asDouble(); + double low = lowNode.get(i).asDouble(); + double high = highNode.get(i).asDouble(); + + stocks.add(new StockChartDto(timestamp, openPrice, closePrice, volume, low, high)); + } + + response = restTemplate.getForObject(apiUrl, StockHistoryResponse.class); + + } else { + response = restTemplate.getForObject(apiUrl, StockHistoryResponse.class); + } } catch (RestClientException e) { // Log the exception or perform any other error handling log.error("Error while making API request. Retrying... (Retry count: {})", retryCount + 1); @@ -753,11 +792,18 @@ public class MoneyApiController { } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } + } catch (ClientProtocolException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); } } if (response != null && !response.getS().equals("error")) { - setResponse(response, resolution); + if (StringUtils.equals("M", resolution)) { + + } +// setResponse(response, resolution); // API request successful, return the response return ResponseEntity.ok(response); } else {