1
zj
2024-11-12 cd95c9114fcbb3fc65c666b8b86c378f9a96d55b
websocketSerivce/src/main/java/org/example/server/impl/CurrencySerivceImpl.java
@@ -61,15 +61,24 @@
        ExecutorService executor = Executors.newFixedThreadPool(20); // 适当增加线程数以匹配处理的交易所数量
        // 异步处理每个交易所的数据
        CompletableFuture<Void> mexcFuture = CompletableFuture.runAsync(() -> processMarketData(RedisUtil.keys("mexc"), mexcList, "mexc"), executor);
        CompletableFuture<Void> gateFuture = CompletableFuture.runAsync(() -> processMarketData(RedisUtil.keys("gate"), gateList, "gate"), executor);
        CompletableFuture<Void> bitgetFuture = CompletableFuture.runAsync(() -> processMarketData(RedisUtil.keys("bitget"), bitgetList, "bitget"), executor);
        CompletableFuture<Void> kucoinFuture = CompletableFuture.runAsync(() -> processMarketData(RedisUtil.keys("kucoin"), kucoinList, "kucoin"), executor);
        CompletableFuture<Void> mexcFuture = checkAndProcess("mexc", mexcList, executor);
        CompletableFuture<Void> gateFuture = checkAndProcess("gate", gateList, executor);
        CompletableFuture<Void> bitgetFuture = checkAndProcess("bitget", bitgetList, executor);
        CompletableFuture<Void> kucoinFuture = checkAndProcess("kucoin", kucoinList, executor);
        // 等待所有任务完成
        CompletableFuture.allOf(mexcFuture, gateFuture, bitgetFuture, kucoinFuture).join();
        executor.shutdown(); // 关闭线程池
    }
    private CompletableFuture<Void> checkAndProcess(String exchangeName, List<MarketBo> marketList, ExecutorService executor) {
        return CompletableFuture.runAsync(() -> {
            Set<String> keys = RedisUtil.keys(exchangeName);
            if (keys != null && !keys.isEmpty()) {
                processMarketData(keys, marketList, exchangeName);
            }
        }, executor);
    }
    private void processMexc() {
@@ -197,9 +206,14 @@
                if (markets1.getBids() == null) continue;
                for (int j = 0; j < exchanges.length; j++) {
                    if (i == j) continue;
                    MarketBo markets2 = exchangeMap.get(exchanges[j]);
                    if(markets1.getExchange().equals(markets2.getExchange())){
                        continue;
                    }
                    if (markets2.getAsks() == null) continue;
                    CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
@@ -235,20 +249,24 @@
    }
    private static void assembleMarketDataOut(String coinName, MarketBo markets1, MarketBo markets2, BigDecimal profitPercentage, BigDecimal buyPrice, BigDecimal sellPrice, List<MarketDataOut> marketDataOuts, String formattedDateTime) {
        MarketDataOut marketDataOut = new MarketDataOut();
        marketDataOut.setBaseAsset(coinName.replaceAll("USDT","").toLowerCase().toUpperCase()); // 设置基础资产
        marketDataOut.setBuyingPlatform(capitalizeFirstLetter(markets1.getExchange())); // 设置买入平台,首字母大写
        marketDataOut.setSellPlatform(capitalizeFirstLetter(markets2.getExchange())); // 设置卖出平台,首字母大写
        marketDataOut.setSpread(profitPercentage.setScale(4, RoundingMode.DOWN).toPlainString()); // 设置利润百分比
        marketDataOut.setBuyPrice(buyPrice.toPlainString()); // 设置买入价格
        marketDataOut.setSellPrice(sellPrice.toPlainString()); // 设置卖出价格
        marketDataOut.setBuyNumber(markets1.getBids().getV().setScale(4, RoundingMode.HALF_UP).toPlainString()); // 设置买入数量
        marketDataOut.setSellNumber(markets2.getAsks().getV().setScale(4, RoundingMode.HALF_UP).toPlainString()); // 设置卖出数量
        marketDataOut.setBuyTotalPrice((markets1.getBids().getP().multiply(markets1.getBids().getV())).setScale(0, RoundingMode.HALF_UP).toPlainString()); // 设置买入总价
        marketDataOut.setSellTotalPrice((markets2.getAsks().getP().multiply(markets2.getAsks().getV())).setScale(0,RoundingMode.HALF_UP).toPlainString()); // 设置卖出总价
        marketDataOut.setServceTime(formattedDateTime); // 设置服务时间
        marketDataOut.setBuyAndSell(marketDataOut.getBaseAsset()+marketDataOut.getBuyingPlatform()+marketDataOut.getSellPlatform());
        marketDataOuts.add(marketDataOut); // 添加到输出列表
        if(coinName.contains(String.valueOf("USDT"))){
            MarketDataOut marketDataOut = new MarketDataOut();
            marketDataOut.setBaseAsset(coinName.replaceAll("USDT","").toLowerCase().toUpperCase()); // 设置基础资产
            marketDataOut.setBuyingPlatform(capitalizeFirstLetter(markets1.getExchange())); // 设置买入平台,首字母大写
            marketDataOut.setSellPlatform(capitalizeFirstLetter(markets2.getExchange())); // 设置卖出平台,首字母大写
            marketDataOut.setSpread(profitPercentage.setScale(4, RoundingMode.DOWN).toPlainString()); // 设置利润百分比
            marketDataOut.setBuyPrice(buyPrice.toPlainString()); // 设置买入价格
            marketDataOut.setSellPrice(sellPrice.toPlainString()); // 设置卖出价格
            marketDataOut.setBuyNumber(markets1.getBids().getV().setScale(4, RoundingMode.HALF_UP).toPlainString()); // 设置买入数量
            marketDataOut.setSellNumber(markets2.getAsks().getV().setScale(4, RoundingMode.HALF_UP).toPlainString()); // 设置卖出数量
            marketDataOut.setBuyTotalPrice((markets1.getBids().getP().multiply(markets1.getBids().getV())).setScale(0, RoundingMode.HALF_UP).toPlainString()); // 设置买入总价
            marketDataOut.setSellTotalPrice((markets2.getAsks().getP().multiply(markets2.getAsks().getV())).setScale(0,RoundingMode.HALF_UP).toPlainString()); // 设置卖出总价
            marketDataOut.setServceTime(formattedDateTime); // 设置服务时间
            marketDataOut.setBuyAndSell(marketDataOut.getBaseAsset()+marketDataOut.getBuyingPlatform()+marketDataOut.getSellPlatform());
            marketDataOuts.add(marketDataOut); // 添加到输出列表
        }else{
            System.out.println(coinName);
        }
    }
    public static String capitalizeFirstLetter(String word) {
@@ -276,7 +294,10 @@
    public void quotationCalculation(){
        extracted();
        findProfitablePairs(mexcList, gateList, bitgetList, kucoinList); // 请确保这些变量有定义和赋值
        // 检查列表是否为空并调用 findProfitablePairs 方法
        if (!mexcList.isEmpty() || !gateList.isEmpty() || !bitgetList.isEmpty() || !kucoinList.isEmpty()) {
            findProfitablePairs(mexcList, gateList, bitgetList, kucoinList);
        }
    }
    public void scheduler(){