Administrator
7 days ago ff340aba27d8990386fa788a182c2ace0a4c2ad8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package com.xcong.excoin.modules.okxNewPrice.okxWs;
 
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
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.okxWs.param.TradeRequestParam;
import com.xcong.excoin.modules.okxNewPrice.utils.WsMapBuild;
import com.xcong.excoin.modules.okxNewPrice.utils.WsParamBuild;
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, TradeRequestParam tradeRequestParam) {
 
 
        log.info("开始执行TradeOrderWs{}......", JSONUtil.parse(tradeRequestParam));
        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,ordType, 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;
        }
        if (BigDecimal.ZERO.compareTo(new BigDecimal(sz)) >= 0) {
            log.warn("下单数量{}不允许下单,取消发送", sz);
            return;
        }
 
        /**
         * 检验账户和仓位是否准备就绪
         * 开多:买入开多(side 填写 buy; posSide 填写 long )
         * 开空:卖出开空(side 填写 sell; posSide 填写 short ) 需要检验账户通道是否准备就绪
         * 平多:卖出平多(side 填写 sell;posSide 填写 long )
         * 平空:买入平空(side 填写 buy; posSide 填写 short ) 需要检验仓位通道是否准备就绪
         */
        //买入开多、卖出开空则验证仓位通道是否准备就绪
 
        String positionAccountName = PositionsWs.initAccountName(accountName, posSide);
        boolean b = posSide.equals(CoinEnums.POSSIDE_LONG.getCode()) && side.equals(CoinEnums.SIDE_BUY.getCode());
        boolean c = posSide.equals(CoinEnums.POSSIDE_SHORT.getCode()) && side.equals(CoinEnums.SIDE_SELL.getCode());
        if ( b || c ){
            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;
            }
            String accountReadyState = AccountWs.getAccountMap(accountName).get(CoinEnums.READY_STATE.name());
            if (!CoinEnums.READY_STATE_YES.getCode().equals(accountReadyState)) {
                log.info("账户通道未就绪,取消发送");
                return;
            }
        }
 
        try {
            JSONArray argsArray = new JSONArray();
            JSONObject args = new JSONObject();
            args.put("instId", instId);
            args.put("tdMode", tdMode);
            args.put("clOrdId", clOrdId);
            args.put("side", side);
 
            args.put("posSide", posSide);
            args.put("ordType", ordType);
            args.put("sz", sz);
            argsArray.add(args);
 
            String connId = WsParamBuild.getOrderNum(ORDERWS_CHANNEL);
            JSONObject jsonObject = WsParamBuild.buildJsonObject(connId, ORDERWS_CHANNEL, argsArray);
            webSocketClient.send(jsonObject.toJSONString());
            log.info("发送下单频道:{},数量:{}", side, sz);
 
            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);
        }
    }
 
    public static void orderZhiYingZhiSunEventNoState(WebSocketClient webSocketClient, TradeRequestParam tradeRequestParam) {
 
 
        log.info("开始执行TradeOrderWs......");
        if (tradeRequestParam == null){
 
            log.warn("下单{}参数缺失,取消发送",tradeRequestParam);
            return;
        }
        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();
        /**
         * 校验必要参数
         * 验证下单参数是否存在空值
         */
        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)
                        || StrUtil.isBlank(markPx)
 
        ){
            log.warn("下单参数缺失,取消发送");
            return;
        }
        log.info("账户:{},类型:{},触发价格:{},币种:{},方向:{},买卖:{},数量:{},是否允许下单:{},编号:{},",
                accountName,ordType, markPx, instId, posSide,side,  sz, tradeType, clOrdId);
        //验证是否允许下单
        if (StrUtil.isNotEmpty(tradeType) && OrderParamEnums.TRADE_NO.getValue().equals(tradeType)) {
            log.warn("账户{}不允许下单,取消发送", accountName);
            return;
        }
 
        /**
         * 检验账户和仓位是否准备就绪
         * 开多:买入开多(side 填写 buy; posSide 填写 long )
         * 开空:卖出开空(side 填写 sell; posSide 填写 short ) 需要检验账户通道是否准备就绪
         * 平多:卖出平多(side 填写 sell;posSide 填写 long )
         * 平空:买入平空(side 填写 buy; posSide 填写 short ) 需要检验仓位通道是否准备就绪
         */
 
        try {
            JSONArray argsArray = new JSONArray();
            JSONObject args = new JSONObject();
            args.put("instId", instId);
            args.put("tdMode", tdMode);
            args.put("clOrdId", clOrdId);
            args.put("side", side);
 
            args.put("posSide", posSide);
            args.put("ordType", ordType);
            args.put("sz", sz);
            args.put("px", markPx);
            argsArray.add(args);
 
            String connId = WsParamBuild.getOrderNum(ORDERWS_CHANNEL);
            JSONObject jsonObject = WsParamBuild.buildJsonObject(connId, ORDERWS_CHANNEL, argsArray);
            webSocketClient.send(jsonObject.toJSONString());
            log.info("发送下单频道:{},数量:{}", side, sz);
 
        } catch (Exception e) {
            log.error("下单构建失败", e);
        }
    }
 
 
 
    /**
     * 计算盈亏金额。
     *
     * @param faceValue 面值
     * @param position 持仓数量
     * @param markPrice 标记价格
     * @param openPrice 开仓价格
     * @param isLong 是否为多头仓位
     * @param minTickSz 最小变动单位精度
     * @return 盈亏金额,保留指定精度的小数位
     */
    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);
    }
 
}