From adbdffdb3b80eed8c7110c0583f8ae2f216b7990 Mon Sep 17 00:00:00 2001
From: zyy <zyy@email.com>
Date: Mon, 13 Oct 2025 18:22:59 +0800
Subject: [PATCH] K线优化
---
trading-order-huobi/src/main/java/com.yami.trading.huobi/data/job/AbstractGetDataJob.java | 328 ++++++++++++++++++++----------------
trading-order-huobi/src/main/java/com.yami.trading.huobi/hobi/HobiDataService.java | 2
trading-order-huobi/src/main/java/com.yami.trading.huobi/data/DataCache.java | 9
trading-order-huobi/src/main/java/com.yami.trading.huobi/data/internal/CryptosKlineServiceImpl.java | 58 ++---
trading-order-huobi/src/main/java/com.yami.trading.huobi/data/AdjustmentValueCache.java | 40 ++-
trading-order-huobi/src/main/java/com.yami.trading.huobi/hobi/internal/HobiDataServiceImpl.java | 77 ++++++++
6 files changed, 319 insertions(+), 195 deletions(-)
diff --git a/trading-order-huobi/src/main/java/com.yami.trading.huobi/data/AdjustmentValueCache.java b/trading-order-huobi/src/main/java/com.yami.trading.huobi/data/AdjustmentValueCache.java
index 8ae745f..e72e26d 100644
--- a/trading-order-huobi/src/main/java/com.yami.trading.huobi/data/AdjustmentValueCache.java
+++ b/trading-order-huobi/src/main/java/com.yami.trading.huobi/data/AdjustmentValueCache.java
@@ -3,6 +3,7 @@
import com.yami.trading.huobi.data.model.AdjustmentValue;
import java.math.BigDecimal;
+import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -32,29 +33,36 @@
- //计算已分配次数
- 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;
- }
+ //存储预生成的随机分配值列表
+ private static final Map<String, List<BigDecimal>> preAllocationList = new ConcurrentHashMap<>();
- public static Map<String, BigDecimal> getTotalValue() {
- return totalValue;
- }
+ //记录当前分配到第几个值
+ private static final Map<String, Integer> currentAllocationIndex = new ConcurrentHashMap<>();
- public static Map<String, BigDecimal> getAccumulatedValue() {
- return accumulatedValue;
- }
+ //记录前一个值的波动方向
+ private static final Map<String, List<Boolean>> upDownTrend = new ConcurrentHashMap<>();
public static Map<String, Integer> getFrequency() {
return frequency;
}
+
+ public static Map<String, List<BigDecimal>> getPreAllocationList() {
+ return preAllocationList;
+ }
+
+
+ public static Map<String, Integer> getCurrentAllocationIndex() {
+ return currentAllocationIndex;
+ }
+
+ public static Map<String, List<Boolean>> getUpDownTrend() {
+ return upDownTrend;
+ }
+
+
+
}
diff --git a/trading-order-huobi/src/main/java/com.yami.trading.huobi/data/DataCache.java b/trading-order-huobi/src/main/java/com.yami.trading.huobi/data/DataCache.java
index 39acc33..9562b96 100644
--- a/trading-order-huobi/src/main/java/com.yami.trading.huobi/data/DataCache.java
+++ b/trading-order-huobi/src/main/java/com.yami.trading.huobi/data/DataCache.java
@@ -30,7 +30,10 @@
*/
private volatile static Map<String, KlineTimeObject> kline = new ConcurrentHashMap<String, KlineTimeObject>();
-
+ /**
+ * 上一条K线数据
+ */
+ private volatile static Map<String, Kline> lastKline = new ConcurrentHashMap<String, Kline>();
/**
* 24小时的历史记录
*/
@@ -116,6 +119,10 @@
return kline.get(key);
}
+ public static Map<String, Kline> getLastKline() {
+ return lastKline;
+ }
+
public static TrendTimeObject getTrend(String symbol) {
return trend.get(symbol);
}
diff --git a/trading-order-huobi/src/main/java/com.yami.trading.huobi/data/internal/CryptosKlineServiceImpl.java b/trading-order-huobi/src/main/java/com.yami.trading.huobi/data/internal/CryptosKlineServiceImpl.java
index 29edd48..adb3830 100644
--- a/trading-order-huobi/src/main/java/com.yami.trading.huobi/data/internal/CryptosKlineServiceImpl.java
+++ b/trading-order-huobi/src/main/java/com.yami.trading.huobi/data/internal/CryptosKlineServiceImpl.java
@@ -179,8 +179,7 @@
@Override
public void saveOneByHuoBi(String symbol, String line) {
- List<Kline> list = hobiDataService.kline(itemService.findBySymbol(symbol).getSymbol(), line, 2,
- 0);
+ List<Kline> list = hobiDataService.kline(itemService.findBySymbol(symbol).getSymbol(), line, 2);
if (list == null || list.isEmpty()) {
return;
}
@@ -193,39 +192,31 @@
kline.setLow(kline.getLow().add(currentValue));
kline.setHigh(kline.getHigh().add(currentValue));
kline.setAdjusted(true);
-
-
- Kline lastOne = null;
-
- List<Kline> lastList = this.find(symbol, line, 1);
- if (lastList.size() > 0) {
- lastOne = list.get(0);
- }
- if (lastOne != null) {
- BigDecimal diff = kline.getOpen().subtract(lastOne.getClose());
- if (diff.compareTo(BigDecimal.ZERO) > 0) { //补差
- kline.setOpen(kline.getOpen().subtract(diff));
- kline.setLow(kline.getLow().subtract(diff));
- } else if (diff.compareTo(BigDecimal.ZERO) < 0) {
- kline.setOpen(kline.getOpen().subtract(diff));
- kline.setHigh(kline.getHigh().subtract(diff));
- }
- }
- /*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);
- }*/
}
}
+
+
+ //获取上一条数据
+ Kline lastKline = DataCache.getLastKline().get(symbol + line);;
+ if (lastKline == null) {
+ List<Kline> lastList = this.find(symbol, line, 1);
+ if (lastList.size() > 0) {
+ lastKline = lastList.get(0);
+ }
+ }
+ //补差
+ if (lastKline != null) {
+ BigDecimal diff = kline.getOpen().subtract(lastKline.getClose());
+ if (diff.compareTo(BigDecimal.ZERO) > 0) {
+ kline.setOpen(kline.getOpen().subtract(diff));
+ kline.setLow(kline.getLow().subtract(diff));
+ } else if (diff.compareTo(BigDecimal.ZERO) < 0) {
+ kline.setOpen(kline.getOpen().subtract(diff));
+ kline.setHigh(kline.getHigh().subtract(diff));
+ }
+ }
+
+
RequestDataHelper.set("symbol", symbol);
klineDBService.save(kline);
KlineTimeObject timeObject = DataCache.getKline(symbol, line);
@@ -235,6 +226,7 @@
timeObject.getKline().add(kline);
timeObject.setLastTime(new Date());
DataCache.putKline(symbol, line, timeObject);
+ DataCache.getLastKline().put(symbol + line, kline);
RequestDataHelper.clear();
}
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 9410086..9144f87 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,6 +17,7 @@
import java.math.BigDecimal;
import java.math.RoundingMode;
+import java.util.ArrayList;
import java.util.List;
import java.util.Random;
@@ -55,179 +56,165 @@
try {
String symbol = realtime.getSymbol();
- Integer decimal = itemService.getDecimal(symbol);
+ Integer decimal = itemService.getDecimal(symbol); // 虚拟币通常8位小数,需保留足够精度
Item item = this.itemService.findBySymbol(symbol);
BigDecimal currentValue = AdjustmentValueCache.getCurrentValue().get(symbol);
AdjustmentValue delayValue = AdjustmentValueCache.getDelayValue().get(symbol);
+
if (delayValue != null) {
- //延时几次 缓存frequency
+
+ // 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%(避免视觉跳变)
+
+
Integer frequency = AdjustmentValueCache.getFrequency().get(symbol);
- if (frequency == null) { //首次计算 缓存
+ 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) {
frequency = (int) Arith.div(Arith.mul(delayValue.getSecond(), 1000.0D), this.interval);
AdjustmentValueCache.getFrequency().put(symbol, frequency);
+
+ 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 sum = BigDecimal.ZERO;
+
+ // 优化1:最后k个值分散偏差(k=频率的2%,最少5个,确保最后一根K线无异常)
+ int lastK = Math.max(5, frequency / 50);
+ int normalCount = frequency - lastK;
+
+ // 优化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) {
+ // 保持趋势:上涨则继续涨(或小跌),下跌则继续跌(或小涨)
+ if (lastUp) {
+ // 前一个上涨:本次波动范围 [0, +baseFluctuation*1.2](允许小回调)
+ randomValue = generateDirectionalValue(average, 0, baseFluctuation * 1.2, decimal + 4);
+ } else {
+ // 前一个下跌:本次波动范围 [-baseFluctuation*1.2, 0](允许小反弹)
+ randomValue = generateDirectionalValue(average, -baseFluctuation * 1.2, 0, decimal + 4);
+ }
+ } else {
+ // 反转趋势:正常基础波动
+ randomValue = generateTruncatedGaussianValue(average, baseFluctuation, decimal + 4);
+ }
+
+ // 优化3:强约束相邻值波动——不超过前一个值的5%
+ BigDecimal lastValue = preAllocationList.get(i - 1);
+ BigDecimal maxAdjacent = lastValue.multiply(new BigDecimal(maxAdjacentFluctuation / 100.0))
+ .setScale(decimal + 4, RoundingMode.HALF_UP);
+ BigDecimal adjacentDiff = randomValue.subtract(lastValue).abs();
+ if (adjacentDiff.compareTo(maxAdjacent) > 0) {
+ randomValue = lastValue.add(
+ adjacentDiff.divide(randomValue.subtract(lastValue), decimal + 4, RoundingMode.HALF_UP)
+ .multiply(maxAdjacent)
+ );
+ }
+ }
+
+ preAllocationList.add(randomValue);
+ sum = sum.add(randomValue);
+ // 记录当前值的波动方向(与平均值对比)
+ upDownTrend.add(randomValue.compareTo(average) > 0);
+ }
+
+ // 优化4:最后k个值——极小波动+偏差分散(确保最后一根K线平滑)
+ BigDecimal remaining = totalValue.subtract(sum);
+ BigDecimal avgLastK = remaining.divide(new BigDecimal(lastK), decimal + 4, RoundingMode.HALF_UP);
+ 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);
+ }
+ // 最后1个值:仅承担剩余微小偏差(波动≤基础波动的1/5)
+ BigDecimal finalValue = totalValue.subtract(sum);
+ BigDecimal maxFinalFluct = avgLastK.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);
+
+ // 缓存新增:波动方向列表(用于趋势一致性)
+ AdjustmentValueCache.getPreAllocationList().put(symbol, preAllocationList);
+ AdjustmentValueCache.getCurrentAllocationIndex().put(symbol, 0);
+ AdjustmentValueCache.getUpDownTrend().put(symbol, upDownTrend);
+ currentIndex = 0;
+ }
}
+ // 后续分配逻辑(不变,仅需在清理缓存时移除upDownTrend)
if (frequency <= 1) {
+ // 单次分配逻辑
if (currentValue == null) {
- AdjustmentValueCache.getCurrentValue().put(symbol, delayValue.getValue());
+ AdjustmentValueCache.getCurrentValue().put(symbol, delayValue.getValue().setScale(decimal, RoundingMode.HALF_UP));
} else {
AdjustmentValueCache.getCurrentValue().put(symbol,
- delayValue.getValue().add(currentValue));
+ currentValue.add(delayValue.getValue()).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);
+ cleanUpKLineCache(symbol); // K线场景专属缓存清理
} else {
- /*// 本次调整值
- BigDecimal currentValue_frequency = delayValue.getValue().divide(new BigDecimal(frequency), decimal, RoundingMode.HALF_UP);
+ // 按预分配列表取值
+ if (preAllocationList != null && currentIndex != null && currentIndex < preAllocationList.size()) {
+ BigDecimal currentValue_frequency = preAllocationList.get(currentIndex)
+ .setScale(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.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);
+ // 更新当前值(K线价格)
+ if (currentValue == null) {
+ AdjustmentValueCache.getCurrentValue().put(symbol, currentValue_frequency);
} 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);
+ AdjustmentValueCache.getCurrentValue().put(symbol,
+ currentValue.add(currentValue_frequency).setScale(decimal, RoundingMode.HALF_UP));
}
- currentValue_frequency = average.multiply(randomFactor).setScale(delayDecimal, RoundingMode.HALF_UP);
+ // 更新延迟值和索引
+ delayValue.setValue(delayValue.getValue().subtract(currentValue_frequency)
+ .setScale(decimal, RoundingMode.HALF_UP));
+ delayValue.setSecond(Arith.sub(delayValue.getSecond(), Arith.div(this.interval, 1000.0D)));
+ AdjustmentValueCache.getDelayValue().put(symbol, delayValue);
- // 核心修改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);
- }
+ int nextIndex = currentIndex + 1;
+ AdjustmentValueCache.getCurrentAllocationIndex().put(symbol, nextIndex);
+
+ // 分配完成,清理缓存
+ if (nextIndex >= frequency) {
+ cleanUpKLineCache(symbol);
}
- //剩余的待分配值
- 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;
+ // 保存K线数据更新
+ if (!item.getAdjustmentValue().equals(AdjustmentValueCache.getCurrentValue().get(symbol))) {
+ item.setAdjustmentValue(AdjustmentValueCache.getCurrentValue().get(symbol));
+ itemService.saveOrUpdate(item);
}
-
- // 直接使用公共变量作为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.setScale(decimal, RoundingMode.HALF_UP));
- } else {
- AdjustmentValueCache.getCurrentValue().put(symbol,
- currentValue.add(currentValue_frequency).setScale(decimal, RoundingMode.HALF_UP));
- }
-
- // 更新延迟值
- delayValue.setValue(delayValue.getValue().subtract(currentValue_frequency).setScale(decimal, RoundingMode.HALF_UP));
- 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);
}
}
}
@@ -290,4 +277,55 @@
this.dataDBService.saveAsyn(realtime);
}
+
+
+ // ------------------------------ 工具方法 ------------------------------
+ /**
+ * 生成截断正态分布值(限制在平均值±fluctuation范围内,模拟真实小波动)
+ */
+ private BigDecimal generateTruncatedGaussianValue(BigDecimal average, double fluctuation, int scale) {
+ double factor = nextGaussian() * 0.25; // 标准差0.25,99.7%概率在±0.75内(更集中)
+ factor = Math.max(-1.0, Math.min(1.0, factor)); // 截断极端值
+ BigDecimal fluctuationValue = average.multiply(new BigDecimal(factor * fluctuation))
+ .setScale(scale, RoundingMode.HALF_UP);
+ return average.add(fluctuationValue);
+ }
+
+ /**
+ * 生成指定方向的波动值(如[0, +0.1]表示只涨不跌)
+ */
+ private BigDecimal generateDirectionalValue(BigDecimal average, double minFactor, double maxFactor, int scale) {
+ double factor = minFactor + Math.random() * (maxFactor - minFactor);
+ BigDecimal fluctuationValue = average.multiply(new BigDecimal(factor))
+ .setScale(scale, RoundingMode.HALF_UP);
+ return average.add(fluctuationValue);
+ }
+
+ /**
+ * K线场景专属缓存清理(含波动方向列表)
+ */
+ private void cleanUpKLineCache(String symbol) {
+ AdjustmentValueCache.getDelayValue().remove(symbol);
+ AdjustmentValueCache.getFrequency().remove(symbol);
+ AdjustmentValueCache.getPreAllocationList().remove(symbol);
+ AdjustmentValueCache.getCurrentAllocationIndex().remove(symbol);
+ AdjustmentValueCache.getUpDownTrend().remove(symbol); // 新增:清理波动方向缓存
+ }
+
+ /**
+ * 获取K线周期(根据symbol或配置判断,示例返回1=1min,5=5min等)
+ */
+ private int getKLineCycle(String symbol) {
+ // 实际场景可从配置或symbol后缀获取(如BTC-USDT-1MIN → 1)
+ return 1;
+ }
+
+ /**
+ * 正态分布随机数生成(均值0,标准差1)
+ */
+ private static double nextGaussian() {
+ double u1 = Math.random();
+ double u2 = Math.random();
+ return Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);
+ }
}
diff --git a/trading-order-huobi/src/main/java/com.yami.trading.huobi/hobi/HobiDataService.java b/trading-order-huobi/src/main/java/com.yami.trading.huobi/hobi/HobiDataService.java
index 215b2cb..34ed48c 100644
--- a/trading-order-huobi/src/main/java/com.yami.trading.huobi/hobi/HobiDataService.java
+++ b/trading-order-huobi/src/main/java/com.yami.trading.huobi/hobi/HobiDataService.java
@@ -89,6 +89,8 @@
*/
public List<Kline> kline(String symbol, String period, Integer num, int maximum);
+ public List<Kline> kline(String symbol, String period, Integer num);
+
/**
* 市场深度数据(20档)
*/
diff --git a/trading-order-huobi/src/main/java/com.yami.trading.huobi/hobi/internal/HobiDataServiceImpl.java b/trading-order-huobi/src/main/java/com.yami.trading.huobi/hobi/internal/HobiDataServiceImpl.java
index 32d54f5..27cff5c 100644
--- a/trading-order-huobi/src/main/java/com.yami.trading.huobi/hobi/internal/HobiDataServiceImpl.java
+++ b/trading-order-huobi/src/main/java/com.yami.trading.huobi/hobi/internal/HobiDataServiceImpl.java
@@ -221,6 +221,83 @@
return list;
}
+ @Override
+ public List<Kline> kline(String symbol, String period, Integer num) {
+ List<Kline> list = new ArrayList<Kline>();
+ Item item = itemService.findBySymbol(symbol);
+ if (item == null) {
+ return list;
+ }
+ try {
+ Map<String, Object> param = new HashMap<String, Object>();
+ param.put("symbol", symbol);
+ param.put("period", period);
+ if (num == null) {
+ if (Kline.PERIOD_1MIN.equals(period)) {
+ param.put("size", 1440);
+ }
+ if (Kline.PERIOD_5MIN.equals(period)) {
+ param.put("size", 576);
+ }
+ if (Kline.PERIOD_15MIN.equals(period)) {
+ param.put("size", 576);
+ }
+ if (Kline.PERIOD_30MIN.equals(period)) {
+ param.put("size", 576);
+ }
+ if (Kline.PERIOD_60MIN.equals(period)) {
+ param.put("size", 576);
+ }
+
+ if (Kline.PERIOD_4HOUR.equals(period)) {
+ param.put("size", 576);
+ }
+ if (Kline.PERIOD_1DAY.equals(period)) {
+ param.put("size", 500);
+ }
+ if (Kline.PERIOD_1MON.equals(period)) {
+ param.put("size", 500);
+ }
+ if (Kline.PERIOD_1WEEK.equals(period)) {
+ param.put("size", 500);
+ }
+
+ } else {
+ param.put("size", num);
+ }
+
+ String result = HttpHelper.getJSONFromHttp(Config.url + Config.kline, param, HttpMethodType.GET);
+ JSONObject resultJson = JSON.parseObject(result);
+ String status = resultJson.getString("status");
+ if ("ok".equals(status)) {
+ JSONArray dataArray = resultJson.getJSONArray("data");
+ /**
+ * 丢弃第一行数据
+ */
+ int start = 1;
+ if (num != null && num == 1) start = 0;
+ for (int i = start; i < dataArray.size(); i++) {
+ JSONObject realtimeJson = dataArray.getJSONObject(i);
+ Kline kline = new Kline();
+ kline.setSymbol(item.getSymbol());
+ kline.setPeriod(period);
+ kline.setTs(Long.valueOf(realtimeJson.getString("id") + "000"));
+ kline.setOpen(realtimeJson.getBigDecimal("open"));
+ kline.setClose(realtimeJson.getBigDecimal("close"));
+ kline.setHigh(realtimeJson.getBigDecimal("high"));
+ kline.setLow(realtimeJson.getBigDecimal("low"));
+ kline.setVolume(realtimeJson.getBigDecimal("vol"));
+ kline.setAmount(realtimeJson.getBigDecimal("amount"));
+ list.add(kline);
+ }
+
+ }
+ } catch (Exception e) {
+ logger.error("error", e);
+ }
+ return list;
+ }
+
/**
* 市场深度数据(20档),包装,数据本地化处理
*/
--
Gitblit v1.9.3