package com.xcong.excoin.modules.gateApi;
|
|
// Import classes:
|
import io.gate.gateapi.ApiClient;
|
import io.gate.gateapi.ApiException;
|
import io.gate.gateapi.Configuration;
|
import io.gate.gateapi.GateApiException;
|
import io.gate.gateapi.api.FuturesApi;
|
import io.gate.gateapi.auth.*;
|
import io.gate.gateapi.models.*;
|
import io.gate.gateapi.api.AccountApi;
|
import lombok.extern.slf4j.Slf4j;
|
|
import java.io.IOException;
|
import java.math.BigDecimal;
|
|
|
/**
|
* Gate SDK API 使用示例。
|
*
|
* <h3>演示内容</h3>
|
* <ol>
|
* <li>通过 API Key/Secret 初始化客户端</li>
|
* <li>查询期货账户余额</li>
|
* <li>设置双向持仓模式</li>
|
* <li>设置杠杆倍数</li>
|
* <li>切换保证金模式(全仓/逐仓)</li>
|
* </ol>
|
*
|
* <h3>注意</h3>
|
* 此文件仅作为 SDK 用法参考,不影响实际策略运行。
|
* 当前策略中相同功能的实现在 {@code GateGridTradeService.init()} 中。
|
*
|
* @author Administrator
|
*/
|
@Slf4j
|
public class Example {
|
/**
|
* 示例入口:依次执行查询账户 → 设置双向持仓 → 设杠杆 → 切换保证金模式。
|
*/
|
public static void main(String[] args) {
|
ApiClient defaultClient = Configuration.getDefaultApiClient();
|
defaultClient.setBasePath("https://api-testnet.gateapi.io/api/v4");
|
// defaultClient.setBasePath("https://api.gateio.ws/api/v4");
|
|
// Configure APIv4 authorization: apiv4
|
defaultClient.setApiKeySecret("d90ca272391992b8e74f8f92cedb21ec", "1861e4f52de4bb53369ea3208d9ede38ece4777368030f96c77d27934c46c274");
|
|
try {
|
|
String CONTRACT = "ETH_USDT";
|
String SETTLE = "usdt";
|
//保证金模式 isolated/cross
|
String MARGINMODE = "CROSS";
|
String LEVERAGE = "100";
|
FuturesApi futuresApi = new FuturesApi(defaultClient);
|
FuturesAccount account = futuresApi.listFuturesAccounts(SETTLE);
|
log.info("[Gate] 初始本金: {} USDT", account.getTotal());
|
|
//设置持仓模式为双向持仓
|
Boolean inDualMode = account.getInDualMode();
|
if (!inDualMode) {
|
try {
|
futuresApi.setDualModeCall(SETTLE,true,null).execute();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
|
try {
|
futuresApi.updateDualModePositionLeverageCall(
|
SETTLE, CONTRACT, LEVERAGE,
|
null, null).execute();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
|
if (!MARGINMODE.equals(account.getMarginMode())) {
|
|
UpdateDualCompPositionCrossModeRequest updateDualCompPositionCrossModeRequest = new UpdateDualCompPositionCrossModeRequest();
|
updateDualCompPositionCrossModeRequest.setMode(MARGINMODE);
|
updateDualCompPositionCrossModeRequest.setContract(CONTRACT);
|
try {
|
futuresApi.updateDualCompPositionCrossModeCall(SETTLE, updateDualCompPositionCrossModeRequest, null).execute();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
} catch (GateApiException e) {
|
System.err.println(String.format("Gate api exception, label: %s, message: %s", e.getErrorLabel(), e.getMessage()));
|
e.printStackTrace();
|
} catch (ApiException e) {
|
System.err.println("Exception when calling AccountApi#getAccountDetail");
|
e.printStackTrace();
|
}
|
}
|
}
|