-
Notifications
You must be signed in to change notification settings - Fork 1
/
PerExchangeMetadataSampleAlgorithm.java
123 lines (95 loc) · 4.68 KB
/
PerExchangeMetadataSampleAlgorithm.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
package deltix.ember.service.algorithm.samples.lp;
import com.epam.deltix.dfp.Decimal;
import com.epam.deltix.dfp.Decimal64Utils;
import deltix.anvil.util.CharSequenceUtil;
import deltix.anvil.util.Factory;
import deltix.anvil.util.annotation.Alphanumeric;
import deltix.ember.message.smd.InstrumentType;
import deltix.ember.message.smd.InstrumentUpdate;
import deltix.ember.service.algorithm.*;
import deltix.ember.service.algorithm.md.*;
import deltix.ember.service.oms.cache.OrdersCacheSettings;
import javax.annotation.Nullable;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// This example shows how an algorithm can track an imaginary per-exchange security metadata property myCustomField //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@SuppressWarnings("unused") // this is just a sample
public class PerExchangeMetadataSampleAlgorithm extends AbstractAlgorithm<AlgoOrder, InstrumentDataWithPerExchangeState> {
PerExchangeMetadataSampleAlgorithm(AlgorithmContext context, OrdersCacheSettings cacheSettings) {
super(context, cacheSettings);
}
/**
* This method shows how to access some custom attribute defined for exchange specific security metadata record (like BTC/USD.COINBASE)
* @param symbol Instrument symbol (e.g. BTC/USD)
* @param exchangeId exchange (as alphanumeric-encoded value), for example COINBASE
* @return value of "customField" attribute (defined for BTC/USD.COINBASE) or NULL
*/
@Decimal
public long getMyCustomField (CharSequence symbol, @Alphanumeric long exchangeId) {
InstrumentDataWithPerExchangeState instrument = get(symbol);
return (instrument != null) ? instrument.getMyCustomField(exchangeId) : Decimal64Utils.NULL;
}
/// region Factories
protected MarketDataProcessor<InstrumentDataWithPerExchangeState> createMarketDataProcessor(@Nullable MarketSubscription subscription) {
return new PerExchangeMarketDataProcessor<>(subscription, 128, createInstrumentDataFactory(), context.getErrorThrottlerManager());
}
@Override
protected InstrumentDataFactory<InstrumentDataWithPerExchangeState> createInstrumentDataFactory() {
return InstrumentDataWithPerExchangeState::new;
}
@Override
protected Factory<AlgoOrder> createParentOrderFactory() {
return AlgoOrder::new;
}
@Override
protected Factory<ChildOrder<AlgoOrder>> createChildOrderFactory() {
return ChildOrder::new;
}
/// endregion
}
/** Defines per-instrument data structure that we want to keep. Will also keep per-exchange MyPerExchangeInstrumentMetadata */
class InstrumentDataWithPerExchangeState extends AbstractInstrumentDataEx<MyPerExchangeInstrumentMetadata> {
public InstrumentDataWithPerExchangeState(final CharSequence symbol, final InstrumentType instrumentType) {
super (symbol, instrumentType);
}
@Override
protected MyPerExchangeInstrumentMetadata createPerExchangeInstrument(@Alphanumeric long exchangeId) {
return new MyPerExchangeInstrumentMetadata(exchangeId);
}
public long getMyCustomField(@Alphanumeric long exchangeId) {
MyPerExchangeInstrumentMetadata lpInfo = getPerExchangeInstrument(exchangeId);
@Decimal long result = Decimal64Utils.NULL;
if (lpInfo != null)
result = lpInfo.getMyCustomField();
return result;
}
}
/** This structure will keep per-exchange attributes of each instrument */
class MyPerExchangeInstrumentMetadata extends PerExchangeAlgoInstrumentData {
@Decimal
private long myCustomField; // this is just an example of custom attribute that comes from per-exchange security metadata
public MyPerExchangeInstrumentMetadata(@Alphanumeric long exchangeId) {
super(exchangeId);
}
@Decimal
public long getMyCustomField() {
return myCustomField;
}
public void setMyCustomField(@Decimal long myCustomField) {
this.myCustomField = myCustomField;
}
/**
* Will be called on each instrument update (in local "securities" TimeBase stream)
*
* @param instrument instrument that is loaded or updated in Timebase "securities" stream
* @param key custom attribute key
* @param value custom attribute value
*/
@Override
protected void updateCustomAttribute(InstrumentUpdate instrument, CharSequence key, CharSequence value) {
if (CharSequenceUtil.equals("customField", key))
setMyCustomField(Decimal64Utils.tryParse(value, Decimal64Utils.NULL));
else
super.updateCustomAttribute(instrument, key, value);
}
}