Administrator
4 days ago 04063bcb7b9e9d8e0242c1313f54ccc1b71f0b6e
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
package com.xcong.excoin.modules.okxApi;
 
import java.math.BigDecimal;
 
/**
 * OKX 交易模块全局配置 — 盈利回收策略的参数入口。
 *
 * <p>通过 Builder 模式将所有运行参数集中管理,避免策略参数散落在多个类中。
 *
 * <h3>参数分类</h3>
 * <table>
 *   <tr><th>类别</th><th>参数</th><th>用途</th></tr>
 *   <tr><td>认证</td><td>apiKey, apiSecret, passphrase</td><td>REST/WS 签名认证</td></tr>
 *   <tr><td>交易标的</td><td>contract, leverage, baseQuantity</td><td>合约、杠杆、张数</td></tr>
 *   <tr><td>持仓</td><td>marginMode, positionMode</td><td>全仓/逐仓、双向持仓</td></tr>
 *   <tr><td>风控</td><td>maxLoss, maxPositionSize</td><td>最大亏损阈值、单边持仓上限</td></tr>
 *   <tr><td>盈亏计算</td><td>contractMultiplier, priceScale, unrealizedPnlPriceMode</td><td>合约乘数、价格精度、计价模式</td></tr>
 *   <tr><td>策略核心</td><td>profitTriggerRatio, reinvestRatio</td><td>盈利触发比例、再投资比例</td></tr>
 * </table>
 *
 * @author Administrator
 */
public class OkxConfig {
 
    /**
     * 未实现盈亏(unrealizedPnl)计价模式。
     */
    public enum PnLPriceMode {
        /** 按最新成交价计算未实现盈亏 */
        LAST_PRICE,
        /** 按标记价格计算未实现盈亏 */
        MARK_PRICE
    }
 
    // ==================== 认证信息 ====================
 
    private final String apiKey;
    private final String apiSecret;
    private final String passphrase;
 
    // ==================== 交易标的 ====================
 
    private final String contract;
    private final String leverage;
    private final String marginMode;
    private final String positionMode;
 
    // ==================== 策略参数 ====================
 
    /** 基底开仓张数(初始化时多空各开的张数,如 "10") */
    private final String baseQuantity;
    /** 最大亏损阈值(USDT),触发后策略停止 */
    private final BigDecimal maxLoss;
    /** 是否为生产环境 */
    private final boolean isProduction;
    /** 合约乘数(单张合约代表的基础资产数量,如 BTC-USDT-SWAP=0.01) */
    private final BigDecimal contractMultiplier;
    /** 价格精度(交易所价格的小数位数) */
    private final int priceScale;
    /** 未实现盈亏计价模式 */
    private final PnLPriceMode unrealizedPnlPriceMode;
 
    // ==================== 盈利回收策略参数 ====================
 
    /** 盈利触发比例(默认0.5=50%),ROI=未实现盈亏/保证金达到此值时触发平仓 */
    private final BigDecimal profitTriggerRatio;
    /** 再投资比例(默认0.5=50%),收益A中用于开反方向仓位的比例 */
    private final BigDecimal reinvestRatio;
 
    // ==================== 风控参数 ====================
 
    /** 反向仓位倍数上限(默认10),反方向最大持仓 = baseQuantity × 此值 */
    private final int maxPositionMultiplier;
    /** 权益重置比例(默认0.05=5%),账户权益 ≥ 初始权益 × (1+此值) 时全平重置 */
    private final BigDecimal equityRestartRatio;
 
    // ==================== 构造器 ====================
 
    private OkxConfig(Builder builder) {
        this.apiKey = builder.apiKey;
        this.apiSecret = builder.apiSecret;
        this.passphrase = builder.passphrase;
        this.contract = builder.contract;
        this.leverage = builder.leverage;
        this.marginMode = builder.marginMode;
        this.positionMode = builder.positionMode;
        this.baseQuantity = builder.baseQuantity;
        this.maxLoss = builder.maxLoss;
        this.isProduction = builder.isProduction;
        this.contractMultiplier = builder.contractMultiplier;
        this.priceScale = builder.priceScale;
        this.unrealizedPnlPriceMode = builder.unrealizedPnlPriceMode;
        this.profitTriggerRatio = builder.profitTriggerRatio;
        this.reinvestRatio = builder.reinvestRatio;
        this.maxPositionMultiplier = builder.maxPositionMultiplier;
        this.equityRestartRatio = builder.equityRestartRatio;
    }
 
    // ==================== REST/WS 地址 ====================
 
    /** @return REST API 基础路径 */
    public String getRestBasePath() {
        return "https://openapi.okx.com";
    }
 
    /** @return WebSocket 公开频道地址 */
    public String getWsPublicUrl() {
        return isProduction
                ? "wss://ws.okx.com:8443/ws/v5/public"
                : "wss://wspap.okx.com:8443/ws/v5/public";
    }
 
    /** @return WebSocket 私有频道地址 */
    public String getWsPrivateUrl() {
        return isProduction
                ? "wss://ws.okx.com:8443/ws/v5/private"
                : "wss://wspap.okx.com:8443/ws/v5/private";
    }
 
    // ==================== 认证信息 ====================
 
    public String getApiKey() { return apiKey; }
    public String getApiSecret() { return apiSecret; }
    public String getPassphrase() { return passphrase; }
 
    // ==================== 交易标的 ====================
 
    public String getContract() { return contract; }
    public String getLeverage() { return leverage; }
 
    // ==================== 持仓配置 ====================
 
    public String getMarginMode() { return marginMode; }
    public String getPositionMode() { return positionMode; }
 
    // ==================== 策略参数 ====================
 
    public String getBaseQuantity() { return baseQuantity; }
    public BigDecimal getMaxLoss() { return maxLoss; }
 
    // ==================== 盈亏计算 ====================
 
    public BigDecimal getContractMultiplier() { return contractMultiplier; }
    public int getPriceScale() { return priceScale; }
    public PnLPriceMode getUnrealizedPnlPriceMode() { return unrealizedPnlPriceMode; }
 
    // ==================== 盈利回收策略参数 ====================
 
    public BigDecimal getProfitTriggerRatio() { return profitTriggerRatio; }
    public BigDecimal getReinvestRatio() { return reinvestRatio; }
 
    // ==================== 风控参数 ====================
 
    /** @return 反向仓位倍数上限,反方向最大持仓 = baseQuantity × 此值 */
    public int getMaxPositionMultiplier() { return maxPositionMultiplier; }
    /** @return 权益重置比例,账户权益 ≥ 初始权益 × (1+此值) 时全平重置 */
    public BigDecimal getEquityRestartRatio() { return equityRestartRatio; }
 
    // ==================== 环境 ====================
 
    public boolean isProduction() { return isProduction; }
 
    // ==================== Builder ====================
 
    public static Builder builder() {
        return new Builder();
    }
 
    /**
     * OkxConfig 的流式构造器。
     *
     * <h3>必填项</h3>
     * {@code apiKey}、{@code apiSecret}、{@code passphrase} 必须设置。
     *
     * <h3>默认值</h3>
     * BTC-USDT-SWAP / 100x / cross / long_short_mode / baseQuantity=10 /
     * maxLoss=15 / profitTriggerRatio=0.5 / reinvestRatio=0.5 /
     * maxPositionMultiplier=10 / equityRestartRatio=0.05
     */
    public static class Builder {
        private String apiKey;
        private String apiSecret;
        private String passphrase;
        private String contract = "BTC-USDT-SWAP";
        private String leverage = "100";
        private String marginMode = "cross";
        private String positionMode = "long_short_mode";
        private String baseQuantity = "10";
        private BigDecimal maxLoss = new BigDecimal("15");
        private boolean isProduction = false;
        private BigDecimal contractMultiplier = new BigDecimal("0.01");
        private int priceScale = 2;
        private PnLPriceMode unrealizedPnlPriceMode = PnLPriceMode.LAST_PRICE;
        private BigDecimal profitTriggerRatio = new BigDecimal("0.5");
        private BigDecimal reinvestRatio = new BigDecimal("0.5");
        private int maxPositionMultiplier = 10;
        private BigDecimal equityRestartRatio = new BigDecimal("0.05");
 
        public Builder apiKey(String v) { this.apiKey = v; return this; }
        public Builder apiSecret(String v) { this.apiSecret = v; return this; }
        public Builder passphrase(String v) { this.passphrase = v; return this; }
        public Builder contract(String v) { this.contract = v; return this; }
        public Builder leverage(String v) { this.leverage = v; return this; }
        public Builder marginMode(String v) { this.marginMode = v; return this; }
        public Builder positionMode(String v) { this.positionMode = v; return this; }
        public Builder baseQuantity(String v) { this.baseQuantity = v; return this; }
        public Builder maxLoss(BigDecimal v) { this.maxLoss = v; return this; }
        public Builder isProduction(boolean v) { this.isProduction = v; return this; }
        public Builder contractMultiplier(BigDecimal v) { this.contractMultiplier = v; return this; }
        public Builder priceScale(int v) { this.priceScale = v; return this; }
        public Builder unrealizedPnlPriceMode(PnLPriceMode v) { this.unrealizedPnlPriceMode = v; return this; }
        public Builder profitTriggerRatio(BigDecimal v) { this.profitTriggerRatio = v; return this; }
        public Builder reinvestRatio(BigDecimal v) { this.reinvestRatio = v; return this; }
        public Builder maxPositionMultiplier(int v) { this.maxPositionMultiplier = v; return this; }
        public Builder equityRestartRatio(BigDecimal v) { this.equityRestartRatio = v; return this; }
 
        public OkxConfig build() {
            return new OkxConfig(this);
        }
    }
}