Administrator
5 days ago 286b64cef4127c2ca917561c474842c375d2ea73
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -5,7 +5,11 @@
import io.gate.gateapi.GateApiException;
import io.gate.gateapi.api.FuturesApi;
import io.gate.gateapi.models.FuturesAccount;
import io.gate.gateapi.models.FuturesInitialOrder;
import io.gate.gateapi.models.FuturesOrder;
import io.gate.gateapi.models.FuturesPriceTrigger;
import io.gate.gateapi.models.FuturesPriceTriggeredOrder;
import io.gate.gateapi.models.TriggerOrderResponse;
import lombok.extern.slf4j.Slf4j;
import java.math.BigDecimal;
@@ -42,6 +46,7 @@
    private final int maxCycles;
    private final BigDecimal maxLoss;
    private final String quantity;
    private final String positionMode;
    private volatile boolean strategyActive = false;
    private int currentCycle = 0;
@@ -54,9 +59,11 @@
    private volatile BigDecimal lastClosePrice;
    public GateGridTradeService(String apiKey, String apiSecret,
                                 String contract, String leverage, String marginMode,
                                 String contract, String leverage,
                                String marginMode,String positionMode,
                                 BigDecimal gridRate, BigDecimal overallTp,
                                 int maxCycles, BigDecimal maxLoss, String quantity) {
                                 int maxCycles, BigDecimal maxLoss,
                                String quantity) {
        this.contract = contract;
        this.leverage = leverage;
        this.marginMode = marginMode;
@@ -65,6 +72,7 @@
        this.maxCycles = maxCycles;
        this.maxLoss = maxLoss;
        this.quantity = quantity;
        this.positionMode = positionMode;
        this.apiClient = new ApiClient();
        this.apiClient.setBasePath("https://api-testnet.gateapi.io/api/v4");
@@ -77,16 +85,18 @@
     */
    public void init() {
        try {
            futuresApi.setPositionMode(SETTLE, "dual");
            log.info("[GateGrid] 已设置双向持仓模式");
            futuresApi.updateContractPositionLeverageCall(
                    SETTLE, contract, leverage, marginMode, "dual", null);
                    SETTLE, contract, leverage, marginMode, positionMode, null);
            log.info("[GateGrid] 已设置杠杆: {}x, 保证金模式: {}", leverage, marginMode);
            FuturesAccount account = futuresApi.listFuturesAccounts(SETTLE);
            log.info("[GateGrid] 账户可用余额: {}, 总资产: {}",
                    account.getAvailable(), account.getTotal());
            String positionModeSet = account.getPositionMode();
            if (!positionMode.equals(positionModeSet)){
                futuresApi.setPositionMode(SETTLE, positionMode);
            }
            log.info("[GateGrid] 已设置双向持仓模式");
        } catch (GateApiException e) {
            log.error("[GateGrid] 初始化失败, label: {}, msg: {}", e.getErrorLabel(), e.getMessage());
        } catch (ApiException e) {
@@ -144,6 +154,7 @@
            longOrderId = longResult.getId();
            longEntryPrice = safeDecimal(longResult.getFillPrice());
            log.info("[GateGrid] 开多成功, price: {}, id: {}", longEntryPrice, longOrderId);
            placeLongTpSl(longEntryPrice);
            FuturesOrder shortOrder = new FuturesOrder();
            shortOrder.setContract(contract);
@@ -155,6 +166,7 @@
            shortOrderId = shortResult.getId();
            shortEntryPrice = safeDecimal(shortResult.getFillPrice());
            log.info("[GateGrid] 开空成功, price: {}, id: {}", shortEntryPrice, shortOrderId);
            placeShortTpSl(shortEntryPrice);
            printGridInfo();
        } catch (GateApiException e) {
@@ -163,6 +175,52 @@
        } catch (Exception e) {
            log.error("[GateGrid] 双开异常", e);
            strategyActive = false;
        }
    }
    private void placeLongTpSl(BigDecimal entryPrice) {
        BigDecimal tpPrice = entryPrice.multiply(BigDecimal.ONE.add(gridRate)).setScale(1, RoundingMode.HALF_UP);
        placePriceTriggeredOrder(tpPrice, FuturesPriceTrigger.RuleEnum.NUMBER_1, "close-long-position", "close_long");
        log.info("[GateGrid] 多头止盈已设置, TP:{}", tpPrice);
    }
    private void placeShortTpSl(BigDecimal entryPrice) {
        BigDecimal tpPrice = entryPrice.multiply(BigDecimal.ONE.subtract(gridRate)).setScale(1, RoundingMode.HALF_UP);
        placePriceTriggeredOrder(tpPrice, FuturesPriceTrigger.RuleEnum.NUMBER_2, "close-short-position", "close_short");
        log.info("[GateGrid] 空头止盈已设置, TP:{}", tpPrice);
    }
    private void placePriceTriggeredOrder(BigDecimal triggerPrice,
                                           FuturesPriceTrigger.RuleEnum rule,
                                           String orderType,
                                           String autoSize) {
        try {
            FuturesPriceTrigger trigger = new FuturesPriceTrigger();
            trigger.setStrategyType(FuturesPriceTrigger.StrategyTypeEnum.NUMBER_0);
            trigger.setPriceType(FuturesPriceTrigger.PriceTypeEnum.NUMBER_0);
            trigger.setPrice(triggerPrice.toString());
            trigger.setRule(rule);
            trigger.setExpiration(0);
            FuturesInitialOrder initial = new FuturesInitialOrder();
            initial.setContract(contract);
            initial.setSize(0L);
            initial.setPrice("0");
            initial.setTif(FuturesInitialOrder.TifEnum.IOC);
            initial.setReduceOnly(true);
            initial.setAutoSize(autoSize);
            FuturesPriceTriggeredOrder order = new FuturesPriceTriggeredOrder();
            order.setTrigger(trigger);
            order.setInitial(initial);
            order.setOrderType(orderType);
            TriggerOrderResponse response = futuresApi.createPriceTriggeredOrder(SETTLE, order);
            log.info("[GateGrid] 止盈条件单已创建, triggerPrice:{}, rule:{}, orderType:{}, autoSize:{}, id:{}",
                    triggerPrice, rule, orderType, autoSize, response.getId());
        } catch (Exception e) {
            log.error("[GateGrid] 止盈条件单创建失败, triggerPrice:{}, rule:{}, orderType:{}, autoSize:{}",
                    triggerPrice, rule, orderType, autoSize, e);
        }
    }
@@ -258,7 +316,9 @@
    }
    private void closeLongPosition() {
        if (longEntryPrice == null) return;
        if (longEntryPrice == null) {
            return;
        }
        try {
            FuturesOrder closeOrder = new FuturesOrder();
            closeOrder.setContract(contract);
@@ -277,7 +337,9 @@
    }
    private void closeShortPosition() {
        if (shortEntryPrice == null) return;
        if (shortEntryPrice == null) {
            return;
        }
        try {
            FuturesOrder closeOrder = new FuturesOrder();
            closeOrder.setContract(contract);
@@ -301,10 +363,22 @@
    }
    private void printGridInfo() {
        BigDecimal longTp = BigDecimal.ZERO;
        BigDecimal longSl = BigDecimal.ZERO;
        BigDecimal shortTp = BigDecimal.ZERO;
        BigDecimal shortSl = BigDecimal.ZERO;
        if (longEntryPrice != null) {
            longTp = longEntryPrice.multiply(BigDecimal.ONE.add(gridRate)).setScale(1, RoundingMode.HALF_UP);
            longSl = longEntryPrice.multiply(BigDecimal.ONE.subtract(gridRate)).setScale(1, RoundingMode.HALF_UP);
        }
        if (shortEntryPrice != null) {
            shortTp = shortEntryPrice.multiply(BigDecimal.ONE.subtract(gridRate)).setScale(1, RoundingMode.HALF_UP);
            shortSl = shortEntryPrice.multiply(BigDecimal.ONE.add(gridRate)).setScale(1, RoundingMode.HALF_UP);
        }
        System.out.println("========== Gate 网格开仓 ==========");
        System.out.println("合约: " + contract + " 杠杆: " + leverage + "x " + marginMode);
        System.out.println("多头入场: " + longEntryPrice);
        System.out.println("空头入场: " + shortEntryPrice);
        System.out.println("多头入场: " + longEntryPrice + " TP: " + longTp + " SL: " + longSl);
        System.out.println("空头入场: " + shortEntryPrice + " TP: " + shortTp + " SL: " + shortSl);
        System.out.println("数量: " + quantity + " 网格间距: " + gridRate.multiply(new BigDecimal("100")) + "%");
        System.out.println("整体止盈: " + overallTp + " USDT 最大循环: " + maxCycles);
        System.out.println("最大亏损: " + maxLoss + " USDT");