/
Layer1ApiMarkersDemo.java
459 lines (391 loc) · 20.3 KB
/
Layer1ApiMarkersDemo.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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
package velox.api.layer1.simpledemo.markers;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import velox.api.layer1.Layer1ApiAdminAdapter;
import velox.api.layer1.Layer1ApiFinishable;
import velox.api.layer1.Layer1ApiInstrumentListener;
import velox.api.layer1.Layer1ApiProvider;
import velox.api.layer1.Layer1CustomPanelsGetter;
import velox.api.layer1.annotations.Layer1ApiVersion;
import velox.api.layer1.annotations.Layer1ApiVersionValue;
import velox.api.layer1.annotations.Layer1Attachable;
import velox.api.layer1.annotations.Layer1StrategyName;
import velox.api.layer1.common.ListenableHelper;
import velox.api.layer1.common.Log;
import velox.api.layer1.data.ExecutionInfo;
import velox.api.layer1.data.InstrumentInfo;
import velox.api.layer1.data.OrderInfoUpdate;
import velox.api.layer1.data.OrderStatus;
import velox.api.layer1.data.OrderType;
import velox.api.layer1.data.TradeInfo;
import velox.api.layer1.datastructure.events.OrderExecutedEvent;
import velox.api.layer1.datastructure.events.OrderUpdatedEvent;
import velox.api.layer1.datastructure.events.OrderUpdatesExecutionsAggregationEvent;
import velox.api.layer1.datastructure.events.TradeAggregationEvent;
import velox.api.layer1.layers.strategies.interfaces.CalculatedResultListener;
import velox.api.layer1.layers.strategies.interfaces.InvalidateInterface;
import velox.api.layer1.layers.strategies.interfaces.Layer1IndicatorColorInterface;
import velox.api.layer1.layers.strategies.interfaces.OnlineCalculatable;
import velox.api.layer1.layers.strategies.interfaces.OnlineValueCalculatorAdapter;
import velox.api.layer1.messages.UserMessageLayersChainCreatedTargeted;
import velox.api.layer1.messages.indicators.DataStructureInterface;
import velox.api.layer1.messages.indicators.DataStructureInterface.StandardEvents;
import velox.api.layer1.messages.indicators.DataStructureInterface.TreeResponseInterval;
import velox.api.layer1.messages.indicators.IndicatorColorInterface;
import velox.api.layer1.messages.indicators.IndicatorColorScheme;
import velox.api.layer1.messages.indicators.IndicatorLineStyle;
import velox.api.layer1.messages.indicators.Layer1ApiDataInterfaceRequestMessage;
import velox.api.layer1.messages.indicators.Layer1ApiUserMessageModifyIndicator;
import velox.api.layer1.messages.indicators.SettingsAccess;
import velox.api.layer1.settings.Layer1ConfigSettingsInterface;
import velox.api.layer1.messages.indicators.Layer1ApiUserMessageModifyIndicator.GraphType;
import velox.colors.ColorsChangedListener;
import velox.gui.StrategyPanel;
import velox.gui.colors.Colors;
import velox.gui.colors.ColorsConfigItem;
@Layer1Attachable
@Layer1StrategyName("Markers demo")
@Layer1ApiVersion(Layer1ApiVersionValue.VERSION2)
public class Layer1ApiMarkersDemo implements
Layer1ApiFinishable,
Layer1ApiAdminAdapter,
Layer1ApiInstrumentListener, OnlineCalculatable,
Layer1CustomPanelsGetter,
Layer1ConfigSettingsInterface,
Layer1IndicatorColorInterface {
private static final String INDICATOR_NAME_TRADE = "Trade markers";
private static final String INDICATOR_NAME_CIRCLES = "Order markers";
private static final String INDICATOR_LINE_COLOR_NAME = "Trade markers line";
private static final Color INDICATOR_LINE_DEFAULT_COLOR = Color.RED;
private static final String INDICATOR_CIRCLES_COLOR_NAME = "Markers order circles";
private static final Color INDICATOR_CIRCLES_DEFAULT_COLOR = Color.GREEN;
private Layer1ApiProvider provider;
private Map<String, MarkersDemoSettings> settingsMap = new HashMap<>();
private SettingsAccess settingsAccess;
private Map<String, String> indicatorsFullNameToUserName = new HashMap<>();
private Map<String, String> indicatorsUserNameToFullName = new HashMap<>();
private Map<String, InvalidateInterface> invalidateInterfaceMap = new ConcurrentHashMap<>();
private Map<String, Double> pipsMap = new ConcurrentHashMap<>();
private DataStructureInterface dataStructureInterface;
private BufferedImage tradeIcon = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
private Map<String, BufferedImage> orderIcons = Collections.synchronizedMap(new HashMap<>());
private Object locker = new Object();
public Layer1ApiMarkersDemo(Layer1ApiProvider provider) {
this.provider = provider;
ListenableHelper.addListeners(provider, this);
// Prepare trade marker
Graphics graphics = tradeIcon.getGraphics();
graphics.setColor(Color.BLUE);
graphics.drawLine(0, 0, 15, 15);
graphics.drawLine(15, 0, 0, 15);
}
@Override
public void finish() {
synchronized (indicatorsFullNameToUserName) {
for (String userName: indicatorsFullNameToUserName.values()) {
provider.sendUserMessage(new Layer1ApiUserMessageModifyIndicator(Layer1ApiMarkersDemo.class, userName, false));
}
}
invalidateInterfaceMap.clear();
}
private Layer1ApiUserMessageModifyIndicator getUserMessageAdd(String userName,
IndicatorLineStyle lineStyle, boolean isAddWidget) {
return new Layer1ApiUserMessageModifyIndicator(Layer1ApiMarkersDemo.class, userName, true,
new IndicatorColorScheme() {
@Override
public ColorDescription[] getColors() {
return new ColorDescription[] {
new ColorDescription(Layer1ApiMarkersDemo.class, INDICATOR_LINE_COLOR_NAME, INDICATOR_LINE_DEFAULT_COLOR, false),
new ColorDescription(Layer1ApiMarkersDemo.class, INDICATOR_CIRCLES_COLOR_NAME, INDICATOR_CIRCLES_DEFAULT_COLOR, false)
};
}
@Override
public String getColorFor(Double value) {
return INDICATOR_LINE_COLOR_NAME;
}
@Override
public ColorIntervalResponse getColorIntervalsList(double valueFrom, double valueTo) {
return new ColorIntervalResponse(new String[] {INDICATOR_LINE_COLOR_NAME}, new double[] {});
}
}, Layer1ApiMarkersDemo.this, lineStyle, Color.white, Color.black, null,
null, null, null, null, GraphType.PRIMARY, isAddWidget, false, null, this, null);
}
@Override
public void onUserMessage(Object data) {
if (data.getClass() == UserMessageLayersChainCreatedTargeted.class) {
UserMessageLayersChainCreatedTargeted message = (UserMessageLayersChainCreatedTargeted) data;
if (message.targetClass == getClass()) {
provider.sendUserMessage(new Layer1ApiDataInterfaceRequestMessage(dataStructureInterface -> this.dataStructureInterface = dataStructureInterface));
addIndicator(INDICATOR_NAME_TRADE);
addIndicator(INDICATOR_NAME_CIRCLES);
}
}
}
@Override
public void onInstrumentAdded(String alias, InstrumentInfo instrumentInfo) {
pipsMap.put(alias, instrumentInfo.pips);
reloadOrderIcon(alias);
}
@Override
public void onInstrumentRemoved(String alias) {
}
@Override
public void onInstrumentNotFound(String symbol, String exchange, String type) {
}
@Override
public void onInstrumentAlreadySubscribed(String symbol, String exchange, String type) {
}
@Override
public void calculateValuesInRange(String indicatorName, String alias, long t0, long intervalWidth, int intervalsNumber,
CalculatedResultListener listener) {
String userName = indicatorsFullNameToUserName.get(indicatorName);
switch (userName) {
case INDICATOR_NAME_TRADE: {
ArrayList<TreeResponseInterval> intervalResponse = dataStructureInterface.get(t0, intervalWidth, intervalsNumber, alias,
new StandardEvents[] {StandardEvents.TRADE});
double lastPrice = ((TradeAggregationEvent) intervalResponse.get(0).events.get(StandardEvents.TRADE.toString())).lastPrice;
for (int i = 1; i <= intervalsNumber; ++i) {
TradeAggregationEvent trades = (TradeAggregationEvent)intervalResponse.get(i).events.get(StandardEvents.TRADE.toString());
if (!Double.isNaN(trades.lastPrice)) {
lastPrice = trades.lastPrice;
}
if (trades.askAggressorMap.isEmpty() && trades.bidAggressorMap.isEmpty()) {
listener.provideResponse(lastPrice);
} else {
listener.provideResponse(new Marker(lastPrice, -tradeIcon.getHeight() / 2, -tradeIcon.getWidth() / 2, tradeIcon));
}
}
listener.setCompleted();
break;
} case INDICATOR_NAME_CIRCLES: {
ArrayList<TreeResponseInterval> intervalResponse = dataStructureInterface.get(t0, intervalWidth, intervalsNumber, alias,
new StandardEvents[] {StandardEvents.ORDER});
for (int i = 1; i <= intervalsNumber; ++i) {
OrderUpdatesExecutionsAggregationEvent orders = (OrderUpdatesExecutionsAggregationEvent) intervalResponse.get(i).events.get(StandardEvents.ORDER.toString());
ArrayList<Marker> result = new ArrayList<>();
BufferedImage orderIcon = orderIcons.get(alias);
for (Object object: orders.orderUpdates) {
if (object instanceof OrderExecutedEvent) {
OrderExecutedEvent orderExecutedEvent = (OrderExecutedEvent) object;
result.add(new Marker(orderExecutedEvent.executionInfo.price / pipsMap.getOrDefault(orderExecutedEvent.alias, 1.),
-orderIcon.getHeight() / 2, -orderIcon.getWidth() / 2, orderIcon));
} else if (object instanceof OrderUpdatedEvent) {
OrderUpdatedEvent orderUpdatedEvent = (OrderUpdatedEvent) object;
if (orderUpdatedEvent.orderInfoUpdate.status == OrderStatus.CANCELLED) {
result.add(new Marker(getActivePrice(orderUpdatedEvent.orderInfoUpdate) / pipsMap.getOrDefault(orderUpdatedEvent.alias, 1.),
-orderIcon.getHeight() / 2, -orderIcon.getWidth() / 2, orderIcon));
}
}
}
listener.provideResponse(result);
}
listener.setCompleted();
break;
} default:
throw new IllegalArgumentException("Unknown indicator name " + indicatorName);
}
}
private double getActivePrice(OrderInfoUpdate orderInfoUpdate) {
return (orderInfoUpdate.type == OrderType.STP || orderInfoUpdate.type == OrderType.STP_LMT)
? orderInfoUpdate.stopPrice : orderInfoUpdate.limitPrice;
}
@Override
public OnlineValueCalculatorAdapter createOnlineValueCalculator(String indicatorName, String indicatorAlias, long time,
Consumer<Object> listener, InvalidateInterface invalidateInterface) {
String userName = indicatorsFullNameToUserName.get(indicatorName);
invalidateInterfaceMap.put(userName, invalidateInterface);
BufferedImage orderIcon = orderIcons.get(indicatorAlias);
switch (userName) {
case INDICATOR_NAME_TRADE:
return new OnlineValueCalculatorAdapter() {
@Override
public void onTrade(String alias, double price, int size, TradeInfo tradeInfo) {
if (alias.equals(indicatorAlias)) {
listener.accept(new Marker(price, -tradeIcon.getHeight() / 2, -tradeIcon.getWidth() / 2, tradeIcon));
}
}
};
case INDICATOR_NAME_CIRCLES:
return new OnlineValueCalculatorAdapter() {
private Map<String, String> orderIdToAlias = new HashMap<>();
@Override
public void onOrderExecuted(ExecutionInfo executionInfo) {
String alias = orderIdToAlias.get(executionInfo.orderId);
if (alias != null) {
if (alias.equals(indicatorAlias)) {
Double pips = pipsMap.get(alias);
if (pips != null) {
listener.accept(new Marker(executionInfo.price / pips, -orderIcon.getHeight() / 2, -orderIcon.getWidth() / 2, orderIcon));
} else {
Log.info("Unknown pips for instrument " + alias);
}
}
} else {
Log.warn("Markers demo: Unknown alias for execution with order id " + executionInfo.orderId);
}
}
@Override
public void onOrderUpdated(OrderInfoUpdate orderInfoUpdate) {
if (orderInfoUpdate.instrumentAlias.equals(indicatorAlias)) {
if (orderInfoUpdate.status == OrderStatus.CANCELLED ||
orderInfoUpdate.status == OrderStatus.DISCONNECTED) {
Double pips = pipsMap.get(orderInfoUpdate.instrumentAlias);
if (pips != null) {
listener.accept(new Marker(getActivePrice(orderInfoUpdate) / pips, -orderIcon.getHeight() / 2, -orderIcon.getWidth() / 2, orderIcon));
} else {
Log.info("Unknown pips for instrument " + orderInfoUpdate.instrumentAlias);
}
}
}
orderIdToAlias.put(orderInfoUpdate.orderId, orderInfoUpdate.instrumentAlias);
}
};
default:
throw new IllegalArgumentException("Unknown indicator name " + indicatorName);
}
}
@Override
public StrategyPanel[] getCustomGuiFor(String alias, String indicatorName) {
StrategyPanel panel = new StrategyPanel("Colors", new GridBagLayout());
panel.setLayout(new GridBagLayout());
GridBagConstraints gbConst;
IndicatorColorInterface indicatorColorInterface = new IndicatorColorInterface() {
@Override
public void set(String name, Color color) {
setColor(alias, name, color);
}
@Override
public Color getOrDefault(String name, Color defaultValue) {
Color color = getSettingsFor(alias).getColor(name);
return color == null ? defaultValue : color;
}
@Override
public void addColorChangeListener(ColorsChangedListener listener) {
}
};
ColorsConfigItem configItemLines = new ColorsConfigItem(INDICATOR_LINE_COLOR_NAME, INDICATOR_LINE_COLOR_NAME, true,
INDICATOR_LINE_DEFAULT_COLOR, indicatorColorInterface, new ColorsChangedListener() {
@Override
public void onColorsChanged() {
InvalidateInterface invalidaInterface = invalidateInterfaceMap.get(INDICATOR_NAME_TRADE);
if (invalidaInterface != null) {
invalidaInterface.invalidate();
}
}
});
ColorsConfigItem configItemCircles = new ColorsConfigItem(INDICATOR_CIRCLES_COLOR_NAME, INDICATOR_CIRCLES_COLOR_NAME, true,
INDICATOR_CIRCLES_DEFAULT_COLOR, indicatorColorInterface, new ColorsChangedListener() {
@Override
public void onColorsChanged() {
reloadOrderIcon(alias);
InvalidateInterface invalidaInterface = invalidateInterfaceMap.get(INDICATOR_NAME_CIRCLES);
if (invalidaInterface != null) {
invalidaInterface.invalidate();
}
}
});
gbConst = new GridBagConstraints();
gbConst.gridx = 0;
gbConst.gridy = 0;
gbConst.weightx = 1;
gbConst.insets = new Insets(5, 5, 5, 5);
gbConst.fill = GridBagConstraints.HORIZONTAL;
panel.add(configItemLines, gbConst);
gbConst = new GridBagConstraints();
gbConst.gridx = 0;
gbConst.gridy = 1;
gbConst.weightx = 1;
gbConst.insets = new Insets(5, 5, 5, 5);
gbConst.fill = GridBagConstraints.HORIZONTAL;
panel.add(configItemCircles, gbConst);
return new StrategyPanel[] {panel};
}
private void reloadOrderIcon(String alias) {
BufferedImage orderIcon = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
Graphics graphics = orderIcon.getGraphics();
graphics.setColor(Colors.TRANSPARENT);
graphics.fillRect(0, 0, 15, 15);
graphics.setColor(getColor(alias, INDICATOR_CIRCLES_COLOR_NAME));
graphics.drawOval(0, 0, 15, 15);
orderIcons.put(alias, orderIcon);
}
public void addIndicator(String userName) {
Layer1ApiUserMessageModifyIndicator message = null;
switch (userName) {
case INDICATOR_NAME_TRADE:
message = getUserMessageAdd(userName, IndicatorLineStyle.SHORT_DASHES_WIDE_LEFT_NARROW_RIGHT, true);
break;
case INDICATOR_NAME_CIRCLES:
message = getUserMessageAdd(userName, IndicatorLineStyle.NONE, true);
break;
default:
Log.warn("Unknwon name for marker indicator: " + userName);
break;
}
if (message != null) {
synchronized (indicatorsFullNameToUserName) {
indicatorsFullNameToUserName.put(message.fullName, message.userName);
indicatorsUserNameToFullName.put(message.userName, message.fullName);
}
provider.sendUserMessage(message);
}
}
@Override
public void acceptSettingsInterface(SettingsAccess settingsAccess) {
this.settingsAccess = settingsAccess;
}
private MarkersDemoSettings getSettingsFor(String alias) {
synchronized (locker) {
MarkersDemoSettings settings = settingsMap.get(alias);
if (settings == null) {
settings = (MarkersDemoSettings) settingsAccess.getSettings(alias, INDICATOR_NAME_CIRCLES, MarkersDemoSettings.class);
settingsMap.put(alias, settings);
}
return settings;
}
}
protected void settingsChanged(String settingsAlias, MarkersDemoSettings settingsObject) {
synchronized (locker) {
settingsAccess.setSettings(settingsAlias, INDICATOR_NAME_CIRCLES, settingsObject, settingsObject.getClass());
}
}
@Override
public void setColor(String alias, String name, Color color) {
MarkersDemoSettings settings = getSettingsFor(alias);
settings.setColor(name, color);
settingsChanged(alias, settings);
}
@Override
public Color getColor(String alias, String name) {
Color color = getSettingsFor(alias).getColor(name);
if (color == null) {
switch (name) {
case INDICATOR_LINE_COLOR_NAME:
color = INDICATOR_LINE_DEFAULT_COLOR;
break;
case INDICATOR_CIRCLES_COLOR_NAME:
color = INDICATOR_CIRCLES_DEFAULT_COLOR;
break;
default:
Log.warn("Layer1ApiMarkersDemo: unknown color name " + name);
color = Color.WHITE;
break;
}
}
return color;
}
@Override
public void addColorChangeListener(ColorsChangedListener listener) {
// every one of our colors is modified only from one place
}
}