| | |
| | | return Result.succeed(this.build(data, line, symbol)); |
| | | } catch (Exception e) { |
| | | logger.error("getKline error", e); |
| | | throw new YamiShopBindException(""); |
| | | throw new YamiShopBindException("Failed to obtain the K-line chart"); |
| | | } |
| | | } |
| | | |
| | | private List<Map<String, Object>> build(List<Kline> data, String line, String symbol) { |
| | | Collections.sort(data); |
| | | // 相同时间戳保留最新一条(进行中K线会覆盖缓存中的旧数据) |
| | | Map<Long, Kline> latestByTs = new LinkedHashMap<>(); |
| | | for (Kline kline : data) { |
| | | latestByTs.put(kline.getTs(), kline); |
| | | } |
| | | data = new ArrayList<>(latestByTs.values()); |
| | | Collections.sort(data); |
| | | int len = data.size(); |
| | | for (int i = 1; i < len; i++) { |
| | | data.get(i).setOpen(data.get(i - 1).getClose()); |
| | | } |
| | | |
| | | |
| | | Set<Long> tsSet = new HashSet<Long>(); |
| | | List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); |
| | | Item bySymbol = itemService.findBySymbol(symbol); |
| | | |
| | | for (int i = 0; i < data.size(); i++) { |
| | | Kline kline = data.get(i); |
| | | Long ts = kline.getTs(); |
| | | if (tsSet.contains(ts)) { |
| | | continue; |
| | | } else { |
| | | tsSet.add(ts); |
| | | } |
| | | String fake = bySymbol.getFake(); |
| | | // if("1".equalsIgnoreCase(fake)){ |
| | | // klineService.smoothlyKline(kline, 0.99); |
| | |
| | | map.put("low", low.setScale(decimal, RoundingMode.HALF_UP)); |
| | | map.put("volume", kline.getVolume()); |
| | | |
| | | if (i == data.size() - 1) { |
| | | Realtime realtime = DataCache.getLatestRealTime(symbol); |
| | | if (realtime != null && realtime.getClose() != null) { |
| | | close = realtime.getClose(); |
| | | map.put("close", close.setScale(decimal, RoundingMode.HALF_UP)); |
| | | if (close.compareTo(high) > 0) { |
| | | high = close; |
| | | //if (line.equalsIgnoreCase(Kline.PERIOD_15MIN) || line.equalsIgnoreCase(Kline.PERIOD_30MIN) || line.equalsIgnoreCase(Kline.PERIOD_60MIN)) { |
| | | if (i == data.size() - 1) { |
| | | Realtime realtime = DataCache.getLatestRealTime(symbol); |
| | | if (realtime != null && realtime.getClose() != null && realtime.getClose().compareTo(BigDecimal.ZERO) > 0) { |
| | | map.put("close", realtime.getClose().setScale(decimal, RoundingMode.HALF_UP)); |
| | | } |
| | | if (close.compareTo(low) < 0) { |
| | | low = close; |
| | | BeforeClose beforeClose = dataDBService.getBeforeClose(kline.getSymbol(), line, ts, realtime); |
| | | if (beforeClose != null) { |
| | | map.put("high", beforeClose.getMaxClose().setScale(decimal, RoundingMode.HALF_UP)); |
| | | map.put("low", beforeClose.getMinClose().setScale(decimal, RoundingMode.HALF_UP)); |
| | | } |
| | | BigDecimal openVal = (BigDecimal) map.get("open"); |
| | | BigDecimal closeVal = (BigDecimal) map.get("close"); |
| | | BigDecimal highVal = (BigDecimal) map.get("high"); |
| | | BigDecimal lowVal = (BigDecimal) map.get("low"); |
| | | if (openVal != null && closeVal != null) { |
| | | map.put("high", highVal.max(openVal).max(closeVal)); |
| | | map.put("low", lowVal.min(openVal).min(closeVal)); |
| | | } |
| | | } |
| | | BeforeClose beforeClose = dataDBService.getBeforeClose(kline.getSymbol(), line, ts, realtime); |
| | | if (beforeClose != null) { |
| | | if (beforeClose.getMaxClose() != null |
| | | && beforeClose.getMaxClose().compareTo(BigDecimal.ZERO) > 0) { |
| | | high = beforeClose.getMaxClose(); |
| | | } |
| | | if (beforeClose.getMinClose() != null |
| | | && beforeClose.getMinClose().compareTo(BigDecimal.ZERO) > 0) { |
| | | low = beforeClose.getMinClose(); |
| | | } |
| | | } |
| | | map.put("high", high.setScale(decimal, RoundingMode.HALF_UP)); |
| | | map.put("low", low.setScale(decimal, RoundingMode.HALF_UP)); |
| | | } |
| | | //} |
| | | list.add(map); |
| | | } |
| | | return list; |