Merge branch 'bug/update_kline_format' into 'develop'
fix bug kline See merge request india/india_market_java!45
This commit is contained in:
6
pom.xml
6
pom.xml
@@ -185,6 +185,12 @@
|
||||
<version>30.1-jre</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>4.5.13</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
48
src/main/java/cn/stock/market/dto/query/StockChartDto.java
Normal file
48
src/main/java/cn/stock/market/dto/query/StockChartDto.java
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
@@ -708,24 +716,24 @@ public class MoneyApiController {
|
||||
int countback = 5;
|
||||
if(StringUtils.equals("H",resolution)){
|
||||
to = (long) (System.currentTimeMillis() / 1000);
|
||||
from = to - (60 * 60);
|
||||
countback = 60;
|
||||
request.setResolution("1");
|
||||
from = to - (10 * 60 * 60 );
|
||||
countback = 328;
|
||||
request.setResolution("60");
|
||||
}else if(StringUtils.equals("D",resolution)){
|
||||
to = (long) (System.currentTimeMillis() / 1000);
|
||||
from = to - (24 * 60 * 60);
|
||||
countback = 390;
|
||||
request.setResolution("1");
|
||||
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");
|
||||
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 {
|
||||
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) {
|
||||
// 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 {
|
||||
|
||||
Reference in New Issue
Block a user