trading-order-admin/src/main/java/com/yami/trading/admin/controller/data/AdminKlineController.java
@@ -7,6 +7,7 @@ import com.yami.trading.common.util.MarketOpenChecker; import com.yami.trading.common.util.UTCDateUtils; import com.yami.trading.huobi.data.internal.CryptosKlineService; import com.yami.trading.huobi.data.internal.DataDBService; import com.yami.trading.huobi.data.internal.KlineInitService; import com.yami.trading.huobi.data.internal.KlineService; import com.yami.trading.service.item.ItemService; @@ -43,6 +44,8 @@ private KlineService klineService; @Autowired private CryptosKlineService cryptosKlineService; @Autowired private DataDBService dataDBService; /** * kline初始化 @@ -85,4 +88,9 @@ return Result.ok("success"); } @GetMapping("cacheBefore24Hour") public Result <String> cacheBefore24Hour(String symbol) { dataDBService.cacheBefore24Hour(symbol); return Result.ok("success"); } } trading-order-admin/src/main/java/com/yami/trading/admin/task/RealtimePushJob.java
@@ -125,7 +125,7 @@ map.put("low", realtime.getLow()); BigDecimal currentValue = AdjustmentValueCache.getCurrentValue().get(symbol); if (currentValue != null) { /*if (currentValue != null) { map.put("low", realtime.getLow().add(currentValue)); } if (currentValue != null) { @@ -134,7 +134,7 @@ map.put("high", realtime.getHigh().add(currentValue)); } map.put("low", newLow); } }*/ if( realtime.getVolume() != null){ map.put("volume", realtime.getVolume().setScale(2, RoundingMode.HALF_UP)); @@ -181,13 +181,13 @@ map.put("high", high != null ? high.setScale(decimal, RoundingMode.HALF_UP) : null); BigDecimal low = realtime.getLow(); map.put("low", low != null ? low.setScale(decimal, RoundingMode.HALF_UP) : null); if (currentValue != null) { /*if (currentValue != null) { BigDecimal newLow = low != null ? low.add(currentValue).setScale(decimal, RoundingMode.HALF_UP) : BigDecimal.ZERO; if (newLow.compareTo(high) > 0) { map.put("high", high.add(currentValue).setScale(decimal, RoundingMode.HALF_UP)); } map.put("low", newLow); } }*/ } } trading-order-admin/src/main/java/com/yami/trading/admin/task/RealtimeWebsocketServer.java
@@ -112,20 +112,22 @@ realtime.setAmount(new BigDecimal(amount)); if (currentValue != null && currentValue != 0) { realtime.setClose(new BigDecimal(Arith.add(close, currentValue.doubleValue()))); realtime.setClose(new BigDecimal(Arith.add(close, currentValue))); //realtime.setVolume(new BigDecimal(Arith.add(vol, Arith.mul(Arith.div(currentValue, close), vol)))); //realtime.setAmount(new BigDecimal(Arith.add(amount, Arith.mul(Arith.div(currentValue, close), amount)))); Double high = DataCache.getRealtimeHigh().get(symbol); Double low = DataCache.getRealtimeLow().get(symbol); if (high != null) { realtime.setHigh(BigDecimal.valueOf(high)); } if (low != null) { realtime.setLow(BigDecimal.valueOf(low)); } } Double high = DataCache.getRealtimeHigh().get(symbol); Double low = DataCache.getRealtimeLow().get(symbol); if (high == null || realtime.getClose().doubleValue() > high) { DataCache.getRealtimeHigh().put(symbol, close); } if ((low == null || realtime.getClose().doubleValue() < low) && realtime.getClose().doubleValue() > 0) { DataCache.getRealtimeLow().put(symbol, close); } Realtime current = DataCache.getRealtime(symbol); if (current == null || current.getTs() != event.getTs()) { @@ -135,13 +137,13 @@ realtime.setAmount(realtime.getAmount().multiply(item.getMultiple())); } if (high != null && high >= realtime.getClose().doubleValue()) { /*if (high != null && high >= realtime.getClose().doubleValue()) { realtime.setHigh(new BigDecimal(high)); } if (low != null && low <= realtime.getClose().doubleValue()) { realtime.setLow(new BigDecimal(low)); } }*/ /*ouble h24Before = DataCache.getRealtime24HBeforeOpen().get(symbol); if (h24Before != null) { trading-order-huobi/src/main/java/com.yami.trading.huobi/data/internal/DataDBService.java
@@ -23,6 +23,11 @@ public Realtime getBefore(String symbol); /** * 缓存数据库前24小时最高最低价格 */ public void cacheBefore24Hour(String symbol); /** * 批量保存 */ public void saveBatch(List<Realtime> entities); trading-order-huobi/src/main/java/com.yami.trading.huobi/data/internal/DataDBServiceImpl.java
@@ -24,6 +24,7 @@ import java.math.BigDecimal; import java.math.RoundingMode; import java.time.*; import java.time.temporal.ChronoUnit; import java.util.*; import java.util.stream.Collectors; @@ -57,8 +58,8 @@ if (itemService.isSuspended(symbol)) { return; } /*if (realtime.getSymbol().equals("axsusdt")) { System.out.println("axsusdt1" + realtime); /*if (realtime.getSymbol().equals("galausdt")) { System.out.println("galausdt111" + realtime); }*/ DataCache.putLatestRealTime(symbol, realtime); DataCache.putLatestOpen(symbol, realtime.getOpen()); @@ -138,6 +139,36 @@ return realtime; } @Override public void cacheBefore24Hour(String symbol) { // 计算“24小时前”的时间戳(毫秒级) long twentyFourHoursAgo = Instant.now().minus(24, ChronoUnit.HOURS).toEpochMilli(); RequestDataHelper.set("symbol", symbol); LambdaQueryWrapper<Realtime> queryWrapper = new LambdaQueryWrapper<Realtime>() .eq(Realtime::getSymbol, symbol) .ge(Realtime::getTs, twentyFourHoursAgo) // 时间戳 >= 24小时前(前24小时内) .orderByDesc(Realtime::getClose) // 24小时最高 .last("LIMIT 1"); Realtime realtimeHigh = realtimeService.getBaseMapper().selectOne(queryWrapper); LambdaQueryWrapper<Realtime> queryWrapper2 = new LambdaQueryWrapper<Realtime>() .eq(Realtime::getSymbol, symbol) .ge(Realtime::getTs, twentyFourHoursAgo) // 时间戳 >= 24小时前(前24小时内) .orderByAsc(Realtime::getClose) // 24小时最低 .last("LIMIT 1"); Realtime realtimeLow = realtimeService.getBaseMapper().selectOne(queryWrapper2); RequestDataHelper.clear(); System.out.println("realtimeHigh:" + realtimeHigh); System.out.println("realtimeLow:" + realtimeLow); if (realtimeHigh != null) { DataCache.getRealtimeHigh().put(symbol, realtimeHigh.getClose().doubleValue()); System.out.println("putRealtimeHigh:" + realtimeHigh.getClose().doubleValue()); } if (realtimeLow != null) { DataCache.getRealtimeLow().put(symbol, realtimeLow.getClose().doubleValue()); System.out.println("putRealtimeLow:" + realtimeLow.getClose().doubleValue()); } } public void deleteRealtime(int days) { for (int i = 0; i <= Constants.TABLE_PARTITIONS - 1; i++) { Map<String, Object> parameters = new HashMap(); trading-order-huobi/src/main/java/com.yami.trading.huobi/data/job/AbstractGetDataJob.java
@@ -184,11 +184,17 @@ realtime.setTs(Long.valueOf(realtime.getTs() + "000")); } realtime.setName(item.getName()); if (high == null || realtime.getHigh().doubleValue() > high) { /*if (high == null || realtime.getHigh().doubleValue() > high) { DataCache.getRealtimeHigh().put(symbol, realtime.getHigh().doubleValue()); } if ((low == null || realtime.getLow().doubleValue() < low) && realtime.getLow().doubleValue() > 0) { DataCache.getRealtimeLow().put(symbol, realtime.getLow().doubleValue()); }*/ if (high != null) { realtime.setHigh(BigDecimal.valueOf(high)); } if (low != null) { realtime.setLow(BigDecimal.valueOf(low)); } this.dataDBService.saveAsyn(realtime); } trading-order-huobi/src/main/java/com.yami.trading.huobi/data/job/HighLowHandleJob.java
@@ -4,6 +4,8 @@ import com.yami.trading.bean.item.domain.Item; import com.yami.trading.common.util.ThreadUtils; import com.yami.trading.huobi.data.DataCache; import com.yami.trading.huobi.data.internal.DataDBService; import com.yami.trading.service.data.DataService; import com.yami.trading.service.item.ItemService; import com.yami.trading.service.syspara.SysparaService; import org.slf4j.Logger; @@ -15,6 +17,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; /** * 最高最低修正 @@ -34,31 +37,45 @@ private SysparaService sysparaService; @Autowired private ItemService itemService; @Autowired private DataService dataService; @Autowired DataDBService dataDBService; public void start() { new Thread(this, "HighLowHandleJob").start(); } @Override public void run() { ThreadUtils.sleep(1000 * 60 * 3); bulidHighLow(); ThreadUtils.sleep(1000 * 60 * 5); while (true) { bulidHighLow(); ThreadUtils.sleep(1000 * 60 * 3); ThreadUtils.sleep(1000 * 60 * 5); } } public void bulidHighLow() { try { if (first) { /*if (first) { // data数据保存间隔时长(毫秒) this.interval = this.sysparaService.find("data_interval").getInteger().intValue() / 1000; first = false; } // 秒 int num = (24 * 60 * 60) / this.interval; int num = (24 * 60 * 60) / this.interval;*/ List<Item> item_list = itemService.findByType(Item.cryptos); item_list = item_list.stream() .filter(x -> x.getAdjustmentValue()!=null && x.getAdjustmentValue().doubleValue()!=0) .collect(Collectors.toList()); for (int i = 0; i < item_list.size(); i++) { String symbol = item_list.get(i).getSymbol(); //重新查询缓存 dataDBService.cacheBefore24Hour(symbol); } /*for (int i = 0; i < item_list.size(); i++) { Item item = item_list.get(i); try { @@ -96,10 +113,10 @@ } catch (Exception e) { logger.error("run fail", e); } } }*/ } catch (Exception e) { logger.error("run fail", e); logger.error("bulidHighLow run fail", e); } }