Administrator
2026-06-05 2fede14ef1191ecd8738af4be3808c087131d8a5
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package com.xcong.excoin.modules.okxNewPrice;
 
import lombok.extern.slf4j.Slf4j;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
 
/**
 * 止损管理器 — 负责基底止损单挂载、止损触发后的逐步缩进、以及止损追挂。
 *
 * <h3>止损策略(对齐 Gate 版本)</h3>
 * <ol>
 *   <li>基底开仓后挂多仓止损(id=-2~-11)和空仓止损(id=2~11),每格 1 倍 quantity</li>
 *   <li>条件单成交后以成交量为粒度追挂止损(extend 系列)</li>
 *   <li>止损触发后向基底方向缩进 1 格,数量由价格差 / 步长决定,N&gt;2 时先取消旧挂单</li>
 * </ol>
 *
 * <h3>线程安全</h3>
 * 本类通过 executor 的异步回调串行化下单操作;对 OkxGridElement 的读写依赖调用方保证有序。
 *
 * @author Administrator
 */
@Slf4j
public class StopLossManager {
 
    private final OkxConfig config;
    private final OkxTradeExecutor executor;
 
    public StopLossManager(OkxConfig config, OkxTradeExecutor executor) {
        this.config = config;
        this.executor = executor;
    }
 
    // ========== 基底止损挂载 ==========
 
    /**
     * 在基底开仓完成后挂载初始止损单。
     * 多仓止损: id=-2 到 -11,每格 quantity 张,方向 sell/long
     * 空仓止损: id=2 到 11,每格 quantity 张,方向 buy/short
     */
    public void setupBaseStopLosses() {
        // 挂多仓止损 (id=-2 到 -11)
        for (int id = -2; id >= -11; id--) {
            OkxGridElement elem = OkxGridElement.findById(id);
            if (elem == null) continue;
            BigDecimal triggerPrice = elem.getGridPrice();
            int finalId = id;
            executor.placeTakeProfit(triggerPrice.toString(), "sell", "long", config.getQuantity(),
                    profitId -> {
                        elem.setLongStopLossOrderId(profitId);
                        OkxGridElement.refreshIndices();
                        log.info("[OKX] 多仓止损已挂, gridId:{}, 触发价:{}, qty:{}, stopLossId:{}",
                                finalId, triggerPrice, config.getQuantity(), profitId);
                    });
        }
 
        // 挂空仓止损 (id=2 到 11)
        for (int id = 2; id <= 11; id++) {
            OkxGridElement elem = OkxGridElement.findById(id);
            if (elem == null) continue;
            BigDecimal triggerPrice = elem.getGridPrice();
            int finalId = id;
            executor.placeTakeProfit(triggerPrice.toString(), "buy", "short", config.getQuantity(),
                    profitId -> {
                        elem.setShortStopLossOrderId(profitId);
                        OkxGridElement.refreshIndices();
                        log.info("[OKX] 空仓止损已挂, gridId:{}, 触发价:{}, qty:{}, stopLossId:{}",
                                finalId, triggerPrice, config.getQuantity(), profitId);
                    });
        }
 
        log.info("[OKX] 止损单已全部挂完, 空仓止损: 2~11, 多仓止损: -2~-11");
    }
 
    // ========== 止损触发处理 ==========
 
    /**
     * 多仓止损触发处理。
     * 止损触发后向基底方向缩进 1 格挂条件多单,数量 = |触发价 - 当前持仓均价| / 网格步长,取整。
     * 若 N &gt; 2,先取消上一步的旧挂单。
     */
    public void handleLongStopLossTriggered(OkxGridElement gridElement, BigDecimal longEntryPrice) {
        int gridId = gridElement.getId();
        int N = Math.abs(gridId);
        gridElement.setLongStopLossOrderId(null);
        log.info("[OKX] 多仓止损触发 gridId:{}, 逐步缩进", gridId);
 
        int newEntryGridId = -(N - 1);
        OkxGridElement newEntryGrid = OkxGridElement.findById(newEntryGridId);
        if (newEntryGrid == null) {
            OkxGridElement.refreshIndices();
            log.warn("[OKX] 多仓止损触发 gridId:{} 找不到入单网格({})", gridId, newEntryGridId);
            return;
        }
 
        if (N > 2) {
            int cancelGridId = -(N - 2);
            OkxGridElement cancelGrid = OkxGridElement.findById(cancelGridId);
            if (cancelGrid != null && cancelGrid.isHasLongOrder()) {
                executor.cancelAlgoOrder(cancelGrid.getLongOrderId(), oid -> {
                    clearLongEntryState(cancelGrid);
                    log.info("[OKX] 多仓止损触发, 取消gridId:{}的多单", cancelGridId);
                });
            }
        }
 
        BigDecimal triggerPrice = newEntryGrid.getGridPrice();
        BigDecimal priceDiff = longEntryPrice.subtract(triggerPrice).abs();
        BigDecimal epsilon = new BigDecimal("0.00000001");
        int count = priceDiff.add(epsilon).divide(config.getStep(), 0, RoundingMode.DOWN).intValue();
        count = Math.max(1, count);
        int entryQty = count * Integer.parseInt(config.getQuantity());
        String size = String.valueOf(entryQty);
        log.info("[OKX] 多仓止损触发 gridId:{}, 在gridId:{}挂{}张多单(价差:{},步长:{},count:{},qty:{})",
                gridId, newEntryGridId, entryQty, priceDiff, config.getStep(), count, config.getQuantity());
        newEntryGrid.getLongTraderParam().setQuantity(size);
        placeEntryOrderWithPreFlag(newEntryGrid, true, triggerPrice, size);
    }
 
    /**
     * 空仓止损触发处理。
     * 止损触发后向基底方向缩进 1 格挂条件空单,数量 = |触发价 - 当前持仓均价| / 网格步长,取整。
     * 若 N &gt; 2,先取消上一步的旧挂单。
     */
    public void handleShortStopLossTriggered(OkxGridElement gridElement, BigDecimal shortEntryPrice) {
        int gridId = gridElement.getId();
        int N = gridId;
        gridElement.setShortStopLossOrderId(null);
        log.info("[OKX] 空仓止损触发 gridId:{}, 逐步缩进", gridId);
 
        int newEntryGridId = N - 1;
        OkxGridElement newEntryGrid = OkxGridElement.findById(newEntryGridId);
        if (newEntryGrid == null) {
            OkxGridElement.refreshIndices();
            log.warn("[OKX] 空仓止损触发 gridId:{} 找不到入单网格({})", gridId, newEntryGridId);
            return;
        }
 
        if (N > 2) {
            int cancelGridId = N - 2;
            OkxGridElement cancelGrid = OkxGridElement.findById(cancelGridId);
            if (cancelGrid != null && cancelGrid.isHasShortOrder()) {
                executor.cancelAlgoOrder(cancelGrid.getShortOrderId(), oid -> {
                    clearShortEntryState(cancelGrid);
                    log.info("[OKX] 空仓止损触发, 取消gridId:{}的空单", cancelGridId);
                });
            }
        }
 
        BigDecimal triggerPrice = newEntryGrid.getGridPrice();
        BigDecimal priceDiff = shortEntryPrice.subtract(triggerPrice).abs();
        BigDecimal epsilon = new BigDecimal("0.00000001");
        int count = priceDiff.add(epsilon).divide(config.getStep(), 0, RoundingMode.DOWN).intValue();
        count = Math.max(1, count);
        int entryQty = count * Integer.parseInt(config.getQuantity());
        String size = String.valueOf(entryQty);
        log.info("[OKX] 空仓止损触发 gridId:{}, 在gridId:{}挂{}张空单(价差:{},步长:{},count:{},qty:{})",
                gridId, newEntryGridId, entryQty, priceDiff, config.getStep(), count, config.getQuantity());
        newEntryGrid.getShortTraderParam().setQuantity(size);
        placeEntryOrderWithPreFlag(newEntryGrid, false, triggerPrice, size);
    }
 
    // ========== 止损追挂 ==========
 
    /**
     * 多仓追挂止损:按 quantity 粒度拆分为 count 个止损单,从当前最远止损格再往外延伸。
     */
    public void extendLongStopLoss(int filledQty) {
        int qty = Integer.parseInt(config.getQuantity());
        int stopLossCount = filledQty / qty;
        int furthestSlId = 0;
        for (OkxGridElement e : config.getGridElements()) {
            if (e.getLongStopLossOrderId() != null && e.getId() < furthestSlId) {
                furthestSlId = e.getId();
            }
        }
        if (furthestSlId == 0) furthestSlId = -11;
        log.info("[OKX] 多仓追挂止损, 当前最远止损gridId:{}, 追加{}单, 每单{}张", furthestSlId, stopLossCount, qty);
        for (int i = 0; i < stopLossCount; i++) {
            int newSlId = furthestSlId - i - 1;
            OkxGridElement elem = OkxGridElement.findById(newSlId);
            if (elem == null) continue;
            BigDecimal triggerPrice = elem.getGridPrice();
            int finalSlId = newSlId;
            executor.placeTakeProfit(triggerPrice.toString(), "sell", "long", config.getQuantity(),
                    profitId -> {
                        elem.setLongStopLossOrderId(profitId);
                        OkxGridElement.refreshIndices();
                        log.info("[OKX] 多仓止损追加, gridId:{}, 触发价:{}, stopLossId:{}", finalSlId, triggerPrice, profitId);
                    });
        }
    }
 
    /**
     * 空仓追挂止损:按 quantity 粒度拆分为 count 个止损单,从当前最远止损格再往外延伸。
     */
    public void extendShortStopLoss(int filledQty) {
        int qty = Integer.parseInt(config.getQuantity());
        int stopLossCount = filledQty / qty;
        int furthestSlId = 0;
        for (OkxGridElement e : config.getGridElements()) {
            if (e.getShortStopLossOrderId() != null && e.getId() > furthestSlId) {
                furthestSlId = e.getId();
            }
        }
        if (furthestSlId == 0) furthestSlId = 11;
        log.info("[OKX] 空仓追挂止损, 当前最远止损gridId:{}, 追加{}单, 每单{}张", furthestSlId, stopLossCount, qty);
        for (int i = 0; i < stopLossCount; i++) {
            int newSlId = furthestSlId + i + 1;
            OkxGridElement elem = OkxGridElement.findById(newSlId);
            if (elem == null) continue;
            BigDecimal triggerPrice = elem.getGridPrice();
            int finalSlId = newSlId;
            executor.placeTakeProfit(triggerPrice.toString(), "buy", "short", config.getQuantity(),
                    profitId -> {
                        elem.setShortStopLossOrderId(profitId);
                        OkxGridElement.refreshIndices();
                        log.info("[OKX] 空仓止损追加, gridId:{}, 触发价:{}, stopLossId:{}", finalSlId, triggerPrice, profitId);
                    });
        }
    }
 
    // ========== 辅助:条件单创建 & 状态回写 ==========
 
    /**
     * 预置标志位后发送条件入单请求,成功/失败均通过回调写入 GridElement 状态。
     */
    void placeEntryOrderWithPreFlag(OkxGridElement gridElement, boolean isLong,
                                     BigDecimal triggerPrice, String size) {
        if (isLong) {
            gridElement.setHasLongOrder(true);
        } else {
            gridElement.setHasShortOrder(true);
        }
        String side = isLong ? "buy" : "sell";
        String posSide = isLong ? "long" : "short";
        executor.placeConditionalEntryOrder(triggerPrice.toString(), side, posSide, size,
                orderId -> {
                    if (isLong) {
                        setLongEntryState(gridElement, orderId, true);
                    } else {
                        setShortEntryState(gridElement, orderId, true);
                    }
                },
                () -> {
                    if (isLong) {
                        gridElement.setHasLongOrder(false);
                        gridElement.setLongOrderId(null);
                    } else {
                        gridElement.setHasShortOrder(false);
                        gridElement.setShortOrderId(null);
                    }
                    OkxGridElement.refreshIndices();
                    log.warn("[OKX] 条件单创建失败,回滚标志位 gridId:{}, isLong:{}", gridElement.getId(), isLong);
                }
        );
    }
 
    // ---- GridElement 状态修改(package-private,OkxGridTradeService 也可调用) ----
 
    void setLongEntryState(OkxGridElement gridElement, String entryId, boolean flag) {
        OkxTraderParam tp = gridElement.getLongTraderParam();
        tp.setEntryOrderId(entryId);
        tp.setEntryOrderPlaced(flag);
        gridElement.setHasLongOrder(flag);
        gridElement.setLongOrderId(entryId);
        OkxGridElement.refreshIndices();
    }
 
    void setShortEntryState(OkxGridElement gridElement, String entryId, boolean flag) {
        OkxTraderParam tp = gridElement.getShortTraderParam();
        tp.setEntryOrderId(entryId);
        tp.setEntryOrderPlaced(flag);
        gridElement.setHasShortOrder(flag);
        gridElement.setShortOrderId(entryId);
        OkxGridElement.refreshIndices();
    }
 
    void clearLongEntryState(OkxGridElement gridElement) {
        setLongEntryState(gridElement, null, false);
    }
 
    void clearShortEntryState(OkxGridElement gridElement) {
        setShortEntryState(gridElement, null, false);
    }
 
    void setLongTakeProfitState(OkxGridElement gridElement, String profitId, boolean flag) {
        OkxTraderParam tp = gridElement.getLongTraderParam();
        tp.setTakeProfitOrderId(profitId);
        tp.setTakeProfitPlaced(flag);
        gridElement.setLongTakeProfitOrderId(profitId);
        OkxGridElement.refreshIndices();
    }
 
    void setShortTakeProfitState(OkxGridElement gridElement, String profitId, boolean flag) {
        OkxTraderParam tp = gridElement.getShortTraderParam();
        tp.setTakeProfitOrderId(profitId);
        tp.setTakeProfitPlaced(flag);
        gridElement.setShortTakeProfitOrderId(profitId);
        OkxGridElement.refreshIndices();
    }
}