From 8302a0d3a49e25b9c2aa5f68000b29210b4fd556 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Mon, 15 Dec 2025 20:46:33 +0800
Subject: [PATCH] feat(okx): 调整下单参数配置
---
src/main/java/com/xcong/excoin/modules/okxNewPrice/okxWs/TradeOrderWs.java | 161 ++++++++++++++++++++++++++++++++++++++++-------------
1 files changed, 121 insertions(+), 40 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 7712098..d50dc5c 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,46 +5,110 @@
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.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, String side, String accountName) {
- String buyCnt = null;
-
- if (OrderParamEnums.ORDERING.getValue().equals(side)) {
+// log.info("开始执行TradeOrderWs......");
+ String accountReadyState = AccountWs.getAccountMap(accountName).get(CoinEnums.READY_STATE.name());
+ if (!CoinEnums.READY_STATE_YES.getCode().equals(accountReadyState)) {
+ log.info("账户通道未就绪,取消发送");
return;
- } else if (OrderParamEnums.HOLDING.getValue().equals(side)) {
- return;
- } else if (OrderParamEnums.INIT.getValue().equals(side)) {
- side = OrderParamEnums.BUY.getValue();
- buyCnt = getRedisValue(redisUtils, InstrumentsWs.INSTRUMENTSWS_CHANNEL, ":ctVal");
- } else if (OrderParamEnums.OUT.getValue().equals(side)) {
- side = OrderParamEnums.SELL.getValue();
- buyCnt = getRedisValue(redisUtils, PositionsWs.POSITIONSWS_CHANNEL, ":pos");
- } else {
- String buyCntNormal = getRedisValue(redisUtils, PositionsWs.POSITIONSWS_CHANNEL, ":buyCnt");
- if (StrUtil.isNotBlank(buyCntNormal)) {
- buyCnt = buyCntNormal;
- }
}
-
+ BigDecimal positionsReadyState = PositionsWs.getAccountMap(accountName).get(CoinEnums.READY_STATE.name()) == null
+ ? BigDecimal.ZERO : PositionsWs.getAccountMap(accountName).get(CoinEnums.READY_STATE.name());
+ if (WsMapBuild.parseBigDecimalSafe(CoinEnums.READY_STATE_YES.getCode()).compareTo(positionsReadyState) != 0) {
+ log.info("仓位通道未就绪,取消发送");
+ return;
+ }
// 校验必要参数
if (StrUtil.isBlank(side)) {
log.warn("下单参数 side 为空,取消发送");
return;
}
+ String posSide = InstrumentsWs.getAccountMap(accountName).get(CoinEnums.POSSIDE.name());
+ // 校验必要参数
+ if (StrUtil.isBlank(posSide)) {
+ log.warn("下单参数 posSide 为空,取消发送");
+ return;
+ }
+
+ String buyCnt = "";
+ if (CoinEnums.POSSIDE_LONG.getCode().equals(posSide)){
+ if (OrderParamEnums.HOLDING.getValue().equals(side)){
+ log.info("当前状态为持仓中,取消发送");
+ return;
+ }else if (OrderParamEnums.OUT.getValue().equals(side)){
+ log.info("当前状态为止损");
+ side = OrderParamEnums.SELL.getValue();
+ buyCnt = String.valueOf(PositionsWs.getAccountMap(accountName).get("pos"));
+ }else if (OrderParamEnums.INIT.getValue().equals(side)){
+ log.info("当前状态为初始化");
+ side = OrderParamEnums.BUY.getValue();
+ buyCnt = InstrumentsWs.getAccountMap(accountName).get(CoinEnums.BUY_CNT_INIT.name());
+ }else if (OrderParamEnums.BUY.getValue().equals(side)){
+ log.info("当前状态为加仓");
+ String buyCntTime = getAccountMap(accountName).get("buyCntTime");
+ String buyCntStr = InstrumentsWs.getAccountMap(accountName).get(CoinEnums.BUY_CNT.name());
+ buyCnt = String.valueOf(new BigDecimal(buyCntTime).multiply(new BigDecimal(buyCntStr)));
+ }else if (OrderParamEnums.SELL.getValue().equals(side)){
+ log.info("当前状态为减仓");
+ buyCnt = String.valueOf(PositionsWs.getAccountMap(accountName).get("pos"));
+ }else{
+ log.warn("交易状态异常,取消发送");
+ return;
+ }
+ }else if (CoinEnums.POSSIDE_SHORT.getCode().equals(posSide)){
+ if (OrderParamEnums.HOLDING.getValue().equals(side)){
+ log.info("当前状态为持仓中,取消发送");
+ return;
+ }else if (OrderParamEnums.OUT.getValue().equals(side)){
+ log.info("当前状态为止损");
+ side = OrderParamEnums.BUY.getValue();
+ buyCnt = String.valueOf(PositionsWs.getAccountMap(accountName).get("pos"));
+ }else if (OrderParamEnums.INIT.getValue().equals(side)){
+ log.info("当前状态为初始化");
+ side = OrderParamEnums.SELL.getValue();
+ buyCnt = InstrumentsWs.getAccountMap(accountName).get(CoinEnums.BUY_CNT_INIT.name());
+ }else if (OrderParamEnums.BUY.getValue().equals(side)){
+ log.info("当前状态为减仓");
+ buyCnt = String.valueOf(PositionsWs.getAccountMap(accountName).get("pos"));
+ }else if (OrderParamEnums.SELL.getValue().equals(side)){
+ log.info("当前状态为加仓");
+ String buyCntTime = getAccountMap(accountName).get("buyCntTime");
+ String buyCntStr = InstrumentsWs.getAccountMap(accountName).get(CoinEnums.BUY_CNT.name());
+ buyCnt = String.valueOf(new BigDecimal(buyCntTime).multiply(new BigDecimal(buyCntStr)));
+ }else{
+ log.warn("交易状态异常,取消发送");
+ return;
+ }
+ }
+
if (StrUtil.isBlank(buyCnt)) {
log.warn("下单数量 buyCnt 为空,取消发送");
@@ -52,45 +116,62 @@
}
try {
- String clOrdId = MallUtils.getOrderNum(side);
+ String clOrdId = WsParamBuild.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("clOrdId", clOrdId);
args.put("side", side);
- args.put("posSide", CoinEnums.POSSIDE_LONG.getCode());
+
+ args.put("posSide", posSide);
args.put("ordType", CoinEnums.ORDTYPE_MARKET.getCode());
args.put("sz", buyCnt);
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());
- }
+
+ WsMapBuild.saveStringToMap(getAccountMap(accountName), "buyCntTime",String.valueOf(BigDecimal.ONE));
+ WsMapBuild.saveStringToMap(getAccountMap(accountName), "clOrdId", clOrdId);
+ WsMapBuild.saveStringToMap(getAccountMap(accountName), "state", CoinEnums.ORDER_FILLED.getCode());
+
+ WsMapBuild.saveBigDecimalToMap(PositionsWs.getAccountMap(accountName), 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);
}
}
+
+
/**
- * 统一封装 Redis Key 构建逻辑
+ * 计算盈亏金额。
*
- * @param redisUtils Redis 工具类实例
- * @param prefix 渠道前缀
- * @param suffix 字段后缀
- * @return Redis 中存储的值
+ * @param faceValue 面值
+ * @param position 持仓数量
+ * @param markPrice 标记价格
+ * @param openPrice 开仓价格
+ * @param isLong 是否为多头仓位
+ * @param minTickSz 最小变动单位精度
+ * @return 盈亏金额,保留指定精度的小数位
*/
- 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);
+ public BigDecimal profit(BigDecimal faceValue, BigDecimal position,
+ BigDecimal markPrice, BigDecimal openPrice, boolean isLong, int minTickSz) {
+ BigDecimal profit = BigDecimal.ZERO;
+ if (isLong) {
+ profit = markPrice.subtract(openPrice)
+ .multiply(faceValue)
+ .multiply(position);
+ } else {
+ profit = openPrice.subtract(markPrice)
+ .multiply(faceValue)
+ .multiply(position);
+ }
+ return profit.setScale(minTickSz, BigDecimal.ROUND_DOWN);
}
-}
+
+}
\ No newline at end of file
--
Gitblit v1.9.1