-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Expand file tree
/
Copy pathSubscriptionDataConfig.cs
More file actions
483 lines (441 loc) · 22 KB
/
Copy pathSubscriptionDataConfig.cs
File metadata and controls
483 lines (441 loc) · 22 KB
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
/*
* 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 NodaTime;
using QuantConnect.Util;
using QuantConnect.Securities;
using System.Collections.Generic;
using QuantConnect.Data.Consolidators;
using static QuantConnect.StringExtensions;
namespace QuantConnect.Data
{
/// <summary>
/// Subscription data required including the type of data.
/// </summary>
public class SubscriptionDataConfig : IEquatable<SubscriptionDataConfig>
{
private readonly bool _mappedConfig;
private readonly SecurityIdentifier _sid;
/// <summary>
/// Event fired when there is a new symbol due to mapping
/// </summary>
public event EventHandler<NewSymbolEventArgs> NewSymbol;
/// <summary>
/// Type of data
/// </summary>
public Type Type { get; }
/// <summary>
/// Security type of this data subscription
/// </summary>
public SecurityType SecurityType => Symbol.SecurityType;
/// <summary>
/// Symbol of the asset we're requesting: this is really a perm tick!!
/// </summary>
public Symbol Symbol { get; private set; }
/// <summary>
/// Trade, quote or open interest data
/// </summary>
public TickType TickType { get; }
/// <summary>
/// Resolution of the asset we're requesting, second minute or tick
/// </summary>
public Resolution Resolution { get; }
/// <summary>
/// Timespan increment between triggers of this data:
/// </summary>
public TimeSpan Increment { get; }
/// <summary>
/// The explicitly declared length of a single bar for this subscription, when the stored data
/// period differs from the nominal period implied by <see cref="Resolution"/>.
/// </summary>
/// <remarks>Null means the period is derived from <see cref="Resolution"/>, which is the default
/// behavior. This allows data whose true bar period is not one of the <see cref="Resolution"/>
/// values to be described accurately, so that bar end times, fill forward and consolidation all
/// operate on the real period instead of an assumed one.</remarks>
public TimeSpan? BarPeriod { get; }
/// <summary>
/// True if wish to send old data when time gaps in data feed.
/// </summary>
public bool FillDataForward { get; }
/// <summary>
/// Boolean Send Data from between 4am - 8am (Equities Setting Only)
/// </summary>
public bool ExtendedMarketHours { get; }
/// <summary>
/// True if this subscription was added for the sole purpose of providing currency conversion rates via <see cref="CashBook.EnsureCurrencyDataFeeds"/>
/// </summary>
public bool IsInternalFeed { get; }
/// <summary>
/// True if this subscription is for custom user data, false for QC data
/// </summary>
public bool IsCustomData { get; }
/// <summary>
/// The sum of dividends accrued in this subscription, used for scaling total return prices
/// </summary>
public decimal SumOfDividends{ get; set; }
/// <summary>
/// Gets the normalization mode used for this subscription
/// </summary>
public DataNormalizationMode DataNormalizationMode { get; set; }
/// <summary>
/// Gets the securities mapping mode used for this subscription
/// </summary>
/// <remarks>This is particular useful when generating continuous futures</remarks>
public DataMappingMode DataMappingMode { get; }
/// <summary>
/// The continuous contract desired offset from the current front month.
/// For example, 0 (default) will use the front month, 1 will use the back month contract
/// </summary>
public uint ContractDepthOffset { get; }
/// <summary>
/// Price Scaling Factor:
/// </summary>
public decimal PriceScaleFactor { get; set; }
/// <summary>
/// Symbol Mapping: When symbols change over time (e.g. CHASE-> JPM) need to update the symbol requested.
/// </summary>
public string MappedSymbol
{
get
{
if (Symbol.HasUnderlying)
{
if (SecurityType == SecurityType.Future)
{
return Symbol.Underlying.ID.ToString();
}
if (SecurityType.IsOption())
{
return Symbol.Underlying.Value;
}
}
return Symbol.Value;
}
set
{
var oldMappedValue = MappedSymbol;
if(ContractDepthOffset == 0 && oldMappedValue == value)
{
// Do less if we can.
// We can only do this for sure if 'ContractDepthOffset' is 0 else the value we got might be outdated and will change bellow
return;
}
var oldSymbol = Symbol;
Symbol = Symbol.UpdateMappedSymbol(value, ContractDepthOffset);
if (MappedSymbol != oldMappedValue)
{
NewSymbol?.Invoke(this, new NewSymbolEventArgs(Symbol, oldSymbol));
}
}
}
/// <summary>
/// Gets the market / scope of the symbol
/// </summary>
public string Market => Symbol.ID.Market;
/// <summary>
/// Gets the data time zone for this subscription
/// </summary>
public DateTimeZone DataTimeZone { get; }
/// <summary>
/// Gets the exchange time zone for this subscription
/// </summary>
public DateTimeZone ExchangeTimeZone { get; }
/// <summary>
/// Consolidators that are registred with this subscription
/// </summary>
public ISet<IDataConsolidator> Consolidators { get; }
/// <summary>
/// Gets whether or not this subscription should have filters applied to it (market hours/user filters from security)
/// </summary>
public bool IsFilteredSubscription { get; }
/// <summary>
/// Constructor for Data Subscriptions
/// </summary>
/// <param name="objectType">Type of the data objects.</param>
/// <param name="symbol">Symbol of the asset we're requesting</param>
/// <param name="resolution">Resolution of the asset we're requesting</param>
/// <param name="dataTimeZone">The time zone the raw data is time stamped in</param>
/// <param name="exchangeTimeZone">Specifies the time zone of the exchange for the security this subscription is for. This
/// is this output time zone, that is, the time zone that will be used on BaseData instances</param>
/// <param name="fillForward">Fill in gaps with historical data</param>
/// <param name="extendedHours">Equities only - send in data from 4am - 8pm</param>
/// <param name="isInternalFeed">Set to true if this subscription is added for the sole purpose of providing currency conversion rates,
/// setting this flag to true will prevent the data from being sent into the algorithm's OnData methods</param>
/// <param name="isCustom">True if this is user supplied custom data, false for normal QC data</param>
/// <param name="tickType">Specifies if trade or quote data is subscribed</param>
/// <param name="isFilteredSubscription">True if this subscription should have filters applied to it (market hours/user filters from security), false otherwise</param>
/// <param name="dataNormalizationMode">Specifies normalization mode used for this subscription</param>
/// <param name="dataMappingMode">The contract mapping mode to use for the security</param>
/// <param name="contractDepthOffset">The continuous contract desired offset from the current front month.
/// For example, 0 (default) will use the front month, 1 will use the back month contract</param>
/// <param name="barPeriod">The explicitly declared length of a single bar, when the stored data period
/// differs from the nominal period implied by <paramref name="resolution"/>. Null derives it from the resolution</param>
public SubscriptionDataConfig(Type objectType,
Symbol symbol,
Resolution resolution,
DateTimeZone dataTimeZone,
DateTimeZone exchangeTimeZone,
bool fillForward,
bool extendedHours,
bool isInternalFeed,
bool isCustom = false,
TickType? tickType = null,
bool isFilteredSubscription = true,
DataNormalizationMode dataNormalizationMode = DataNormalizationMode.Adjusted,
DataMappingMode dataMappingMode = DataMappingMode.OpenInterest,
uint contractDepthOffset = 0,
bool mappedConfig = false,
TimeSpan? barPeriod = null)
{
if (objectType == null) throw new ArgumentNullException(nameof(objectType));
if (symbol == null) throw new ArgumentNullException(nameof(symbol));
if (dataTimeZone == null) throw new ArgumentNullException(nameof(dataTimeZone));
if (exchangeTimeZone == null) throw new ArgumentNullException(nameof(exchangeTimeZone));
if (barPeriod.HasValue && barPeriod.Value <= TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(barPeriod), barPeriod, "Bar period must be positive");
}
if (barPeriod.HasValue && resolution == Resolution.Tick)
{
throw new ArgumentException("Bar period is not supported for tick resolution", nameof(barPeriod));
}
Type = objectType;
Resolution = resolution;
_sid = symbol.ID;
Symbol = symbol;
ExtendedMarketHours = extendedHours && LeanData.SupportsExtendedMarketHours(Type);
PriceScaleFactor = 1;
IsInternalFeed = isInternalFeed;
IsCustomData = isCustom;
DataTimeZone = dataTimeZone;
_mappedConfig = mappedConfig;
DataMappingMode = dataMappingMode;
ExchangeTimeZone = exchangeTimeZone;
ContractDepthOffset = contractDepthOffset;
IsFilteredSubscription = isFilteredSubscription;
Consolidators = new ConcurrentSet<IDataConsolidator>();
DataNormalizationMode = dataNormalizationMode;
TickType = tickType ?? LeanData.GetCommonTickTypeForCommonDataTypes(objectType, SecurityType);
Increment = barPeriod ?? resolution.ToTimeSpan();
BarPeriod = barPeriod;
//Ticks are individual sales and fillforward doesn't apply.
FillDataForward = resolution == Resolution.Tick ? false : fillForward;
}
/// <summary>
/// Copy constructor with overrides
/// </summary>
/// <param name="config">The config to copy, then overrides are applied and all option</param>
/// <param name="objectType">Type of the data objects.</param>
/// <param name="symbol">Symbol of the asset we're requesting</param>
/// <param name="resolution">Resolution of the asset we're requesting</param>
/// <param name="dataTimeZone">The time zone the raw data is time stamped in</param>
/// <param name="exchangeTimeZone">Specifies the time zone of the exchange for the security this subscription is for. This
/// is this output time zone, that is, the time zone that will be used on BaseData instances</param>
/// <param name="fillForward">Fill in gaps with historical data</param>
/// <param name="extendedHours">Equities only - send in data from 4am - 8pm</param>
/// <param name="isInternalFeed">Set to true if this subscription is added for the sole purpose of providing currency conversion rates,
/// setting this flag to true will prevent the data from being sent into the algorithm's OnData methods</param>
/// <param name="isCustom">True if this is user supplied custom data, false for normal QC data</param>
/// <param name="tickType">Specifies if trade or quote data is subscribed</param>
/// <param name="isFilteredSubscription">True if this subscription should have filters applied to it (market hours/user filters from security), false otherwise</param>
/// <param name="dataNormalizationMode">Specifies normalization mode used for this subscription</param>
/// <param name="dataMappingMode">The contract mapping mode to use for the security</param>
/// <param name="contractDepthOffset">The continuous contract desired offset from the current front month.
/// For example, 0 (default) will use the front month, 1 will use the back month contract</param>
/// <param name="mappedConfig">True if this is created as a mapped config. This is useful for continuous contract at live trading
/// where we subscribe to the mapped symbol but want to preserve uniqueness</param>
/// <param name="barPeriod">The explicitly declared length of a single bar. When not provided, the source config's
/// value is inherited, unless <paramref name="resolution"/> changes the resolution, in which case the declared
/// period no longer describes the requested data and is dropped</param>
public SubscriptionDataConfig(SubscriptionDataConfig config,
Type objectType = null,
Symbol symbol = null,
Resolution? resolution = null,
DateTimeZone dataTimeZone = null,
DateTimeZone exchangeTimeZone = null,
bool? fillForward = null,
bool? extendedHours = null,
bool? isInternalFeed = null,
bool? isCustom = null,
TickType? tickType = null,
bool? isFilteredSubscription = null,
DataNormalizationMode? dataNormalizationMode = null,
DataMappingMode? dataMappingMode = null,
uint? contractDepthOffset = null,
bool? mappedConfig = null,
TimeSpan? barPeriod = null)
: this(
objectType ?? config.Type,
symbol ?? config.Symbol,
resolution ?? config.Resolution,
dataTimeZone ?? config.DataTimeZone,
exchangeTimeZone ?? config.ExchangeTimeZone,
fillForward ?? config.FillDataForward,
extendedHours ?? config.ExtendedMarketHours,
isInternalFeed ?? config.IsInternalFeed,
isCustom ?? config.IsCustomData,
tickType ?? config.TickType,
isFilteredSubscription ?? config.IsFilteredSubscription,
dataNormalizationMode ?? config.DataNormalizationMode,
dataMappingMode ?? config.DataMappingMode,
contractDepthOffset ?? config.ContractDepthOffset,
mappedConfig ?? false,
barPeriod ?? GetInheritedBarPeriod(config, resolution)
)
{
PriceScaleFactor = config.PriceScaleFactor;
SumOfDividends = config.SumOfDividends;
Consolidators = config.Consolidators;
}
/// <summary>
/// Helper for the copy constructor. A declared bar period describes the data of a specific resolution,
/// so it is only inherited when the copy keeps the source resolution.
/// </summary>
private static TimeSpan? GetInheritedBarPeriod(SubscriptionDataConfig config, Resolution? resolution)
{
if (resolution.HasValue && resolution.Value != config.Resolution)
{
return null;
}
return config.BarPeriod;
}
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <returns>
/// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
/// </returns>
/// <param name="other">An object to compare with this object.</param>
public bool Equals(SubscriptionDataConfig other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return _sid.Equals(other._sid) && Type == other.Type
&& TickType == other.TickType
&& Resolution == other.Resolution
&& FillDataForward == other.FillDataForward
&& ExtendedMarketHours == other.ExtendedMarketHours
&& IsInternalFeed == other.IsInternalFeed
&& IsCustomData == other.IsCustomData
&& DataTimeZone.Equals(other.DataTimeZone)
&& DataMappingMode == other.DataMappingMode
&& ExchangeTimeZone.Equals(other.ExchangeTimeZone)
&& ContractDepthOffset == other.ContractDepthOffset
&& IsFilteredSubscription == other.IsFilteredSubscription
&& BarPeriod == other.BarPeriod
&& _mappedConfig == other._mappedConfig;
}
/// <summary>
/// Determines whether the specified object is equal to the current object.
/// </summary>
/// <returns>
/// true if the specified object is equal to the current object; otherwise, false.
/// </returns>
/// <param name="obj">The object to compare with the current object. </param>
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((SubscriptionDataConfig) obj);
}
/// <summary>
/// Serves as the default hash function.
/// </summary>
/// <returns>
/// A hash code for the current object.
/// </returns>
public override int GetHashCode()
{
unchecked
{
var hashCode = _sid.GetHashCode();
hashCode = (hashCode*397) ^ Type.GetHashCode();
hashCode = (hashCode*397) ^ (int) TickType;
hashCode = (hashCode*397) ^ (int) Resolution;
hashCode = (hashCode*397) ^ FillDataForward.GetHashCode();
hashCode = (hashCode*397) ^ ExtendedMarketHours.GetHashCode();
hashCode = (hashCode*397) ^ IsInternalFeed.GetHashCode();
hashCode = (hashCode*397) ^ IsCustomData.GetHashCode();
hashCode = (hashCode*397) ^ DataMappingMode.GetHashCode();
hashCode = (hashCode*397) ^ DataTimeZone.Id.GetHashCode();// timezone hash is expensive, use id instead
hashCode = (hashCode*397) ^ ExchangeTimeZone.Id.GetHashCode();// timezone hash is expensive, use id instead
hashCode = (hashCode*397) ^ ContractDepthOffset.GetHashCode();
hashCode = (hashCode*397) ^ IsFilteredSubscription.GetHashCode();
hashCode = (hashCode*397) ^ BarPeriod.GetHashCode();
hashCode = (hashCode*397) ^ _mappedConfig.GetHashCode();
return hashCode;
}
}
/// <summary>
/// Override equals operator
/// </summary>
public static bool operator ==(SubscriptionDataConfig left, SubscriptionDataConfig right)
{
return Equals(left, right);
}
/// <summary>
/// Override not equals operator
/// </summary>
public static bool operator !=(SubscriptionDataConfig left, SubscriptionDataConfig right)
{
return !Equals(left, right);
}
/// <summary>
/// Returns a string that represents the current object.
/// </summary>
/// <returns>
/// A string that represents the current object.
/// </returns>
/// <filterpriority>2</filterpriority>
public override string ToString()
{
return ToString(Symbol.Value);
}
/// <summary>
/// Returns a string that represents the current object.
/// </summary>
/// <param name="symbol">Symbol to use in the string representation of the object</param>
/// <returns>/// A string that represents the current object.</returns>
public string ToString(string symbol)
{
return Invariant($"{symbol},#{ContractDepthOffset},{MappedSymbol},{Resolution},{Type.Name},{TickType},{DataNormalizationMode},{DataMappingMode}{(IsInternalFeed ? ",Internal" : string.Empty)}");
}
/// <summary>
/// New base class for all event classes.
/// </summary>
public class NewSymbolEventArgs : EventArgs
{
/// <summary>
/// The old symbol instance
/// </summary>
public Symbol Old { get; }
/// <summary>
/// The new symbol instance
/// </summary>
public Symbol New { get; }
/// <summary>
/// Create an instance of NewSymbolEventArgs
/// </summary>
/// <param name="new"></param>
/// <param name="old"></param>
public NewSymbolEventArgs(Symbol @new, Symbol old)
{
New = @new;
Old = old;
}
}
}
}