generated from QuantConnect/Lean.Brokerages.Template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathTradierBrokerage.DataQueueHandler.cs
281 lines (241 loc) · 9.64 KB
/
TradierBrokerage.DataQueueHandler.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
/*
* 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 Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NodaTime;
using QuantConnect.Configuration;
using QuantConnect.Data;
using QuantConnect.Data.Market;
using QuantConnect.Packets;
using QuantConnect.Util;
using RestSharp;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
namespace QuantConnect.Brokerages.Tradier
{
/// <summary>
/// Tradier Class: IDataQueueHandler implementation
/// </summary>
public partial class TradierBrokerage
{
#region IDataQueueHandler implementation
private const string WebSocketUrl = "wss://ws.tradier.com/v1/markets/events";
private object _streamSessionLock = new ();
private TradierStreamSession _streamSession;
private readonly ConcurrentDictionary<string, Symbol> _subscribedTickers = new ConcurrentDictionary<string, Symbol>();
/// <summary>
/// Sets the job we're subscribing for
/// </summary>
/// <param name="job">Job we're subscribing for</param>
public void SetJob(LiveNodePacket job)
{
var useSandbox = bool.Parse(job.BrokerageData["tradier-use-sandbox"]);
if (job.BrokerageData.TryGetValue("tradier-environment", out string environment) && !string.IsNullOrEmpty(environment))
{
useSandbox = environment.ToLowerInvariant() == "paper";
}
var accountId = job.BrokerageData["tradier-account-id"];
var accessToken = job.BrokerageData["tradier-access-token"];
var aggregator = Composer.Instance.GetExportedValueByTypeName<IDataAggregator>(Config.Get("data-aggregator", "QuantConnect.Lean.Engine.DataFeeds.AggregationManager"), forceTypeNameOnExisting: false);
Initialize(
wssUrl: WebSocketUrl,
accountId: accountId,
accessToken: accessToken,
useSandbox: useSandbox,
algorithm: null,
orderProvider: null,
securityProvider: null,
aggregator: aggregator);
if (!IsConnected)
{
Connect();
}
}
/// <summary>
/// Subscribe to the specified configuration
/// </summary>
/// <param name="dataConfig">defines the parameters to subscribe to a data feed</param>
/// <param name="newDataAvailableHandler">handler to be fired on new data available</param>
/// <returns>The new enumerator for this subscription request</returns>
public IEnumerator<BaseData> Subscribe(SubscriptionDataConfig dataConfig, EventHandler newDataAvailableHandler)
{
// streaming is not supported by sandbox
if (_useSandbox)
{
throw new NotSupportedException(
"TradierBrokerage.DataQueueHandler.Subscribe(): The sandbox does not support data streaming.");
}
if (!CanSubscribe(dataConfig.Symbol))
{
return null;
}
var enumerator = _aggregator.Add(dataConfig, newDataAvailableHandler);
SubscriptionManager.Subscribe(dataConfig);
return enumerator;
}
private bool CanSubscribe(Symbol symbol)
{
return (symbol.ID.SecurityType == SecurityType.Equity || symbol.ID.SecurityType == SecurityType.Option)
&& !symbol.Value.Contains("-UNIVERSE-")
// continuous futures and canonical symbols not supported
&& !symbol.IsCanonical();
}
/// <summary>
/// Removes the specified configuration
/// </summary>
/// <param name="dataConfig">Subscription config to be removed</param>
public void Unsubscribe(SubscriptionDataConfig dataConfig)
{
SubscriptionManager.Unsubscribe(dataConfig);
_aggregator.Remove(dataConfig);
}
protected override bool Subscribe(IEnumerable<Symbol> symbols)
{
var symbolsAdded = false;
foreach (var symbol in symbols)
{
if (!symbol.IsCanonical())
{
var ticker = _symbolMapper.GetBrokerageSymbol(symbol);
if (!_subscribedTickers.ContainsKey(ticker))
{
_subscribedTickers.TryAdd(ticker, symbol);
symbolsAdded = true;
}
}
}
if (symbolsAdded)
{
_subscribeProcedure.Set();
}
return true;
}
private bool Unsubscribe(IEnumerable<Symbol> symbols)
{
var symbolsRemoved = false;
foreach (var symbol in symbols)
{
if (!symbol.IsCanonical())
{
var ticker = _symbolMapper.GetBrokerageSymbol(symbol);
if (_subscribedTickers.ContainsKey(ticker))
{
Symbol removedSymbol;
_subscribedTickers.TryRemove(ticker, out removedSymbol);
symbolsRemoved = true;
}
}
}
if (symbolsRemoved)
{
_subscribeProcedure.Set();
}
return true;
}
private void SendSubscribeMessage()
{
var tickers = _subscribedTickers.Keys.ToList();
if(tickers.Count == 0)
{
// Tradier expects at least one symbol
tickers = new List<string> { "$empty$" };
}
var obj = new
{
sessionid = GetStreamSession().SessionId,
symbols = tickers,
filter = new[] { "trade", "quote" },
linebreak = true
};
var json = JsonConvert.SerializeObject(obj);
WebSocket.Send(json);
}
/// <summary>
/// Handles websocket received messages
/// </summary>
protected override void OnMessage(object sender, WebSocketMessage webSocketMessage)
{
var e = (WebSocketClientWrapper.TextMessage)webSocketMessage.Data;
var obj = JObject.Parse(e.Message);
JToken error;
if (obj.TryGetValue("error", out error))
{
OnMessage(new BrokerageMessageEvent(BrokerageMessageType.Error, -1, error.Value<string>()));
return;
}
var tsd = obj.ToObject<TradierStreamData>();
if (tsd?.Type == "trade" || tsd?.Type == "quote")
{
var tick = CreateTick(tsd);
if (tick != null)
{
_aggregator.Update(tick);
}
}
}
/// <summary>
/// Create a tick from the tradier stream data
/// </summary>
/// <param name="tsd">Tradier stream data object</param>
/// <returns>LEAN Tick object</returns>
private Tick CreateTick(TradierStreamData tsd)
{
Symbol symbol;
if (!_subscribedTickers.TryGetValue(tsd.Symbol, out symbol))
{
// Not subscribed to this symbol.
return null;
}
if (tsd.Type == "trade")
{
// Occasionally Tradier sends trades with 0 volume?
if (tsd.TradeSize == 0 || !tsd.TradePrice.HasValue) return null;
}
// Tradier trades are US NY time only. Convert local server time to NY Time:
var utc = tsd.GetTickTimestamp();
// Occasionally Tradier sends old ticks every 20sec-ish if no trading?
if (DateTime.UtcNow - utc > TimeSpan.FromSeconds(10)) return null;
// Convert the timestamp to exchange timezone and pass into algorithm
var time = utc.ConvertTo(DateTimeZone.Utc, TimeZones.NewYork);
switch (tsd.Type)
{
case "trade":
return new Tick(time, symbol, "", tsd.TradeExchange, (int)tsd.TradeSize, tsd.TradePrice.Value);
case "quote":
return new Tick(time, symbol, "", "", tsd.BidSize, tsd.BidPrice, tsd.AskSize, tsd.AskPrice);
}
return null;
}
/// <summary>
/// Get the current Tradier stream session
/// </summary>
private TradierStreamSession GetStreamSession()
{
lock (_streamSessionLock)
{
if (_streamSession == null || !_streamSession.IsValid)
{
var request = new RestRequest("markets/events/session", Method.POST);
_streamSession = Execute<TradierStreamSession>(request, TradierApiRequestType.Data, "stream");
}
return _streamSession;
}
}
#endregion IDataQueueHandler implementation
}
}