forked from QuantConnect/Lean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApiWebSocketConnection.cs
322 lines (279 loc) · 11.3 KB
/
ApiWebSocketConnection.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
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using Newtonsoft.Json;
using QuantConnect.Configuration;
using QuantConnect.Data;
using QuantConnect.Logging;
using QuantConnect.Util;
using WebSocketSharp;
using WebSocketSharp.Net;
namespace QuantConnect.Api
{
/// <summary>
/// Manages the web socket connection for live data
/// </summary>
public class ApiWebSocketConnection
{
private readonly ConcurrentQueue<BaseData> _baseDataFromServer = new ConcurrentQueue<BaseData>();
private readonly HashSet<Symbol> _subscribedSymbols = new HashSet<Symbol>();
private EventHandler _updateSubscriptions;
private readonly object _locker = new object();
private const int MaxRetryAttempts = 10;
private bool _initialized = false;
private readonly int _userId;
private readonly string _token;
private readonly string _liveDataUrl = Config.Get("live-data-url", "https://www.quantconnect.com/api/v2/live/data");
private readonly int _liveDataPort = Config.GetInt("live-data-port", 443);
private WebSocket _ws;
private UriBuilder _builder;
/// <summary>
/// Initialize a new WebSocketConnection instance
/// </summary>
/// <param name="userId">QuantConnect user id</param>
/// <param name="token">QuantConnect Api Token</param>
public ApiWebSocketConnection(int userId, string token)
{
_userId = userId;
_token = token;
}
/// <summary>
/// Initialize the web socket connection to the live data server
/// </summary>
public void Initialize()
{
_initialized = true;
_builder = new UriBuilder(new Uri(_liveDataUrl)) { Port = _liveDataPort };
_ws = new WebSocket(_builder.ToString());
var connectionRetryAttempts = 0;
var timeStamp = (int)Time.TimeStamp();
var hash = Api.CreateSecureHash(timeStamp, _token);
_ws.SetCookie(new Cookie("Timestamp", timeStamp.ToString()));
_ws.SetCookie(new Cookie("hash", hash));
_ws.SetCookie(new Cookie("uid", _userId.ToString()));
// Message received from server
_ws.OnMessage += (sender, e) =>
{
lock (_locker)
{
IEnumerable<BaseData> baseDatas = new List<BaseData>();
try
{
baseDatas = BaseData.DeserializeMessage(e.Data);
}
catch
{
Log.Error("ApiWebSocketConnection.OnMessage(): An error was received from the server: {0}", e.Data);
}
foreach (var baseData in baseDatas)
{
_baseDataFromServer.Enqueue(baseData);
}
}
};
// Error has in web socket connection
_ws.OnError += (sender, e) =>
{
Log.Error("WebSocketConnection.Initialize(): Web socket connection error: {0}", e.Message);
if (!_ws.IsAlive && connectionRetryAttempts < MaxRetryAttempts)
{
Log.Trace(
"WebSocketConnection.Initialize(): Attempting to reconnect {0}/{1}",
connectionRetryAttempts, MaxRetryAttempts);
connectionRetryAttempts++;
_ws.Connect();
}
else
{
Log.Trace(
"WebSocketConnection.Initialize(): Could not reconnect to web socket server. " +
"Closing web socket.");
if (_updateSubscriptions != null) _updateSubscriptions -= UpdateSubscriptions;
_updateSubscriptions = null;
}
};
// Connection was closed
_ws.OnClose += (sender, e) =>
{
Log.Trace(
"WebSocketConnection.Initialize(): Web socket connection closed: {0}, {1}",
e.Code, e.Reason);
if (!_ws.IsAlive && connectionRetryAttempts < MaxRetryAttempts && e.Code == (ushort)CloseStatusCode.Abnormal)
{
Log.Error(
"WebSocketConnection.Initialize(): Web socket was closed abnormally. " +
"Attempting to reconnect {0}/{1}",
connectionRetryAttempts, MaxRetryAttempts);
connectionRetryAttempts++;
_ws.Connect();
}
else
{
Log.Trace(
"WebSocketConnection.Initialize(): Could not reconnect to web socket server. " +
"Closing web socket.");
if (_updateSubscriptions != null) _updateSubscriptions -= UpdateSubscriptions;
_updateSubscriptions = null;
}
};
// Connection opened
_ws.OnOpen += (sender, e) =>
{
SendSubscription();
connectionRetryAttempts = 0;
};
_updateSubscriptions += UpdateSubscriptions;
_ws.Connect();
}
/// <summary>
/// Get queued data that's been returned from the live server
/// </summary>
/// <returns></returns>
public IEnumerable<BaseData> GetLiveData()
{
lock (_locker)
{
while (!_baseDataFromServer.IsEmpty)
{
BaseData b;
if (_baseDataFromServer.TryDequeue(out b))
yield return b;
}
}
}
/// <summary>
/// Adds the specified symbols to the subscription
/// </summary>
/// <param name="symbols">The symbols to be added keyed by SecurityType</param>
public void Subscribe(IEnumerable<Symbol> symbols)
{
lock (_locker)
{
var symbolsToSubscribe = (from symbol in symbols
where !_subscribedSymbols.Contains(symbol) && CanSubscribe(symbol)
select symbol).ToList();
if (symbolsToSubscribe.Count == 0)
{
Log.Trace("WebSocketConnection.Subscribe(): Cannot subscribe to requested symbols. Either symbols are not supported or requested subscriptions already exist.");
return;
}
foreach (var symbol in symbolsToSubscribe)
{
_subscribedSymbols.Add(symbol);
}
Log.Trace("WebSocketConnection.Subscribe(): Subscribed to: {0}", string.Join(",", symbolsToSubscribe.Select(x => x.Value)));
}
if (_initialized)
OnUpdate();
else
Initialize();
}
/// <summary>
/// Removes the specified symbols to the subscription
/// </summary>
/// <param name="symbols">The symbols to be removed keyed by SecurityType</param>
public void Unsubscribe(IEnumerable<Symbol> symbols)
{
lock (_locker)
{
var symbolsToUnsubscribe = (from symbol in symbols
where _subscribedSymbols.Contains(symbol)
select symbol).ToList();
if (symbolsToUnsubscribe.Count == 0)
{
Log.Trace("WebSocketConnection.Unsubscribe(): Cannot unsubscribe from requested symbols. No existing subscriptions found for: {0}", string.Join(",", symbols.Select(x => x.Value)));
return;
}
foreach (var symbol in symbolsToUnsubscribe)
{
_subscribedSymbols.Remove(symbol);
}
}
if (_initialized)
OnUpdate();
else
Initialize();
}
/// <summary>
/// Raise event that will change subscription
/// </summary>
private void OnUpdate()
{
var handler = _updateSubscriptions;
if (handler != null)
{
handler.Invoke(this, null);
}
}
/// <summary>
/// Returns true if this supports the specified symbol
/// </summary>
private static bool CanSubscribe(Symbol symbol)
{
// ignore unsupported security types
if (symbol.ID.SecurityType != SecurityType.Equity &&
symbol.ID.SecurityType != SecurityType.Cfd &&
symbol.ID.SecurityType != SecurityType.Forex &&
symbol.ID.SecurityType != SecurityType.Crypto)
{
Log.Trace("WebSocketConnection.CanSubscribe(): Unsupported security type, {0}.", symbol.ID.SecurityType);
return false;
}
// ignore unsupported markets
if (symbol.ID.Market != Market.Oanda &&
symbol.ID.Market != Market.FXCM &&
symbol.ID.Market != Market.USA)
{
Log.Trace("WebSocketConnection.CanSubscribe(): Unsupported market, {0}.", symbol.ID.Market);
return false;
}
// ignore universe symbols
if (symbol.Value.Contains("-UNIVERSE-"))
{
Log.Trace("WebSocketConnection.CanSubscribe(): Universe Symbols not supported.");
return false;
};
// If we made it this far, the symbol is supported
return true;
}
/// <summary>
/// Send current list of symbols to libe data server
/// </summary>
private void SendSubscription()
{
lock (_locker)
{
_ws.Send(JsonConvert.SerializeObject(_subscribedSymbols));
Log.Trace(
"WebSocketConnection.SendSubscription(): Sent {0} subscriptions to: {1}",
_subscribedSymbols.Count,
_builder);
}
}
/// <summary>
/// Update subscriptions
/// </summary>
private void UpdateSubscriptions(object sender, EventArgs eventArgs)
{
if (_ws != null) SendSubscription();
}
}
}