新版仿ok交易所-后端
1
zyy
2025-10-21 bb020bc9f8c6edca7125229e10d1ea279726fc8c
trading-order-huobi/src/main/java/com.yami.trading.huobi/data/job/AbstractGetDataJob.java
@@ -6,6 +6,7 @@
import com.yami.trading.common.util.Arith;
import com.yami.trading.huobi.data.AdjustmentValueCache;
import com.yami.trading.huobi.data.DataCache;
import com.yami.trading.huobi.data.RandomNumbersGenerator;
import com.yami.trading.huobi.data.internal.DataDBService;
import com.yami.trading.huobi.data.model.AdjustmentValue;
import com.yami.trading.huobi.hobi.HobiDataService;
@@ -60,53 +61,76 @@
                if (delayValue != null) {
                    if (delayValue.getSecond() < 0) {
                        AdjustmentValueCache.getDelayValue().remove(symbol);
                        AdjustmentValueCache.getPreAllocatedAdjustments().remove(symbol);
                        AdjustmentValueCache.getCurrentAdjustmentIndex().remove(symbol);
                        return;
                        clean(symbol);
                        continue;
                    }
                    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);
                    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 (currentIndex < frequency) {
                        BigDecimal currentAdjust = adjustments.get(currentIndex);
                        // 更新当前值(累加正负调整值)
                    if (frequency <= 1) {
                        // 单次分配:直接用带符号值,避免符号丢失
                        BigDecimal delayVal = delayValue.getValue().setScale(decimal, RoundingMode.HALF_UP);
                        if (currentValue == null) {
                            AdjustmentValueCache.getCurrentValue().put(symbol, currentAdjust.setScale(decimal, RoundingMode.HALF_UP));
                            AdjustmentValueCache.getCurrentValue().put(symbol, delayVal);
                        } else {
                            AdjustmentValueCache.getCurrentValue().put(symbol, currentValue.add(currentAdjust).setScale(decimal, RoundingMode.HALF_UP));
                            AdjustmentValueCache.getCurrentValue().put(symbol, currentValue.add(delayVal).setScale(decimal, RoundingMode.HALF_UP));
                        }
                        // 更新延时值(剩余值和时间)
                        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);
                        // 索引递增,完成后清理缓存
                        int nextIndex = currentIndex + 1;
                        AdjustmentValueCache.getCurrentAdjustmentIndex().put(symbol, nextIndex);
                        if (nextIndex >= frequency) {
                            AdjustmentValueCache.getDelayValue().remove(symbol);
                            AdjustmentValueCache.getPreAllocatedAdjustments().remove(symbol);
                            AdjustmentValueCache.getCurrentAdjustmentIndex().remove(symbol);
                        }
                        // 持久化更新
                        BigDecimal newAdjustValue = AdjustmentValueCache.getCurrentValue().get(symbol);
                        if (!item.getAdjustmentValue().equals(newAdjustValue)) {
                            item.setAdjustmentValue(newAdjustValue);
                        if (!item.getAdjustmentValue().equals(AdjustmentValueCache.getCurrentValue().get(symbol))) {
                            item.setAdjustmentValue(AdjustmentValueCache.getCurrentValue().get(symbol));
                            itemService.saveOrUpdate(item);
                        }
                        clean(symbol);
                    } else {
                        List<BigDecimal> adjustments = AdjustmentValueCache.getPreAllocatedAdjustments().get(symbol);
                        Integer currentIndex = AdjustmentValueCache.getCurrentAdjustmentIndex().get(symbol);
                        // 首次执行:生成含正负值的调整序列
                        if (adjustments == null || currentIndex == null) {
                            //分几段执行
                            int nums = Math.max(10, frequency / 10);
                            List<BigDecimal> result = RandomNumbersGenerator.generateNumbers(delayValue.getValue(), nums, decimal + 4);
                            for (int i = 0; i < result.size(); i++) {
                                if (adjustments == null) {
                                    adjustments = new ArrayList<>();
                                }
                                adjustments.addAll(generateRandomAdjustments(result.get(i), frequency / 10, decimal));
                            }
                            currentIndex = 0;
                            AdjustmentValueCache.getPreAllocatedAdjustments().put(symbol, adjustments);
                            AdjustmentValueCache.getCurrentAdjustmentIndex().put(symbol, currentIndex);
                        }
                        // 分步应用调整值(确保正负交替)
                        if (currentIndex < frequency) {
                            BigDecimal currentAdjust = adjustments.get(currentIndex);
                            // 更新当前值(累加正负调整值)
                            if (currentValue == null) {
                                AdjustmentValueCache.getCurrentValue().put(symbol, currentAdjust.setScale(decimal, RoundingMode.HALF_UP));
                            } else {
                                AdjustmentValueCache.getCurrentValue().put(symbol, currentValue.add(currentAdjust).setScale(decimal, RoundingMode.HALF_UP));
                            }
                            // 更新延时值(剩余值和时间)
                            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);
                            // 索引递增,完成后清理缓存
                            int nextIndex = currentIndex + 1;
                            AdjustmentValueCache.getCurrentAdjustmentIndex().put(symbol, nextIndex);
                            if (nextIndex >= frequency) {
                                clean(symbol);
                            }
                            // 持久化更新
                            BigDecimal newAdjustValue = AdjustmentValueCache.getCurrentValue().get(symbol);
                            if (!item.getAdjustmentValue().equals(newAdjustValue)) {
                                item.setAdjustmentValue(newAdjustValue);
                                itemService.saveOrUpdate(item);
                            }
                        }
                    }
                }
@@ -169,6 +193,12 @@
        this.dataDBService.saveAsyn(realtime);
    }
    private void clean(String symbol){
        AdjustmentValueCache.getDelayValue().remove(symbol);
        AdjustmentValueCache.getPreAllocatedAdjustments().remove(symbol);
        AdjustmentValueCache.getCurrentAdjustmentIndex().remove(symbol);
        AdjustmentValueCache.getFrequency().remove(symbol);
    }
    private List<BigDecimal> generateRandomAdjustments(BigDecimal totalValue, int count, int decimal) {
        List<BigDecimal> adjustments = new ArrayList<>(count);
@@ -278,7 +308,16 @@
    public static void main(String[] args) {
        AbstractGetDataJob abstractGetDataJob = new CryptosGetDataJob();
        List<BigDecimal> list = abstractGetDataJob.generateRandomAdjustments(new BigDecimal(0.002), 300, 8);
        List<BigDecimal> list = new ArrayList<>();
        int frequency = 200;
        int decimal = 6;
        //分几段执行
        int nums = Math.max(10, frequency / 10);
        List<BigDecimal> result = RandomNumbersGenerator.generateNumbers(BigDecimal.valueOf(0.02), nums, decimal + 4);
        for (int i = 0; i < result.size(); i++) {
            list.addAll(abstractGetDataJob.generateRandomAdjustments(result.get(i), frequency / 10, decimal));
        }
        BigDecimal sum = BigDecimal.ZERO;
        int num = 0;
        int dmt = 1;