Lean  $LEAN_TAG$
SubscriptionDataConfig.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using System;
17 using NodaTime;
18 using QuantConnect.Util;
20 using System.Collections.Generic;
22 using static QuantConnect.StringExtensions;
23 
24 namespace QuantConnect.Data
25 {
26  /// <summary>
27  /// Subscription data required including the type of data.
28  /// </summary>
29  public class SubscriptionDataConfig : IEquatable<SubscriptionDataConfig>
30  {
31  private readonly bool _mappedConfig;
32  private readonly SecurityIdentifier _sid;
33 
34  /// <summary>
35  /// Event fired when there is a new symbol due to mapping
36  /// </summary>
37  public event EventHandler<NewSymbolEventArgs> NewSymbol;
38 
39  /// <summary>
40  /// Type of data
41  /// </summary>
42  public Type Type { get; }
43 
44  /// <summary>
45  /// Security type of this data subscription
46  /// </summary>
48 
49  /// <summary>
50  /// Symbol of the asset we're requesting: this is really a perm tick!!
51  /// </summary>
52  public Symbol Symbol { get; private set; }
53 
54  /// <summary>
55  /// Trade, quote or open interest data
56  /// </summary>
57  public TickType TickType { get; }
58 
59  /// <summary>
60  /// Resolution of the asset we're requesting, second minute or tick
61  /// </summary>
62  public Resolution Resolution { get; }
63 
64  /// <summary>
65  /// Timespan increment between triggers of this data:
66  /// </summary>
67  public TimeSpan Increment { get; }
68 
69  /// <summary>
70  /// True if wish to send old data when time gaps in data feed.
71  /// </summary>
72  public bool FillDataForward { get; }
73 
74  /// <summary>
75  /// Boolean Send Data from between 4am - 8am (Equities Setting Only)
76  /// </summary>
77  public bool ExtendedMarketHours { get; }
78 
79  /// <summary>
80  /// True if this subscription was added for the sole purpose of providing currency conversion rates via <see cref="CashBook.EnsureCurrencyDataFeeds"/>
81  /// </summary>
82  public bool IsInternalFeed { get; }
83 
84  /// <summary>
85  /// True if this subscription is for custom user data, false for QC data
86  /// </summary>
87  public bool IsCustomData { get; }
88 
89  /// <summary>
90  /// The sum of dividends accrued in this subscription, used for scaling total return prices
91  /// </summary>
92  public decimal SumOfDividends{ get; set; }
93 
94  /// <summary>
95  /// Gets the normalization mode used for this subscription
96  /// </summary>
98 
99  /// <summary>
100  /// Gets the securities mapping mode used for this subscription
101  /// </summary>
102  /// <remarks>This is particular useful when generating continuous futures</remarks>
104 
105  /// <summary>
106  /// The continuous contract desired offset from the current front month.
107  /// For example, 0 (default) will use the front month, 1 will use the back month contract
108  /// </summary>
109  public uint ContractDepthOffset { get; }
110 
111  /// <summary>
112  /// Price Scaling Factor:
113  /// </summary>
114  public decimal PriceScaleFactor { get; set; }
115 
116  /// <summary>
117  /// Symbol Mapping: When symbols change over time (e.g. CHASE-> JPM) need to update the symbol requested.
118  /// </summary>
119  public string MappedSymbol
120  {
121  get
122  {
123  if (Symbol.HasUnderlying)
124  {
125  if (SecurityType == SecurityType.Future)
126  {
127  return Symbol.Underlying.ID.ToString();
128  }
129  if (SecurityType.IsOption())
130  {
131  return Symbol.Underlying.Value;
132  }
133  }
134  return Symbol.Value;
135  }
136  set
137  {
138  var oldMappedValue = MappedSymbol;
139  if(ContractDepthOffset == 0 && oldMappedValue == value)
140  {
141  // Do less if we can.
142  // We can only do this for sure if 'ContractDepthOffset' is 0 else the value we got might be outdated and will change bellow
143  return;
144  }
145  var oldSymbol = Symbol;
147 
148  if (MappedSymbol != oldMappedValue)
149  {
150  NewSymbol?.Invoke(this, new NewSymbolEventArgs(Symbol, oldSymbol));
151  }
152  }
153  }
154 
155  /// <summary>
156  /// Gets the market / scope of the symbol
157  /// </summary>
158  public string Market => Symbol.ID.Market;
159 
160  /// <summary>
161  /// Gets the data time zone for this subscription
162  /// </summary>
163  public DateTimeZone DataTimeZone { get; }
164 
165  /// <summary>
166  /// Gets the exchange time zone for this subscription
167  /// </summary>
168  public DateTimeZone ExchangeTimeZone { get; }
169 
170  /// <summary>
171  /// Consolidators that are registred with this subscription
172  /// </summary>
173  public ISet<IDataConsolidator> Consolidators { get; }
174 
175  /// <summary>
176  /// Gets whether or not this subscription should have filters applied to it (market hours/user filters from security)
177  /// </summary>
178  public bool IsFilteredSubscription { get; }
179 
180  /// <summary>
181  /// Constructor for Data Subscriptions
182  /// </summary>
183  /// <param name="objectType">Type of the data objects.</param>
184  /// <param name="symbol">Symbol of the asset we're requesting</param>
185  /// <param name="resolution">Resolution of the asset we're requesting</param>
186  /// <param name="dataTimeZone">The time zone the raw data is time stamped in</param>
187  /// <param name="exchangeTimeZone">Specifies the time zone of the exchange for the security this subscription is for. This
188  /// is this output time zone, that is, the time zone that will be used on BaseData instances</param>
189  /// <param name="fillForward">Fill in gaps with historical data</param>
190  /// <param name="extendedHours">Equities only - send in data from 4am - 8pm</param>
191  /// <param name="isInternalFeed">Set to true if this subscription is added for the sole purpose of providing currency conversion rates,
192  /// setting this flag to true will prevent the data from being sent into the algorithm's OnData methods</param>
193  /// <param name="isCustom">True if this is user supplied custom data, false for normal QC data</param>
194  /// <param name="tickType">Specifies if trade or quote data is subscribed</param>
195  /// <param name="isFilteredSubscription">True if this subscription should have filters applied to it (market hours/user filters from security), false otherwise</param>
196  /// <param name="dataNormalizationMode">Specifies normalization mode used for this subscription</param>
197  /// <param name="dataMappingMode">The contract mapping mode to use for the security</param>
198  /// <param name="contractDepthOffset">The continuous contract desired offset from the current front month.
199  /// For example, 0 (default) will use the front month, 1 will use the back month contract</param>
200  public SubscriptionDataConfig(Type objectType,
201  Symbol symbol,
202  Resolution resolution,
203  DateTimeZone dataTimeZone,
204  DateTimeZone exchangeTimeZone,
205  bool fillForward,
206  bool extendedHours,
207  bool isInternalFeed,
208  bool isCustom = false,
209  TickType? tickType = null,
210  bool isFilteredSubscription = true,
211  DataNormalizationMode dataNormalizationMode = DataNormalizationMode.Adjusted,
212  DataMappingMode dataMappingMode = DataMappingMode.OpenInterest,
213  uint contractDepthOffset = 0,
214  bool mappedConfig = false)
215  {
216  if (objectType == null) throw new ArgumentNullException(nameof(objectType));
217  if (symbol == null) throw new ArgumentNullException(nameof(symbol));
218  if (dataTimeZone == null) throw new ArgumentNullException(nameof(dataTimeZone));
219  if (exchangeTimeZone == null) throw new ArgumentNullException(nameof(exchangeTimeZone));
220 
221  Type = objectType;
222  Resolution = resolution;
223  _sid = symbol.ID;
224  Symbol = symbol;
225  ExtendedMarketHours = extendedHours;
226  PriceScaleFactor = 1;
227  IsInternalFeed = isInternalFeed;
228  IsCustomData = isCustom;
229  DataTimeZone = dataTimeZone;
230  _mappedConfig = mappedConfig;
231  DataMappingMode = dataMappingMode;
232  ExchangeTimeZone = exchangeTimeZone;
233  ContractDepthOffset = contractDepthOffset;
234  IsFilteredSubscription = isFilteredSubscription;
236  DataNormalizationMode = dataNormalizationMode;
237 
239 
240  Increment = resolution.ToTimeSpan();
241  //Ticks are individual sales and fillforward doesn't apply.
242  FillDataForward = resolution == Resolution.Tick ? false : fillForward;
243  }
244 
245  /// <summary>
246  /// Copy constructor with overrides
247  /// </summary>
248  /// <param name="config">The config to copy, then overrides are applied and all option</param>
249  /// <param name="objectType">Type of the data objects.</param>
250  /// <param name="symbol">Symbol of the asset we're requesting</param>
251  /// <param name="resolution">Resolution of the asset we're requesting</param>
252  /// <param name="dataTimeZone">The time zone the raw data is time stamped in</param>
253  /// <param name="exchangeTimeZone">Specifies the time zone of the exchange for the security this subscription is for. This
254  /// is this output time zone, that is, the time zone that will be used on BaseData instances</param>
255  /// <param name="fillForward">Fill in gaps with historical data</param>
256  /// <param name="extendedHours">Equities only - send in data from 4am - 8pm</param>
257  /// <param name="isInternalFeed">Set to true if this subscription is added for the sole purpose of providing currency conversion rates,
258  /// setting this flag to true will prevent the data from being sent into the algorithm's OnData methods</param>
259  /// <param name="isCustom">True if this is user supplied custom data, false for normal QC data</param>
260  /// <param name="tickType">Specifies if trade or quote data is subscribed</param>
261  /// <param name="isFilteredSubscription">True if this subscription should have filters applied to it (market hours/user filters from security), false otherwise</param>
262  /// <param name="dataNormalizationMode">Specifies normalization mode used for this subscription</param>
263  /// <param name="dataMappingMode">The contract mapping mode to use for the security</param>
264  /// <param name="contractDepthOffset">The continuous contract desired offset from the current front month.
265  /// For example, 0 (default) will use the front month, 1 will use the back month contract</param>
266  /// <param name="mappedConfig">True if this is created as a mapped config. This is useful for continuous contract at live trading
267  /// where we subscribe to the mapped symbol but want to preserve uniqueness</param>
269  Type objectType = null,
270  Symbol symbol = null,
271  Resolution? resolution = null,
272  DateTimeZone dataTimeZone = null,
273  DateTimeZone exchangeTimeZone = null,
274  bool? fillForward = null,
275  bool? extendedHours = null,
276  bool? isInternalFeed = null,
277  bool? isCustom = null,
278  TickType? tickType = null,
279  bool? isFilteredSubscription = null,
280  DataNormalizationMode? dataNormalizationMode = null,
281  DataMappingMode? dataMappingMode = null,
282  uint? contractDepthOffset = null,
283  bool? mappedConfig = null)
284  : this(
285  objectType ?? config.Type,
286  symbol ?? config.Symbol,
287  resolution ?? config.Resolution,
288  dataTimeZone ?? config.DataTimeZone,
289  exchangeTimeZone ?? config.ExchangeTimeZone,
290  fillForward ?? config.FillDataForward,
291  extendedHours ?? config.ExtendedMarketHours,
292  isInternalFeed ?? config.IsInternalFeed,
293  isCustom ?? config.IsCustomData,
294  tickType ?? config.TickType,
295  isFilteredSubscription ?? config.IsFilteredSubscription,
296  dataNormalizationMode ?? config.DataNormalizationMode,
297  dataMappingMode ?? config.DataMappingMode,
298  contractDepthOffset ?? config.ContractDepthOffset,
299  mappedConfig ?? false
300  )
301  {
304  Consolidators = config.Consolidators;
305  }
306 
307  /// <summary>
308  /// Indicates whether the current object is equal to another object of the same type.
309  /// </summary>
310  /// <returns>
311  /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
312  /// </returns>
313  /// <param name="other">An object to compare with this object.</param>
314  public bool Equals(SubscriptionDataConfig other)
315  {
316  if (ReferenceEquals(null, other)) return false;
317  if (ReferenceEquals(this, other)) return true;
318  return _sid.Equals(other._sid) && Type == other.Type
319  && TickType == other.TickType
320  && Resolution == other.Resolution
321  && FillDataForward == other.FillDataForward
323  && IsInternalFeed == other.IsInternalFeed
324  && IsCustomData == other.IsCustomData
325  && DataTimeZone.Equals(other.DataTimeZone)
326  && DataMappingMode == other.DataMappingMode
327  && ExchangeTimeZone.Equals(other.ExchangeTimeZone)
330  && _mappedConfig == other._mappedConfig;
331  }
332 
333  /// <summary>
334  /// Determines whether the specified object is equal to the current object.
335  /// </summary>
336  /// <returns>
337  /// true if the specified object is equal to the current object; otherwise, false.
338  /// </returns>
339  /// <param name="obj">The object to compare with the current object. </param>
340  public override bool Equals(object obj)
341  {
342  if (ReferenceEquals(null, obj)) return false;
343  if (ReferenceEquals(this, obj)) return true;
344  if (obj.GetType() != GetType()) return false;
345  return Equals((SubscriptionDataConfig) obj);
346  }
347 
348  /// <summary>
349  /// Serves as the default hash function.
350  /// </summary>
351  /// <returns>
352  /// A hash code for the current object.
353  /// </returns>
354  public override int GetHashCode()
355  {
356  unchecked
357  {
358  var hashCode = _sid.GetHashCode();
359  hashCode = (hashCode*397) ^ Type.GetHashCode();
360  hashCode = (hashCode*397) ^ (int) TickType;
361  hashCode = (hashCode*397) ^ (int) Resolution;
362  hashCode = (hashCode*397) ^ FillDataForward.GetHashCode();
363  hashCode = (hashCode*397) ^ ExtendedMarketHours.GetHashCode();
364  hashCode = (hashCode*397) ^ IsInternalFeed.GetHashCode();
365  hashCode = (hashCode*397) ^ IsCustomData.GetHashCode();
366  hashCode = (hashCode*397) ^ DataMappingMode.GetHashCode();
367  hashCode = (hashCode*397) ^ DataTimeZone.Id.GetHashCode();// timezone hash is expensive, use id instead
368  hashCode = (hashCode*397) ^ ExchangeTimeZone.Id.GetHashCode();// timezone hash is expensive, use id instead
369  hashCode = (hashCode*397) ^ ContractDepthOffset.GetHashCode();
370  hashCode = (hashCode*397) ^ IsFilteredSubscription.GetHashCode();
371  hashCode = (hashCode*397) ^ _mappedConfig.GetHashCode();
372  return hashCode;
373  }
374  }
375 
376  /// <summary>
377  /// Override equals operator
378  /// </summary>
380  {
381  return Equals(left, right);
382  }
383 
384  /// <summary>
385  /// Override not equals operator
386  /// </summary>
388  {
389  return !Equals(left, right);
390  }
391 
392  /// <summary>
393  /// Returns a string that represents the current object.
394  /// </summary>
395  /// <returns>
396  /// A string that represents the current object.
397  /// </returns>
398  /// <filterpriority>2</filterpriority>
399  public override string ToString()
400  {
401  return Invariant($"{Symbol.Value},#{ContractDepthOffset},{MappedSymbol},{Resolution},{Type.Name},{TickType},{DataNormalizationMode},{DataMappingMode}{(IsInternalFeed ? ",Internal" : string.Empty)}");
402  }
403 
404  /// <summary>
405  /// New base class for all event classes.
406  /// </summary>
407  public class NewSymbolEventArgs : EventArgs
408  {
409  /// <summary>
410  /// The old symbol instance
411  /// </summary>
412  public Symbol Old { get; }
413 
414  /// <summary>
415  /// The new symbol instance
416  /// </summary>
417  public Symbol New { get; }
418 
419  /// <summary>
420  /// Create an instance of NewSymbolEventArgs
421  /// </summary>
422  /// <param name="new"></param>
423  /// <param name="old"></param>
424  public NewSymbolEventArgs(Symbol @new, Symbol old)
425  {
426  New = @new;
427  Old = old;
428  }
429  }
430  }
431 }