Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetHistoricalPrice.java
More file actions
339 lines (297 loc) · 13.4 KB
/
Copy pathgetHistoricalPrice.java
File metadata and controls
339 lines (297 loc) · 13.4 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
// Note: Following libraries are required:
//
// 1) 'jackson-all-xxx.jar' with MAVEN dependency: groupId 'org.codehaus.jackson', artifactId 'jackson' and version 1.9.9
// or download from main project at 'http://www.java2s.com/Code/Jar/j/Downloadjacksonall199jar.htm'
//
// 2) 'httpclient-xxx.jar' with MAVEN dependency: groupId 'org.apache.httpcomponents', artifactId 'fluent-hc' and version 4.5
// or download from main project at 'https://hc.apache.org'
public class getHistoricalPrice {
private static final boolean ssl = true;
private static final String URL = "/getHistoricalPrice";
private static String domain;
//private static String url_stream;
private static String url_polling;
private static String url_challenge;
private static String url_token;
private static String user;
private static String password;
private static String authentication_port;
private static String request_port;
private static String ssl_cert;
private static String challenge;
private static String token;
//private static int interval;
public static class hftRequest {
public getAuthorizationChallengeRequest getAuthorizationChallenge;
public getAuthorizationTokenRequest getAuthorizationToken;
public getHistoricalPriceRequest getHistoricalPrice;
public hftRequest( String user) {
this.getAuthorizationChallenge = new getAuthorizationChallengeRequest(user);
}
public hftRequest( String user, String challengeresp ) {
this.getAuthorizationToken = new getAuthorizationTokenRequest(user, challengeresp);
}
public hftRequest( String user, String token, List<String> security, List<String> tinterface, String granularity, String side, int number ) {
this.getHistoricalPrice = new getHistoricalPriceRequest(user, token, security, tinterface, granularity, side, number);
}
}
public static class hftResponse {
public getAuthorizationChallengeResponse getAuthorizationChallengeResponse;
public getAuthorizationTokenResponse getAuthorizationTokenResponse;
public getHistoricalPriceResponse getHistoricalPriceResponse;
}
public static class getAuthorizationChallengeRequest {
public String user;
public getAuthorizationChallengeRequest( String user ) {
this.user = user;
}
}
public static class getAuthorizationChallengeResponse {
public String challenge;
public String timestamp;
}
public static class getAuthorizationTokenRequest {
public String user;
public String challengeresp;
public getAuthorizationTokenRequest( String user, String challengeresp ) {
this.user = user;
this.challengeresp = challengeresp;
}
}
public static class getAuthorizationTokenResponse {
public String token;
public String timestamp;
}
public static class getHistoricalPriceRequest {
public String user;
public String token;
public List<String> security;
public List<String> tinterface;
public String granularity;
public String side;
public int number;
public getHistoricalPriceRequest( String user, String token, List<String> security, List<String> tinterface, String granularity, String side, int number ) {
this.user = user;
this.token = token;
this.security = security;
this.tinterface = tinterface;
this.granularity = granularity;
this.side = side;
this.number = number;
}
}
public static class getHistoricalPriceResponse {
public int result;
public String message;
public List<candleTick> candle;
public String timestamp;
}
public static class candleTick {
public String security;
public String tinterface;
public int timestamp;
public String side;
public double open;
public double high;
public double low;
public double close;
public int ticks;
}
public static void main(String[] args) throws IOException, DecoderException, CertificateException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
// get properties from file
getProperties();
final ObjectMapper mapper = new ObjectMapper();
List<Header> headers = new ArrayList<Header>();
headers.add( new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json") );
headers.add( new BasicHeader(HttpHeaders.ACCEPT, "application/json") );
CloseableHttpClient client=null;
if (ssl){
// get certificate
CertificateFactory cf = CertificateFactory.getInstance("X.509");
URL url = new URL(ssl_cert);
URLConnection connection = url.openConnection();
InputStream in = connection.getInputStream();
Certificate cert = cf.generateCertificate(in);
//System.out.println("Cert:\n===================\n" + cert.getPublicKey().toString() + "\n");
in.close();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(null); // You don't need the KeyStore instance to come from a file.
ks.setCertificateEntry("cert", cert);
tmf.init(ks);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tmf.getTrustManagers(), null);
client = HttpClients.custom().setSSLContext(sslContext).setDefaultHeaders(headers).build();
}
else{
client = HttpClients.custom().setDefaultHeaders(headers).build();
}
// Create a custom response handler
ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
@Override
public String handleResponse(final HttpResponse httpresponse) throws ClientProtocolException, IOException {
int status = httpresponse.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = httpresponse.getEntity();
// --------------------------------------------------------------
// Wait for response from server (polling)
// --------------------------------------------------------------
try {
InputStreamReader stream = new InputStreamReader(entity.getContent());
BufferedReader bufferedReader = new BufferedReader(stream);
String line = null;
while ((line = bufferedReader.readLine()) != null) {
hftResponse response = mapper.readValue(line, hftResponse.class);
if (response.getAuthorizationChallengeResponse != null){
challenge = response.getAuthorizationChallengeResponse.challenge;
return null;
}
if (response.getAuthorizationTokenResponse != null){
token = response.getAuthorizationTokenResponse.token;
return null;
}
if (response.getHistoricalPriceResponse != null){
if (response.getHistoricalPriceResponse.candle != null){
for (candleTick tick : response.getHistoricalPriceResponse.candle){
System.out.println("Security: " + tick.security + " tinterface: " + tick.tinterface + " TimeStamp: " + tick.timestamp + " Side: " + tick.side + " Open: " + tick.open + " High: " + tick.high + " Low: " + tick.low + " Close: " + tick.close + " Ticks: " + tick.ticks);
}
}
if (response.getHistoricalPriceResponse.message != null){
System.out.println("Message from server: " + response.getHistoricalPriceResponse.message);
}
}
}
}
catch (IOException e) { e.printStackTrace(); }
catch (Exception e) { e.printStackTrace(); }
return null;
} else {
throw new ClientProtocolException("Unexpected response status: " + status);
}
}
};
try {
hftRequest hftrequest;
StringEntity request;
HttpPost httpRequest;
// get challenge
hftrequest = new hftRequest(user);
mapper.setSerializationInclusion(Inclusion.NON_NULL);
mapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
request = new StringEntity(mapper.writeValueAsString(hftrequest));
System.out.println(mapper.writeValueAsString(hftrequest));
httpRequest = new HttpPost(domain + ":" + authentication_port + url_challenge);
httpRequest.setEntity(request);
client.execute(httpRequest, responseHandler);
// create challenge response
byte[] a = Hex.decodeHex(challenge.toCharArray());
byte[] b = password.getBytes();
byte[] c = new byte[a.length + b.length];
System.arraycopy(a, 0, c, 0, a.length);
System.arraycopy(b, 0, c, a.length, b.length);
byte[] d = DigestUtils.sha1(c);
String challengeresp = Hex.encodeHexString(d);
// get token with challenge response
hftrequest = new hftRequest(user, challengeresp);
mapper.setSerializationInclusion(Inclusion.NON_NULL);
mapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
request = new StringEntity(mapper.writeValueAsString(hftrequest));
System.out.println(mapper.writeValueAsString(hftrequest));
httpRequest = new HttpPost(domain + ":" + authentication_port + url_token);
httpRequest.setEntity(request);
client.execute(httpRequest, responseHandler);
// -----------------------------------------
// Prepare and send a getHistoricalPrices request
// -----------------------------------------
hftrequest = new hftRequest(user, token, Arrays.asList("EUR/USD", "GBP/USD"), null, "s1", "ask", 3);
mapper.setSerializationInclusion(Inclusion.NON_NULL);
mapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
request = new StringEntity(mapper.writeValueAsString(hftrequest));
System.out.println(mapper.writeValueAsString(hftrequest));
httpRequest = new HttpPost(domain + ":" + request_port + url_polling + URL);
httpRequest.setEntity(request);
client.execute(httpRequest, responseHandler);
} finally {
client.close();
}
}
public static void getProperties(){
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream("config.properties");
prop.load(input);
//url_stream = prop.getProperty("url-stream");
url_polling = prop.getProperty("url-polling");
url_challenge = prop.getProperty("url-challenge");
url_token = prop.getProperty("url-token");
user = prop.getProperty("user");
password = prop.getProperty("password");
//interval = Integer.parseInt(prop.getProperty("interval"));
if (ssl){
domain = prop.getProperty("ssl-domain");
authentication_port = prop.getProperty("ssl-authentication-port");
request_port = prop.getProperty("ssl-request-port");
ssl_cert = prop.getProperty("ssl-cert");
}
else{
domain = prop.getProperty("domain");
authentication_port = prop.getProperty("authentication-port");
request_port = prop.getProperty("request-port");
}
}
catch (IOException ex) {
ex.printStackTrace();
}
finally {
if (input != null) {
try {
input.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
public getHistoricalPrice() {
super();
}
}
You can’t perform that action at this time.
