Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFutureTest.java
More file actions
Latest commit
156 lines (138 loc) · 6.92 KB
/
Copy pathFutureTest.java
File metadata and controls
156 lines (138 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
packageio.gate.apidemo;
importio.gate.gateapi.ApiClient;
importio.gate.gateapi.ApiException;
importio.gate.gateapi.GateApiException;
importio.gate.gateapi.api.FuturesApi;
importio.gate.gateapi.api.WalletApi;
importio.gate.gateapi.models.*;
importjava.math.BigDecimal;
importjava.math.MathContext;
importjava.math.RoundingMode;
importjava.util.List;
publicclassFutureTest {
privatefinalRunConfigconfig;
publicFutureTest(RunConfigconfig) {
this.config = config;
}
publicvoidrun() throwsApiException {
// if testing against TestNet
booleanuseTestNet = this.config.isUseTest();
// set test contract
Stringsettle = "usdt";
Stringcontract = "BTC_USDT";
// Initialize API client
ApiClientclient = newApiClient();
// Setting basePath is optional. It defaults to https://api.gateio.ws/api/v4
client.setBasePath(this.config.getHostUsed());
client.setApiKeySecret(this.config.getApiKey(), this.config.getApiSecret());
FuturesApifuturesApi = newFuturesApi(client);
// update position leverage
Stringleverage = "3";
futuresApi.updatePositionLeverage(settle, contract, leverage, "0");
// retrieve position information
LongpositionSize = 0L;
try {
Positionposition = futuresApi.getPosition(settle, contract);
positionSize = position.getSize();
assertpositionSize != null;
} catch (GateApiExceptione) {
// ignore no position error
if (!"POSITION_NOT_FOUND".equals(e.getErrorLabel())) {
throwe;
}
}
// set order size
ContractfuturesContract = futuresApi.getFuturesContract(settle, contract);
LongorderSize = 10L;
if (futuresContract.getOrderSizeMin() != null && futuresContract.getOrderSizeMin() > orderSize) {
orderSize = futuresContract.getOrderSizeMin();
}
if (positionSize < 0) {
// if short, set size to negative
orderSize = -orderSize;
}
// example to update risk limit
assertfuturesContract.getRiskLimitBase() != null;
assertfuturesContract.getRiskLimitStep() != null;
StringriskLimit = newBigDecimal(futuresContract.getRiskLimitBase()).add(newBigDecimal(futuresContract.getRiskLimitStep()))
.toPlainString();
futuresApi.updatePositionRiskLimit(settle, contract, riskLimit);
// retrieve last price to calculate margin needed
List<FuturesTicker> tickers = futuresApi.listFuturesTickers(settle).contract(contract).execute();
StringlastPrice = tickers.get(0).getLast();
assertlastPrice != null;
System.out.printf("last price of contract %s: %s\n", contract, lastPrice);
MathContextmc = newMathContext(8, RoundingMode.UP);
assertfuturesContract.getQuantoMultiplier() != null;
BigDecimalmargin = newBigDecimal(orderSize).multiply(newBigDecimal(lastPrice), mc)
.multiply(newBigDecimal(futuresContract.getQuantoMultiplier()), mc)
.divide(newBigDecimal(leverage), mc)
.multiply(newBigDecimal("1.1"));
System.out.printf("needs margin amount: %s\n", margin.toPlainString());
// if balance not enough, transfer from spot account
Stringavailable = "0";
try {
FuturesAccountfuturesAccounts = futuresApi.listFuturesAccounts(settle);
available = futuresAccounts.getAvailable();
assertavailable != null;
} catch (GateApiExceptione) {
if (!"USER_NOT_FOUND".equals(e.getErrorLabel())) {
throwe;
}
}
System.out.printf("Futures account available %s %s\n", available, settle.toUpperCase());
if (newBigDecimal(available).compareTo(margin) < 0) {
if (useTestNet) {
System.err.println("TestNet account balance not enough, make a transferal on web");
return;
}
// make sure balance is enough
Transfertransfer = newTransfer().amount(margin.toPlainString())
.currency(settle.toUpperCase())
.from(Transfer.FromEnum.SPOT)
.to(Transfer.ToEnum.FUTURES)
.settle(settle);
WalletApiwalletApi = newWalletApi(client);
walletApi.transfer(transfer);
}
// example to cancel all open orders in contract
futuresApi.cancelFuturesOrders(settle, contract, "");
// order using market price
FuturesOrderfuturesOrder = newFuturesOrder();
futuresOrder.setContract(contract);
futuresOrder.setSize(orderSize);
futuresOrder.setPrice("0");
futuresOrder.setTif(FuturesOrder.TifEnum.IOC);
FuturesOrderorderResponse;
try {
orderResponse = futuresApi.createFuturesOrder(settle, futuresOrder);
} catch (GateApiExceptione) {
System.err.printf("error encountered creating futures order: %s\n", e);
return;
}
assertorderResponse.getId() != null;
System.out.printf("Order %s created with status: %s\n", orderResponse.getId(), orderResponse.getStatus());
if (orderResponse.getStatus() == FuturesOrder.StatusEnum.OPEN) {
FuturesOrderorder = futuresApi.getFuturesOrder(settle, orderResponse.getId().toString());
assertorder.getId() != null;
System.out.printf("Order %s status %s, total size %d, left %s\n", order.getId(), order.getStatus(), order.getSize(),
order.getLeft());
futuresApi.cancelFuturesOrder(settle, order.getId().toString());
System.out.printf("Order %d cancelled\n", order.getId());
} elseif (orderResponse.getStatus() == FuturesOrder.StatusEnum.FINISHED) {
List<MyFuturesTrade> orderTrades = futuresApi.getMyTrades(settle).contract(contract).order(orderResponse.getId()).execute();
assertorderTrades.size() > 0;
longtradeSize = 0L;
for (MyFuturesTradet : orderTrades) {
assertt.getOrderId() != null && t.getOrderId().equals(orderResponse.getId().toString());
assertt.getSize() != null;
tradeSize += t.getSize();
System.out.printf("Order %s filled size %s with price %s\n", t.getOrderId(), t.getSize(), t.getPrice());
}
asserttradeSize == orderSize;
// example to update position margin
Stringchange = "0.01";
futuresApi.updatePositionMargin(settle, contract, change);
}
}
}