From 8beede41ea527872eee2572f2271110ac7a14d9a Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Tue, 19 May 2026 10:11:37 +0800
Subject: [PATCH] 第二个版本

---
 src/main/java/com/xcong/excoin/modules/gateApi/GateConfig.java |   80 +++++++++++++++++++++++++++++++++++----
 1 files changed, 71 insertions(+), 9 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
index b65d0aa..3d6edfe 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateConfig.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateConfig.java
@@ -1,6 +1,8 @@
 package com.xcong.excoin.modules.gateApi;
 
 import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * Gate 模块统一配置。
@@ -8,20 +10,27 @@
  * <p>通过 Builder 模式集中管理所有运行参数,避免参数散落在多个文件中。
  * 提供 REST API 和 WebSocket 地址的自动环境切换(测试网/生产网)。
  *
+ * <h3>关键参数</h3>
+ * <ul>
+ *   <li><b>网格参数</b>:gridRate(比例间距)、step(绝对步长 = shortBaseEntryPrice × gridRate,运行时计算)、
+ *       gridQueueSize(队列容量)</li>
+ *   <li><b>止盈止损</b>:overallTp(整体止盈 USDT)、maxLoss(最大亏损 USDT)</li>
+ *   <li><b>风险控制</b>:marginRatioLimit(保证金占比上限,超限跳过开仓)、leverage(杠杆)、
+ *       marginMode(全仓/逐仓)、positionMode(单向/双向)</li>
+ *   <li><b>盈亏计算</b>:contractMultiplier(合约乘数)、unrealizedPnlPriceMode(计价模式:最新价/标记价)</li>
+ * </ul>
+ *
  * <h3>使用示例</h3>
  * <pre>
  *   GateConfig config = GateConfig.builder()
  *       .apiKey("...")
  *       .apiSecret("...")
- *       .contract("XAU_USDT")
+ *       .contract("ETH_USDT")
  *       .leverage("100")
  *       .gridRate(new BigDecimal("0.0035"))
- *       .contractMultiplier("0.001")
+ *       .contractMultiplier("0.01")
  *       .isProduction(false)
  *       .build();
- *
- *   String restUrl = config.getRestBasePath();  // 自动返回测试网或生产网地址
- *   String wsUrl   = config.getWsUrl();
  * </pre>
  *
  * <h3>默认值</h3>
@@ -36,11 +45,18 @@
  */
 public class GateConfig {
 
-    /** 未实现盈亏计价模式 */
+    /**
+     * 未实现盈亏(unrealizedPnl)计价模式。
+     *
+     * <ul>
+     *   <li>{@link #LAST_PRICE} — 按最新成交价计算,变动快、更贴近实际平仓价</li>
+     *   <li>{@link #MARK_PRICE} — 按标记价格计算,变动平稳、过滤市场噪音</li>
+     * </ul>
+     */
     public enum PnLPriceMode {
-        /** 按最新成交价计算 */
+        /** 按最新成交价计算未实现盈亏 */
         LAST_PRICE,
-        /** 按标记价格计算 */
+        /** 按标记价格计算未实现盈亏,需通过 {@link GateGridTradeService#setMarkPrice(BigDecimal)} 注入 */
         MARK_PRICE
     }
 
@@ -74,8 +90,18 @@
     private final BigDecimal marginRatioLimit;
     /** 合约乘数(单张合约代表的基础资产数量,如 BTC_USDT=0.001, ETH_USDT=0.01) */
     private final BigDecimal contractMultiplier;
+    /** 价格精度(交易所价格的最小小数位数,如 XAU_USDT=1 表示 0.1 精度,ETH_USDT=2 表示 0.01 精度) */
+    private final int priceScale;
     /** 未实现盈亏计价模式:最新价 / 标记价格 */
     private final PnLPriceMode unrealizedPnlPriceMode;
+    /** 网格绝对步长(shortBaseEntryPrice × gridRate),运行时由队列生成逻辑设置 */
+    private BigDecimal step;
+    /** 网格元素列表,由队列初始化时同步填充,包含完整的多空仓挂单状态 */
+    private volatile List<GridElement> gridElements = new ArrayList<>();
+    /** 基座多头挂单参数,在基座成交后由 tryGenerateQueues 填充 */
+    private TraderParam baseLongTraderParam;
+    /** 基座空头挂单参数,在基座成交后由 tryGenerateQueues 填充 */
+    private TraderParam baseShortTraderParam;
 
     private GateConfig(Builder builder) {
         this.apiKey = builder.apiKey;
@@ -93,6 +119,7 @@
         this.gridQueueSize = builder.gridQueueSize;
         this.marginRatioLimit = builder.marginRatioLimit;
         this.contractMultiplier = builder.contractMultiplier;
+        this.priceScale = builder.priceScale;
         this.unrealizedPnlPriceMode = builder.unrealizedPnlPriceMode;
     }
 
@@ -169,8 +196,39 @@
 
     /** @return 合约乘数(单张合约代表的基础资产数量,如 ETH_USDT=0.01) */
     public BigDecimal getContractMultiplier() { return contractMultiplier; }
+    /** @return 价格精度(交易所价格的小数位数,如 1=0.1精度,2=0.01精度),用于价格四舍五入 */
+    public int getPriceScale() { return priceScale; }
     /** @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 网格元素列表(队列初始化后填充),线程不安全,仅由策略线程单线程写入 */
+    public List<GridElement> getGridElements() { return gridElements; }
+    /** 设置网格元素列表(由队列生成逻辑写入),同时重建全局 ID 索引 */
+    public void setGridElements(List<GridElement> gridElements) {
+        this.gridElements = gridElements;
+        GridElement.rebuildIndex(gridElements);
+    }
+
+    // ==================== 基座挂单参数 ====================
+
+    /** @return 基座多头挂单参数 */
+    public TraderParam getBaseLongTraderParam() { return baseLongTraderParam; }
+    /** 设置基座多头挂单参数(由 tryGenerateQueues 在基座成交后填充) */
+    public void setBaseLongTraderParam(TraderParam baseLongTraderParam) { this.baseLongTraderParam = baseLongTraderParam; }
+
+    /** @return 基座空头挂单参数 */
+    public TraderParam getBaseShortTraderParam() { return baseShortTraderParam; }
+    /** 设置基座空头挂单参数(由 tryGenerateQueues 在基座成交后填充) */
+    public void setBaseShortTraderParam(TraderParam baseShortTraderParam) { this.baseShortTraderParam = baseShortTraderParam; }
 
     // ==================== 环境 ====================
 
@@ -217,11 +275,13 @@
         /** 补仓最大重试次数,默认 3 */
         private int reopenMaxRetries = 3;
         /** 网格队列容量,默认 50 */
-        private int gridQueueSize = 50;
+        private int gridQueueSize = 300;
         /** 保证金占初始本金比例上限,默认 0.2(20%) */
         private BigDecimal marginRatioLimit = new BigDecimal("0.2");
         /** 合约乘数,默认 0.001 */
         private BigDecimal contractMultiplier = new BigDecimal("0.001");
+        /** 价格精度(交易所价格的小数位数),默认 1(0.1 精度) */
+        private int priceScale = 1;
         /** 未实现盈亏计价模式,默认 LAST_PRICE(最新成交价) */
         private PnLPriceMode unrealizedPnlPriceMode = PnLPriceMode.LAST_PRICE;
 
@@ -251,6 +311,8 @@
         public Builder reopenMaxRetries(int reopenMaxRetries) { this.reopenMaxRetries = reopenMaxRetries; return this; }
         /** 设置合约乘数(单张合约代表的基础资产数量) */
         public Builder contractMultiplier(BigDecimal contractMultiplier) { this.contractMultiplier = contractMultiplier; return this; }
+        /** 设置价格精度(交易所价格的小数位数,如 1=0.1精度,2=0.01精度) */
+        public Builder priceScale(int priceScale) { this.priceScale = priceScale; return this; }
         /** 设置未实现盈亏计价模式 */
         public Builder unrealizedPnlPriceMode(PnLPriceMode mode) { this.unrealizedPnlPriceMode = mode; return this; }
 

--
Gitblit v1.9.1