Administrator
3 days ago 5c3737c9718ad01a4d2a4a02c09548fcc3966438
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
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;
 
    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 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 int getGridQueueSize() { return gridQueueSize; }
    public BigDecimal getMarginRatioLimit() { return marginRatioLimit; }
    public BigDecimal getContractMultiplier() { return contractMultiplier; }
    public PnLPriceMode getUnrealizedPnlPriceMode() { return unrealizedPnlPriceMode; }
 
    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;
        private int gridQueueSize = 50;
        private BigDecimal marginRatioLimit = new BigDecimal("0.2");
        private BigDecimal contractMultiplier = new BigDecimal("0.001");
        private PnLPriceMode unrealizedPnlPriceMode = PnLPriceMode.LAST_PRICE;
 
        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 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);
        }
    }
}