优化BG平仓挂单

This commit is contained in:
tony 2026-07-12 00:50:12 +08:00
parent 08c5559cf6
commit 059f339ade

View File

@ -585,7 +585,7 @@ public class AutoGridStrategy extends BaseStrategy {
String clientOrderId = buildClientOrderId("BUY", normalizedPrice); String clientOrderId = buildClientOrderId("BUY", normalizedPrice);
try { try {
JSONObject orderInfo = buildOrderRequest("buy", "open", "limit", normalizedPrice, baseQuantity, false); JSONObject orderInfo = buildOrderRequest("buy", "open", "limit", normalizedPrice, baseQuantity);
orderInfo.put("clientOid", clientOrderId); orderInfo.put("clientOid", clientOrderId);
JSONObject result = bitGetClient.placeOrder(orderInfo); JSONObject result = bitGetClient.placeOrder(orderInfo);
@ -633,8 +633,8 @@ public class AutoGridStrategy extends BaseStrategy {
String clientOrderId = buildClientOrderId("SELL", position.entryPrice); String clientOrderId = buildClientOrderId("SELL", position.entryPrice);
try { try {
JSONObject orderInfo = buildOrderRequest("sell", "close", "limit", sellPrice, JSONObject orderInfo = buildOrderRequest("buy", "close", "limit", sellPrice,
position.quantity, true); position.quantity);
orderInfo.put("clientOid", clientOrderId); orderInfo.put("clientOid", clientOrderId);
JSONObject result = bitGetClient.placeOrder(orderInfo); JSONObject result = bitGetClient.placeOrder(orderInfo);
@ -694,7 +694,7 @@ public class AutoGridStrategy extends BaseStrategy {
if (isOpenBuy(side, tradeSide)) { if (isOpenBuy(side, tradeSide)) {
onBuyFilled(price, volume, orderId, clientOid, price); onBuyFilled(price, volume, orderId, clientOid, price);
} else if (isCloseSell(side, tradeSide)) { } else if (isCloseBuy(side, tradeSide)) {
onSellFilled(price, volume, orderId, clientOid); onSellFilled(price, volume, orderId, clientOid);
} }
} }
@ -717,9 +717,9 @@ public class AutoGridStrategy extends BaseStrategy {
BigDecimal orderPrice = order.getBigDecimal("price"); BigDecimal orderPrice = order.getBigDecimal("price");
if ("cancelled".equals(status) || "canceled".equals(status)) { if ("cancelled".equals(status) || "canceled".equals(status)) {
if ("buy".equals(side)) { if (isOpenBuy(side, tradeSide)) {
removePendingBuy(orderId, clientOid, orderPrice); removePendingBuy(orderId, clientOid, orderPrice);
} else if ("sell".equals(side)) { } else if (isCloseBuy(side, tradeSide)) {
removePendingSell(orderId, clientOid, orderPrice); removePendingSell(orderId, clientOid, orderPrice);
} }
return; return;
@ -740,7 +740,7 @@ public class AutoGridStrategy extends BaseStrategy {
if (isOpenBuy(side, tradeSide)) { if (isOpenBuy(side, tradeSide)) {
onBuyFilled(fillPrice, volume, orderId, clientOid, orderPrice); onBuyFilled(fillPrice, volume, orderId, clientOid, orderPrice);
} else if (isCloseSell(side, tradeSide)) { } else if (isCloseBuy(side, tradeSide)) {
onSellFilled(fillPrice, volume, orderId, clientOid); onSellFilled(fillPrice, volume, orderId, clientOid);
} }
} }
@ -863,14 +863,8 @@ public class AutoGridStrategy extends BaseStrategy {
|| "buy_single".equals(tradeSide); || "buy_single".equals(tradeSide);
} }
private boolean isCloseSell(String side, String tradeSide) { private boolean isCloseBuy(String side, String tradeSide) {
if (!"sell".equals(side)) { return "buy".equals(side) && "close".equals(tradeSide);
return false;
}
return tradeSide == null
|| "close".equals(tradeSide)
|| "sell_single".equals(tradeSide)
|| "reduce_sell_single".equals(tradeSide);
} }
private BigDecimal firstPositive(BigDecimal... values) { private BigDecimal firstPositive(BigDecimal... values) {
@ -1008,7 +1002,7 @@ public class AutoGridStrategy extends BaseStrategy {
gridBuyOrder.exchangeOrderId = exchangeOrderId; gridBuyOrder.exchangeOrderId = exchangeOrderId;
pendingBuyOrders.put(priceKey(price), gridBuyOrder); pendingBuyOrders.put(priceKey(price), gridBuyOrder);
buyCount++; buyCount++;
} else if ("sell".equals(side) && "close".equals(tradeSide)) { } else if ("buy".equals(side) && "close".equals(tradeSide)) {
if (clientOid == null) { if (clientOid == null) {
clientOid = "SYNC_SELL_" + price.toPlainString(); clientOid = "SYNC_SELL_" + price.toPlainString();
} }
@ -1251,7 +1245,7 @@ public class AutoGridStrategy extends BaseStrategy {
// ======================== 工具方法 ======================== // ======================== 工具方法 ========================
private JSONObject buildOrderRequest(String side, String tradeSide, String orderType, private JSONObject buildOrderRequest(String side, String tradeSide, String orderType,
BigDecimal price, BigDecimal size, boolean reduceOnly) { BigDecimal price, BigDecimal size) {
JSONObject orderInfo = new JSONObject(); JSONObject orderInfo = new JSONObject();
orderInfo.put("symbol", getPlainSymbol()); orderInfo.put("symbol", getPlainSymbol());
orderInfo.put("marginMode", MARGIN_MODE); orderInfo.put("marginMode", MARGIN_MODE);
@ -1262,9 +1256,6 @@ public class AutoGridStrategy extends BaseStrategy {
if (price != null) { if (price != null) {
orderInfo.put("price", normalizePrice(price).toPlainString()); orderInfo.put("price", normalizePrice(price).toPlainString());
} }
if (reduceOnly) {
orderInfo.put("reduceOnly", "YES");
}
return orderInfo; return orderInfo;
} }