Administrator
3 days ago 1819eaee93e47888dad4e1be4e3c611576426581
refactor(gateApi): 优化网格交易价格队列添加逻辑

- 修改多头价格队列添加方式,改为按网格步长计算价格点
- 添加跳过多头均价附近重复价格点的逻辑
- 修改空头价格队列添加方式,改为按网格步长计算价格点
- 添加跳过空头均价附近重复价格点的逻辑
- 保留价格队列排序功能并限制队列大小
- 增加队列状态和跳过数量的日志输出
1 files modified
28 ■■■■■ changed files
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java 28 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -388,11 +388,23 @@
        }
        synchronized (longPriceQueue) {
            longPriceQueue.addAll(matched);
            BigDecimal first = longPriceQueue.isEmpty() ? matched.get(matched.size() - 1) : longPriceQueue.get(0);
            BigDecimal step = config.getGridRate();
            int added = 0;
            for (int i = 1; i <= matched.size(); i++) {
                BigDecimal elem = first.multiply(BigDecimal.ONE.subtract(step.multiply(BigDecimal.valueOf(i)))).setScale(1, RoundingMode.HALF_UP);
                if (longEntryPrice.compareTo(BigDecimal.ZERO) > 0
                        && elem.subtract(longEntryPrice).abs().compareTo(longEntryPrice.multiply(step)) < 0) {
                    continue;
                }
                longPriceQueue.add(elem);
                added++;
            }
            longPriceQueue.sort(BigDecimal::compareTo);
            while (longPriceQueue.size() > config.getGridQueueSize()) {
                longPriceQueue.remove(longPriceQueue.size() - 1);
            }
            log.info("[Gate] 现多队列:{}, 跳过{}个(贴近多仓均价)", longPriceQueue, matched.size() - added);
        }
    }
@@ -434,11 +446,23 @@
        }
        synchronized (shortPriceQueue) {
            shortPriceQueue.addAll(matched);
            BigDecimal first = shortPriceQueue.isEmpty() ? matched.get(0) : shortPriceQueue.get(0);
            BigDecimal step = config.getGridRate();
            int added = 0;
            for (int i = 1; i <= matched.size(); i++) {
                BigDecimal elem = first.multiply(BigDecimal.ONE.add(step.multiply(BigDecimal.valueOf(i)))).setScale(1, RoundingMode.HALF_UP);
                if (shortEntryPrice.compareTo(BigDecimal.ZERO) > 0
                        && elem.subtract(shortEntryPrice).abs().compareTo(shortEntryPrice.multiply(step)) < 0) {
                    continue;
                }
                shortPriceQueue.add(elem);
                added++;
            }
            shortPriceQueue.sort((a, b) -> b.compareTo(a));
            while (shortPriceQueue.size() > config.getGridQueueSize()) {
                shortPriceQueue.remove(shortPriceQueue.size() - 1);
            }
            log.info("[Gate] 现空队列:{}, 跳过{}个(贴近空仓均价)", shortPriceQueue, matched.size() - added);
        }
    }