-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathDXFeedConnect.java
More file actions
142 lines (130 loc) · 5.42 KB
/
DXFeedConnect.java
File metadata and controls
142 lines (130 loc) · 5.42 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
/*
* !++
* QDS - Quick Data Signalling Library
* !-
* Copyright (C) 2002 - 2021 Devexperts LLC
* !-
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at
* http://mozilla.org/MPL/2.0/.
* !__
*/
package com.dxfeed.sample.api;
import com.devexperts.util.TimeFormat;
import com.dxfeed.api.DXEndpoint;
import com.dxfeed.api.DXFeed;
import com.dxfeed.api.DXFeedSubscription;
import com.dxfeed.api.DXFeedTimeSeriesSubscription;
import com.dxfeed.event.EventType;
import com.dxfeed.event.TimeSeriesEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class DXFeedConnect {
public static void main(String[] args) {
if (args.length < 2) {
String eventTypeNames = getEventTypeNames(EventType.class);
System.err.println("usage: DXFeedConnect <types> <symbols> [<time>]");
System.err.println("where: <types> is comma-separated list of dxfeed event type (" + eventTypeNames + ")");
System.err.println(" <symbols> is comma-separated list of security symbols to get events for (e.g. \"IBM,C,SPX\")");
System.err.println(" for Candle event specify symbol with aggregation like in \"IBM{=15m}\"");
System.err.println(" <time> is a fromTime for time-series subscription");
return;
}
String argTypes = args[0];
String argSymbols = args[1];
String argTime = args.length > 2 ? args[2] : null;
String[] symbols = parseSymbols(argSymbols);
try {
for (String type : argTypes.split(",")) {
if (argTime != null) {
connectTimeSeriesEvent(type, argTime, symbols);
} else {
connectEvent(type, symbols);
}
}
Thread.sleep(Long.MAX_VALUE);
} catch (Exception e) {
e.printStackTrace();
System.exit(1); // shutdown on any error
}
}
private static String[] parseSymbols(String symbolList) {
List<String> result = new ArrayList<>();
int parentheses = 0; // # of encountered parentheses of any type
StringBuilder sb = new StringBuilder();
for (int i = 0; i < symbolList.length(); i++) {
char ch = symbolList.charAt(i);
switch (ch) {
case '{': case '(': case '[':
parentheses++;
sb.append(ch);
break;
case '}': case ')': case ']':
if (parentheses > 0)
parentheses--;
sb.append(ch);
break;
case ',':
if (parentheses == 0) {
// not in parenthesis -- comma is a symbol list separator
result.add(sb.toString());
sb.setLength(0);
} else {
sb.append(ch);
}
break;
default:
sb.append(ch);
}
}
result.add(sb.toString());
return result.toArray(new String[result.size()]);
}
private static void connectEvent(String type, String... symbols) {
Class<?> eventType = findEventType(type, EventType.class);
DXFeedSubscription<Object> sub = DXFeed.getInstance().createSubscription(eventType);
sub.addEventListener(DXFeedConnect::printEvents);
sub.addSymbols(symbols);
}
private static void connectTimeSeriesEvent(String type, String fromTime, String... symbols) {
Class<TimeSeriesEvent<?>> eventType = findEventType(type, TimeSeriesEvent.class);
long from = TimeFormat.DEFAULT.parse(fromTime).getTime();
DXFeedTimeSeriesSubscription<TimeSeriesEvent<?>> sub =
DXFeed.getInstance().createTimeSeriesSubscription(eventType);
sub.addEventListener(DXFeedConnect::printEvents);
sub.setFromTime(from);
sub.addSymbols(symbols);
}
private static void printEvents(List<?> events) {
for (Object event : events) {
System.out.println(event);
}
}
// ---- Utility methods to make this sample generic for use with any event type as specified on command line ----
public static String getEventTypeNames(Class<?> baseClass) {
StringBuilder sb = new StringBuilder();
for (String s : getEventTypesMap(baseClass).keySet()) {
if (sb.length() > 0)
sb.append(", ");
sb.append(s);
}
return sb.toString();
}
@SuppressWarnings("unchecked")
public static <T> Class<T> findEventType(String type, Class<? super T> baseClass) {
Class<?> result = getEventTypesMap(baseClass).get(type);
if (result == null)
throw new IllegalArgumentException("Cannot find " + baseClass.getSimpleName() + " '" + type + "'");
return (Class<T>) result;
}
private static Map<String, Class<?>> getEventTypesMap(Class<?> baseClass) {
TreeMap<String, Class<?>> result = new TreeMap<>();
for (Class<?> eventType : DXEndpoint.getInstance().getEventTypes()) {
if (baseClass.isAssignableFrom(eventType))
result.put(eventType.getSimpleName(), eventType);
}
return result;
}
}