forked from chaychan/BlogFileResource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
124 lines (110 loc) · 5.18 KB
/
Main.java
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
package com.okex.open.api;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.okex.open.api.bean.trade.param.PlaceOrder;
import com.okex.open.api.config.APIConfiguration;
import com.okex.open.api.service.account.impl.AccountAPIServiceImpl;
import com.okex.open.api.service.marketData.impl.MarketDataAPIServiceImpl;
import com.okex.open.api.service.trade.impl.TradeAPIServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Main {
private static final Logger LOG = LoggerFactory.getLogger(Main.class);
private static APIConfiguration config = new APIConfiguration();
private static AccountAPIServiceImpl accountAPIService;
private static MarketDataAPIServiceImpl marketDataAPIService;
private static TradeAPIServiceImpl tradeAPIService;
private static double buyPrice = 41800;
private static double sellPrice = 42000;
public static void main(String[] args) {
config.setEndpoint("https://www.okexa.com/");
config.setApiKey("e94f4620-fe2d-4b16-81f6-8c8c4ce76466");
config.setSecretKey("9BA33E4388F47426DB3F89B7012873BF");
config.setPassphrase("zzq960309X");
config.setPrint(false);
accountAPIService = new AccountAPIServiceImpl(config);
marketDataAPIService = new MarketDataAPIServiceImpl(config);
tradeAPIService = new TradeAPIServiceImpl(config);
getTicker();
// System.out.println("balance: " + getBalance(accountAPIService));
}
public static double getBalance(AccountAPIServiceImpl accountAPIService, String eq) {
JSONObject result = accountAPIService.getBalance(eq);
double balance = 0;
if (result != null && result.getJSONArray("data") != null) {
JSONObject result2 = (JSONObject) result.getJSONArray("data").get(0);
if (result2 != null && result2.getJSONArray("details") != null) {
JSONObject result3 = (JSONObject) result2.getJSONArray("details").get(0);
if (result3 != null) {
String t = result3.getString("availEq");
if (t != null) {
balance = Double.parseDouble(t);
}
}
}
System.out.println(eq + " balance: " + balance);
}
return balance;
}
C:\Users\Administrator\Downloads\Open-API-SDK-V5-main\okex-java-sdk-api-v5\src\main\java\com\okex\open\api\Main.java
public static void buyOrder(TradeAPIServiceImpl tradeAPIService) {
PlaceOrder placeOrder = new PlaceOrder();
placeOrder.setInstId("BTC-USDT");
placeOrder.setTdMode("cash");
placeOrder.setSide("buy");
placeOrder.setOrdType("limit");
placeOrder.setSz("0.0036");
placeOrder.setPx("" + buyPrice);
JSONObject result = tradeAPIService.placeOrder(placeOrder);
System.out.println("buy: " + result.toJSONString());
// toResultString(LOG, "result", result);
}
public static void sellOrder(TradeAPIServiceImpl tradeAPIService) {
PlaceOrder placeOrder = new PlaceOrder();
placeOrder.setInstId("BTC-USDT");
placeOrder.setTdMode("cash");
placeOrder.setSide("sell");
placeOrder.setOrdType("limit");
placeOrder.setSz("0.00359");
placeOrder.setPx("" + sellPrice);
JSONObject result = tradeAPIService.placeOrder(placeOrder);
System.out.println("sell: " + result.toJSONString());
// toResultString(LOG, "result", result);
}
public static void toResultString(Logger log, String flag, Object object) {
StringBuilder su = new StringBuilder();
su.append("\n").append("=====>").append(flag).append(":\n").append(JSON.toJSONString(object));
log.info(su.toString());
}
public static void getTicker() {
System.out.println("重新启动");
boolean flag = true;
while (flag) {
double last = 999999999;
try {
JSONObject result = marketDataAPIService.getTicker("BTC-USDT-SWAP");
if (result != null && result.getJSONArray("data") != null) {
JSONObject result2 = (JSONObject) result.getJSONArray("data").get(0);
String t = result2.getString("last");
if (t != null) {
last = Double.parseDouble(t);
}
System.out.println("last: " + last);
double balance = getBalance(accountAPIService,"USDT");
double btcBalance = getBalance(accountAPIService,"BTC");
if (last <= buyPrice && balance > 150) {
buyOrder(tradeAPIService);
} else if (last > sellPrice && balance < 150) {
//todo 卖出 划转
sellOrder(tradeAPIService);
}
}
Thread.sleep(3000);
} catch (InterruptedException e) {
flag = false;
getTicker();
e.printStackTrace();
}
}
}
}