Administrator
2026-06-02 d663147264d0fc97e9e7ddbd4aeee69e833ad025
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
package com.xcong.excoin.modules.okxNewPrice;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
 
/**
 * OKX 网格交易模块全局配置,策略的唯一参数入口。
 *
 * <h3>定位</h3>
 * 通过 Builder 模式将所有运行参数集中管理,避免策略参数散落在多个类中。
 * 运行时动态参数(step、gridElements、baseLongTraderParam、baseShortTraderParam)
 * 由 {@link OkxGridTradeService} 在策略执行过程中写入。
 *
 * <h3>参数分类</h3>
 * <table>
 *   <tr><th>类别</th><th>参数</th><th>用途</th></tr>
 *   <tr><td>认证</td><td>apiKey, secretKey, passphrase</td><td>REST/WS 签名认证</td></tr>
 *   <tr><td>交易标的</td><td>instId, leverage, quantity, ctVal</td><td>合约、杠杆、张数、面值</td></tr>
 *   <tr><td>持仓</td><td>tdMode, posSide</td><td>全仓/逐仓、多/空</td></tr>
 *   <tr><td>网格策略</td><td>gridRate, gridQueueSize, priceScale</td><td>间距比例、队列容量、价格精度</td></tr>
 *   <tr><td>止盈止损</td><td>expectedProfit, maxLoss</td><td>整体止盈/亏损阈值(USDT),触发后策略重置</td></tr>
 *   <tr><td>风控</td><td>marginRatioLimit</td><td>保证金占比上限</td></tr>
 *   <tr><td>运行时</td><td>step, gridElements, baseLongTraderParam, baseShortTraderParam</td><td>由策略动态填充</td></tr>
 * </table>
 *
 * @author Administrator
 */
public class OkxConfig {
 
    /** OKX API 密钥 */
    private final String apiKey;
    /** OKX API 签名密钥 */
    private final String secretKey;
    /** OKX API 密码短语 */
    private final String passphrase;
    /** 合约名称(如 ETH-USDT-SWAP) */
    private final String instId;
    /** 杠杆倍数 */
    private final String leverage;
    /** 保证金模式(cross / isolated) */
    private final String tdMode;
    /** 持仓方向(long / short),仅用于网格策略中的方向判定 */
    private final String posSide;
    /** 网格间距比例(如 0.0025 表示 0.25%) */
    private final BigDecimal gridRate;
    /** 预期收益(USDT),达到后自动重置策略 */
    private final BigDecimal expectedProfit;
    /** 最大亏损阈值(USDT) */
    private final BigDecimal maxLoss;
    /** 每次下单张数 */
    private final String quantity;
    /** 基底开仓张数(初始化时多空各开的张数) */
    private final String baseQuantity;
    /** 是否为模拟盘 */
    private final boolean isSimulate;
    /** 网格队列容量 */
    private final int gridQueueSize;
    /** 保证金占初始本金比例上限 */
    private final BigDecimal marginRatioLimit;
    /** 合约面值(单张合约代表的基础资产数量,如 ETH=0.1, BTC=0.01) */
    private final BigDecimal ctVal;
    /** 价格精度(交易所价格的最小小数位数,如 1=0.1精度,2=0.01精度) */
    private final int priceScale;
 
    /** 网格绝对步长(shortBaseEntryPrice × gridRate),运行时由队列生成逻辑设置 */
    private BigDecimal step;
    /** 网格元素列表,由队列初始化时同步填充 */
    private volatile List<OkxGridElement> gridElements = new ArrayList<>();
    /** 基座多头挂单参数,在基座成交后填充 */
    private OkxTraderParam baseLongTraderParam;
    /** 基座空头挂单参数,在基座成交后填充 */
    private OkxTraderParam baseShortTraderParam;
 
    private OkxConfig(Builder builder) {
        this.apiKey = builder.apiKey;
        this.secretKey = builder.secretKey;
        this.passphrase = builder.passphrase;
        this.instId = builder.instId;
        this.leverage = builder.leverage;
        this.tdMode = builder.tdMode;
        this.posSide = builder.posSide;
        this.gridRate = builder.gridRate;
        this.expectedProfit = builder.expectedProfit;
        this.maxLoss = builder.maxLoss;
        this.quantity = builder.quantity;
        this.baseQuantity = builder.baseQuantity;
        this.isSimulate = builder.isSimulate;
        this.gridQueueSize = builder.gridQueueSize;
        this.marginRatioLimit = builder.marginRatioLimit;
        this.ctVal = builder.ctVal;
        this.priceScale = builder.priceScale;
    }
 
    // ==================== 认证信息 ====================
    public String getApiKey() { return apiKey; }
    public String getSecretKey() { return secretKey; }
    public String getPassphrase() { return passphrase; }
 
    // ==================== 交易标的 ====================
    public String getInstId() { return instId; }
    public String getLeverage() { return leverage; }
    public String getTdMode() { return tdMode; }
    public String getPosSide() { return posSide; }
 
    // ==================== 策略参数 ====================
    public BigDecimal getGridRate() { return gridRate; }
    public BigDecimal getExpectedProfit() { return expectedProfit; }
    public BigDecimal getMaxLoss() { return maxLoss; }
    public String getQuantity() { return quantity; }
    public String getBaseQuantity() { return baseQuantity; }
    public int getGridQueueSize() { return gridQueueSize; }
 
    // ==================== 风控 ====================
    public BigDecimal getMarginRatioLimit() { return marginRatioLimit; }
    public BigDecimal getCtVal() { return ctVal; }
    public int getPriceScale() { return priceScale; }
 
    // ==================== 环境 ====================
    public boolean isSimulate() { return isSimulate; }
 
    // ==================== 运行时参数 ====================
    public BigDecimal getStep() { return step; }
    public void setStep(BigDecimal step) { this.step = step; }
 
    public List<OkxGridElement> getGridElements() { return gridElements; }
    public void setGridElements(List<OkxGridElement> gridElements) {
        this.gridElements = gridElements;
        OkxGridElement.rebuildIndex(gridElements);
    }
 
    public OkxTraderParam getBaseLongTraderParam() { return baseLongTraderParam; }
    public void setBaseLongTraderParam(OkxTraderParam baseLongTraderParam) { this.baseLongTraderParam = baseLongTraderParam; }
    public OkxTraderParam getBaseShortTraderParam() { return baseShortTraderParam; }
    public void setBaseShortTraderParam(OkxTraderParam baseShortTraderParam) { this.baseShortTraderParam = baseShortTraderParam; }
 
    public static Builder builder() {
        return new Builder();
    }
 
    public static class Builder {
        private String apiKey;
        private String secretKey;
        private String passphrase;
        private String instId = "ETH-USDT-SWAP";
        private String leverage = "100";
        private String tdMode = "cross";
        private String posSide = "long";
        private BigDecimal gridRate = new BigDecimal("0.0025");
        private BigDecimal expectedProfit = new BigDecimal("2");
        private BigDecimal maxLoss = new BigDecimal("15");
        private String quantity = "1";
        private String baseQuantity = "10";
        private boolean isSimulate = false;
        private int gridQueueSize = 300;
        private BigDecimal marginRatioLimit = new BigDecimal("0.2");
        private BigDecimal ctVal = new BigDecimal("0.01");
        private int priceScale = 2;
 
        public Builder apiKey(String apiKey) { this.apiKey = apiKey; return this; }
        public Builder secretKey(String secretKey) { this.secretKey = secretKey; return this; }
        public Builder passphrase(String passphrase) { this.passphrase = passphrase; return this; }
        public Builder instId(String instId) { this.instId = instId; return this; }
        public Builder leverage(String leverage) { this.leverage = leverage; return this; }
        public Builder tdMode(String tdMode) { this.tdMode = tdMode; return this; }
        public Builder posSide(String posSide) { this.posSide = posSide; return this; }
        public Builder gridRate(BigDecimal gridRate) { this.gridRate = gridRate; return this; }
        public Builder expectedProfit(BigDecimal expectedProfit) { this.expectedProfit = expectedProfit; return this; }
        public Builder maxLoss(BigDecimal maxLoss) { this.maxLoss = maxLoss; return this; }
        public Builder quantity(String quantity) { this.quantity = quantity; return this; }
        public Builder baseQuantity(String baseQuantity) { this.baseQuantity = baseQuantity; return this; }
        public Builder isSimulate(boolean isSimulate) { this.isSimulate = isSimulate; return this; }
        public Builder gridQueueSize(int gridQueueSize) { this.gridQueueSize = gridQueueSize; return this; }
        public Builder marginRatioLimit(BigDecimal marginRatioLimit) { this.marginRatioLimit = marginRatioLimit; return this; }
        public Builder ctVal(BigDecimal ctVal) { this.ctVal = ctVal; return this; }
        public Builder priceScale(int priceScale) { this.priceScale = priceScale; return this; }
 
        public OkxConfig build() {
            return new OkxConfig(this);
        }
    }
}