| | |
| | | import io.gate.gateapi.models.*; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import okhttp3.ConnectionPool; |
| | | import okhttp3.OkHttpClient; |
| | | |
| | | import java.io.IOException; |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | import com.xcong.excoin.modules.gateApi.wsHandler.handler.CandlestickChannelHandler; |
| | | import com.xcong.excoin.modules.gateApi.wsHandler.handler.PositionClosesChannelHandler; |
| | |
| | | private volatile BigDecimal shortPositionSize = BigDecimal.ZERO; |
| | | private Long userId; |
| | | private volatile BigDecimal initialPrincipal = BigDecimal.ZERO; |
| | | private volatile GateKlineWebSocketClient wsClient; |
| | | |
| | | public GateGridTradeService(GateConfig config) { |
| | | this.config = config; |
| | | ApiClient apiClient = createApiClient(config); |
| | | ApiClient apiClient = new ApiClient(); |
| | | apiClient.setBasePath(config.getRestBasePath()); |
| | | apiClient.setApiKeySecret(config.getApiKey(), config.getApiSecret()); |
| | | this.futuresApi = new FuturesApi(apiClient); |
| | | this.executor = new GateTradeExecutor(apiClient, config.getContract()); |
| | | } |
| | | |
| | | private static ApiClient createApiClient(GateConfig config) { |
| | | OkHttpClient httpClient = new OkHttpClient.Builder() |
| | | .connectTimeout(30, TimeUnit.SECONDS) |
| | | .readTimeout(60, TimeUnit.SECONDS) |
| | | .writeTimeout(20, TimeUnit.SECONDS) |
| | | .connectionPool(new ConnectionPool(5, 5, TimeUnit.SECONDS)) |
| | | .retryOnConnectionFailure(true) |
| | | .build(); |
| | | ApiClient client = new ApiClient(); |
| | | client.setBasePath(config.getRestBasePath()); |
| | | client.setApiKeySecret(config.getApiKey(), config.getApiSecret()); |
| | | client.setHttpClient(httpClient); |
| | | return client; |
| | | } |
| | | |
| | | // ---- 初始化 ---- |
| | |
| | | */ |
| | | public void init() { |
| | | try { |
| | | ApiClient detailClient = createApiClient(config); |
| | | ApiClient detailClient = new ApiClient(); |
| | | detailClient.setBasePath(config.getRestBasePath()); |
| | | detailClient.setApiKeySecret(config.getApiKey(), config.getApiSecret()); |
| | | AccountDetail detail = new AccountApi(detailClient).getAccountDetail(); |
| | | this.userId = detail.getUserId(); |
| | | log.info("[Gate] 用户ID: {}", userId); |
| | |
| | | |
| | | //初始化0位置的开仓,并且用空的开仓价格,作为价格基准来划分网格 |
| | | if (state == StrategyState.WAITING_KLINE) { |
| | | if (wsClient == null || !wsClient.areAllSubscribed()) { |
| | | return; |
| | | } |
| | | state = StrategyState.OPENING; |
| | | log.info("[Gate] 首根K线到达,开基底仓位 多空各{}张...", config.getBaseQuantity()); |
| | | executor.openLong(config.getBaseQuantity(), (orderId) -> { |
| | |
| | | public Long getUserId() { return userId; } |
| | | /** @return 当前策略状态 */ |
| | | public StrategyState getState() { return state; } |
| | | /** 注入WS客户端,用于订阅状态检查 */ |
| | | public void setWsClient(GateKlineWebSocketClient wsClient) { this.wsClient = wsClient; } |
| | | } |