Administrator
2025-12-23 5175edfc6e1b09bf34672479ea5784e529a570f1
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
package com.xcong.excoin.modules.okxNewPrice.okxpi.config.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xcong.excoin.modules.okxNewPrice.okxpi.config.Dto.SubmitOrderReqDto;
import com.xcong.excoin.modules.okxNewPrice.okxpi.config.ExchangeInfoEnum;
import com.xcong.excoin.modules.okxNewPrice.okxpi.config.ExchangeLoginEventService;
import com.xcong.excoin.modules.okxNewPrice.okxpi.config.ExchangeLoginService;
import com.xcong.excoin.modules.okxNewPrice.okxpi.config.OKXAccount;
import com.xcong.excoin.modules.okxNewPrice.okxpi.config.enums.DefaultUrls;
import com.xcong.excoin.modules.okxNewPrice.okxpi.config.enums.HttpMethod;
import com.xcong.excoin.modules.okxNewPrice.okxpi.config.utils.OKXContants;
import lombok.extern.slf4j.Slf4j;
 
import java.math.BigDecimal;
import java.util.*;
 
@Slf4j
public class ExchangeLoginEventServiceImpl implements ExchangeLoginEventService {
 
 
    private OKXAccount OKXAccount;
    private String apiKey;
    private String secretKey;
    private String passphrase;
    private boolean accountType;
 
    public ExchangeLoginEventServiceImpl(String apiKey, String secretKey, String passphrase, boolean accountType) {
        this.apiKey = apiKey;
        this.secretKey = secretKey;
        this.passphrase = passphrase;
        this.accountType = accountType;
        OKXAccount = new OKXAccount(
                accountType ? DefaultUrls.USDM_PROD_URL : DefaultUrls.USDM_UAT_URL,
                apiKey,
                secretKey,
                passphrase,
                !accountType);
    }
 
    @Override
    public String exchangeInfo(LinkedHashMap<String, Object> parameters) {
        return OKXAccount.requestHandler.sendPublicRequest(OKXAccount.baseUrl, OKXContants.INSTRUMENTS,parameters, HttpMethod.GET, OKXAccount.isSimluate());
    }
 
    @Override
    public String lineHistory(LinkedHashMap<String, Object> parameters) {
        return OKXAccount.requestHandler.sendPublicRequest(OKXAccount.baseUrl, OKXContants.K_LINE_HISTORY,parameters, HttpMethod.GET, OKXAccount.isSimluate());
    }
 
    @Override
    public String balance(LinkedHashMap<String, Object> parameters) {
        return OKXAccount.requestHandler.sendSignedRequest(OKXAccount.baseUrl, OKXContants.BALANCE, parameters, HttpMethod.GET, OKXAccount.isSimluate());
    }
 
    @Override
    public String positions(LinkedHashMap<String, Object> parameters) {
        return OKXAccount.requestHandler.sendSignedRequest(OKXAccount.baseUrl, OKXContants.POSITIONS, parameters, HttpMethod.GET, OKXAccount.isSimluate());
    }
 
    @Override
    public String positionsHistory(LinkedHashMap<String, Object> parameters) {
        return OKXAccount.requestHandler.sendSignedRequest(OKXAccount.baseUrl, OKXContants.POSITIONS_HISTORY, parameters, HttpMethod.GET, OKXAccount.isSimluate());
    }
 
    @Override
    public boolean cancelOrder(String originOrderId,String instId) {
        LinkedHashMap<String, Object> parameters = new LinkedHashMap<>();
        parameters.put("instId", instId);
        parameters.put("clOrdId", originOrderId);
 
        String s = OKXAccount.requestHandler.sendSignedRequest(OKXAccount.baseUrl, OKXContants.CANCEL_ORDER, parameters, HttpMethod.POST, OKXAccount.isSimluate());
        log.info("[{}] 收到撤单请求,返回", s);
        return true;
    }
 
    @Override
    public String submitOrder(SubmitOrderReqDto submitOrderReq) {
        log.info("收到下单请求,参数:[{}]", submitOrderReq);
        LinkedHashMap<String, Object> parameters = new LinkedHashMap<>();
 
        String side = submitOrderReq.getSide();
        String type = submitOrderReq.getOrdType();
        String positionSides = submitOrderReq.getPosSide();
        // 开仓
        //开多:买多BUY、LONG
        //开空:卖空SELL、SHORT
        if ("buy".equals(side)) {
            if ("limit".equals(type)) {
                parameters.put("px", submitOrderReq.getPx());
                parameters.put("ordType", "limit");
            }
            // 持仓方向
            parameters.put("posSide", positionSides);
            //placeOrderReq.setPosSide(positionSide);
            // slTriggerPx 止损触发价,如果填写此参数,必须填写 止损委托价
            // slOrdPx 止损委托价,如果填写此参数,必须填写 止损触发价
 
            if (new BigDecimal(submitOrderReq.getSlTriggerPx()).compareTo(BigDecimal.ZERO) > 0) {
                Map<String, Object> attachAlgoOrder = new HashMap<>();
                // 如果是开多 止损价小于传来的价格
 
                List<Map<String, Object>> attachAlgoOrds = new ArrayList<>();
                // 如果是开空 止损价小于传来的价格
                attachAlgoOrder.put("slTriggerPx", submitOrderReq.getSlTriggerPx());
                attachAlgoOrder.put("slOrdPx", submitOrderReq.getSlOrdPx());
                attachAlgoOrds.add(attachAlgoOrder);
                parameters.put("attachAlgoOrds", attachAlgoOrds);
            } else {
                BigDecimal price = new BigDecimal(submitOrderReq.getPrice());
                BigDecimal stopPrice = BigDecimal.ZERO;
                if ("buy".equalsIgnoreCase(side)) {
                    stopPrice = price.multiply(new BigDecimal("0.99")).setScale(2,BigDecimal.ROUND_DOWN);
                } else {
                    stopPrice = price.multiply(new BigDecimal("1.01")).setScale(2,BigDecimal.ROUND_DOWN);
                }
                Map<String, Object> attachAlgoOrder = new HashMap<>();
                // 如果是开多 止损价小于传来的价格
                List<Map<String, Object>> attachAlgoOrds = new ArrayList<>();
                // 如果是开空 止损价小于传来的价格
                attachAlgoOrder.put("slTriggerPx", stopPrice);
                attachAlgoOrder.put("slOrdPx", stopPrice);
                attachAlgoOrds.add(attachAlgoOrder);
                parameters.put("attachAlgoOrds", attachAlgoOrds);
            }
        } else {
            // 平仓
            //平空:卖空BUY、SHORT
            //平多:卖多SELL、LONG
            //side = (CoreEnum.DirectionEnum.D_Buy.getNumber() == direction.getNumber()) ? "BUY" : "SELL";
            // 平仓方向
            //positionSide = (CoreEnum.DirectionEnum.D_Buy.getNumber() == direction.getNumber()) ? "SHORT" : "LONG";
            if ("limit".equals(type)) {
                //placeOrderReq.setPx(new BigDecimal(submitOrderReq.price()));
                parameters.put("px", submitOrderReq.getPrice());
                //parameters.put("timeInForce", timeInForce);
            }
            //placeOrderReq.setPosSide(positionSide);
            parameters.put("posSide", positionSides);
        }
 
        //订单种类,市价单不传价格
        parameters.put("instId", submitOrderReq.getInstId());
        parameters.put("side", side);
        //placeOrderReq.setSide(side);
        parameters.put("ordType", type);
        //placeOrderReq.setSz(new BigDecimal(quantity));
        parameters.put("sz", submitOrderReq.getSz());
        //placeOrderReq.setClOrdId(submitOrderReq.originOrderId());
        parameters.put("clOrdId", submitOrderReq.getClOrdId());
        parameters.put("tdMode", submitOrderReq.getTdMode());
        log.info("下单参数:[{}]",JSON.toJSONString(parameters));
        String placeOrderRspOkxRestResponse = OKXAccount.requestHandler.sendSignedRequest(OKXAccount.baseUrl, OKXContants.ORDER, parameters, HttpMethod.POST, OKXAccount.isSimluate());
        log.info("收到下单返回,响应:[{}]", JSON.parseObject(placeOrderRspOkxRestResponse).get("data"));
        return submitOrderReq.getClOrdId();
    }
 
    @Override
    public String tickerMess(LinkedHashMap<String, Object> parameters) {
        return OKXAccount.requestHandler.sendSignedRequest(OKXAccount.baseUrl, OKXContants.TICKER, parameters, HttpMethod.GET, OKXAccount.isSimluate());
    }
 
    @Override
    public String getOrderMessage(LinkedHashMap<String, Object> parameters) {
        return OKXAccount.requestHandler.sendSignedRequest(OKXAccount.baseUrl, OKXContants.TICKER, parameters, HttpMethod.GET, OKXAccount.isSimluate());
    }
 
}