新版仿ok交易所-后端
1
zyy3
2025-10-13 d4352284dc9049fa0d954b562ebb4632b34f4aee
1
1 files modified
112 ■■■■ changed files
trading-order-huobi/src/main/java/com.yami.trading.huobi/data/job/AbstractGetDataJob.java 112 ●●●● patch | view | raw | blame | history
trading-order-huobi/src/main/java/com.yami.trading.huobi/data/job/AbstractGetDataJob.java
@@ -63,10 +63,11 @@
                if (delayValue != null) {
                    if (delayValue.getValue().compareTo(BigDecimal.ZERO) == 0 || delayValue.getSecond() <= 0) {
                        cleanUpKLineCache(symbol); // 清理缓存,跳过后续计算
                        return;
                    }
                    // K线场景专属参数(可根据周期动态调整)
                    //int kLineCycle = getKLineCycle(symbol); // 获取K线周期(如1min=1, 5min=5)
                    //double baseFluctuation = kLineCycle <= 1 ? 0.05 : 0.08; // 短周期(1min)±5%,长周期(5min)±8%
                    double baseFluctuation = 0.1;
                    int trendConsistencyRate = 70; // 70%概率保持与前一个值的波动方向一致(模拟趋势)
                    int maxAdjacentFluctuation = 5; // 相邻值波动不超过前一个值的5%(避免视觉跳变)
@@ -75,7 +76,6 @@
                    Integer frequency = AdjustmentValueCache.getFrequency().get(symbol);
                    List<BigDecimal> preAllocationList = AdjustmentValueCache.getPreAllocationList().get(symbol);
                    Integer currentIndex = AdjustmentValueCache.getCurrentAllocationIndex().get(symbol);
                    // 新增:记录前一个值的波动方向(用于趋势一致性)
                    List<Boolean> upDownTrend = AdjustmentValueCache.getUpDownTrend().getOrDefault(symbol, new ArrayList<>());
                    if (frequency == null) {
@@ -84,83 +84,97 @@
                        if (frequency > 1) {
                            preAllocationList = new ArrayList<>(frequency);
                            BigDecimal totalValue = delayValue.getValue();
                            // 虚拟币精度高,中间计算保留decimal+4位(避免精度丢失)
                            BigDecimal average = totalValue.divide(new BigDecimal(frequency), decimal + 4, RoundingMode.HALF_UP);
                            BigDecimal totalValue = delayValue.getValue(); // 支持正负值
                            // 核心修改1:用绝对值算平均值(避免负数波动范围反向),最后补回原符号
                            BigDecimal totalAbs = totalValue.abs();
                            BigDecimal averageAbs = totalAbs.divide(new BigDecimal(frequency), decimal + 4, RoundingMode.HALF_UP);
                            BigDecimal average = averageAbs.multiply(totalValue.signum() == 1 ? BigDecimal.ONE : BigDecimal.ONE.negate());
                            BigDecimal sum = BigDecimal.ZERO;
                            // 优化1:最后k个值分散偏差(k=频率的2%,最少5个,确保最后一根K线无异常)
                            int lastK = Math.max(5, frequency / 50);
                            int normalCount = frequency - lastK;
                            // 优化2:前n-k个值——模拟真实行情波动(截断正态分布+趋势一致性)
                            // 优化2:前n-k个值——模拟真实行情波动(兼容正负)
                            for (int i = 0; i < normalCount; i++) {
                                BigDecimal randomValue;
                                if (i == 0) {
                                    // 第一个值:在基础波动范围内随机
                                    // 第一个值:基于带符号平均值波动,保留原符号
                                    randomValue = generateTruncatedGaussianValue(average, baseFluctuation, decimal + 4);
                                } else {
                                    // 非第一个值:70%概率保持与前一个值的波动方向一致
                                    boolean lastUp = upDownTrend.get(i - 1);
                                    boolean keepTrend = Math.random() * 100 <= trendConsistencyRate;
                                    if (keepTrend) {
                                        // 保持趋势:上涨则继续涨(或小跌),下跌则继续跌(或小涨)
                                        // 核心修改2:按前值绝对值算波动幅度,避免负数趋势误判
                                        BigDecimal lastVal = preAllocationList.get(i - 1);
                                        double fluctRange = lastVal.abs().multiply(new BigDecimal(baseFluctuation * 1.2)).doubleValue();
                                        if (lastUp) {
                                            // 前一个上涨:本次波动范围 [0, +baseFluctuation*1.2](允许小回调)
                                            randomValue = generateDirectionalValue(average, 0, baseFluctuation * 1.2, decimal + 4);
                                            // 前一个上涨(无论正负,比前值大即为涨):波动范围[0, fluctRange]
                                            randomValue = generateDirectionalValue(lastVal, 0.0, fluctRange, decimal + 4);
                                        } else {
                                            // 前一个下跌:本次波动范围 [-baseFluctuation*1.2, 0](允许小反弹)
                                            randomValue = generateDirectionalValue(average, -baseFluctuation * 1.2, 0, decimal + 4);
                                            // 前一个下跌(比前值小即为跌):波动范围[-fluctRange, 0]
                                            randomValue = generateDirectionalValue(lastVal, -fluctRange, 0.0, decimal + 4);
                                        }
                                    } else {
                                        // 反转趋势:正常基础波动
                                        // 反转趋势:基于带符号平均值波动
                                        randomValue = generateTruncatedGaussianValue(average, baseFluctuation, decimal + 4);
                                    }
                                    // 优化3:强约束相邻值波动——不超过前一个值的5%
                                    // 核心修改3:相邻波动约束——用前值绝对值算最大波动,超限时按符号截断
                                    BigDecimal lastValue = preAllocationList.get(i - 1);
                                    BigDecimal maxAdjacent = lastValue.multiply(new BigDecimal(maxAdjacentFluctuation / 100.0))
                                    BigDecimal maxAdjacent = lastValue.abs()
                                            .multiply(new BigDecimal(maxAdjacentFluctuation / 100.0))
                                            .setScale(decimal + 4, RoundingMode.HALF_UP);
                                    BigDecimal adjacentDiff = randomValue.subtract(lastValue).abs();
                                    if (adjacentDiff.compareTo(maxAdjacent) > 0) {
                                    BigDecimal adjacentDiff = randomValue.subtract(lastValue);
                                    if (adjacentDiff.abs().compareTo(maxAdjacent) > 0) {
                                        randomValue = lastValue.add(
                                                adjacentDiff.divide(randomValue.subtract(lastValue), decimal + 4, RoundingMode.HALF_UP)
                                                        .multiply(maxAdjacent)
                                                adjacentDiff.signum() == 1 ? maxAdjacent : maxAdjacent.negate()
                                        );
                                    }
                                }
                                preAllocationList.add(randomValue);
                                sum = sum.add(randomValue);
                                // 记录当前值的波动方向(与平均值对比)
                                upDownTrend.add(randomValue.compareTo(average) > 0);
                                // 核心修改4:涨跌趋势改为“与前值对比”(而非平均值),兼容正负
                                boolean currentUp = (i == 0)
                                        ? randomValue.compareTo(average) > 0
                                        : randomValue.compareTo(preAllocationList.get(i - 1)) > 0;
                                upDownTrend.add(currentUp);
                            }
                            // 优化4:最后k个值——极小波动+偏差分散(确保最后一根K线平滑)
                            // 优化4:最后k个值——兼容负数总和
                            BigDecimal remaining = totalValue.subtract(sum);
                            BigDecimal avgLastK = remaining.divide(new BigDecimal(lastK), decimal + 4, RoundingMode.HALF_UP);
                            // 用剩余值绝对值算平均,补回符号
                            BigDecimal remainingAbs = remaining.abs();
                            BigDecimal avgLastKAbs = remainingAbs.divide(new BigDecimal(lastK), decimal + 4, RoundingMode.HALF_UP);
                            BigDecimal avgLastK = avgLastKAbs.multiply(remaining.signum() == 1 ? BigDecimal.ONE : BigDecimal.ONE.negate());
                            for (int i = 0; i < lastK - 1; i++) {
                                // 最后k-1个值:波动范围缩小到基础波动的1/3(±1.7%~2.7%)
                                BigDecimal smallFluctValue = generateTruncatedGaussianValue(avgLastK, baseFluctuation / 3, decimal + 4);
                                preAllocationList.add(smallFluctValue);
                                sum = sum.add(smallFluctValue);
                                upDownTrend.add(smallFluctValue.compareTo(avgLastK) > 0);
                                upDownTrend.add(smallFluctValue.compareTo(preAllocationList.get(preAllocationList.size() - 2)) > 0);
                            }
                            // 最后1个值:仅承担剩余微小偏差(波动≤基础波动的1/5)
                            // 最后1个值:兼容负数偏差
                            BigDecimal finalValue = totalValue.subtract(sum);
                            BigDecimal maxFinalFluct = avgLastK.multiply(new BigDecimal(baseFluctuation / 5))
                            BigDecimal maxFinalFluct = avgLastK.abs()
                                    .multiply(new BigDecimal(baseFluctuation / 5))
                                    .setScale(decimal + 4, RoundingMode.HALF_UP);
                            if (finalValue.abs().compareTo(maxFinalFluct) > 0) {
                                // 超限时按符号截断,保留原方向
                                finalValue = maxFinalFluct.multiply(finalValue.signum() == 1 ? BigDecimal.ONE : BigDecimal.ONE.negate());
                                // 若超出范围,反向微调前一个值(确保总和正确)
                                BigDecimal prevLastValue = preAllocationList.get(preAllocationList.size() - 1);
                                preAllocationList.set(preAllocationList.size() - 1, prevLastValue.add(totalValue.subtract(sum).subtract(finalValue)));
                            }
                            preAllocationList.add(finalValue);
                            upDownTrend.add(finalValue.compareTo(avgLastK) > 0);
                            upDownTrend.add(finalValue.compareTo(preAllocationList.get(preAllocationList.size() - 2)) > 0);
                            // 缓存新增:波动方向列表(用于趋势一致性)
                            // 缓存更新
                            AdjustmentValueCache.getPreAllocationList().put(symbol, preAllocationList);
                            AdjustmentValueCache.getCurrentAllocationIndex().put(symbol, 0);
                            AdjustmentValueCache.getUpDownTrend().put(symbol, upDownTrend);
@@ -168,49 +182,45 @@
                        }
                    }
                    // 后续分配逻辑(不变,仅需在清理缓存时移除upDownTrend)
                    // 后续分配逻辑:兼容正负延时值
                    if (frequency <= 1) {
                        // 单次分配逻辑
                        // 单次分配:直接用带符号值,避免符号丢失
                        BigDecimal delayVal = delayValue.getValue().setScale(decimal, RoundingMode.HALF_UP);
                        if (currentValue == null) {
                            AdjustmentValueCache.getCurrentValue().put(symbol, delayValue.getValue().setScale(decimal, RoundingMode.HALF_UP));
                            AdjustmentValueCache.getCurrentValue().put(symbol, delayVal);
                        } else {
                            AdjustmentValueCache.getCurrentValue().put(symbol,
                                    currentValue.add(delayValue.getValue()).setScale(decimal, RoundingMode.HALF_UP));
                            AdjustmentValueCache.getCurrentValue().put(symbol, currentValue.add(delayVal).setScale(decimal, RoundingMode.HALF_UP));
                        }
                        if (!item.getAdjustmentValue().equals(AdjustmentValueCache.getCurrentValue().get(symbol))) {
                            item.setAdjustmentValue(AdjustmentValueCache.getCurrentValue().get(symbol));
                            itemService.saveOrUpdate(item);
                        }
                        cleanUpKLineCache(symbol); // K线场景专属缓存清理
                        cleanUpKLineCache(symbol);
                    } else {
                        // 按预分配列表取值
                        // 按预分配列表取值(列表已兼容正负)
                        if (preAllocationList != null && currentIndex != null && currentIndex < preAllocationList.size()) {
                            BigDecimal currentValue_frequency = preAllocationList.get(currentIndex)
                                    .setScale(decimal, RoundingMode.HALF_UP);
                            BigDecimal currentValueFrequency = preAllocationList.get(currentIndex).setScale(decimal, RoundingMode.HALF_UP);
                            // 更新当前值(K线价格)
                            // 更新当前值:带符号累加
                            if (currentValue == null) {
                                AdjustmentValueCache.getCurrentValue().put(symbol, currentValue_frequency);
                                AdjustmentValueCache.getCurrentValue().put(symbol, currentValueFrequency);
                            } else {
                                AdjustmentValueCache.getCurrentValue().put(symbol,
                                        currentValue.add(currentValue_frequency).setScale(decimal, RoundingMode.HALF_UP));
                                AdjustmentValueCache.getCurrentValue().put(symbol, currentValue.add(currentValueFrequency).setScale(decimal, RoundingMode.HALF_UP));
                            }
                            // 更新延迟值和索引
                            delayValue.setValue(delayValue.getValue().subtract(currentValue_frequency)
                                    .setScale(decimal, RoundingMode.HALF_UP));
                            // 更新延迟值:带符号减法
                            BigDecimal updatedDelayVal = delayValue.getValue().subtract(currentValueFrequency).setScale(decimal, RoundingMode.HALF_UP);
                            delayValue.setValue(updatedDelayVal);
                            delayValue.setSecond(Arith.sub(delayValue.getSecond(), Arith.div(this.interval, 1000.0D)));
                            AdjustmentValueCache.getDelayValue().put(symbol, delayValue);
                            int nextIndex = currentIndex + 1;
                            AdjustmentValueCache.getCurrentAllocationIndex().put(symbol, nextIndex);
                            // 分配完成,清理缓存
                            if (nextIndex >= frequency) {
                                cleanUpKLineCache(symbol);
                            }
                            // 保存K线数据更新
                            if (!item.getAdjustmentValue().equals(AdjustmentValueCache.getCurrentValue().get(symbol))) {
                                item.setAdjustmentValue(AdjustmentValueCache.getCurrentValue().get(symbol));
                                itemService.saveOrUpdate(item);