Administrator
11 hours ago 2ba5828bc900a5a3646d9dfe78f53ff68e444fd4
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
package com.xcong.excoin.modules.gateApi;
 
import java.math.BigDecimal;
 
/**
 * Gate 模块统一配置。
 *
 * <p>通过 Builder 模式集中管理所有运行参数,避免参数散落在多个文件中。
 * 提供 REST API 和 WebSocket 地址的自动环境切换(测试网/生产网)。
 *
 * <h3>使用示例</h3>
 * <pre>
 *   GateConfig config = GateConfig.builder()
 *       .apiKey("...")
 *       .apiSecret("...")
 *       .contract("XAU_USDT")
 *       .leverage("100")
 *       .gridRate(new BigDecimal("0.0035"))
 *       .contractMultiplier("0.001")
 *       .isProduction(false)
 *       .build();
 *
 *   String restUrl = config.getRestBasePath();  // 自动返回测试网或生产网地址
 *   String wsUrl   = config.getWsUrl();
 * </pre>
 *
 * <h3>默认值</h3>
 * <ul>
 *   <li>合约: BTC_USDT, 杠杆: 10x, 全仓, 双向持仓</li>
 *   <li>网格间距: 0.35%, 队列容量: 50, 保证金比例上限: 20%</li>
 *   <li>止盈: 0.5 USDT, 亏损上限: 7.5 USDT</li>
 *   <li>数量: 1 张, 合约乘数: 0.001, 环境: 测试网</li>
 * </ul>
 *
 * @author Administrator
 */
public class GateConfig {
 
    /** 未实现盈亏计价模式 */
    public enum PnLPriceMode {
        /** 按最新成交价计算 */
        LAST_PRICE,
        /** 按标记价格计算 */
        MARK_PRICE
    }
 
    /** Gate API v4 密钥 */
    private final String apiKey;
    /** Gate API v4 签名密钥 */
    private final String apiSecret;
    /** 合约名称(如 XAU_USDT) */
    private final String contract;
    /** 杠杆倍数 */
    private final String leverage;
    /** 保证金模式(cross / isolated) */
    private final String marginMode;
    /** 持仓模式(single / dual / dual_plus) */
    private final String positionMode;
    /** 网格间距比例(如 0.0035 表示 0.35%) */
    private final BigDecimal gridRate;
    /** 整体止盈阈值(USDT) */
    private final BigDecimal overallTp;
    /** 最大亏损阈值(USDT) */
    private final BigDecimal maxLoss;
    /** 下单数量(合约张数) */
    private final String quantity;
    /** 是否为生产环境 */
    private final boolean isProduction;
    /** 补仓最大重试次数 */
    private final int reopenMaxRetries;
    /** 网格队列容量 */
    private final int gridQueueSize;
    /** 保证金占初始本金比例上限 */
    private final BigDecimal marginRatioLimit;
    /** 合约乘数(单张合约代表的基础资产数量,如 BTC_USDT=0.001, ETH_USDT=0.01) */
    private final BigDecimal contractMultiplier;
    /** 未实现盈亏计价模式:最新价 / 标记价格 */
    private final PnLPriceMode unrealizedPnlPriceMode;
    /** 网格绝对步长(shortBaseEntryPrice × gridRate),运行时由队列生成逻辑设置 */
    private BigDecimal step;
 
    private GateConfig(Builder builder) {
        this.apiKey = builder.apiKey;
        this.apiSecret = builder.apiSecret;
        this.contract = builder.contract;
        this.leverage = builder.leverage;
        this.marginMode = builder.marginMode;
        this.positionMode = builder.positionMode;
        this.gridRate = builder.gridRate;
        this.overallTp = builder.overallTp;
        this.maxLoss = builder.maxLoss;
        this.quantity = builder.quantity;
        this.isProduction = builder.isProduction;
        this.reopenMaxRetries = builder.reopenMaxRetries;
        this.gridQueueSize = builder.gridQueueSize;
        this.marginRatioLimit = builder.marginRatioLimit;
        this.contractMultiplier = builder.contractMultiplier;
        this.unrealizedPnlPriceMode = builder.unrealizedPnlPriceMode;
    }
 
    // ==================== REST/WS 地址 ====================
 
    /**
     * 根据环境返回 REST API 基础路径。
     * <ul>
     *   <li>测试网: {@code https://api-testnet.gateapi.io/api/v4}</li>
     *   <li>生产网: {@code https://api.gateio.ws/api/v4}</li>
     * </ul>
     */
    public String getRestBasePath() {
        return isProduction
                ? "https://api.gateio.ws/api/v4"
                : "https://api-testnet.gateapi.io/api/v4";
    }
 
    /**
     * 根据环境返回 WebSocket 地址。
     * <ul>
     *   <li>测试网: {@code wss://ws-testnet.gate.com/v4/ws/futures/usdt}</li>
     *   <li>生产网: {@code wss://fx-ws.gateio.ws/v4/ws/usdt}</li>
     * </ul>
     */
    public String getWsUrl() {
        return isProduction
                ? "wss://fx-ws.gateio.ws/v4/ws/usdt"
                : "wss://ws-testnet.gate.com/v4/ws/futures/usdt";
    }
 
    // ==================== 认证信息 ====================
 
    /** @return Gate API v4 密钥 */
    public String getApiKey() { return apiKey; }
    /** @return Gate API v4 签名密钥,用于 HMAC-SHA512 签名 */
    public String getApiSecret() { return apiSecret; }
 
    // ==================== 交易标的 ====================
 
    /** @return 合约名称(如 ETH_USDT、XAU_USDT) */
    public String getContract() { return contract; }
    /** @return 杠杆倍数(如 "100" 表示 100x) */
    public String getLeverage() { return leverage; }
 
    // ==================== 持仓配置 ====================
 
    /** @return 保证金模式(cross=全仓 / isolated=逐仓) */
    public String getMarginMode() { return marginMode; }
    /** @return 持仓模式(single=单向 / dual=双向 / dual_plus) */
    public String getPositionMode() { return positionMode; }
 
    // ==================== 策略参数 ====================
 
    /** @return 网格间距比例(如 0.0015 表示 0.15%),用于生成价格队列和计算止盈价 */
    public BigDecimal getGridRate() { return gridRate; }
    /** @return 整体止盈阈值(USDT),累计已实现盈亏 ≥ 此值时策略停止 */
    public BigDecimal getOverallTp() { return overallTp; }
    /** @return 最大亏损阈值(USDT),累计已实现盈亏 ≤ -此值时策略停止 */
    public BigDecimal getMaxLoss() { return maxLoss; }
    /** @return 每次下单的张数(如 "1" 表示 1 张合约) */
    public String getQuantity() { return quantity; }
    /** @return 网格价格队列的容量上限(超出时截断尾部) */
    public int getGridQueueSize() { return gridQueueSize; }
 
    // ==================== 风险控制 ====================
 
    /** @return 保证金占初始本金比例上限(默认 0.2 即 20%),超限跳过开仓 */
    public BigDecimal getMarginRatioLimit() { return marginRatioLimit; }
    /** @return 补仓最大重试次数(当前版本未使用) */
    public int getReopenMaxRetries() { return reopenMaxRetries; }
 
    // ==================== 盈亏计算 ====================
 
    /** @return 合约乘数(单张合约代表的基础资产数量,如 ETH_USDT=0.01) */
    public BigDecimal getContractMultiplier() { return contractMultiplier; }
    /** @return 未实现盈亏计价模式:LAST_PRICE(最新成交价)/ MARK_PRICE(标记价格) */
    public PnLPriceMode getUnrealizedPnlPriceMode() { return unrealizedPnlPriceMode; }
 
    // ==================== 运行时参数 ====================
 
    /** @return 网格绝对步长(shortBaseEntryPrice × gridRate),运行时设置 */
    public BigDecimal getStep() { return step; }
    /** 设置网格绝对步长(由 generateShortQueue 在运行时计算并注入) */
    public void setStep(BigDecimal step) { this.step = step; }
 
    // ==================== 环境 ====================
 
    /** @return 是否为生产环境(true=实盘生产网 / false=模拟盘测试网) */
    public boolean isProduction() { return isProduction; }
 
    public static Builder builder() {
        return new Builder();
    }
 
    /**
     * GateConfig 的流式构造器,提供合理的默认值。
     *
     * <h3>必填项</h3>
     * {@code apiKey} 和 {@code apiSecret} 必须设置,其余参数均有默认值。
     *
     * <h3>默认值</h3>
     * BTC_USDT / 10x / cross(全仓) / dual(双向) / gridRate=0.35% /
     * overallTp=0.5 / maxLoss=7.5 / quantity=1 / isProduction=false
     */
    public static class Builder {
        /** Gate API v4 密钥(必填) */
        private String apiKey;
        /** Gate API v4 签名密钥(必填) */
        private String apiSecret;
        /** 合约名称,默认 BTC_USDT */
        private String contract = "BTC_USDT";
        /** 杠杆倍数,默认 "10" */
        private String leverage = "10";
        /** 保证金模式,默认 "cross"(全仓) */
        private String marginMode = "cross";
        /** 持仓模式,默认 "dual"(双向) */
        private String positionMode = "dual";
        /** 网格间距比例,默认 0.0035(0.35%) */
        private BigDecimal gridRate = new BigDecimal("0.0035");
        /** 整体止盈阈值(USDT),默认 0.5 */
        private BigDecimal overallTp = new BigDecimal("0.5");
        /** 最大亏损阈值(USDT),默认 7.5 */
        private BigDecimal maxLoss = new BigDecimal("7.5");
        /** 每次下单张数,默认 "1" */
        private String quantity = "1";
        /** 是否为生产环境,默认 false(测试网) */
        private boolean isProduction = false;
        /** 补仓最大重试次数,默认 3 */
        private int reopenMaxRetries = 3;
        /** 网格队列容量,默认 50 */
        private int gridQueueSize = 50;
        /** 保证金占初始本金比例上限,默认 0.2(20%) */
        private BigDecimal marginRatioLimit = new BigDecimal("0.2");
        /** 合约乘数,默认 0.001 */
        private BigDecimal contractMultiplier = new BigDecimal("0.001");
        /** 未实现盈亏计价模式,默认 LAST_PRICE(最新成交价) */
        private PnLPriceMode unrealizedPnlPriceMode = PnLPriceMode.LAST_PRICE;
 
        /** 设置 API Key */
        public Builder apiKey(String apiKey) { this.apiKey = apiKey; return this; }
        /** 设置 API Secret */
        public Builder apiSecret(String apiSecret) { this.apiSecret = apiSecret; return this; }
        /** 设置合约名称 */
        public Builder contract(String contract) { this.contract = contract; return this; }
        /** 设置杠杆倍数 */
        public Builder leverage(String leverage) { this.leverage = leverage; return this; }
        /** 设置保证金模式(cross=全仓 / isolated=逐仓) */
        public Builder marginMode(String marginMode) { this.marginMode = marginMode; return this; }
        /** 设置持仓模式(single=单向 / dual=双向) */
        public Builder positionMode(String positionMode) { this.positionMode = positionMode; return this; }
        /** 设置网格间距比例 */
        public Builder gridRate(BigDecimal gridRate) { this.gridRate = gridRate; return this; }
        /** 设置整体止盈阈值(USDT) */
        public Builder overallTp(BigDecimal overallTp) { this.overallTp = overallTp; return this; }
        /** 设置最大亏损阈值(USDT) */
        public Builder maxLoss(BigDecimal maxLoss) { this.maxLoss = maxLoss; return this; }
        /** 设置每次下单张数 */
        public Builder quantity(String quantity) { this.quantity = quantity; return this; }
        /** 设置环境(true=实盘生产网 / false=模拟盘测试网) */
        public Builder isProduction(boolean isProduction) { this.isProduction = isProduction; return this; }
        /** 设置补仓最大重试次数 */
        public Builder reopenMaxRetries(int reopenMaxRetries) { this.reopenMaxRetries = reopenMaxRetries; return this; }
        /** 设置合约乘数(单张合约代表的基础资产数量) */
        public Builder contractMultiplier(BigDecimal contractMultiplier) { this.contractMultiplier = contractMultiplier; return this; }
        /** 设置未实现盈亏计价模式 */
        public Builder unrealizedPnlPriceMode(PnLPriceMode mode) { this.unrealizedPnlPriceMode = mode; return this; }
 
        public GateConfig build() {
            return new GateConfig(this);
        }
    }
}