Administrator
4 days ago bfe3af2d95418b326d707834be6c6ba91f86ecb5
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
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"))
 *       .isProduction(false)
 *       .build();
 *
 *   String restUrl = config.getRestBasePath();  // 自动返回测试网或生产网地址
 *   String wsUrl   = config.getWsUrl();
 * </pre>
 *
 * <h3>默认值</h3>
 * <ul>
 *   <li>合约: BTC_USDT, 杠杆: 10x, 全仓, 双向持仓</li>
 *   <li>网格: 0.35%, 止盈: 0.5 USDT, 亏损: 7.5 USDT</li>
 *   <li>数量: 1 张, 环境: 测试网, 重试: 3 次</li>
 * </ul>
 *
 * @author Administrator
 */
public class GateConfig {
 
    /** 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 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;
    }
 
    /**
     * 根据环境返回 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";
    }
 
    public String getApiKey() { return apiKey; }
    public String getApiSecret() { return apiSecret; }
    public String getContract() { return contract; }
    public String getLeverage() { return leverage; }
    public String getMarginMode() { return marginMode; }
    public String getPositionMode() { return positionMode; }
    public BigDecimal getGridRate() { return gridRate; }
    public BigDecimal getOverallTp() { return overallTp; }
    public BigDecimal getMaxLoss() { return maxLoss; }
    public String getQuantity() { return quantity; }
    public boolean isProduction() { return isProduction; }
    public int getReopenMaxRetries() { return reopenMaxRetries; }
 
    public static Builder builder() {
        return new Builder();
    }
 
    /**
     * GateConfig 的流式构造器,提供合理的默认值。
     */
    public static class Builder {
        private String apiKey;
        private String apiSecret;
        private String contract = "BTC_USDT";
        private String leverage = "10";
        private String marginMode = "cross";
        private String positionMode = "dual";
        private BigDecimal gridRate = new BigDecimal("0.0035");
        private BigDecimal overallTp = new BigDecimal("0.5");
        private BigDecimal maxLoss = new BigDecimal("7.5");
        private String quantity = "1";
        private boolean isProduction = false;
        private int reopenMaxRetries = 3;
 
        public Builder apiKey(String apiKey) { this.apiKey = apiKey; return this; }
        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; }
        public Builder marginMode(String marginMode) { this.marginMode = marginMode; return this; }
        public Builder positionMode(String positionMode) { this.positionMode = positionMode; 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 reopenMaxRetries(int reopenMaxRetries) { this.reopenMaxRetries = reopenMaxRetries; return this; }
 
        public GateConfig build() {
            return new GateConfig(this);
        }
    }
}