Merge branch 'bug/update_kline_format' into 'develop'

fix bug kline

See merge request india/india_market_java!45
This commit is contained in:
vpckiet
2024-10-10 01:47:17 +00:00
3 changed files with 117 additions and 17 deletions

View File

@@ -185,6 +185,12 @@
<version>30.1-jre</version> <version>30.1-jre</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@@ -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;
}
}

View File

@@ -9,12 +9,14 @@ import cn.stock.market.domain.basic.repository.OptionalStockRepository;
import cn.stock.market.dto.OptionalStockResponse; import cn.stock.market.dto.OptionalStockResponse;
import cn.stock.market.dto.StockHistoryRequest; import cn.stock.market.dto.StockHistoryRequest;
import cn.stock.market.dto.StockHistoryResponse; 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.infrastructure.db.po.QMoneyStockPO;
import cn.stock.market.utils.HttpRequest; import cn.stock.market.utils.HttpRequest;
import cn.stock.market.utils.NseIndiaRequest; import cn.stock.market.utils.NseIndiaRequest;
import cn.stock.market.utils.ServerResponse; import cn.stock.market.utils.ServerResponse;
import cn.stock.market.web.annotations.EncryptFilter; import cn.stock.market.web.annotations.EncryptFilter;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.cache.Cache; import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheBuilder;
@@ -28,6 +30,12 @@ import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils; 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.Jsoup;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
@@ -706,26 +714,26 @@ public class MoneyApiController {
Long to = null; Long to = null;
Long from = null; Long from = null;
int countback = 5; int countback = 5;
if (StringUtils.equals("H", resolution)) { if(StringUtils.equals("H",resolution)){
to = (long) (System.currentTimeMillis() / 1000); to = (long) (System.currentTimeMillis() / 1000);
from = to - (60 * 60); from = to - (10 * 60 * 60 );
countback = 60; countback = 328;
request.setResolution("1"); request.setResolution("60");
} else if (StringUtils.equals("D", resolution)) { }else if(StringUtils.equals("D",resolution)){
to = (long) (System.currentTimeMillis() / 1000); to = (long) (System.currentTimeMillis() / 1000);
from = to - (24 * 60 * 60); from = to - (2 * 30 * 24 * 60 * 60 );
countback = 390; countback = 329;
request.setResolution("1"); request.setResolution("1D");
} else if (StringUtils.equals("W", resolution)) { } else if (StringUtils.equals("W", resolution)) {
to = (long) (System.currentTimeMillis() / 1000); to = (long) (System.currentTimeMillis() / 1000);
from = to - (7 * 24 * 60 * 60); from = to - (7 * 24 * 60 * 60);
countback = 471; countback = 471;
request.setResolution("5"); request.setResolution("1W");
} else if (StringUtils.equals("M", resolution)) { } else if (StringUtils.equals("M", resolution)) {
to = (long) (System.currentTimeMillis() / 1000); to = (long) (System.currentTimeMillis() / 1000);
from = to - (35 * 24 * 60 * 60); from = to - (2 * 30 * 24 * 60 * 60 );
countback = 328; countback = 329;
request.setResolution("30"); request.setResolution("1D");
} }
request.setFrom(from); request.setFrom(from);
@@ -740,7 +748,38 @@ public class MoneyApiController {
while (response == null && retryCount < maxRetries) { while (response == null && retryCount < maxRetries) {
try { 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<StockChartDto> 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) { } catch (RestClientException e) {
// Log the exception or perform any other error handling // Log the exception or perform any other error handling
log.error("Error while making API request. Retrying... (Retry count: {})", retryCount + 1); log.error("Error while making API request. Retrying... (Retry count: {})", retryCount + 1);
@@ -753,11 +792,18 @@ public class MoneyApiController {
} catch (InterruptedException ex) { } catch (InterruptedException ex) {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} }
} catch (ClientProtocolException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} }
} }
if (response != null && !response.getS().equals("error")) { if (response != null && !response.getS().equals("error")) {
setResponse(response, resolution); if (StringUtils.equals("M", resolution)) {
}
// setResponse(response, resolution);
// API request successful, return the response // API request successful, return the response
return ResponseEntity.ok(response); return ResponseEntity.ok(response);
} else { } else {