-
Notifications
You must be signed in to change notification settings - Fork 1
/
BitmexDataFeed.cs
578 lines (454 loc) · 18.7 KB
/
BitmexDataFeed.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
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using Newtonsoft.Json;
using WebSocket4Net;
using WTT.BitmexDataFeed.Properties;
using WTT.IDataFeed;
namespace WTT.BitmexDataFeed
{
public class BitmexDataFeed : IDataFeed.IDataFeed
{
private readonly ctlLogin _loginForm = new ctlLogin();
private readonly Queue<HistoryRequest> _requests = new Queue<HistoryRequest>();
private readonly List<string> _symbols = new List<string>();
private readonly AutoResetEvent m_OpenedEvent = new AutoResetEvent(false);
//string format for dates is: 2018-09-21T00:00:00.0000000Z
private readonly CultureInfo MyCultureInfo = new CultureInfo("en-EN");
private bool _isWebClientBusy;
private BitMEXApi bitmex;
private WebSocket webSocket;
private string bitmexKey { get; set; }
private string bitmexSecret { get; set; }
public event EventHandler<MessageEventArgs> OnNewMessage;
public event EventHandler<DataFeedStatusEventArgs> OnNewStatus;
public event EventHandler<DataFeedStatusTimeEventArgs> OnNewStatusTime;
#region Not supported DataFeed methods
//... unused
public double GetSymbolOffset(string symbol)
{
return 0.0;
}
#endregion
#region Helper Functions
private void FireConnectionStatus(string message)
{
OnNewMessage?.Invoke(this, new MessageEventArgs {Message = message, Icon = OutputIcon.Warning});
}
#endregion
#region Login/logout
public string Name => "Bitmex";
public Control GetLoginControl()
{
return _loginForm;
}
public bool ValidateLoginParams()
{
//Check for valid ApiKey
bitmexKey = _loginForm.ApiKey;
Settings.Default.ApiKey = bitmexKey;
bitmexSecret = _loginForm.Secret;
Settings.Default.Secret = bitmexSecret;
try
{
Settings.Default.Save();
}
catch
{
}
return true;
}
//... BitMEX WebSocket connection opened
private void OnConnected(object sender, EventArgs e)
{
FireConnectionStatus("Connected to BitMEX streaming websocket.");
//... setup basic hello message
//... nothing to do for this websocket
m_OpenedEvent.Set();
}
//... BitMEX WebSocket Message Received
private void OnMessage(object sender, MessageReceivedEventArgs e)
{
//Debug only
//FireConnectionStatus("i:"+e.Message);
var jsonMessage = e.Message;
try
{
var unpacked = JsonConvert.DeserializeObject<dynamic>(jsonMessage);
if (unpacked == null) return;
var table = (string) unpacked.table;
var action = (string) unpacked.action;
if (table == "trade" && action == "insert")
foreach (var trade in unpacked.data)
{
var priceUpate = new QuoteData();
priceUpate.Symbol = trade.symbol;
priceUpate.Price = trade.price;
var parsedDate = DateTime.Parse((string) trade.timestamp, MyCultureInfo);
priceUpate.TradeTime = parsedDate;
priceUpate.TradedVolume = (long)trade.size;
//...update charts
BrodcastQuote(priceUpate);
}
}
catch
{
//could not decode data from streaming socket
//should not happen, but just in case...
FireConnectionStatus("Error unpacking quote.");
}
}
public bool Login()
{
//... setup the web API connection
bitmex = new BitMEXApi(bitmexKey, bitmexSecret);
//...Initiate the WebSocket
var socketAddress = "wss://www.bitmex.com/realtime";
webSocket = new WebSocket(socketAddress);
webSocket.Opened += OnConnected;
//not implemented yet: websocket.Error += new EventHandler<ErrorEventArgs>(websocket_Error);
//not implemented yet: websocket.Closed += new EventHandler(websocket_Closed);
webSocket.MessageReceived += OnMessage;
webSocket.Open();
if (!m_OpenedEvent.WaitOne(10000))
{
FireConnectionStatus("Failed to open BitMEX websocket on time.");
return false;
}
return true;
}
public bool Logout()
{
webSocket?.Close();
FireConnectionStatus("Disconnected from " + Name);
return true;
}
#endregion
#region Realtime-feed
//...Bitmex WebSocket Message class
public class BitmexSocketMessage
{
public string op { get; set; }
public List<string> args { get; set; }
}
private readonly List<IDataSubscriber> _subscribers = new List<IDataSubscriber>();
public void InitDataSubscriber(IDataSubscriber subscriber)
{
lock (_subscribers)
{
if (!_subscribers.Contains(subscriber))
_subscribers.Add(subscriber);
}
}
public void RemoveDataSubscriber(IDataSubscriber subscriber)
{
lock (_subscribers)
{
if (_subscribers.Contains(subscriber))
{
_subscribers.Remove(subscriber);
subscriber.UnSubscribeAll();
}
}
}
public void Subscribe(string symbol)
{
//...failsafe...
if (webSocket == null) return;
symbol = symbol.ToUpper();
if (_symbols.Contains(symbol)) return;
lock (_symbols)
{
_symbols.Add(symbol);
}
FireConnectionStatus("Listening to " + symbol);
//...send out the message to listen for data
var message = new BitmexSocketMessage {op = "subscribe", args = new List<string> {$"trade:{symbol}"}};
var _webSocketMessage = JsonConvert.SerializeObject(message);
webSocket.Send(_webSocketMessage);
}
public void UnSubscribe(string symbol)
{
//failsafe...
if (webSocket == null) return;
if (!_symbols.Contains(symbol))
return;
lock (_subscribers)
{
if (_subscribers.Any(s => s.IsSymbolWatching(symbol)))
return;
}
lock (_symbols)
{
_symbols.Remove(symbol);
}
FireConnectionStatus("De-Listening to " + symbol);
//...send out the message to de-listen for data
var message = new BitmexSocketMessage {op = "unsubscribe", args = new List<string> {$"trade:{symbol}"}};
var _webSocketMessage = JsonConvert.SerializeObject(message);
webSocket.Send(_webSocketMessage);
}
private void BrodcastQuote(QuoteData quote)
{
lock (_subscribers)
{
foreach (var subscriber in _subscribers) subscriber.OnPriceUpdate(quote);
}
}
#endregion
#region History
private class HistoryRequest
{
public ChartSelection ChartSelection { get; set; }
public IHistorySubscriber HistorySubscriber { get; set; }
}
public void GetHistory(ChartSelection selection, IHistorySubscriber subscriber)
{
var request = new HistoryRequest {ChartSelection = selection, HistorySubscriber = subscriber};
request.ChartSelection.Symbol = request.ChartSelection.Symbol.ToUpper();
if (!ValidateRequest(request))
{
SendNoHistory(request);
return;
}
ThreadPool.QueueUserWorkItem(state => ProcessRequest(request));
}
private bool ValidateRequest(HistoryRequest request)
{
return request != null &&
request.ChartSelection != null &&
!string.IsNullOrEmpty(request.ChartSelection.Symbol) &&
(request.ChartSelection.Periodicity == EPeriodicity.Daily ||
request.ChartSelection.Periodicity == EPeriodicity.Hourly ||
request.ChartSelection.Periodicity == EPeriodicity.Minutely
) &&
request.HistorySubscriber != null &&
request.ChartSelection.ChartType == EChartType.TimeBased;
}
private void ProcessRequest(HistoryRequest request)
{
_requests.Enqueue(request);
ProcessNextRequest();
}
private void ProcessNextRequest()
{
if (_isWebClientBusy || _requests.Count == 0)
return;
_isWebClientBusy = true;
var _currentRequest = _requests.Dequeue();
var isFail = false;
var response = string.Empty;
var datasets = new List<dynamic>();
try
{
var paginating = true;
var pageSize = 750; //... max page count to receive via one API call
var totalBarCounter = 0;
var start = 0;
var endingTime =
DateTime.UtcNow.AddDays(1).ToString("yyyy-MM-dd"); //ensure we get always the latest current bar
do
{
//DEBUG: FireConnectionStatus(FormRequestString(_currentRequest.ChartSelection));
try
{
var param = FormRequest(_currentRequest.ChartSelection, start, pageSize, endingTime);
var auth = !(string.IsNullOrEmpty(bitmexKey) || string.IsNullOrEmpty(bitmexSecret));
response = bitmex.Query("GET", "/trade/bucketed", param, auth);
//get the data from the API call
var current_dataset =
JsonConvert.DeserializeObject<dynamic>(response);
//check for data
if (((ICollection) current_dataset).Count < pageSize) paginating = false;
//add to API return array
datasets.Add(current_dataset);
totalBarCounter += ((ICollection) current_dataset).Count;
//...check if we have enough data
if (totalBarCounter >= _currentRequest.ChartSelection.Bars)
paginating = false;
start += pageSize;
}
catch (Exception ex)
{
paginating = false;
FireConnectionStatus("Error getting history API data for " +
_currentRequest.ChartSelection.Symbol);
}
} while (paginating);
FireConnectionStatus("Received bars: " + totalBarCounter);
}
catch (Exception ex)
{
try
{
FireConnectionStatus("Error: " + ex.Message);
}
catch
{
}
isFail = true;
}
if (isFail)
SendNoHistory(_currentRequest);
else
ProcessResponseAndSend(_currentRequest, datasets);
_isWebClientBusy = false;
ProcessNextRequest();
}
//...generate API GET request format
private Dictionary<string, string> FormRequest(ChartSelection selection, int start, int count,
string endingTime)
{
//API Endpoint, e.g.
//https://www.bitmex.com/api/v1/trade/bucketed?binSize=1m&partial=false&count=100&reverse=false&startTime=2018-10-10
//Set correct binSize
//available options: 1m,5m,1h,1d
var ep = "";
switch (selection.Periodicity)
{
case EPeriodicity.Hourly:
if ((int) selection.Interval != 1)
{
MessageBox.Show("BitemexApi supports only 1m, 5m, 1h, 1d.");
return null;
}
ep = $"{(int) selection.Interval}h";
break;
case EPeriodicity.Minutely:
if ((int) selection.Interval != 1 && (int) selection.Interval != 5)
{
MessageBox.Show("BitemexApi supports only 1m, 5m, 1h, 1d.");
return null;
}
ep = $"{(int) selection.Interval}m";
break;
case EPeriodicity.Daily:
if ((int) selection.Interval != 1)
{
MessageBox.Show("BitemexApi supports only 1m, 5m, 1h, 1d.");
return null;
}
ep = $"{(int) selection.Interval}d";
break;
default:
MessageBox.Show("BitemexApi supports only 1m, 5m, 1h, 1d.");
return null;
}
//debug only
//FireConnectionStatus("Get: " + requestUrl);
//always ensure we have the last active bar
var param = new Dictionary<string, string>();
param["symbol"] = selection.Symbol;
param["binSize"] = ep; //... available options: 1m,5m,1h,1d
param["partial"] =
true.ToString(); //... will send in-progress (incomplete) bins for the current time period
param["start"] = start.ToString();
param["count"] = count.ToString();
//param["filter"] = "{\"open\":true}";
//param["columns"] = "";
param["reverse"] = true.ToString();
//param["startTime"] = "";
param["endTime"] = endingTime;
return param;
}
private void SendNoHistory(HistoryRequest request)
{
ThreadPool.QueueUserWorkItem(
state => request.HistorySubscriber.OnHistoryIncome(request.ChartSelection.Symbol, new List<BarData>()));
}
private void ProcessResponseAndSend(HistoryRequest request, List<dynamic> datasets)
{
var periodicity = request.ChartSelection.Periodicity;
var interval = (int) request.ChartSelection.Interval;
//...failsafe
if (datasets == null)
{
SendNoHistory(request);
return;
}
//check for data
if (datasets.Count < 1)
{
MessageBox.Show("Error: not enough data");
SendNoHistory(request);
return;
}
//... convert API return values into bars
var retBars = new List<BarData>();
foreach (var set in datasets)
{
IEnumerable<BarData> _retBars = FromOHLCVBars(set);
foreach (var newbar in _retBars)
{
//... correct bar time to start-time of bar
//... returned API timestamp for bin is "closing-time" of bar
switch (periodicity)
{
case EPeriodicity.Hourly:
newbar.TradeDate = newbar.TradeDate.AddHours(-1 * interval);
break;
case EPeriodicity.Minutely:
newbar.TradeDate = newbar.TradeDate.AddMinutes(-1 * interval);
break;
case EPeriodicity.Daily:
newbar.TradeDate = newbar.TradeDate.AddDays(-1 * interval);
break;
}
//... skip overlapping bars based on different requests send
if (retBars.Any(b => b.TradeDate == newbar.TradeDate))
continue;
retBars.Add(newbar);
}
}
if (retBars.Count < 4)
{
MessageBox.Show("Error: not enough data");
SendNoHistory(request);
return;
}
//...order
retBars = retBars.OrderBy(data => data.TradeDate).ToList();
//... if request`s bars amount was <= 0 then send all bars
if (request.ChartSelection.Bars > 0)
if (retBars.Count > request.ChartSelection.Bars)
retBars = retBars.Skip(retBars.Count - request.ChartSelection.Bars).ToList();
ThreadPool.QueueUserWorkItem(
state => request.HistorySubscriber.OnHistoryIncome(request.ChartSelection.Symbol, retBars));
}
//...Convert bar data message from API to internal bar data object
private IEnumerable<BarData> FromOHLCVBars(dynamic records)
{
var count = ((ICollection) records).Count;
var retBars = new List<BarData>(count);
foreach (var observation in records)
{
var barData = new BarData();
try
{
//info: timestamp is closing of bar time! (=next bar starting time!)
var parsedDate = DateTime.Parse((string) observation.timestamp, MyCultureInfo);
barData.TradeDate = parsedDate;
barData.Open = (double) observation.open;
barData.High = (double) observation.high;
barData.Low = (double) observation.low;
barData.Close = (double) observation.close;
barData.Volume = (double) observation.volume;
}
catch (Exception ex)
{
MessageBox.Show("Exception in data parsing:" + ex.Message + "--" + observation.timestamp);
continue;
}
if (barData.TradeDate == default(DateTime))
continue;
if (barData.Close != 0) retBars.Add(barData); // Dont add empty field with no close...!
}
return retBars;
}
#endregion
}
}