From 39b09846041021c5bd0fea92d981ccb3490638d1 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Fri, 08 May 2026 23:56:43 +0800
Subject: [PATCH] fix(gateApi): 修复双向持仓模式下的杠杆设置功能
---
src/main/java/com/xcong/excoin/modules/gateApi/GateConfig.java | 186 ++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 186 insertions(+), 0 deletions(-)
diff --git a/src/main/java/com/xcong/excoin/modules/gateApi/GateConfig.java b/src/main/java/com/xcong/excoin/modules/gateApi/GateConfig.java
new file mode 100644
index 0000000..ca6446c
--- /dev/null
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateConfig.java
@@ -0,0 +1,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);
+ }
+ }
+}
--
Gitblit v1.9.1