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 pathSpotTest.java
More file actions
Latest commit
83 lines (70 loc) · 3.48 KB
/
Copy pathSpotTest.java
File metadata and controls
83 lines (70 loc) · 3.48 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
packageio.gate.apidemo;
importio.gate.gateapi.ApiClient;
importio.gate.gateapi.ApiException;
importio.gate.gateapi.api.SpotApi;
importio.gate.gateapi.models.*;
importjava.math.BigDecimal;
importjava.util.List;
classSpotTest {
privatefinalRunConfigconfig;
SpotTest(RunConfigconfig) {
this.config = config;
}
publicvoidrun() throwsApiException {
StringcurrencyPair = "GT_USDT";
Stringcurrency = currencyPair.split("_")[1];
// Initialize API client
ApiClientclient = newApiClient();
client.setApiKeySecret(this.config.getApiKey(), this.config.getApiSecret());
// Setting basePath is optional. It defaults to https://api.gateio.ws/api/v4
client.setBasePath(this.config.getHostUsed());
SpotApispotApi = newSpotApi(client);
CurrencyPairpair = spotApi.getCurrencyPair(currencyPair);
System.out.println("testing against currency pair: " + currencyPair);
StringminAmount = pair.getMinBaseAmount();
// get last price
List<Ticker> tickers = spotApi.listTickers().currencyPair(currencyPair).execute();
asserttickers.size() == 1;
StringlastPrice = tickers.get(0).getLast();
assertlastPrice != null;
// make sure balance is enough
BigDecimalorderAmount = newBigDecimal(minAmount).multiply(newBigDecimal("2"));
List<SpotAccount> accounts = spotApi.listSpotAccounts().currency(currency).execute();
assertaccounts.size() == 1;
BigDecimalavailable = newBigDecimal(accounts.get(0).getAvailable());
System.out.printf("Account available: %s %s\n", available.toPlainString(), currency);
if (available.compareTo(orderAmount) < 0) {
System.err.println("Account balance not enough");
return;
}
// create spot order
Orderorder = newOrder();
order.setAccount(Order.AccountEnum.SPOT);
order.setAutoBorrow(false);
order.setTimeInForce(Order.TimeInForceEnum.GTC);
order.setType(Order.TypeEnum.LIMIT);
order.setAmount(orderAmount.toPlainString());
order.setPrice(lastPrice);
order.setSide(Order.SideEnum.BUY);
order.setCurrencyPair(currencyPair);
System.out.printf("place a spot %s order in %s with amount %s and price %s\n", order.getSide(), order.getCurrencyPair(),
order.getAmount(), order.getPrice());
Ordercreated = spotApi.createOrder(order);
System.out.printf("order created with id %s, status %s\n", created.getId(), created.getStatus());
if (Order.StatusEnum.OPEN.equals(created.getStatus())) {
OrderorderResult = spotApi.getOrder(created.getId(), currencyPair, null);
System.out.printf("order %s filled: %s, left: %s\n", orderResult.getId(), orderResult.getFilledTotal(), orderResult.getLeft());
Orderresult = spotApi.cancelOrder(orderResult.getId(), currencyPair, null);
if (Order.StatusEnum.CANCELLED.equals(result.getStatus())) {
System.out.println("order " + orderResult.getId() + " canceled\n");
}
} else {
// Thread.sleep(5000);
List<Trade> trades = spotApi.listMyTrades(currencyPair).orderId(created.getId()).execute();
asserttrades.size() > 0;
for (Tradet : trades) {
System.out.printf("order %s filled %s with price %s\n", t.getOrderId(), t.getAmount(), t.getPrice());
}
}
}
}