From d6f1a81f8af23e55bcb2a44909efcfbf61cd34fe Mon Sep 17 00:00:00 2001
From: zyy <zyy@email.com>
Date: Mon, 20 Oct 2025 18:53:51 +0800
Subject: [PATCH] 1

---
 trading-order-huobi/src/main/java/com.yami.trading.huobi/data/job/AbstractGetDataJob.java |  323 +++++++++++++++++++++++++++--------------------------
 1 files changed, 166 insertions(+), 157 deletions(-)

diff --git a/trading-order-huobi/src/main/java/com.yami.trading.huobi/data/job/AbstractGetDataJob.java b/trading-order-huobi/src/main/java/com.yami.trading.huobi/data/job/AbstractGetDataJob.java
index 8c22ccd..9ae134f 100644
--- a/trading-order-huobi/src/main/java/com.yami.trading.huobi/data/job/AbstractGetDataJob.java
+++ b/trading-order-huobi/src/main/java/com.yami.trading.huobi/data/job/AbstractGetDataJob.java
@@ -17,8 +17,8 @@
 
 import java.math.BigDecimal;
 import java.math.RoundingMode;
+import java.util.ArrayList;
 import java.util.List;
-import java.util.Random;
 
 
 public abstract class AbstractGetDataJob implements Runnable {
@@ -45,8 +45,6 @@
 
     public abstract String getName();
 
-    // 在类中定义静态Random实例
-    private static final Random random = new Random();
 
     public abstract void realtimeHandle(String symbols);
 
@@ -61,172 +59,53 @@
                 AdjustmentValue delayValue = AdjustmentValueCache.getDelayValue().get(symbol);
 
                 if (delayValue != null) {
-                    //延时几次 缓存frequency
-                    Integer frequency = AdjustmentValueCache.getFrequency().get(symbol);
-                    if (frequency == null) { //首次计算 缓存
-                        frequency = (int) Arith.div(Arith.mul(delayValue.getSecond(), 1000.0D), this.interval);
-                        AdjustmentValueCache.getFrequency().put(symbol, frequency);
+                    if (delayValue.getSecond() < 0) {
+                        AdjustmentValueCache.getDelayValue().remove(symbol);
+                        AdjustmentValueCache.getPreAllocatedAdjustments().remove(symbol);
+                        AdjustmentValueCache.getCurrentAdjustmentIndex().remove(symbol);
+                        return;
+                    }
+                    int frequency = (int) Arith.div(Arith.mul(delayValue.getSecond(), 1000.0D), this.interval);
+                    List<BigDecimal> adjustments = AdjustmentValueCache.getPreAllocatedAdjustments().get(symbol);
+                    Integer currentIndex = AdjustmentValueCache.getCurrentAdjustmentIndex().get(symbol);
+
+                    // 首次执行:生成含正负值的调整序列
+                    if (adjustments == null || currentIndex == null) {
+                        adjustments = generateRandomAdjustments(delayValue.getValue(), frequency, decimal);
+                        currentIndex = 0;
+                        AdjustmentValueCache.getPreAllocatedAdjustments().put(symbol, adjustments);
+                        AdjustmentValueCache.getCurrentAdjustmentIndex().put(symbol, currentIndex);
                     }
 
-                    if (frequency <= 1) {
+                    // 分步应用调整值(确保正负交替)
+                    if (currentIndex < frequency) {
+                        BigDecimal currentAdjust = adjustments.get(currentIndex);
+
+                        // 更新当前值(累加正负调整值)
                         if (currentValue == null) {
-                            AdjustmentValueCache.getCurrentValue().put(symbol, delayValue.getValue());
+                            AdjustmentValueCache.getCurrentValue().put(symbol, currentAdjust.setScale(decimal, RoundingMode.HALF_UP));
                         } else {
-                            AdjustmentValueCache.getCurrentValue().put(symbol,
-                                    delayValue.getValue().add(currentValue));
+                            AdjustmentValueCache.getCurrentValue().put(symbol, currentValue.add(currentAdjust).setScale(decimal, RoundingMode.HALF_UP));
                         }
 
-                        if (!item.getAdjustmentValue().equals(AdjustmentValueCache.getCurrentValue().get(symbol))) {
-                            item.setAdjustmentValue(AdjustmentValueCache.getCurrentValue().get(symbol));
-                            itemService.saveOrUpdate(item);
-                        }
-                        AdjustmentValueCache.getDelayValue().remove(symbol);
-                        AdjustmentValueCache.getFrequency().remove(symbol);
-                    } else {
-                        /*// 本次调整值
-                        BigDecimal currentValue_frequency = delayValue.getValue().divide(new BigDecimal(frequency), decimal, RoundingMode.HALF_UP);
-
-                        if (currentValue == null) {
-                            AdjustmentValueCache.getCurrentValue().put(symbol, currentValue_frequency);
-                        } else {
-                            AdjustmentValueCache.getCurrentValue().put(symbol,
-                                    currentValue.add(currentValue_frequency));
-                        }
-
-                        delayValue.setValue(delayValue.getValue().subtract(currentValue_frequency));
+                        // 更新延时值(剩余值和时间)
+                        delayValue.setValue(delayValue.getValue().subtract(currentAdjust).setScale(decimal, RoundingMode.HALF_UP));
                         delayValue.setSecond(Arith.sub(delayValue.getSecond(), Arith.div(this.interval, 1000.0D)));
                         AdjustmentValueCache.getDelayValue().put(symbol, delayValue);
 
-                        if (!item.getAdjustmentValue().equals(AdjustmentValueCache.getCurrentValue().get(symbol))) {
-                            item.setAdjustmentValue(AdjustmentValueCache.getCurrentValue().get(symbol));
-                            itemService.saveOrUpdate(item);
-                        }*/
-                        //计算延时加大精度
-                        Integer delayDecimal = 10;
-                        // 保存原始总值用于计算随机分配
-                        BigDecimal totalValue = delayValue.getValue();
-                        // 计算已分配次数(从缓存中获取)
-                        Integer allocatedCount = AdjustmentValueCache.getAllocatedCount().get(symbol);
-                        if (allocatedCount == null) {
-                            allocatedCount = 0;
-                            // 首次分配时保存总值到缓存,用于后续计算
-                            AdjustmentValueCache.getTotalValue().put(symbol, totalValue);
-                        } else {
-                            //不是首次
-                            totalValue = AdjustmentValueCache.getTotalValue().get(symbol);
-                        }
-
-                        // ########## 新增:判断调整方向(正向/负向)##########
-                        boolean isPositiveAdjustment = totalValue.compareTo(BigDecimal.ZERO) > 0; // 正向调整(>0)
-                        boolean isNegativeAdjustment = totalValue.compareTo(BigDecimal.ZERO) < 0; // 负向调整(<0)
-
-                        BigDecimal currentValue_frequency;
-                        // 计算剩余分配次数
-                        int remainingAllocations = frequency - allocatedCount;
-
-                        if (remainingAllocations > 1) {
-                            BigDecimal average = totalValue.divide(new BigDecimal(frequency), delayDecimal, RoundingMode.HALF_UP);
-                            BigDecimal randomFactor;
-
-                            // 根据调整方向动态调整负因子概率
-                            double negativeProbability = isNegativeAdjustment ? 0.6 : 0.3;
-                            if (Math.random() <= negativeProbability) {
-                                // 负因子:范围 [-0.5, 0)
-                                double randomNeg = -0.5 + Math.random() * 0.5; // 简化计算:max - min = 0 - (-0.5) = 0.5
-                                randomFactor = new BigDecimal(randomNeg).setScale(delayDecimal, RoundingMode.HALF_UP);
-                            } else {
-                                // 正因子:范围 [0.3, 1.7)
-                                double randomPos = 0.3 + Math.random() * 1.4; // 简化计算:max - min = 1.7 - 0.3 = 1.4
-                                randomFactor = new BigDecimal(randomPos).setScale(delayDecimal, RoundingMode.HALF_UP);
-                            }
-
-                            currentValue_frequency = average.multiply(randomFactor).setScale(delayDecimal, RoundingMode.HALF_UP);
-
-                            // 核心修改1:根据调整方向动态约束累计值
-                            BigDecimal currentAccumulated = AdjustmentValueCache.getAccumulatedValue().getOrDefault(symbol, BigDecimal.ZERO);
-                            BigDecimal tempAccumulated = currentAccumulated.add(currentValue_frequency);
-                            if (isPositiveAdjustment) {
-                                // 正向调整:累计值不能为负
-                                if (tempAccumulated.compareTo(BigDecimal.ZERO) < 0) {
-                                    currentValue_frequency = BigDecimal.ONE.divide(new BigDecimal("10").pow(delayDecimal), delayDecimal, RoundingMode.HALF_UP);
-                                }
-                            } else if (isNegativeAdjustment) {
-                                // 负向调整:累计值不能小于目标值(避免过度减值)
-                                if (tempAccumulated.compareTo(totalValue) < 0) {
-                                    currentValue_frequency = totalValue.subtract(currentAccumulated).divide(new BigDecimal(2), delayDecimal, RoundingMode.HALF_UP);
-                                }
-                            }
-
-                            //剩余的待分配值
-                            BigDecimal remainingValue = totalValue.subtract(currentAccumulated);
-                            //本次分配后剩余的待分配值
-                            BigDecimal tempDelayValue = remainingValue.subtract(currentValue_frequency);
-
-                            // 提取公共变量(剩余值的80%)
-                            BigDecimal remaining80Percent = remainingValue.multiply(new BigDecimal("0.8"))
-                                    .setScale(delayDecimal, RoundingMode.HALF_UP);
-
-                            // 统一判断“是否需要修正剩余值”
-                            boolean needFixRemaining = (isPositiveAdjustment && tempDelayValue.compareTo(BigDecimal.ZERO) < 0)
-                                    || (isNegativeAdjustment && tempDelayValue.compareTo(totalValue) > 0);
-                            if (needFixRemaining) {
-                                // 直接使用公共变量,避免重复计算
-                                currentValue_frequency = remaining80Percent;
-                            }
-
-                            // 直接使用公共变量作为maxAllowed,无需重复计算
-                            if ((isPositiveAdjustment && currentValue_frequency.compareTo(remaining80Percent) > 0)
-                                    || (isNegativeAdjustment && currentValue_frequency.compareTo(remaining80Percent) < 0)) {
-                                currentValue_frequency = remaining80Percent;
-                            }
-
-                        } else {
-                            // 最后一次分配兜底(支持负值)
-                            BigDecimal accumulated = AdjustmentValueCache.getAccumulatedValue().getOrDefault(symbol, BigDecimal.ZERO);
-                            currentValue_frequency = totalValue.subtract(accumulated).setScale(delayDecimal, RoundingMode.HALF_UP);
-
-                            // 正向调整:最后一次分配值不能为负;负向调整:不能为正(无重复,无需优化)
-                            if (isPositiveAdjustment && currentValue_frequency.compareTo(BigDecimal.ZERO) < 0) {
-                                currentValue_frequency = BigDecimal.ZERO.setScale(delayDecimal, RoundingMode.HALF_UP);
-                            } else if (isNegativeAdjustment && currentValue_frequency.compareTo(BigDecimal.ZERO) > 0) {
-                                currentValue_frequency = BigDecimal.ZERO.setScale(delayDecimal, RoundingMode.HALF_UP);
-                            }
-                        }
-
-
-                        // 更新累计值
-                        BigDecimal newAccumulated = AdjustmentValueCache.getAccumulatedValue().getOrDefault(symbol, BigDecimal.ZERO)
-                                .add(currentValue_frequency);
-                        AdjustmentValueCache.getAccumulatedValue().put(symbol, newAccumulated);
-                        // 更新分配次数
-                        AdjustmentValueCache.getAllocatedCount().put(symbol, allocatedCount + 1);
-
-                        // 更新当前值
-                        if (currentValue == null) {
-                            AdjustmentValueCache.getCurrentValue().put(symbol, currentValue_frequency);
-                        } else {
-                            AdjustmentValueCache.getCurrentValue().put(symbol,
-                                    currentValue.add(currentValue_frequency));
-                        }
-
-                        // 更新延迟值
-                        delayValue.setValue(delayValue.getValue().subtract(currentValue_frequency));
-                        delayValue.setSecond(Arith.sub(delayValue.getSecond(), Arith.div(this.interval, 1000.0D)));
-                        AdjustmentValueCache.getDelayValue().put(symbol, delayValue);
-
-                        // 如果是最后一次分配,清理缓存
-                        if (remainingAllocations <= 1) {
-                            AdjustmentValueCache.getAllocatedCount().remove(symbol);
-                            AdjustmentValueCache.getTotalValue().remove(symbol);
-                            AdjustmentValueCache.getAccumulatedValue().remove(symbol);
-                            AdjustmentValueCache.getFrequency().remove(symbol);
-
+                        // 索引递增,完成后清理缓存
+                        int nextIndex = currentIndex + 1;
+                        AdjustmentValueCache.getCurrentAdjustmentIndex().put(symbol, nextIndex);
+                        if (nextIndex >= frequency) {
                             AdjustmentValueCache.getDelayValue().remove(symbol);
+                            AdjustmentValueCache.getPreAllocatedAdjustments().remove(symbol);
+                            AdjustmentValueCache.getCurrentAdjustmentIndex().remove(symbol);
                         }
 
-                        // 保存更新
-                        if (!item.getAdjustmentValue().equals(AdjustmentValueCache.getCurrentValue().get(symbol))) {
-                            item.setAdjustmentValue(AdjustmentValueCache.getCurrentValue().get(symbol));
+                        // 持久化更新
+                        BigDecimal newAdjustValue = AdjustmentValueCache.getCurrentValue().get(symbol);
+                        if (!item.getAdjustmentValue().equals(newAdjustValue)) {
+                            item.setAdjustmentValue(newAdjustValue);
                             itemService.saveOrUpdate(item);
                         }
                     }
@@ -290,4 +169,134 @@
         this.dataDBService.saveAsyn(realtime);
     }
 
+
+    private List<BigDecimal> generateRandomAdjustments(BigDecimal totalValue, int count, int decimal) {
+        List<BigDecimal> adjustments = new ArrayList<>(count);
+        BigDecimal sum = BigDecimal.ZERO; // 整体累积和
+        boolean isOverallUp = totalValue.signum() > 0; // 整体大趋势
+
+        // 1. 大幅扩大基础波动幅度(比之前再提高30%+,确保单次波动更剧烈)
+        BigDecimal baseFluct = totalValue.abs().multiply(new BigDecimal(isOverallUp ? 0.25 : 0.22)) // 上涨时25%,下跌时22%
+                .max(new BigDecimal("0.001")); // 最小波动阈值提高到0.001,避免微小波动
+
+        // 2. 极大放宽累积范围(允许更大中途偏离,给子周期正负波动留空间)
+        BigDecimal minPreSum = totalValue.multiply(new BigDecimal("0.5")); // 整体最低允许50%
+        BigDecimal maxPreSum = totalValue.multiply(new BigDecimal("1.5")); // 整体最高允许150%
+
+        // 3. 几乎取消“不动”状态(概率1%,突出剧烈波动)
+        double flatProbability = 0.01;
+
+        // 4. 子周期配置:每20次为一个子周期,每个子周期随机生成正负趋势
+        int subCycleLength = 20; // 子周期长度
+        Boolean currentSubCycleUp = null; // 当前子周期的趋势(null表示未初始化)
+        BigDecimal subSum = BigDecimal.ZERO; // 当前子周期的累积和
+
+        for (int i = 0; i < count - 1; i++) {
+            // 每进入新的子周期(i为0或20的倍数),随机初始化子周期趋势(50%正,50%负)
+            if (i % subCycleLength == 0) {
+                currentSubCycleUp = Math.random() < 0.5; // 子周期趋势随机正负
+                subSum = BigDecimal.ZERO; // 重置子周期累积和
+            }
+
+            // 计算当前允许的累积总和范围(动态适配剩余步数)
+            BigDecimal remainingCount = new BigDecimal(count - 1 - i);
+            BigDecimal minCurrentSum = sum.add(minPreSum.subtract(sum).divide(remainingCount, decimal + 4, RoundingMode.HALF_UP));
+            BigDecimal maxCurrentSum = sum.add(maxPreSum.subtract(sum).divide(remainingCount, decimal + 4, RoundingMode.HALF_UP));
+
+            // 极低概率“不动”
+            boolean isFlat = Math.random() < flatProbability;
+            if (isFlat) {
+                BigDecimal adjustment = BigDecimal.ZERO.setScale(decimal, RoundingMode.HALF_UP);
+                adjustments.add(adjustment);
+                sum = sum.add(adjustment);
+                subSum = subSum.add(adjustment); // 累加子周期和
+                continue;
+            }
+
+            // 5. 动态调整涨跌概率:优先满足“子周期正负交替”,再适配整体趋势
+            double upProbability;
+            // 子周期内:若子周期趋势为正,80%概率涨;为负,80%概率跌(确保子周期总和有明确正负)
+            if (currentSubCycleUp) {
+                upProbability = 0.8;
+            } else {
+                upProbability = 0.2;
+            }
+            // 二次修正:若子周期累积和已偏离目标(如子周期应为正但当前为负),进一步提高对应概率
+            if (currentSubCycleUp && subSum.compareTo(BigDecimal.ZERO) < 0) {
+                upProbability = Math.min(0.98, upProbability + 0.2); // 子周期需正但当前负,大幅提高涨概率
+            } else if (!currentSubCycleUp && subSum.compareTo(BigDecimal.ZERO) > 0) {
+                upProbability = Math.max(0.02, upProbability - 0.2); // 子周期需负但当前正,大幅提高跌概率
+            }
+            // 三次修正:确保整体累积和不偏离太远(弱于子周期优先级)
+            if (sum.compareTo(minPreSum) < 0) {
+                upProbability = Math.min(0.98, upProbability + 0.1);
+            } else if (sum.compareTo(maxPreSum) > 0) {
+                upProbability = Math.max(0.02, upProbability - 0.1);
+            }
+            boolean isCurrentUp = Math.random() < upProbability;
+
+            // 6. 生成超大随机幅度(0.6~1.2倍baseFluct,跳过中小幅度,直接用大波动)
+            double randomRate = 0.6 + Math.random() * 0.6; // 范围:0.6~1.2(确保单次波动至少是baseFluct的60%)
+            BigDecimal fluct = baseFluct.multiply(new BigDecimal(randomRate))
+                    .setScale(decimal + 4, RoundingMode.HALF_UP);
+
+            // 7. 生成当前调整值(允许接近上限的剧烈波动)
+            BigDecimal adjustment;
+            if (isCurrentUp) {
+                adjustment = fluct.setScale(decimal, RoundingMode.HALF_UP);
+                // 超上限时仅截断到上限(不额外缩小,保留大涨幅)
+                if (sum.add(adjustment).compareTo(maxCurrentSum) > 0) {
+                    adjustment = maxCurrentSum.subtract(sum).setScale(decimal, RoundingMode.HALF_UP);
+                }
+            } else {
+                adjustment = fluct.negate().setScale(decimal, RoundingMode.HALF_UP);
+                // 低于下限时仅截断到下限(保留大跌幅)
+                if (sum.add(adjustment).compareTo(minCurrentSum) < 0) {
+                    adjustment = minCurrentSum.subtract(sum).setScale(decimal, RoundingMode.HALF_UP);
+                }
+            }
+
+            adjustments.add(adjustment);
+            sum = sum.add(adjustment);
+            subSum = subSum.add(adjustment); // 累加子周期和
+        }
+
+        // 8. 最后一个值:允许极大补平幅度(不超过baseFluct的8倍,适配剧烈波动后的差额)
+        BigDecimal lastAdjustment = totalValue.subtract(sum)
+                .setScale(decimal, RoundingMode.HALF_UP);
+        BigDecimal maxLastAdjust = baseFluct.multiply(new BigDecimal(8)); // 从5倍提高到8倍
+        if (lastAdjustment.abs().compareTo(maxLastAdjust) > 0) {
+            lastAdjustment = maxLastAdjust.multiply(lastAdjustment.signum() == 1 ? BigDecimal.ONE : BigDecimal.ONE.negate());
+            // 微调前一个值分担差额(确保总和准确)
+            BigDecimal prevAdjust = adjustments.get(adjustments.size() - 1);
+            adjustments.set(adjustments.size() - 1, prevAdjust.add(totalValue.subtract(sum).subtract(lastAdjustment)));
+        }
+        adjustments.add(lastAdjustment);
+
+        return adjustments;
+    }
+
+    public static void main(String[] args) {
+        AbstractGetDataJob abstractGetDataJob = new CryptosGetDataJob();
+        List<BigDecimal> list = abstractGetDataJob.generateRandomAdjustments(new BigDecimal(0.002), 300, 8);
+        BigDecimal sum = BigDecimal.ZERO;
+        int num = 0;
+        int dmt = 1;
+        BigDecimal numd = BigDecimal.ZERO;
+        for (int i = 0; i < list.size(); i++) {
+            sum = sum.add(list.get(i));
+            System.out.println((i+1) + "~                      " + list.get(i));
+            System.out.println(sum);
+
+            numd = numd.add(list.get(i));
+            num++;
+            if (num == 20) {
+                System.out.println(dmt+"ddd" + numd);
+                dmt++;
+                num=0;
+                numd=BigDecimal.ZERO;
+            }
+        }
+    }
+
 }

--
Gitblit v1.9.3