This repository was archived by the owner on Aug 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathTestRealTimeBars.java
More file actions
377 lines (324 loc) · 9.58 KB
/
Copy pathTestRealTimeBars.java
File metadata and controls
377 lines (324 loc) · 9.58 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package com.sample.realtimebars;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import com.ib.client.Contract;
import com.ib.client.ContractDetails;
import com.ib.client.EClientSocket;
import com.ib.client.EWrapper;
import com.ib.client.Execution;
import com.ib.client.Order;
import com.ib.client.OrderState;
public class TestRealTimeBars implements EWrapper
{
public Hashtable<Integer, DataHolder> ht = new Hashtable<Integer, DataHolder> ();
private int nextSymbolID = -1;
private EClientSocket client = null;
public TestRealTimeBars ()
{
client = new EClientSocket (this);
client.eConnect (null, 7600, 0);
try {Thread.sleep (1000);} catch (Exception e) {};
}
public void bondContractDetails (int reqId, ContractDetails contractDetails)
{
}
public void contractDetails (int reqId, ContractDetails contractDetails)
{
}
public void contractDetailsEnd (int reqId)
{
}
public void fundamentalData (int reqId, String data)
{
}
public void bondContractDetails (ContractDetails contractDetails)
{
}
public void contractDetails (ContractDetails contractDetails)
{
}
public void currentTime (long time)
{
}
public void execDetails (int orderId, Contract contract, Execution execution)
{
}
public void historicalData (int reqId, String date, double open,
double high, double low, double close, int volume, int count,
double WAP, boolean hasGaps)
{
}
public void managedAccounts (String accountsList)
{
}
public void openOrder (int orderId, Contract contract, Order order,
OrderState orderState)
{
}
public void orderStatus (int orderId, String status, int filled,
int remaining, double avgFillPrice, int permId, int parentId,
double lastFillPrice, int clientId, String whyHeld)
{
}
public void receiveFA (int faDataType, String xml)
{
}
public void scannerData (int reqId, int rank,
ContractDetails contractDetails, String distance, String benchmark,
String projection, String legsStr)
{
}
public void scannerDataEnd (int reqId)
{
}
public void scannerParameters (String xml)
{
}
public void tickEFP (int symbolId, int tickType, double basisPoints,
String formattedBasisPoints, double impliedFuture, int holdDays,
String futureExpiry, double dividendImpact, double dividendsToExpiry)
{
}
public void tickGeneric (int symbolId, int tickType, double value)
{
}
public void tickOptionComputation (int symbolId, int field,
double impliedVol, double delta, double modelPrice,
double pvDividend)
{
}
public void tickPrice (int symbolId, int field, double price,
int canAutoExecute)
{
}
public void tickSize (int symbolId, int field, int size)
{
}
public void tickString (int symbolId, int tickType, String value)
{
}
public void updateAccountTime (String timeStamp)
{
}
public void updateAccountValue (String key, String value, String currency,
String accountName)
{
}
public void updateMktDepth (int symbolId, int position, int operation,
int side, double price, int size)
{
}
public void updateMktDepthL2 (int symbolId, int position,
String marketMaker, int operation, int side, double price, int size)
{
}
public void updateNewsBulletin (int msgId, int msgType, String message,
String origExchange)
{
}
public void updatePortfolio (Contract contract, int position,
double marketPrice, double marketValue, double averageCost,
double unrealizedPNL, double realizedPNL, String accountName)
{
}
public void connectionClosed ()
{
}
public void error (Exception e)
{
e.printStackTrace ();
}
public void error (String str)
{
System.err.println (str);
}
public void error (int id, int errorCode, String errorMsg)
{
System.err.println ("error (id, errorCode, errorMsg): id=" + id + ". errorCode=" + errorCode + ". errorMsg=" + errorMsg);
}
public void nextValidId (int orderId)
{
nextSymbolID = orderId;
}
public synchronized int getNextID ()
{
return (nextSymbolID++);
}
public void realtimeBar (int reqId, long time, double open, double high,
double low, double close, long volume, double wap, int count)
{
try
{
DataHolder holder = ht.get (reqId);
if (holder != null)
holder.newBar ();
}
catch (Exception e)
{
e.printStackTrace ();
}
}
private class DataHolder
{
private int requestID = 0;
private Contract contract = null;
private long lastBarReceived = 0;
public DataHolder (Contract contract, int requestID)
{
this.requestID = requestID;
this.contract = contract;
}
public void newBar ()
{
lastBarReceived = System.currentTimeMillis ();
}
public long getLastBarReceived ()
{
return (lastBarReceived);
}
public int getRequestID ()
{
return (requestID);
}
public Contract getContract ()
{
return (contract);
}
}
// This simply requests to monitor all symbols (calling reqRealTimeBars () every
// 300 millis).
public void doit ()
{
String symbols[] = getSymbols ();
System.out.println ("ATTEMPTING TO MONITOR " + symbols.length + " SYMBOLS");
for (int i=0; i<symbols.length; i++)
{
Contract contract = new Contract ();
contract.m_symbol = symbols[i];
contract.m_exchange = "SMART";
contract.m_secType = "STK";
DataHolder holder = new DataHolder (contract, getNextID ());
try {Thread.sleep (300);} catch (Exception e) {};
System.out.println ("Requesting: " + holder.getRequestID () + ". " + holder.getContract ().m_symbol);
client.reqRealTimeBars (holder.getRequestID (), holder.getContract (), 5, "TRADES", false);
ht.put (holder.getRequestID (), holder);
}
// Now that all symbols are being monitored, let's just watch to how many
// are actually receiving data.
while (true)
{
try {Thread.sleep (1000);} catch (Exception e) {};
System.out.println (getSymbolsReceivingData ().size () + " symbols receiving data");
}
}
public static void main (String args[])
{
try
{
TestRealTimeBars test = new TestRealTimeBars ();
test.doit ();
}
catch (Exception e)
{
e.printStackTrace ();
}
}
private ArrayList<String> getSymbolsReceivingData ()
{
Enumeration<Integer> en = ht.keys ();
ArrayList<String> symbols = new ArrayList<String> ();
try
{
while (en.hasMoreElements ())
{
DataHolder holder = ht.get (en.nextElement ());
if (holder != null)
{
if (holder.getLastBarReceived () != 0)
{
if (holder.getLastBarReceived () + 1000*30 >= System.currentTimeMillis ())
symbols.add (holder.getContract ().m_symbol);
}
}
}
}
catch (Exception e)
{
return (symbols);
}
return (symbols);
}
public static String[] getSymbols ()
{
String str[] = {
"MSFT",
"INTC",
"ORCL",
"WNR",
"WMT",
"WM",
"WFR",
"WFC",
"WDC",
"WB", // 10
"WAG",
"VOD",
"VLO",
"VIV",
"USB",
"UPL",
"UNP",
"UNM",
"UNH",
"UMC", // 20
"UIS",
"UFS",
"UBS",
"TYC",
"TMA",
"TLM",
"TJX",
"TIN",
"TIF",
"TIE", // 30
"THC",
"TGT",
"TEX",
"TER",
"TCB",
"TAP",
"YUM",
"YGE",
"XTO",
"XRX", // 40
"XOM",
"XL",
"T",
"SYY",
"SWY",
"SWN",
"SWC",
"SUN",
"SU",
"STX", // 50
"STT",
"STP",
"STM",
"STI",
"SRP",
"SPR",
"SPF",
"SOV",
"SO",
"SNV", //60
"SNE",
"SLW",
"SLT",
"SLM",
"SLE",
"SLB" // 66
};
return (str);
}
}