From c8b80dc38d75e89aa44574659b154ddea2e8fce5 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Wed, 07 Jan 2026 14:16:32 +0800
Subject: [PATCH] refactor(okx): 重构WebSocket客户端实现止盈止损逻辑
---
src/main/java/com/xcong/excoin/modules/okxNewPrice/OkxNewPriceWebSocketClient.java | 80 +++++++++++++++++++++------------------
1 files changed, 43 insertions(+), 37 deletions(-)
diff --git a/src/main/java/com/xcong/excoin/modules/okxNewPrice/OkxNewPriceWebSocketClient.java b/src/main/java/com/xcong/excoin/modules/okxNewPrice/OkxNewPriceWebSocketClient.java
index 181ecd0..94a7c43 100644
--- a/src/main/java/com/xcong/excoin/modules/okxNewPrice/OkxNewPriceWebSocketClient.java
+++ b/src/main/java/com/xcong/excoin/modules/okxNewPrice/OkxNewPriceWebSocketClient.java
@@ -4,12 +4,16 @@
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xcong.excoin.modules.okxNewPrice.celue.CaoZuoService;
+import com.xcong.excoin.modules.okxNewPrice.okxWs.AccountWs;
+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.okxWs.param.TradeRequestParam;
import com.xcong.excoin.modules.okxNewPrice.okxWs.wanggeList.WangGeListEnum;
import com.xcong.excoin.modules.okxNewPrice.okxWs.wanggeList.WangGeListService;
import com.xcong.excoin.modules.okxNewPrice.utils.SSLConfig;
+import com.xcong.excoin.modules.okxNewPrice.utils.WsMapBuild;
import com.xcong.excoin.utils.RedisUtils;
import java.math.BigDecimal;
import lombok.extern.slf4j.Slf4j;
@@ -310,24 +314,46 @@
}
// 获取所有OkxQuantWebSocketClient实例
for (OkxQuantWebSocketClient client : clientManager.getAllClients()) {
- String accountName = getAccountNameFromClient(client);
+ String accountName = client.getAccountName();
if (accountName != null) {
- /**
- * 处理历史网格的订单
- * 根据历史网格的开单方向,是否需要止损处理
- * 如果方向一致就不需要处理
- * 如果不一致则需要处理
- */
- String fangXiang = gridByPriceNew.getFang_xiang();
- String fangXiangOld = CoinEnums.POSSIDE_LONG.equals(fangXiang) ? CoinEnums.POSSIDE_SHORT.getCode() : CoinEnums.POSSIDE_LONG.getCode();
- log.info("历史网格方向为:{}", fangXiangOld);
- TradeRequestParam tradeRequestParamOld = caoZuoService.caoZuoZhiSunEvent(accountName, markPx, fangXiangOld);
- TradeOrderWs.orderEvent(client.getWebSocketClient(), tradeRequestParamOld);
-
- /**
- * 处理当前网格的订单,触发量化操作
- */
log.info("当前价格{}属于网格: {}-{}({}-{})", markPx, gridByPriceNew.getName(),gridByPriceNew.getFang_xiang(), gridByPriceNew.getJiage_xiaxian(), gridByPriceNew.getJiage_shangxian());
+ //处理历史网格信息
+ String fangXiang = gridByPriceNew.getFang_xiang();
+ String posSideOld = null;
+ if (CoinEnums.POSSIDE_LONG.getCode().equals(fangXiang)) {
+ posSideOld = CoinEnums.POSSIDE_SHORT.getCode();
+ }
+ if (CoinEnums.POSSIDE_SHORT.getCode().equals(fangXiang)) {
+ posSideOld = CoinEnums.POSSIDE_LONG.getCode();
+ }
+
+ String positionAccountName = PositionsWs.initAccountName(accountName, posSideOld);
+ // 判断是否保证金超标
+ if (
+ PositionsWs.getAccountMap(positionAccountName).get("pos") != null
+ && PositionsWs.getAccountMap(positionAccountName).get("pos").compareTo(BigDecimal.ZERO) > 0
+ ){
+ BigDecimal avgPx = PositionsWs.getAccountMap(positionAccountName).get("avgPx");
+ WangGeListEnum gridByPriceOld = WangGeListEnum.getGridByPrice(avgPx);
+ if (gridByPriceOld != null){
+ String zhiSunDian = gridByPriceOld.getZhi_sun_dian();
+ if (CoinEnums.POSSIDE_SHORT.getCode().equals(posSideOld)) {
+ if (new BigDecimal(markPx).compareTo(new BigDecimal(zhiSunDian)) > 0){
+ TradeRequestParam tradeRequestParam = caoZuoService.caoZuoZhiSunEvent(accountName, markPx, posSideOld);
+ TradeOrderWs.orderEvent(client.getWebSocketClient(), tradeRequestParam);
+
+ }
+ }
+ if (CoinEnums.POSSIDE_LONG.getCode().equals(posSideOld)) {
+ if (new BigDecimal(markPx).compareTo(new BigDecimal(zhiSunDian)) < 0){
+ TradeRequestParam tradeRequestParam = caoZuoService.caoZuoZhiSunEvent(accountName, markPx, posSideOld);
+ TradeOrderWs.orderEvent(client.getWebSocketClient(), tradeRequestParam);
+ }
+ }
+ }
+ }
+
+ //当前下单
wangGeListService.initWangGe(markPx);
TradeRequestParam tradeRequestParam = caoZuoService.caoZuoHandler(accountName, markPx, gridByPriceNew.getFang_xiang());
TradeOrderWs.orderEvent(client.getWebSocketClient(), tradeRequestParam);
@@ -336,26 +362,6 @@
}
} catch (Exception e) {
log.error("触发量化操作失败", e);
- }
- }
-
- /**
- * 从OkxQuantWebSocketClient实例中获取账号名称
- * 由于OkxQuantWebSocketClient没有直接暴露账号名称的方法,这里需要通过反射获取
- * 更好的方式是修改OkxQuantWebSocketClient,添加getAccountName方法
- */
- private String getAccountNameFromClient(OkxQuantWebSocketClient client) {
- try {
- // 通过反射获取account字段的值
- java.lang.reflect.Field accountField = OkxQuantWebSocketClient.class.getDeclaredField("account");
- accountField.setAccessible(true);
- Object account = accountField.get(client);
- // 调用account的name()方法获取账号名称
- java.lang.reflect.Method nameMethod = account.getClass().getMethod("name");
- return (String) nameMethod.invoke(account);
- } catch (Exception e) {
- log.error("获取账号名称失败", e);
- return null;
}
}
@@ -448,7 +454,7 @@
*/
private void reconnectWithBackoff() throws InterruptedException {
int attempt = 0;
- int maxAttempts = 5;
+ int maxAttempts = 3;
long delayMs = 5000;
while (attempt < maxAttempts) {
--
Gitblit v1.9.1