From bdbdd07ad9d3bcb82e9f15fbda005962d35cf948 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Fri, 19 Dec 2025 13:17:34 +0800
Subject: [PATCH] feat(okx): 切换OKX WebSocket客户端至实盘环境
---
src/main/java/com/xcong/excoin/modules/okxNewPrice/okxWs/TradeOrderWs.java | 159 ++++++++++++++++++++++++++++------------------------
1 files changed, 85 insertions(+), 74 deletions(-)
diff --git a/src/main/java/com/xcong/excoin/modules/okxNewPrice/okxWs/TradeOrderWs.java b/src/main/java/com/xcong/excoin/modules/okxNewPrice/okxWs/TradeOrderWs.java
index 093ac5e..0238445 100644
--- a/src/main/java/com/xcong/excoin/modules/okxNewPrice/okxWs/TradeOrderWs.java
+++ b/src/main/java/com/xcong/excoin/modules/okxNewPrice/okxWs/TradeOrderWs.java
@@ -5,99 +5,123 @@
import com.alibaba.fastjson.JSONObject;
import com.xcong.excoin.modules.okxNewPrice.okxWs.enums.CoinEnums;
import com.xcong.excoin.modules.okxNewPrice.okxWs.enums.OrderParamEnums;
-import com.xcong.excoin.modules.okxNewPrice.okxpi.MallUtils;
+import com.xcong.excoin.modules.okxNewPrice.okxWs.param.TradeRequestParam;
+import com.xcong.excoin.modules.okxNewPrice.utils.WsMapBuild;
import com.xcong.excoin.modules.okxNewPrice.utils.WsParamBuild;
-import com.xcong.excoin.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.java_websocket.client.WebSocketClient;
import java.math.BigDecimal;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
/**
+ * 交易订单处理类,负责构建和发送订单请求到OKX WebSocket
+ *
* @author Administrator
*/
@Slf4j
public class TradeOrderWs {
+ // 使用双层Map,第一层key为账号名称,第二层key为数据key
+ public static final Map<String, Map<String,String>> TRADEORDERWSMAP = new ConcurrentHashMap<>();
+
+ // 获取指定账号的Map,如果不存在则创建
+ public static Map<String, String> getAccountMap(String accountName) {
+ return TRADEORDERWSMAP.computeIfAbsent(accountName, k -> new ConcurrentHashMap<>());
+ }
+
public static final String ORDERWS_CHANNEL = "order";
- public static void orderEvent(WebSocketClient webSocketClient, RedisUtils redisUtils, String side) {
+ public static void orderEvent(WebSocketClient webSocketClient, TradeRequestParam tradeRequestParam) {
+
log.info("开始执行TradeOrderWs......");
- if (StrUtil.isBlank( side)){
- log.warn("止损了,下次再战...");
+ String accountName = tradeRequestParam.getAccountName();
+ String markPx = tradeRequestParam.getMarkPx();
+ String instId = tradeRequestParam.getInstId();
+ String tdMode = tradeRequestParam.getTdMode();
+ String posSide = tradeRequestParam.getPosSide();
+ String ordType = tradeRequestParam.getOrdType();
+
+ String tradeType = tradeRequestParam.getTradeType();
+
+ String clOrdId = tradeRequestParam.getClOrdId();
+ String side = tradeRequestParam.getSide();
+ String sz = tradeRequestParam.getSz();
+ log.info("账户:{},触发价格:{},币种:{},方向:{},买卖:{},数量:{},是否允许下单:{},编号:{},",
+ accountName, markPx, instId, posSide,side, sz, tradeType, clOrdId);
+ //验证是否允许下单
+ if (StrUtil.isNotEmpty(tradeType) && OrderParamEnums.TRADE_NO.getValue().equals(tradeType)) {
+ log.warn("账户{}不允许下单,取消发送", accountName);
+ return;
+ }
+ /**
+ * 校验必要参数
+ * 验证下单参数是否存在空值
+ */
+ if (
+ StrUtil.isBlank(accountName)
+ || StrUtil.isBlank(instId)
+ || StrUtil.isBlank(tdMode)
+ || StrUtil.isBlank(posSide)
+ || StrUtil.isBlank(ordType)
+ || StrUtil.isBlank(clOrdId)
+ || StrUtil.isBlank(side)
+ || StrUtil.isBlank(sz)
+
+ ){
+ log.warn("下单参数缺失,取消发送");
return;
}
- String buyCnt = null;
- String ctval = getRedisValue(redisUtils, InstrumentsWs.INSTRUMENTSWS_CHANNEL, ":ctVal");
- String buyCntNormal = getRedisValue(redisUtils, PositionsWs.POSITIONSWS_CHANNEL, ":buyCnt");
- String pos = getRedisValue(redisUtils, PositionsWs.POSITIONSWS_CHANNEL, ":pos");
- if (OrderParamEnums.ORDERING.getValue().equals(side)) {
- return;
- } else if (OrderParamEnums.HOLDING.getValue().equals(side)) {
- return;
- } else if (OrderParamEnums.INIT.getValue().equals(side)) {
- side = OrderParamEnums.BUY.getValue();
- if (StrUtil.isNotBlank(buyCntNormal) && BigDecimal.ZERO.compareTo(new BigDecimal(buyCntNormal)) > 0) {
- buyCnt = buyCntNormal;
- }else{
- buyCnt = ctval;
- }
- } else if (OrderParamEnums.OUT.getValue().equals(side)) {
- log.info(OrderParamEnums.getNameByValue(OrderParamEnums.OUT.getValue()));
- side = OrderParamEnums.SELL.getValue();
- buyCnt = pos;
- } else if (OrderParamEnums.BUY.getValue().equals(side)){
- side = OrderParamEnums.BUY.getValue();
- if (StrUtil.isNotBlank(buyCntNormal) && BigDecimal.ZERO.compareTo(new BigDecimal(buyCntNormal)) > 0) {
- buyCnt = buyCntNormal;
- }else{
- buyCnt = ctval;
- }
- }else if (OrderParamEnums.SELL.getValue().equals(side)){
- side = OrderParamEnums.SELL.getValue();
- buyCnt = getRedisValue(redisUtils, PositionsWs.POSITIONSWS_CHANNEL, ":pos");
- }else{
- log.warn("操作信号异常,请检查下单操作...");
+ /**
+ * 检验账户和仓位是否准备就绪
+ * 开多:买入开多(side 填写 buy; posSide 填写 long )
+ * 开空:卖出开空(side 填写 sell; posSide 填写 short ) 需要检验账户通道是否准备就绪
+ * 平多:卖出平多(side 填写 sell;posSide 填写 long )
+ * 平空:买入平空(side 填写 buy; posSide 填写 short ) 需要检验仓位通道是否准备就绪
+ */
+
+ String positionAccountName = PositionsWs.initAccountName(accountName, posSide);
+ BigDecimal positionsReadyState = PositionsWs.getAccountMap(positionAccountName).get(CoinEnums.READY_STATE.name()) == null
+ ? BigDecimal.ZERO : PositionsWs.getAccountMap(positionAccountName).get(CoinEnums.READY_STATE.name());
+ if (WsMapBuild.parseBigDecimalSafe(CoinEnums.READY_STATE_YES.getCode()).compareTo(positionsReadyState) != 0) {
+ log.info("仓位{}通道未就绪,取消发送",positionAccountName);
return;
}
-
- // 校验必要参数
- if (StrUtil.isBlank(side)) {
- log.warn("下单参数 side 为空,取消发送");
- return;
- }
-
- if (StrUtil.isBlank(buyCnt)) {
- log.warn("下单数量 buyCnt 为空,取消发送");
+ String accountReadyState = AccountWs.getAccountMap(accountName).get(CoinEnums.READY_STATE.name());
+ if (!CoinEnums.READY_STATE_YES.getCode().equals(accountReadyState)) {
+ log.info("账户通道未就绪,取消发送");
return;
}
try {
- String clOrdId = MallUtils.getOrderNum(side);
JSONArray argsArray = new JSONArray();
JSONObject args = new JSONObject();
- args.put("instId", CoinEnums.HE_YUE.getCode());
- args.put("tdMode", CoinEnums.CROSS.getCode());
+ args.put("instId", instId);
+ args.put("tdMode", tdMode);
args.put("clOrdId", clOrdId);
args.put("side", side);
- args.put("posSide", CoinEnums.POSSIDE_LONG.getCode());
- args.put("ordType", CoinEnums.ORDTYPE_MARKET.getCode());
- args.put("sz", buyCnt);
+
+ args.put("posSide", posSide);
+ args.put("ordType", ordType);
+ args.put("sz", sz);
argsArray.add(args);
- String connId = MallUtils.getOrderNum(ORDERWS_CHANNEL);
+ String connId = WsParamBuild.getOrderNum(ORDERWS_CHANNEL);
JSONObject jsonObject = WsParamBuild.buildJsonObject(connId, ORDERWS_CHANNEL, argsArray);
webSocketClient.send(jsonObject.toJSONString());
- log.info("发送下单频道:{},数量:{}", side, buyCnt);
- boolean setResult =
- redisUtils.set(ORDERWS_CHANNEL + ":" + CoinEnums.HE_YUE.getCode() + ":clOrdId", clOrdId, 0)
- && redisUtils.set(ORDERWS_CHANNEL + ":" + CoinEnums.HE_YUE.getCode() + ":state", CoinEnums.ORDER_FILLED.getCode(), 0)
- && redisUtils.set(InstrumentsWs.INSTRUMENTSWS_CHANNEL + ":" + CoinEnums.HE_YUE.getCode() + ":state", OrderParamEnums.STATE_4.getValue(), 0);
- if (!setResult) {
- log.warn("Redis set operation failed for key: order:{}", CoinEnums.HE_YUE.getCode());
- }
+ log.info("发送下单频道:{},数量:{}", side, sz);
+
+ WsMapBuild.saveStringToMap(getAccountMap(accountName), "clOrdId", clOrdId);
+ WsMapBuild.saveStringToMap(getAccountMap(accountName), "state", CoinEnums.ORDER_FILLED.getCode());
+ /**
+ * 将状态更新为未准备就绪
+ */
+ WsMapBuild.saveBigDecimalToMap(PositionsWs.getAccountMap(positionAccountName), CoinEnums.READY_STATE.name(), WsMapBuild.parseBigDecimalSafe(CoinEnums.READY_STATE_NO.getCode()));
+ WsMapBuild.saveStringToMap(AccountWs.getAccountMap(accountName), CoinEnums.READY_STATE.name(), CoinEnums.READY_STATE_NO.getCode());
+
} catch (Exception e) {
log.error("下单构建失败", e);
}
@@ -131,17 +155,4 @@
return profit.setScale(minTickSz, BigDecimal.ROUND_DOWN);
}
- /**
- * 统一封装 Redis Key 构建逻辑
- *
- * @param redisUtils Redis 工具类实例
- * @param prefix 渠道前缀
- * @param suffix 字段后缀
- * @return Redis 中存储的值
- */
- private static String getRedisValue(RedisUtils redisUtils, String prefix, String suffix) {
- String key = prefix + ":" + CoinEnums.HE_YUE.getCode() + suffix;
- Object valueObj = redisUtils.get(key);
- return valueObj == null ? null : String.valueOf(valueObj);
- }
-}
+}
\ No newline at end of file
--
Gitblit v1.9.1