forked from StockSharp/StockSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Level1DepthBuilderAdapter.cs
294 lines (229 loc) · 6.66 KB
/
Level1DepthBuilderAdapter.cs
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
namespace StockSharp.Algo
{
using System.Collections.Generic;
using System.Linq;
using Ecng.Collections;
using Ecng.Common;
using StockSharp.Logging;
using StockSharp.Messages;
/// <summary>
/// Level1 depth builder adapter.
/// </summary>
public class Level1DepthBuilderAdapter : MessageAdapterWrapper
{
private sealed class Level1DepthBuilder
{
private decimal? _bidPrice, _askPrice, _bidVolume, _askVolume;
public Level1DepthBuilder(SecurityId securityId)
{
SecurityId = securityId;
}
public readonly SecurityId SecurityId;
public QuoteChangeMessage Process(Level1ChangeMessage message)
{
var bidPrice = message.TryGetDecimal(Level1Fields.BestBidPrice);
var askPrice = message.TryGetDecimal(Level1Fields.BestAskPrice);
if (bidPrice == null && askPrice == null)
return null;
var bidVolume = message.TryGetDecimal(Level1Fields.BestBidVolume);
var askVolume = message.TryGetDecimal(Level1Fields.BestAskVolume);
if (_bidPrice == bidPrice && _askPrice == askPrice && _bidVolume == bidVolume && _askVolume == askVolume)
return null;
_bidPrice = bidPrice;
_askPrice = askPrice;
_bidVolume = bidVolume;
_askVolume = askVolume;
return new QuoteChangeMessage
{
SecurityId = SecurityId,
ServerTime = message.ServerTime,
LocalTime = message.LocalTime,
BuildFrom = DataType.Level1,
Bids = bidPrice == null ? ArrayHelper.Empty<QuoteChange>() : new[] { new QuoteChange(bidPrice.Value, bidVolume ?? 0) },
Asks = askPrice == null ? ArrayHelper.Empty<QuoteChange>() : new[] { new QuoteChange(askPrice.Value, askVolume ?? 0) },
};
}
}
private class BookInfo
{
public BookInfo(Level1DepthBuilder builder) => Builder = builder;
public readonly Level1DepthBuilder Builder;
public readonly CachedSynchronizedSet<long> SubscriptionIds = new CachedSynchronizedSet<long>();
}
private readonly SyncObject _syncObject = new SyncObject();
private readonly Dictionary<long, BookInfo> _byId = new Dictionary<long, BookInfo>();
private readonly Dictionary<SecurityId, BookInfo> _online = new Dictionary<SecurityId, BookInfo>();
/// <summary>
/// Initializes a new instance of the <see cref="Level1DepthBuilderAdapter"/>.
/// </summary>
/// <param name="innerAdapter">Inner message adapter.</param>
public Level1DepthBuilderAdapter(IMessageAdapter innerAdapter)
: base(innerAdapter)
{
}
/// <inheritdoc />
public override bool SendInMessage(Message message)
{
switch (message.Type)
{
case MessageTypes.Reset:
{
lock (_syncObject)
{
_byId.Clear();
_online.Clear();
}
break;
}
case MessageTypes.MarketData:
{
var mdMsg = (MarketDataMessage)message;
if (mdMsg.IsSubscribe)
{
if (mdMsg.SecurityId == default || mdMsg.DataType2 != DataType.MarketDepth)
break;
if (mdMsg.BuildMode == MarketDataBuildModes.Load)
break;
if (mdMsg.BuildFrom != null && mdMsg.BuildFrom != DataType.Level1)
break;
var transId = mdMsg.TransactionId;
lock (_syncObject)
{
var info = new BookInfo(new Level1DepthBuilder(mdMsg.SecurityId));
info.SubscriptionIds.Add(transId);
_byId.Add(transId, info);
}
mdMsg = mdMsg.TypedClone();
mdMsg.DataType2 = DataType.Level1;
message = mdMsg;
this.AddDebugLog("L1->OB {0} added.", transId);
}
else
{
RemoveSubscription(mdMsg.OriginalTransactionId);
}
break;
}
}
return base.SendInMessage(message);
}
private void RemoveSubscription(long id)
{
lock (_syncObject)
{
var changeId = true;
if (!_byId.TryGetAndRemove(id, out var info))
{
changeId = false;
info = _online.FirstOrDefault(p => p.Value.SubscriptionIds.Contains(id)).Value;
if (info == null)
return;
}
var secId = info.Builder.SecurityId;
if (info != _online.TryGetValue(secId))
return;
info.SubscriptionIds.Remove(id);
var ids = info.SubscriptionIds.Cache;
if (ids.Length == 0)
_online.Remove(secId);
else if (changeId)
_byId.Add(ids[0], info);
}
this.AddDebugLog("L1->OB {0} removed.", id);
}
/// <inheritdoc />
protected override void OnInnerAdapterNewOutMessage(Message message)
{
List<QuoteChangeMessage> books = null;
switch (message.Type)
{
case MessageTypes.SubscriptionResponse:
{
var responseMsg = (SubscriptionResponseMessage)message;
if (!responseMsg.IsOk())
RemoveSubscription(responseMsg.OriginalTransactionId);
break;
}
case MessageTypes.SubscriptionFinished:
{
RemoveSubscription(((SubscriptionFinishedMessage)message).OriginalTransactionId);
break;
}
case MessageTypes.SubscriptionOnline:
{
var id = ((SubscriptionOnlineMessage)message).OriginalTransactionId;
lock (_syncObject)
{
if (_byId.TryGetValue(id, out var info))
{
var secId = info.Builder.SecurityId;
if (_online.TryGetValue(secId, out var online))
{
online.SubscriptionIds.Add(id);
_byId.Remove(id);
}
else
{
_online.Add(secId, info);
}
}
}
break;
}
case MessageTypes.Level1Change:
{
lock (_syncObject)
{
if (_byId.Count == 0 && _online.Count == 0)
break;
}
var level1Msg = (Level1ChangeMessage)message;
var ids = level1Msg.GetSubscriptionIds();
HashSet<long> leftIds = null;
lock (_syncObject)
{
foreach (var id in ids)
{
if (!_byId.TryGetValue(id, out var info))
continue;
var quoteMsg = info.Builder.Process(level1Msg);
if (quoteMsg == null)
continue;
quoteMsg.SetSubscriptionIds(info.SubscriptionIds.Cache);
if (books == null)
books = new List<QuoteChangeMessage>();
books.Add(quoteMsg);
if (leftIds == null)
leftIds = new HashSet<long>(ids);
leftIds.RemoveRange(info.SubscriptionIds.Cache);
}
}
if (leftIds != null)
{
if (leftIds.Count == 0)
{
message = null;
break;
}
level1Msg.SetSubscriptionIds(leftIds.ToArray());
}
break;
}
}
if (message != null)
base.OnInnerAdapterNewOutMessage(message);
if (books != null)
{
foreach (var book in books)
{
base.OnInnerAdapterNewOutMessage(book);
}
}
}
/// <summary>
/// Create a copy of <see cref="Level1DepthBuilderAdapter"/>.
/// </summary>
/// <returns>Copy.</returns>
public override IMessageChannel Clone() => new Level1DepthBuilderAdapter(InnerAdapter.TypedClone());
}
}