-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigOneExample.java
More file actions
208 lines (173 loc) · 7.99 KB
/
BigOneExample.java
File metadata and controls
208 lines (173 loc) · 7.99 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package com.libra.exchange.exchanges.bigone;
import com.google.api.client.util.Maps;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.UUID;
/**
* Created by rongqiang.li on 18/2/15.
* <p>
* contact: lrqstudy@gmail.com
*/
public class BigOneExample {
public final static String BIGONE_BASE_URL = "https://api.big.one";
public final static String BIGONE_GET_ACCOUNT_COMMAND = "/accounts";
public final static String BIGONE_ORDERS_COMMAND = "/orders";
private static final String USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36";
private static Logger logger = LoggerFactory.getLogger(BigOneExample.class);
public static final String BIG_ONE_API_KEY = "YourApiKey";// https://big.one/settings
public static void main(String[] args) {
testGetAccountInfoList();
testTrade();
}
private static void testGetAccountInfoList() {
BigOneExample bigOneExample = new BigOneExample();
bigOneExample.getAccountInfoList();
}
private static void testTrade() {
//0.00102BTC 价格购买 10EOS
BigOneExample bigOneExample = new BigOneExample();
BigDecimal price = new BigDecimal("0.00106");
BigDecimal amount = new BigDecimal("10");
amount = amount.setScale(2, BigDecimal.ROUND_CEILING);
bigOneExample.trade("EOS-BTC", price, amount, 2, "ASK");//buy
}
public void getOpenOrderList(String currencyPair) {
StringBuilder sb = new StringBuilder();
sb.append(BIGONE_BASE_URL).append(BIGONE_ORDERS_COMMAND).append("?market=").append(currencyPair);
String result = executeCommandWithGet(sb.toString());
System.out.println(result);
}
public void getAccountInfoList() {
try {
StringBuilder sb = new StringBuilder();
sb.append(BIGONE_BASE_URL).
append(BIGONE_GET_ACCOUNT_COMMAND).
append("/");
String resultStr = executeCommandWithGet(sb.toString());
System.out.println(resultStr);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
public String buy(String currrencyPair, BigDecimal price, BigDecimal amount, Integer precisionSize) {
amount = amount.setScale(precisionSize, BigDecimal.ROUND_CEILING);
String result = trade(currrencyPair, price, amount, precisionSize, "BID");
return result;
}
private String trade(String currencyPair, BigDecimal price, BigDecimal amount, Integer precisionSize, String orderSide) {
try {
Map<String, String> params = Maps.newHashMap();
amount = amount.setScale(precisionSize, BigDecimal.ROUND_CEILING);//设置最小的交易量精度,如果是 10.01 这样的格式精度设置为2, 如果为6.3256 这样的精度设置为4,表示小数后四位
params.put("order_market", currencyPair);
params.put("order_side", orderSide);
params.put("price", price.toPlainString());
params.put("amount", amount.toPlainString());
StringBuilder sb = new StringBuilder();
sb.append(BIGONE_BASE_URL).append(BIGONE_ORDERS_COMMAND);
String result = executeCommandWithPost(sb.toString(), params);
System.out.println(result);
return result;
} catch (Exception e) {
logger.error(" sell error" + e.getMessage(), e);
return "error";
}
}
public String sell(String currrencyPair, BigDecimal price, BigDecimal amount, Integer precisionSize) {
Map<String, String> params = Maps.newHashMap();
amount = amount.setScale(precisionSize, BigDecimal.ROUND_CEILING);
params.put("order_market", currrencyPair);
params.put("order_side", "ASK");
params.put("price", price.toPlainString());
params.put("amount", amount.toPlainString());
return trade(currrencyPair, price, amount, precisionSize, "ASK");
}
private String executeCommandWithPost(String urlStr, Map<String, String> paramsMap) {
HttpURLConnection conn = null;
try {
URL url = new URL(urlStr);
conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoOutput(true);
conn.setRequestProperty("Authorization", buildAuthorizationHeaderValue());
conn.setRequestProperty("Big-Device-Id", UUID.randomUUID().toString());
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.setRequestMethod("POST");
String postData;
JSONObject jsonObject = new JSONObject();
for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
jsonObject.put(entry.getKey(), entry.getValue());
}
postData = jsonObject.toString();
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
out.write(postData);
out.close();
String responseResult = convertStreamToString(conn.getInputStream());
logger.info(" call url={}, responseResult={} ", responseResult);
return responseResult;
} catch (MalformedURLException e) {
throw new RuntimeException("call url error" + e.getMessage(), e);
} catch (Exception e) {
throw new RuntimeException("call url error" + e.getMessage(), e);
} finally {
if (conn != null) {
conn.disconnect();
}
}
}
private String buildAuthorizationHeaderValue() {
StringBuilder sb = new StringBuilder();
//TODO you api key in bigone ;Bearer 是固定写法请照抄,这块我当时也没想到是这样的,跟我调试过大部分的 API不一样,以为 Bearer 是个代名词,用各种东西试了都是400,发现还是自己太无知,不知道这是一种特定写法
sb.append("Bearer ").append(BIG_ONE_API_KEY);//https://big.one/settings
return sb.toString();
}
private String executeCommandWithGet(String urlStr) {
HttpURLConnection conn = null;
try {
URL url = new URL(urlStr);
conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoOutput(true);
conn.setRequestProperty("Authorization", buildAuthorizationHeaderValue());
conn.setRequestProperty("Big-Device-Id", UUID.randomUUID().toString());
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.setRequestMethod("GET");
String responseResult = convertStreamToString(conn.getInputStream());
logger.info(" call url={}, responseResult={} ", responseResult);
return responseResult;
} catch (MalformedURLException e) {
throw new RuntimeException("call url error" + e.getMessage(), e);
} catch (IOException e) {
throw new RuntimeException("call url error" + e.getMessage(), e);
} finally {
if (conn != null) {
conn.disconnect();
}
}
}
private String convertStreamToString(InputStream is) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line).append('\n');
}
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}