From 1fefaefc56960c2900f40084aab2d501c1c1eddf Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Wed, 10 Dec 2025 14:23:38 +0800
Subject: [PATCH] feat(okxNewPrice): 实现加仓逻辑以优化持仓管理
---
src/main/java/com/xcong/excoin/modules/okxNewPrice/celue/CaoZuoServiceImpl.java | 221 ++++++++++++++++++++++++++++++++++++++++--------------
1 files changed, 162 insertions(+), 59 deletions(-)
diff --git a/src/main/java/com/xcong/excoin/modules/okxNewPrice/celue/CaoZuoServiceImpl.java b/src/main/java/com/xcong/excoin/modules/okxNewPrice/celue/CaoZuoServiceImpl.java
index cc43fd7..ea2f2bb 100644
--- a/src/main/java/com/xcong/excoin/modules/okxNewPrice/celue/CaoZuoServiceImpl.java
+++ b/src/main/java/com/xcong/excoin/modules/okxNewPrice/celue/CaoZuoServiceImpl.java
@@ -1,7 +1,10 @@
package com.xcong.excoin.modules.okxNewPrice.celue;
+import cn.hutool.core.util.StrUtil;
+import com.xcong.excoin.modules.okxNewPrice.okxWs.AccountWs;
import com.xcong.excoin.modules.okxNewPrice.okxWs.InstrumentsWs;
import com.xcong.excoin.modules.okxNewPrice.okxWs.PositionsWs;
+import com.xcong.excoin.modules.okxNewPrice.okxWs.TradeOrderWs;
import com.xcong.excoin.modules.okxNewPrice.okxWs.enums.CoinEnums;
import com.xcong.excoin.modules.okxNewPrice.okxWs.enums.OrderParamEnums;
import com.xcong.excoin.modules.okxNewPrice.wangge.WangGeQueue;
@@ -30,6 +33,19 @@
private final RedisUtils redisUtils;
private final WangGeService wangGeService;
+
+ // 构造Redis键名
+ final String coinCode = CoinEnums.HE_YUE.getCode();
+ final String instrumentsStateKey = InstrumentsWs.INSTRUMENTSWS_CHANNEL + ":" + coinCode + ":state";
+ final String instrumentsOutKey = InstrumentsWs.INSTRUMENTSWS_CHANNEL+":" + coinCode+":out";
+ final String positionsMarkPxKey = PositionsWs.POSITIONSWS_CHANNEL + ":" + coinCode + ":markPx";
+ final String positionsAvgPxKey = PositionsWs.POSITIONSWS_CHANNEL + ":" + coinCode + ":avgPx";
+ final String positionsOrderPriceKey = PositionsWs.POSITIONSWS_CHANNEL + ":" + coinCode + ":orderPrice";
+ final String positionsUplKey = PositionsWs.POSITIONSWS_CHANNEL + ":" + coinCode + ":upl";
+ final String positionsRealizedPnlKey = PositionsWs.POSITIONSWS_CHANNEL + ":" + coinCode + ":realizedPnl";
+ final String positionsImrKey = PositionsWs.POSITIONSWS_CHANNEL + ":" + coinCode + ":imr";
+ final String positionsPosKey = PositionsWs.POSITIONSWS_CHANNEL + ":" + coinCode + ":pos";
+
/**
* 执行主要的操作逻辑,包括读取合约状态、获取市场价格信息,
* 并根据当前持仓均价和标记价格决定是否执行买卖操作。
@@ -38,35 +54,69 @@
*/
@Override
public String caoZuo() {
- // 构造Redis键名
- final String coinCode = CoinEnums.HE_YUE.getCode();
- final String instrumentsKey = InstrumentsWs.INSTRUMENTSWS_CHANNEL + ":" + coinCode + ":state";
- final String positionsMarkPxKey = PositionsWs.POSITIONSWS_CHANNEL + ":" + coinCode + ":markPx";
- final String positionsAvgPxKey = PositionsWs.POSITIONSWS_CHANNEL + ":" + coinCode + ":avgPx";
- final String positionsOrderPriceKey = PositionsWs.POSITIONSWS_CHANNEL + ":" + coinCode + ":orderPrice";
+ log.info("开始执行操作CaoZuoServiceImpl......");
+ String outStr = (String) redisUtils.get(instrumentsOutKey);
+ if (OrderParamEnums.OUT_YES.getValue().equals(outStr)){
+ log.error("止损过了......冷静一下,等待下次入场......");
+ return null;
+ }
- // 获取合约状态
- String state = (String) redisUtils.get(instrumentsKey);
- if (state == null || !OrderParamEnums.STATE_1.getValue().equals(state)) {
+ // 获取合约执行操作状态
+ String state = (String) redisUtils.get(instrumentsStateKey);
+ if (OrderParamEnums.STATE_4.getValue().equals(state)) {
+ log.error("操作下单中,等待......");
+ return OrderParamEnums.ORDERING.getValue();
+ }
+ if (OrderParamEnums.STATE_3.getValue().equals(state)){
+ log.error("持仓盈亏超过下单总保证金,冷静止损......");
+ redisUtils.set(instrumentsOutKey, OrderParamEnums.OUT_YES.getValue(), 0);
+ return OrderParamEnums.OUT.getValue();
+ }
+ if (OrderParamEnums.STATE_2.getValue().equals(state)){
+ log.error("持仓盈亏抗压......");
return OrderParamEnums.HOLDING.getValue();
}
- if (OrderParamEnums.STATE_4.getValue().equals(state)) {
- return OrderParamEnums.ORDERING.getValue();
+ if (OrderParamEnums.STATE_0.getValue().equals(state)){
+ log.error("请检查系统参数,不允许开仓......");
+ return OrderParamEnums.HOLDING.getValue();
+ }
+
+ String pos = (String) redisUtils.get(positionsPosKey);
+ if (StrUtil.isBlank(pos) || BigDecimal.ZERO.compareTo( new BigDecimal(pos)) <= 0) {
+ log.error("未获取到持仓数量");
+ return OrderParamEnums.INIT.getValue();
+ }
+
+ String uplStr = (String) redisUtils.get(positionsUplKey);
+ if (StrUtil.isBlank(uplStr)){
+ return OrderParamEnums.INIT.getValue();
+ }
+ //可使用的总保证金
+ String totalOrderUsdtKey = AccountWs.ACCOUNTWS_CHANNEL + ":" + CoinEnums.USDT.getCode() + ":totalOrderUsdt";
+ String totalOrderUsdt = (String) redisUtils.get(totalOrderUsdtKey);
+ BigDecimal upl = new BigDecimal(uplStr);
+ if (BigDecimal.ZERO.compareTo(upl) >= 0){
+ upl = upl.multiply(new BigDecimal("-1"));
+
+ if (upl.compareTo(new BigDecimal(totalOrderUsdt)) >= 0) {
+ log.error("持仓盈亏超过下单总保证金,止损冷静一天......");
+ return OrderParamEnums.OUT.getValue();
+ }
}
log.info(OrderParamEnums.getNameByValue(state));
// 获取标记价格和平均持仓价格
- Object markPxObj = redisUtils.get(positionsMarkPxKey);
- Object avgPxObj = redisUtils.get(positionsAvgPxKey);
+ String markPxObj = (String) redisUtils.get(positionsMarkPxKey);
+ String avgPxObj = (String) redisUtils.get(positionsAvgPxKey);
- if (markPxObj == null || avgPxObj == null) {
+ if (StrUtil.isBlank(markPxObj) || StrUtil.isBlank(avgPxObj)) {
return OrderParamEnums.INIT.getValue();
}
try {
- BigDecimal markPx = new BigDecimal((String) markPxObj);
- BigDecimal avgPx = new BigDecimal((String) avgPxObj);
+ BigDecimal markPx = new BigDecimal( markPxObj);
+ BigDecimal avgPx = new BigDecimal( avgPxObj);
log.info("开仓价格: {}, 当前价格:{},匹配队列中......", avgPx, markPx);
@@ -83,8 +133,14 @@
// 判断是加仓还是减仓
if (avgPx.compareTo(markPx) > 0) {
+ log.info("开始加仓...");
+ if (queueKaiCang.isEmpty()) {
+ // 队列为空
+ log.info("开始加仓,但是超出了网格设置...");
+ return side;
+ }
DescBigDecimal kaiCang = queueKaiCang.peek();
- if (kaiCang != null && kaiCang.getValue().compareTo(markPx) >= 0) {
+ if (kaiCang != null && markPx.compareTo(kaiCang.getValue()) <= 0 && avgPx.compareTo(kaiCang.getValue()) >= 0) {
log.info("开始加仓...开仓队列价格大于当前价格{}>{}", kaiCang.getValue(), markPx);
side = OrderParamEnums.BUY.getValue();
redisUtils.set(positionsOrderPriceKey, String.valueOf(kaiCang.getValue()), 0);
@@ -92,11 +148,62 @@
log.info("未触发加仓......,等待");
}
} else if (avgPx.compareTo(markPx) < 0) {
+ log.info("开始减仓...");
+ if (queuePingCang.isEmpty()) {
+ // 队列为空
+ log.info("开始减仓,但是超出了网格设置...");
+ return side;
+ }
AscBigDecimal pingCang = queuePingCang.peek();
- if (pingCang != null && pingCang.getValue().compareTo(markPx) <= 0) {
+ if (pingCang != null && markPx.compareTo(pingCang.getValue()) >= 0 && avgPx.compareTo(pingCang.getValue()) < 0) {
log.info("开始减仓...平仓队列价格小于当前价格{}<={}", pingCang.getValue(), markPx);
- side = OrderParamEnums.SELL.getValue();
- redisUtils.set(positionsOrderPriceKey, String.valueOf(pingCang.getValue()), 0);
+ //判断当前是否盈利
+ String uplstr = (String) redisUtils.get(positionsUplKey);
+ String realizedPnl = (String) redisUtils.get(positionsRealizedPnlKey);
+ String imr = (String) redisUtils.get(positionsImrKey);
+ if (uplstr != null && realizedPnl != null && imr != null) {
+ BigDecimal uplValue = new BigDecimal(uplstr);
+ BigDecimal realizedPnlValue = new BigDecimal(realizedPnl);
+ BigDecimal imrValue = new BigDecimal(imr).multiply(new BigDecimal(OrderParamEnums.PING_CANG_SHOUYI.getValue()));
+ if (realizedPnlValue.compareTo(BigDecimal.ZERO) <= 0) {
+ BigDecimal realizedPnlValueZheng = realizedPnlValue.multiply(new BigDecimal("-1"));
+ if (uplValue.compareTo(realizedPnlValue) > 0 && uplValue.compareTo(imrValue.add(realizedPnlValueZheng)) >= 0) {
+ log.info("当前未实现盈亏:{}大于预计收益>{},赚钱咯", uplValue, imrValue);
+ redisUtils.set(positionsOrderPriceKey, String.valueOf(pingCang.getValue()), 0);
+ return OrderParamEnums.SELL.getValue();
+ }else{
+ //判断是否加仓(当前持仓过小,可以加仓)
+ boolean isAddCang = doAddCang(imrValue);
+ log.info("减仓过程中没有收获,发现是持仓过小 :{}",isAddCang);
+ if (isAddCang){
+ return OrderParamEnums.BUY.getValue();
+ }
+ log.info("当前未实现盈亏:{}没有大于预计收益>{},钱在路上了", uplValue, imrValue);
+ redisUtils.set(positionsOrderPriceKey, String.valueOf(pingCang.getValue()), 0);
+ return OrderParamEnums.HOLDING.getValue();
+ }
+
+ }else {
+ if (uplValue.compareTo(imrValue) >= 0) {
+ log.info("当前未实现盈亏:{}大于预计收益>{},赚钱咯", uplValue, imrValue);
+ redisUtils.set(positionsOrderPriceKey, String.valueOf(pingCang.getValue()), 0);
+ return OrderParamEnums.SELL.getValue();
+ }else{
+
+ //判断是否加仓(当前持仓过小,可以加仓)
+ boolean isAddCang = doAddCang(imrValue);
+ log.info("减仓过程中没有收获,发现是持仓过小 :{}",isAddCang);
+ if (isAddCang){
+ return OrderParamEnums.BUY.getValue();
+ }
+ log.info("当前未实现盈亏:{}没有大于预计收益>{},钱在路上了", uplValue, imrValue);
+ redisUtils.set(positionsOrderPriceKey, String.valueOf(pingCang.getValue()), 0);
+ return OrderParamEnums.HOLDING.getValue();
+ }
+ }
+ }else {
+ return OrderParamEnums.HOLDING.getValue();
+ }
} else {
log.info("未触发减仓......,等待");
}
@@ -109,6 +216,12 @@
log.error("解析价格失败,请检查Redis中的值是否合法", e);
return OrderParamEnums.HOLDING.getValue();
}
+ }
+
+ private boolean doAddCang(BigDecimal imrValue) {
+ String everyTimeUsdt = (String) redisUtils.get(AccountWs.ACCOUNTWS_CHANNEL + ":" + CoinEnums.USDT.getCode() + ":everyTimeUsdt");
+ BigDecimal everyTimeUsdtValue = new BigDecimal(everyTimeUsdt);
+ return everyTimeUsdtValue.compareTo(imrValue) >= 0;
}
/**
@@ -125,6 +238,7 @@
if (orderPrice == null) {
return;
}
+ log.info("需要移除的价格: {}", orderPrice);
BigDecimal priceDecimal;
try {
@@ -133,49 +247,38 @@
log.warn("无效的价格格式: {}", orderPrice);
return;
}
-
- boolean kaiCangExists = queueKaiCang.stream().anyMatch(item -> item.getValue().equals(priceDecimal));
- if (!kaiCangExists) {
- queueKaiCang.add(new DescBigDecimal(orderPrice));
- } else {
- queueKaiCang.removeIf(item -> item.getValue().equals(priceDecimal));
+ // 删除比该价格大的数据(由于是降序队列,所以是删除value.compareTo(priceDecimal) < 0的元素)
+ queueKaiCang.removeIf(item -> item.getValue().compareTo(priceDecimal) <= 0);
+ // 打印开仓队列
+ StringBuilder kaiCangStr = new StringBuilder();
+ kaiCangStr.append("开仓队列: [");
+ boolean first = true;
+ for (DescBigDecimal item : queueKaiCang) {
+ if (!first) {
+ kaiCangStr.append(", ");
+ }
+ kaiCangStr.append(item.getValue());
+ first = false;
}
+ kaiCangStr.append("]");
+ log.info(kaiCangStr.toString());
- boolean pingCangExists = queuePingCang.stream().anyMatch(item -> item.getValue().equals(priceDecimal));
- if (!pingCangExists) {
- queuePingCang.add(new AscBigDecimal(orderPrice));
- } else {
- queuePingCang.removeIf(item -> item.getValue().equals(priceDecimal));
- }
- }
+ // 删除比该价格小的数据(由于是升序队列,所以是删除value.compareTo(priceDecimal) > 0的元素)
+ queuePingCang.removeIf(item -> item.getValue().compareTo(priceDecimal) >= 0);
- /**
- * 计算盈亏金额。
- *
- * @param faceValue 面值
- * @param position 持仓数量
- * @param contractMultiplier 合约乘数
- * @param markPrice 标记价格
- * @param openPrice 开仓价格
- * @param isLong 是否为多头仓位
- * @param minTickSz 最小变动单位精度
- * @return 盈亏金额,保留指定精度的小数位
- */
- public BigDecimal profit(BigDecimal faceValue, BigDecimal position, BigDecimal contractMultiplier,
- BigDecimal markPrice, BigDecimal openPrice, boolean isLong, int minTickSz) {
- BigDecimal profit = BigDecimal.ZERO;
- if (isLong) {
- profit = markPrice.subtract(openPrice)
- .multiply(faceValue)
- .multiply(contractMultiplier)
- .multiply(position);
- } else {
- profit = openPrice.subtract(markPrice)
- .multiply(faceValue)
- .multiply(contractMultiplier)
- .multiply(position);
+ // 打印平仓队列
+ StringBuilder pingCangStr = new StringBuilder();
+ pingCangStr.append("平仓队列: [");
+ first = true;
+ for (AscBigDecimal item : queuePingCang) {
+ if (!first) {
+ pingCangStr.append(", ");
+ }
+ pingCangStr.append(item.getValue());
+ first = false;
}
- return profit.setScale(minTickSz, BigDecimal.ROUND_DOWN);
+ pingCangStr.append("]");
+ log.info(pingCangStr.toString());
}
}
--
Gitblit v1.9.1