diff --git a/src/main/java/com/sample/trend/strategy/AutoGridStrategy.java b/src/main/java/com/sample/trend/strategy/AutoGridStrategy.java index 69e22ba..97f3bad 100644 --- a/src/main/java/com/sample/trend/strategy/AutoGridStrategy.java +++ b/src/main/java/com/sample/trend/strategy/AutoGridStrategy.java @@ -51,6 +51,7 @@ public class AutoGridStrategy extends BaseStrategy { private final Logger logger = LoggerFactory.getLogger(AutoGridStrategy.class); // 运行参数 + /** 每格购买价值(报价币金额),下单数量 = baseQuantity / price */ private BigDecimal baseQuantity; private BigDecimal atrMultiplier; private String gridGrpScript; @@ -613,9 +614,15 @@ public class AutoGridStrategy extends BaseStrategy { return; } + BigDecimal quantity = quantityFromBaseValue(normalizedPrice); + if (quantity.compareTo(BigDecimal.ZERO) <= 0) { + logger.warn("按购买价值换算数量无效:价值={},价格={}", baseQuantity, normalizedPrice); + return; + } + String clientOrderId = buildClientOrderId("BUY", normalizedPrice); try { - JSONObject orderInfo = buildOrderRequest("buy", "open", "limit", normalizedPrice, baseQuantity); + JSONObject orderInfo = buildOrderRequest("buy", "open", "limit", normalizedPrice, quantity); orderInfo.put("clientOid", clientOrderId); JSONObject result = bitGetClient.placeOrder(orderInfo); @@ -625,8 +632,9 @@ public class AutoGridStrategy extends BaseStrategy { gridBuyOrder.exchangeOrderId = exchangeOrderId; pendingBuyOrders.put(priceKey(normalizedPrice), gridBuyOrder); - logger.info("挂买单成功:价格={},订单号={}", normalizedPrice, exchangeOrderId); - submitOrderSync(exchangeOrderId, clientOrderId, normalizedPrice, baseQuantity, + logger.info("挂买单成功:价格={},数量={}(价值={}),订单号={}", + normalizedPrice, quantity, baseQuantity, exchangeOrderId); + submitOrderSync(exchangeOrderId, clientOrderId, normalizedPrice, quantity, OrderSide.BUY, OrderStatus.NEW); } catch (Exception e) { logger.error("挂买单失败,价格={}", normalizedPrice, e); @@ -1052,7 +1060,8 @@ public class AutoGridStrategy extends BaseStrategy { ? "POS_" + entryPrice.toPlainString() : "SYNC_POS_" + price.toPlainString(); if (quantity == null) { - quantity = baseQuantity; + BigDecimal refPrice = entryPrice != null ? entryPrice : price; + quantity = quantityFromBaseValue(refPrice); } GridSellOrder sellOrder = new GridSellOrder(clientOid, clientPositionId, price, quantity); sellOrder.exchangeOrderId = exchangeOrderId; @@ -1232,7 +1241,7 @@ public class AutoGridStrategy extends BaseStrategy { JSONObject row = new JSONObject(); row.put("type", "待成交买单"); row.put("price", formatPrice(order.buyPrice)); - row.put("quantity", baseQuantity.toPlainString()); + row.put("quantity", quantityFromBaseValue(order.buyPrice).toPlainString()); row.put("takeProfit", "-"); rows.add(row); } @@ -1350,6 +1359,16 @@ public class AutoGridStrategy extends BaseStrategy { return qty.setScale(quantityPrecision, RoundingMode.DOWN); } + /** + * 购买价值换算为合约/币数量:数量 = baseQuantity / price + */ + private BigDecimal quantityFromBaseValue(BigDecimal price) { + if (baseQuantity == null || price == null || price.compareTo(BigDecimal.ZERO) <= 0) { + return BigDecimal.ZERO; + } + return normalizeQuantity(baseQuantity.divide(price, quantityPrecision + 8, RoundingMode.DOWN)); + } + private String priceKey(BigDecimal price) { return normalizePrice(price).toPlainString(); }