新版仿ok交易所-后端
1
zyy
2025-10-10 579e9032affa3493587533fcc1425e2858239696
1
3 files modified
140 ■■■■■ changed files
trading-order-huobi/src/main/java/com.yami.trading.huobi/data/AdjustmentValueCache.java 33 ●●●●● patch | view | raw | blame | history
trading-order-huobi/src/main/java/com.yami.trading.huobi/data/internal/CryptosKlineServiceImpl.java 20 ●●●●● patch | view | raw | blame | history
trading-order-huobi/src/main/java/com.yami.trading.huobi/data/job/AbstractGetDataJob.java 87 ●●●●● patch | view | raw | blame | history
trading-order-huobi/src/main/java/com.yami.trading.huobi/data/AdjustmentValueCache.java
@@ -16,6 +16,8 @@
     */
    private static volatile Map<String, AdjustmentValue> delayValue = new ConcurrentHashMap();
    private static volatile Map<String, BigDecimal> lastValue = new ConcurrentHashMap();
    public static Map<String, BigDecimal> getCurrentValue() {
        return currentValue;
    }
@@ -24,4 +26,35 @@
        return delayValue;
    }
    public static Map<String, BigDecimal> getLastValue() {
        return lastValue;
    }
    //计算已分配次数
    private static final Map<String, Integer> allocatedCount = new ConcurrentHashMap<>();
    //初始总值
    private static final Map<String, BigDecimal> totalValue = new ConcurrentHashMap<>();
    //更新累计值
    private static final Map<String, BigDecimal> accumulatedValue = new ConcurrentHashMap<>();
    //初始次数
    private static final Map<String, Integer> frequency = new ConcurrentHashMap<>();
    // 相应的getter方法
    public static Map<String, Integer> getAllocatedCount() {
        return allocatedCount;
    }
    public static Map<String, BigDecimal> getTotalValue() {
        return totalValue;
    }
    public static Map<String, BigDecimal> getAccumulatedValue() {
        return accumulatedValue;
    }
    public static Map<String, Integer> getFrequency() {
        return frequency;
    }
}
trading-order-huobi/src/main/java/com.yami.trading.huobi/data/internal/CryptosKlineServiceImpl.java
@@ -37,6 +37,12 @@
    @Autowired
    private NamedParameterJdbcOperations namedParameterJdbcTemplate;
/*    public static void main(String[] args) {
        BigDecimal a = BigDecimal.valueOf(0.006);
        BigDecimal b = BigDecimal.valueOf(3600);
        BigDecimal c = a.divide(b, 6, BigDecimal.ROUND_HALF_UP);
        System.out.println(c);
    }*/
    @Override
    public void initBySql(String symbol) {
        this.bulidBySql(symbol, Kline.PERIOD_1MIN);
@@ -188,6 +194,20 @@
                kline.setLow(kline.getLow().add(currentValue));
                kline.setHigh(kline.getHigh().add(currentValue));
                kline.setAdjusted(true);
                BigDecimal last = AdjustmentValueCache.getLastValue().get(symbol);
                if (last != null && last.compareTo(BigDecimal.ZERO) > 0) {
                    BigDecimal adj = currentValue.subtract(last);
                    if (adj.compareTo(BigDecimal.ZERO) > 0) { //递增
                        kline.setOpen(kline.getOpen().subtract(adj));
                        kline.setLow(kline.getLow().subtract(adj));
                    } else if(adj.compareTo(BigDecimal.ZERO) < 0){ //递减
                        kline.setOpen(kline.getOpen().subtract(adj));
                        kline.setHigh(kline.getHigh().subtract(adj));
                    }
                } else {
                    AdjustmentValueCache.getLastValue().put(symbol, currentValue);
                }
            }
        }
        klineDBService.save(kline);
trading-order-huobi/src/main/java/com.yami.trading.huobi/data/job/AbstractGetDataJob.java
@@ -18,6 +18,7 @@
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Random;
public abstract class AbstractGetDataJob implements Runnable {
@@ -44,6 +45,8 @@
    public abstract String getName();
    // 在类中定义静态Random实例
    private static final Random random = new Random();
    public abstract void realtimeHandle(String symbols);
@@ -58,8 +61,12 @@
                AdjustmentValue delayValue = AdjustmentValueCache.getDelayValue().get(symbol);
                if (delayValue != null) {
                    // 延时几次
                    int frequency = (int) Arith.div(Arith.mul(delayValue.getSecond(), 1000.0D), this.interval);
                    //延时几次 缓存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 (frequency <= 1) {
                        if (currentValue == null) {
@@ -74,8 +81,9 @@
                            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) {
@@ -92,6 +100,79 @@
                        if (!item.getAdjustmentValue().equals(AdjustmentValueCache.getCurrentValue().get(symbol))) {
                            item.setAdjustmentValue(AdjustmentValueCache.getCurrentValue().get(symbol));
                            itemService.saveOrUpdate(item);
                        }*/
                        // 保存原始总值用于计算随机分配
                        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);
                        }
                        BigDecimal currentValue_frequency;
                        // 计算剩余分配次数
                        int remainingAllocations = frequency - allocatedCount;
                        if (remainingAllocations > 1) {
                            // 不是最后一次分配,生成随机值
                            // 计算基础平均值
                            BigDecimal average = totalValue.divide(new BigDecimal(frequency), decimal, RoundingMode.HALF_UP);
                            // 生成0.3到1.7之间的随机因子(可根据需求调整)
                            double randomFactor = 0.3 + random.nextDouble() * 1.4;
                            currentValue_frequency = average.multiply(new BigDecimal(randomFactor))
                                    .setScale(decimal, RoundingMode.HALF_UP);
                            // 确保随机值不会超过剩余值的80%,防止最后一次分配出现负值
                            BigDecimal remainingValue = totalValue.subtract(AdjustmentValueCache.getAccumulatedValue().getOrDefault(symbol, BigDecimal.ZERO));
                            BigDecimal maxAllowed = remainingValue.multiply(new BigDecimal(0.8)).setScale(decimal, RoundingMode.HALF_UP);
                            if (currentValue_frequency.compareTo(maxAllowed) > 0) {
                                currentValue_frequency = maxAllowed;
                            }
                        } else {
                            // 最后一次分配,使用剩余的全部值
                            BigDecimal accumulated = AdjustmentValueCache.getAccumulatedValue().getOrDefault(symbol, BigDecimal.ZERO);
                            currentValue_frequency = totalValue.subtract(accumulated).setScale(decimal, 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);
                            AdjustmentValueCache.getDelayValue().remove(symbol);
                        }
                        // 保存更新
                        if (!item.getAdjustmentValue().equals(AdjustmentValueCache.getCurrentValue().get(symbol))) {
                            item.setAdjustmentValue(AdjustmentValueCache.getCurrentValue().get(symbol));
                            itemService.saveOrUpdate(item);
                        }
                    }
                }