Administrator
17 hours ago 6a51f45e6a00b65a9e7b0b0707b453c11311f3ef
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
package com.xcong.excoin.modules.okxApi;
 
import java.math.BigDecimal;
 
/**
 * OKX 模块统一配置。
 *
 * <p>通过 Builder 模式集中管理所有运行参数,避免参数散落在多个文件中。
 * 提供 REST API 和 WebSocket 地址的自动环境切换(模拟盘/实盘)。
 *
 * <h3>使用示例</h3>
 * <pre>
 *   OkxConfig config = OkxConfig.builder()
 *       .apiKey("...")
 *       .secretKey("...")
 *       .passphrase("...")
 *       .contract("BTC-USDT-SWAP")
 *       .leverage("100")
 *       .gridRate(new BigDecimal("0.0035"))
 *       .contractMultiplier(new BigDecimal("1"))
 *       .isProduction(false)
 *       .build();
 *
 *   String wsKlineUrl = config.getWsKlineUrl();
 *   String wsPrivateUrl = config.getWsPrivateUrl();
 * </pre>
 *
 * <h3>默认值</h3>
 * <ul>
 *   <li>合约: BTC-USDT-SWAP, 杠杆: 100x, 全仓</li>
 *   <li>网格间距: 0.35%, 队列容量: 50, 保证金比例上限: 20%</li>
 *   <li>止盈: 5 USDT, 亏损上限: 15 USDT</li>
 *   <li>数量: 1 张, 合约乘数: 1, 环境: 模拟盘</li>
 * </ul>
 *
 * @author Administrator
 */
public class OkxConfig {
 
    public enum PnLPriceMode {
        LAST_PRICE,
        MARK_PRICE
    }
 
    private final String apiKey;
    private final String secretKey;
    private final String passphrase;
    private final String contract;
    private final String leverage;
    private final String marginMode;
    private final String posMode;
    private final BigDecimal gridRate;
    private final BigDecimal overallTp;
    private final BigDecimal maxLoss;
    private final String quantity;
    private final boolean isProduction;
    private final int gridQueueSize;
    private final BigDecimal marginRatioLimit;
    private final BigDecimal contractMultiplier;
    private final PnLPriceMode unrealizedPnlPriceMode;
 
    private OkxConfig(Builder builder) {
        this.apiKey = builder.apiKey;
        this.secretKey = builder.secretKey;
        this.passphrase = builder.passphrase;
        this.contract = builder.contract;
        this.leverage = builder.leverage;
        this.marginMode = builder.marginMode;
        this.posMode = builder.posMode;
        this.gridRate = builder.gridRate;
        this.overallTp = builder.overallTp;
        this.maxLoss = builder.maxLoss;
        this.quantity = builder.quantity;
        this.isProduction = builder.isProduction;
        this.gridQueueSize = builder.gridQueueSize;
        this.marginRatioLimit = builder.marginRatioLimit;
        this.contractMultiplier = builder.contractMultiplier;
        this.unrealizedPnlPriceMode = builder.unrealizedPnlPriceMode;
    }
 
    // ==================== WS 地址 ====================
 
    public String getWsKlineUrl() {
        return isProduction
                ? "wss://ws.okx.com:8443/ws/v5/business"
                : "wss://wspap.okx.com:8443/ws/v5/business";
    }
 
    public String getWsPrivateUrl() {
        return isProduction
                ? "wss://ws.okx.com:8443/ws/v5/private"
                : "wss://wspap.okx.com:8443/ws/v5/private";
    }
 
    // ==================== REST 地址 ====================
 
    public String getRestBaseUrl() {
        return isProduction
                ? "https://www.okx.com"
                : "https://www.okx.cab";
    }
 
    // ==================== 认证信息 ====================
 
    public String getApiKey() { return apiKey; }
    public String getSecretKey() { return secretKey; }
    public String getPassphrase() { return passphrase; }
 
    // ==================== 交易标的 ====================
 
    public String getContract() { return contract; }
    public String getLeverage() { return leverage; }
 
    // ==================== 持仓配置 ====================
 
    public String getMarginMode() { return marginMode; }
    public String getPosMode() { return posMode; }
 
    // ==================== 策略参数 ====================
 
    public BigDecimal getGridRate() { return gridRate; }
    public BigDecimal getOverallTp() { return overallTp; }
    public BigDecimal getMaxLoss() { return maxLoss; }
    public String getQuantity() { return quantity; }
    public int getGridQueueSize() { return gridQueueSize; }
 
    // ==================== 风险控制 ====================
 
    public BigDecimal getMarginRatioLimit() { return marginRatioLimit; }
 
    // ==================== 盈亏计算 ====================
 
    public BigDecimal getContractMultiplier() { return contractMultiplier; }
    public PnLPriceMode getUnrealizedPnlPriceMode() { return unrealizedPnlPriceMode; }
 
    // ==================== 环境 ====================
 
    public boolean isProduction() { return isProduction; }
 
    public static Builder builder() {
        return new Builder();
    }
 
    public static class Builder {
        private String apiKey;
        private String secretKey;
        private String passphrase;
        private String contract = "BTC-USDT-SWAP";
        private String leverage = "100";
        private String marginMode = "cross";
        private String posMode = "long_short_mode";
        private BigDecimal gridRate = new BigDecimal("0.0035");
        private BigDecimal overallTp = new BigDecimal("5");
        private BigDecimal maxLoss = new BigDecimal("15");
        private String quantity = "1";
        private boolean isProduction = false;
        private int gridQueueSize = 50;
        private BigDecimal marginRatioLimit = new BigDecimal("0.2");
        private BigDecimal contractMultiplier = new BigDecimal("1");
        private PnLPriceMode unrealizedPnlPriceMode = PnLPriceMode.LAST_PRICE;
 
        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 contract(String contract) { this.contract = contract; return this; }
        public Builder leverage(String leverage) { this.leverage = leverage; return this; }
        public Builder marginMode(String marginMode) { this.marginMode = marginMode; return this; }
        public Builder posMode(String posMode) { this.posMode = posMode; return this; }
        public Builder gridRate(BigDecimal gridRate) { this.gridRate = gridRate; return this; }
        public Builder overallTp(BigDecimal overallTp) { this.overallTp = overallTp; return this; }
        public Builder maxLoss(BigDecimal maxLoss) { this.maxLoss = maxLoss; return this; }
        public Builder quantity(String quantity) { this.quantity = quantity; return this; }
        public Builder isProduction(boolean isProduction) { this.isProduction = isProduction; return this; }
        public Builder contractMultiplier(BigDecimal contractMultiplier) { this.contractMultiplier = contractMultiplier; return this; }
        public Builder unrealizedPnlPriceMode(PnLPriceMode mode) { this.unrealizedPnlPriceMode = mode; return this; }
 
        public OkxConfig build() {
            return new OkxConfig(this);
        }
    }
}