| | |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.yami.trading.bean.data.domain.Kline; |
| | | import com.yami.trading.bean.data.domain.Realtime; |
| | | import com.yami.trading.bean.data.dto.BeforeClose; |
| | | import com.yami.trading.bean.ico.domain.Ico; |
| | | import com.yami.trading.bean.item.domain.Item; |
| | | import com.yami.trading.common.domain.Result; |
| | |
| | | import com.yami.trading.common.util.DateUtils; |
| | | import com.yami.trading.common.web.ResultObject; |
| | | import com.yami.trading.huobi.data.AdjustmentValueCache; |
| | | import com.yami.trading.huobi.data.DataCache; |
| | | import com.yami.trading.huobi.data.internal.DataDBService; |
| | | import com.yami.trading.huobi.data.internal.KlineService; |
| | | import com.yami.trading.service.data.DataService; |
| | | import com.yami.trading.service.etf.MarketService; |
| | |
| | | @Autowired |
| | | private IcoService icoService; |
| | | |
| | | @Autowired |
| | | private DataDBService dataDBService; |
| | | |
| | | @ApiOperation(value = "行情") |
| | | @GetMapping(HOBI + "getKline.action") |
| | | public Result<List<Map<String, Object>>> getKline(@RequestParam String symbol, @RequestParam String line) { |
| | |
| | | } |
| | | // 数据处理 |
| | | List<Kline> data = this.dataService.kline(symbol, line); |
| | | Item item = itemService.findBySymbol(symbol); |
| | | if (item != null) { |
| | | if (item.getType().equals(Item.cryptos) && (item.getCurrencyType() != null && item.getCurrencyType() == 1)) { |
| | | /*QueryWrapper<Ico> iQuery = new QueryWrapper<>(); |
| | | iQuery.eq("symbol", symbol); |
| | | iQuery.eq("symbol_data", item.getSymbolData()); |
| | | Ico ico = icoService.getOne(iQuery); |
| | | if (ico != null) { |
| | | long now = ico.getMarketDate().getTime();*/ |
| | | long now = item.getCreateTimeTs() * 1000; |
| | | data = data.stream().filter(kline -> kline != null |
| | | && kline.getTs() != null |
| | | && kline.getTs() > now) |
| | | .collect(Collectors.toList()); |
| | | //} |
| | | } |
| | | } |
| | | |
| | | if ("1day".equals(line) || "5day".equals(line) || "1mon".equals(line) || "1week".equals(line) || "quarter".equalsIgnoreCase(line) |
| | | || "year".equalsIgnoreCase(line)) { |
| | | for (Kline datum : data) { |
| | |
| | | DateUtils.timeStamp2Date(String.valueOf(datum.getTs()), "MM-dd HH:mm")); |
| | | } |
| | | } |
| | | |
| | | BigDecimal currentValue = AdjustmentValueCache.getCurrentValue().get(symbol); |
| | | if (currentValue != null) { |
| | | data.forEach(kline -> { |
| | | /*logger.info("==currentValue==close:{}, low:{}, high:{}, open:{}, currentValue:{}", |
| | | kline.getClose(), kline.getLow(), kline.getHigh(), kline.getOpen(), currentValue);*/ |
| | | if (!kline.isAdjusted()){ |
| | | if (kline.getClose().compareTo(kline.getLow()) >= 0 && kline.getClose().compareTo(kline.getHigh()) <= 0) { |
| | | kline.setClose(kline.getClose().add(currentValue)); |
| | | } |
| | | kline.setOpen(kline.getOpen().add(currentValue)); |
| | | kline.setLow(kline.getLow().add(currentValue)); |
| | | kline.setHigh(kline.getHigh().add(currentValue)); |
| | | kline.setAdjusted(true); |
| | | } |
| | | }); |
| | | } |
| | | return Result.succeed(this.build(data, line, symbol)); |
| | | } catch (Exception e) { |
| | | logger.error("getKline error", e); |
| | | throw new YamiShopBindException("k线图获取失败"); |
| | | } |
| | | } |
| | | |
| | | |
| | | private List<Map<String, Object>> build(List<Kline> data, String line, String symbol) { |
| | | Collections.sort(data); |
| | |
| | | map.put("high", high.setScale(decimal, RoundingMode.HALF_UP)); |
| | | map.put("low", low.setScale(decimal, RoundingMode.HALF_UP)); |
| | | map.put("volume", kline.getVolume()); |
| | | |
| | | if (i == data.size() - 1 && isRollingIntradayLine(line)) { |
| | | Realtime realtime = DataCache.getLatestRealTime(symbol); |
| | | BigDecimal latestClose = close; |
| | | if (realtime != null && realtime.getClose() != null) { |
| | | latestClose = realtime.getClose(); |
| | | map.put("close", latestClose.setScale(decimal, RoundingMode.HALF_UP)); |
| | | } |
| | | BeforeClose beforeClose = dataDBService.getBeforeClose(kline.getSymbol(), line, ts, realtime); |
| | | BigDecimal periodHigh = high; |
| | | BigDecimal periodLow = low; |
| | | if (beforeClose != null) { |
| | | if (beforeClose.getMaxClose() != null && beforeClose.getMaxClose().compareTo(BigDecimal.ZERO) > 0) { |
| | | periodHigh = beforeClose.getMaxClose(); |
| | | } |
| | | if (beforeClose.getMinClose() != null && beforeClose.getMinClose().compareTo(BigDecimal.ZERO) > 0) { |
| | | periodLow = beforeClose.getMinClose(); |
| | | } |
| | | } |
| | | periodHigh = periodHigh.max(latestClose); |
| | | periodLow = periodLow.min(latestClose); |
| | | map.put("high", periodHigh.setScale(decimal, RoundingMode.HALF_UP)); |
| | | map.put("low", periodLow.setScale(decimal, RoundingMode.HALF_UP)); |
| | | } |
| | | list.add(map); |
| | | } |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * 5/15/30/60 分钟未收盘 K 线需用当前周期内的最高/最低价刷新 |
| | | */ |
| | | private boolean isRollingIntradayLine(String line) { |
| | | return Kline.PERIOD_5MIN.equalsIgnoreCase(line) |
| | | || Kline.PERIOD_15MIN.equalsIgnoreCase(line) |
| | | || Kline.PERIOD_30MIN.equalsIgnoreCase(line) |
| | | || Kline.PERIOD_60MIN.equalsIgnoreCase(line); |
| | | } |
| | | |
| | | } |