Lean  $LEAN_TAG$
BaseData.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 ProtoBuf;
19 using System.IO;
20 using System.Linq;
21 using Newtonsoft.Json;
22 using QuantConnect.Util;
24 using System.Collections.Generic;
25 using QuantConnect.Python;
26 
27 namespace QuantConnect.Data
28 {
29  /// <summary>
30  /// Abstract base data class of QuantConnect. It is intended to be extended to define
31  /// generic user customizable data types while at the same time implementing the basics of data where possible
32  /// </summary>
33  [ProtoContract(SkipConstructor = true)]
34  [ProtoInclude(8, typeof(Tick))]
35  [ProtoInclude(100, typeof(TradeBar))]
36  [ProtoInclude(200, typeof(QuoteBar))]
37  [ProtoInclude(300, typeof(Dividend))]
38  [ProtoInclude(400, typeof(Split))]
39  [PandasIgnoreMembers]
40  public abstract class BaseData : IBaseData
41  {
42  private decimal _value;
43 
44  /// <summary>
45  /// A list of all <see cref="Resolution"/>
46  /// </summary>
47  protected static readonly List<Resolution> AllResolutions =
48  Enum.GetValues(typeof(Resolution)).Cast<Resolution>().ToList();
49 
50  /// <summary>
51  /// A list of <see cref="Resolution.Daily"/>
52  /// </summary>
53  protected static readonly List<Resolution> DailyResolution = new List<Resolution> { Resolution.Daily };
54 
55  /// <summary>
56  /// A list of <see cref="Resolution.Minute"/>
57  /// </summary>
58  protected static readonly List<Resolution> MinuteResolution = new List<Resolution> { Resolution.Minute };
59 
60  /// <summary>
61  /// A list of high <see cref="Resolution"/>, including minute, second, and tick.
62  /// </summary>
63  protected static readonly List<Resolution> HighResolution = new List<Resolution> { Resolution.Minute, Resolution.Second, Resolution.Tick };
64 
65  /// <summary>
66  /// A list of resolutions support by Options
67  /// </summary>
68  protected static readonly List<Resolution> OptionResolutions = new List<Resolution> { Resolution.Daily, Resolution.Hour, Resolution.Minute };
69 
70  /// <summary>
71  /// Market Data Type of this data - does it come in individual price packets or is it grouped into OHLC.
72  /// </summary>
73  /// <remarks>Data is classed into two categories - streams of instantaneous prices and groups of OHLC data.</remarks>
74  [ProtoMember(1)]
75  public MarketDataType DataType { get; set; } = MarketDataType.Base;
76 
77  /// <summary>
78  /// True if this is a fill forward piece of data
79  /// </summary>
80  public bool IsFillForward { get; private set; }
81 
82  /// <summary>
83  /// Current time marker of this data packet.
84  /// </summary>
85  /// <remarks>All data is timeseries based.</remarks>
86  [ProtoMember(2)]
87  public DateTime Time { get; set; }
88 
89  /// <summary>
90  /// The end time of this data. Some data covers spans (trade bars) and as such we want
91  /// to know the entire time span covered
92  /// </summary>
93  // NOTE: This is needed event though the class is marked with [PandasIgnoreMembers] because the property is virtual.
94  // If a derived class overrides it, without [PandasIgnore], the property will not be ignored.
95  [PandasIgnore]
96  public virtual DateTime EndTime
97  {
98  get { return Time; }
99  set { Time = value; }
100  }
101 
102  /// <summary>
103  /// Symbol representation for underlying Security
104  /// </summary>
105  public Symbol Symbol { get; set; } = Symbol.Empty;
106 
107  /// <summary>
108  /// Value representation of this data packet. All data requires a representative value for this moment in time.
109  /// For streams of data this is the price now, for OHLC packets this is the closing price.
110  /// </summary>
111  [ProtoMember(4)]
112  [PandasIgnore]
113  public virtual decimal Value
114  {
115  get
116  {
117  return _value;
118  }
119  set
120  {
121  _value = value;
122  }
123  }
124 
125  /// <summary>
126  /// As this is a backtesting platform we'll provide an alias of value as price.
127  /// </summary>
128  [PandasIgnore]
129  public virtual decimal Price => Value;
130 
131  /// <summary>
132  /// Constructor for initialising the dase data class
133  /// </summary>
134  public BaseData()
135  {
136  //Empty constructor required for fast-reflection initialization
137  }
138 
139  /// <summary>
140  /// Reader converts each line of the data source into BaseData objects. Each data type creates its own factory method, and returns a new instance of the object
141  /// each time it is called. The returned object is assumed to be time stamped in the config.ExchangeTimeZone.
142  /// </summary>
143  /// <param name="config">Subscription data config setup object</param>
144  /// <param name="line">Line of the source document</param>
145  /// <param name="date">Date of the requested data</param>
146  /// <param name="isLiveMode">true if we're in live mode, false for backtesting mode</param>
147  /// <returns>Instance of the T:BaseData object generated by this line of the CSV</returns>
148  public virtual BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, bool isLiveMode)
149  {
150  // stub implementation to prevent compile errors in user algorithms
151  var dataFeed = isLiveMode ? DataFeedEndpoint.LiveTrading : DataFeedEndpoint.Backtesting;
152 #pragma warning disable 618 // This implementation is left here for backwards compatibility of the BaseData API
153  return Reader(config, line, date, dataFeed);
154 #pragma warning restore 618
155  }
156 
157  /// <summary>
158  /// Reader converts each line of the data source into BaseData objects. Each data type creates its own factory method, and returns a new instance of the object
159  /// each time it is called. The returned object is assumed to be time stamped in the config.ExchangeTimeZone.
160  /// </summary>
161  /// <param name="config">Subscription data config setup object</param>
162  /// <param name="stream">The data stream</param>
163  /// <param name="date">Date of the requested data</param>
164  /// <param name="isLiveMode">true if we're in live mode, false for backtesting mode</param>
165  /// <returns>Instance of the T:BaseData object generated by this line of the CSV</returns>
166  public virtual BaseData Reader(SubscriptionDataConfig config, StreamReader stream, DateTime date, bool isLiveMode)
167  {
168  throw new NotImplementedException("Each data types has to implement is own Stream reader");
169  }
170 
171  /// <summary>
172  /// Return the URL string source of the file. This will be converted to a stream
173  /// </summary>
174  /// <param name="config">Configuration object</param>
175  /// <param name="date">Date of this source file</param>
176  /// <param name="isLiveMode">true if we're in live mode, false for backtesting mode</param>
177  /// <returns>String URL of source file.</returns>
178  public virtual SubscriptionDataSource GetSource(SubscriptionDataConfig config, DateTime date, bool isLiveMode)
179  {
180  // stub implementation to prevent compile errors in user algorithms
181  var dataFeed = isLiveMode ? DataFeedEndpoint.LiveTrading : DataFeedEndpoint.Backtesting;
182 #pragma warning disable 618 // This implementation is left here for backwards compatibility of the BaseData API
183  var source = GetSource(config, date, dataFeed);
184 #pragma warning restore 618
185 
186  if (isLiveMode)
187  {
188  // live trading by default always gets a rest endpoint
189  return new SubscriptionDataSource(source, SubscriptionTransportMedium.Rest);
190  }
191 
192  // construct a uri to determine if we have a local or remote file
193  var uri = new Uri(source, UriKind.RelativeOrAbsolute);
194 
195  if (uri.IsAbsoluteUri && !uri.IsLoopback)
196  {
197  return new SubscriptionDataSource(source, SubscriptionTransportMedium.RemoteFile);
198  }
199 
200  return new SubscriptionDataSource(source, SubscriptionTransportMedium.LocalFile);
201  }
202 
203  /// <summary>
204  /// Indicates if there is support for mapping
205  /// </summary>
206  /// <remarks>Relies on the <see cref="Symbol"/> property value</remarks>
207  /// <returns>True indicates mapping should be used</returns>
208  public virtual bool RequiresMapping()
209  {
210  return Symbol.RequiresMapping();
211  }
212 
213  /// <summary>
214  /// Indicates that the data set is expected to be sparse
215  /// </summary>
216  /// <remarks>Relies on the <see cref="Symbol"/> property value</remarks>
217  /// <remarks>This is a method and not a property so that python
218  /// custom data types can override it</remarks>
219  /// <returns>True if the data set represented by this type is expected to be sparse</returns>
220  public virtual bool IsSparseData()
221  {
222  // by default, we'll assume all custom data is sparse data
223  return Symbol.SecurityType == SecurityType.Base;
224  }
225 
226  /// <summary>
227  /// Indicates whether this contains data that should be stored in the security cache
228  /// </summary>
229  /// <returns>Whether this contains data that should be stored in the security cache</returns>
230  public virtual bool ShouldCacheToSecurity()
231  {
232  return true;
233  }
234 
235  /// <summary>
236  /// Gets the default resolution for this data and security type
237  /// </summary>
238  /// <remarks>This is a method and not a property so that python
239  /// custom data types can override it</remarks>
240  public virtual Resolution DefaultResolution()
241  {
242  return Resolution.Minute;
243  }
244 
245  /// <summary>
246  /// Gets the supported resolution for this data and security type
247  /// </summary>
248  /// <remarks>Relies on the <see cref="Symbol"/> property value</remarks>
249  /// <remarks>This is a method and not a property so that python
250  /// custom data types can override it</remarks>
251  public virtual List<Resolution> SupportedResolutions()
252  {
253  if (Symbol.SecurityType.IsOption() || Symbol.SecurityType == SecurityType.Index)
254  {
255  return OptionResolutions;
256  }
257 
258  return AllResolutions;
259  }
260 
261  /// <summary>
262  /// Specifies the data time zone for this data type. This is useful for custom data types
263  /// </summary>
264  /// <remarks>Will throw <see cref="InvalidOperationException"/> for security types
265  /// other than <see cref="SecurityType.Base"/></remarks>
266  /// <returns>The <see cref="DateTimeZone"/> of this data type</returns>
267  public virtual DateTimeZone DataTimeZone()
268  {
269  if (Symbol.SecurityType != SecurityType.Base)
270  {
271  throw new InvalidOperationException("BaseData.DataTimeZone(): is only valid for base data types");
272  }
273  return TimeZones.NewYork;
274  }
275 
276  /// <summary>
277  /// Updates this base data with a new trade
278  /// </summary>
279  /// <param name="lastTrade">The price of the last trade</param>
280  /// <param name="tradeSize">The quantity traded</param>
281  public void UpdateTrade(decimal lastTrade, decimal tradeSize)
282  {
283  Update(lastTrade, 0, 0, tradeSize, 0, 0);
284  }
285 
286  /// <summary>
287  /// Updates this base data with new quote information
288  /// </summary>
289  /// <param name="bidPrice">The current bid price</param>
290  /// <param name="bidSize">The current bid size</param>
291  /// <param name="askPrice">The current ask price</param>
292  /// <param name="askSize">The current ask size</param>
293  public void UpdateQuote(decimal bidPrice, decimal bidSize, decimal askPrice, decimal askSize)
294  {
295  Update(0, bidPrice, askPrice, 0, bidSize, askSize);
296  }
297 
298  /// <summary>
299  /// Updates this base data with the new quote bid information
300  /// </summary>
301  /// <param name="bidPrice">The current bid price</param>
302  /// <param name="bidSize">The current bid size</param>
303  public void UpdateBid(decimal bidPrice, decimal bidSize)
304  {
305  Update(0, bidPrice, 0, 0, bidSize, 0);
306  }
307 
308  /// <summary>
309  /// Updates this base data with the new quote ask information
310  /// </summary>
311  /// <param name="askPrice">The current ask price</param>
312  /// <param name="askSize">The current ask size</param>
313  public void UpdateAsk(decimal askPrice, decimal askSize)
314  {
315  Update(0, 0, askPrice, 0, 0, askSize);
316  }
317 
318  /// <summary>
319  /// Update routine to build a bar/tick from a data update.
320  /// </summary>
321  /// <param name="lastTrade">The last trade price</param>
322  /// <param name="bidPrice">Current bid price</param>
323  /// <param name="askPrice">Current asking price</param>
324  /// <param name="volume">Volume of this trade</param>
325  /// <param name="bidSize">The size of the current bid, if available</param>
326  /// <param name="askSize">The size of the current ask, if available</param>
327  public virtual void Update(decimal lastTrade, decimal bidPrice, decimal askPrice, decimal volume, decimal bidSize, decimal askSize)
328  {
329  Value = lastTrade;
330  }
331 
332  /// <summary>
333  /// Return a new instance clone of this object, used in fill forward
334  /// </summary>
335  /// <remarks>
336  /// This base implementation uses reflection to copy all public fields and properties
337  /// </remarks>
338  /// <param name="fillForward">True if this is a fill forward clone</param>
339  /// <returns>A clone of the current object</returns>
340  public virtual BaseData Clone(bool fillForward)
341  {
342  var clone = Clone();
343  clone.IsFillForward = fillForward;
344  return clone;
345  }
346 
347  /// <summary>
348  /// Return a new instance clone of this object, used in fill forward
349  /// </summary>
350  /// <remarks>
351  /// This base implementation uses reflection to copy all public fields and properties
352  /// </remarks>
353  /// <returns>A clone of the current object</returns>
354  public virtual BaseData Clone()
355  {
356  return (BaseData) ObjectActivator.Clone((object)this);
357  }
358 
359  /// <summary>
360  /// Formats a string with the symbol and value.
361  /// </summary>
362  /// <returns>string - a string formatted as SPY: 167.753</returns>
363  public override string ToString()
364  {
365  return $"{Symbol}: {Value.ToStringInvariant("C")}";
366  }
367 
368  /// <summary>
369  /// Reader converts each line of the data source into BaseData objects. Each data type creates its own factory method, and returns a new instance of the object
370  /// each time it is called.
371  /// </summary>
372  /// <remarks>OBSOLETE:: This implementation is added for backward/forward compatibility purposes. This function is no longer called by the LEAN engine.</remarks>
373  /// <param name="config">Subscription data config setup object</param>
374  /// <param name="line">Line of the source document</param>
375  /// <param name="date">Date of the requested data</param>
376  /// <param name="dataFeed">Type of datafeed we're requesting - a live or backtest feed.</param>
377  /// <returns>Instance of the T:BaseData object generated by this line of the CSV</returns>
378  [Obsolete("Reader(SubscriptionDataConfig, string, DateTime, DataFeedEndpoint) method has been made obsolete, use Reader(SubscriptionDataConfig, string, DateTime, bool) instead.")]
379  public virtual BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, DataFeedEndpoint dataFeed)
380  {
381  throw new InvalidOperationException(
382  $"Please implement Reader(SubscriptionDataConfig, string, DateTime, bool) on your custom data type: {GetType().Name}"
383  );
384  }
385 
386  /// <summary>
387  /// Return the URL string source of the file. This will be converted to a stream
388  /// </summary>
389  /// <remarks>OBSOLETE:: This implementation is added for backward/forward compatibility purposes. This function is no longer called by the LEAN engine.</remarks>
390  /// <param name="config">Configuration object</param>
391  /// <param name="date">Date of this source file</param>
392  /// <param name="datafeed">Type of datafeed we're reqesting - backtest or live</param>
393  /// <returns>String URL of source file.</returns>
394  [Obsolete("GetSource(SubscriptionDataConfig, DateTime, DataFeedEndpoint) method has been made obsolete, use GetSource(SubscriptionDataConfig, DateTime, bool) instead.")]
395  public virtual string GetSource(SubscriptionDataConfig config, DateTime date, DataFeedEndpoint datafeed)
396  {
397  throw new InvalidOperationException(
398  $"Please implement GetSource(SubscriptionDataConfig, DateTime, bool) on your custom data type: {GetType().Name}"
399  );
400  }
401 
402  /// <summary>
403  /// Deserialize the message from the data server
404  /// </summary>
405  /// <param name="serialized">The data server's message</param>
406  /// <returns>An enumerable of base data, if unsuccessful, returns an empty enumerable</returns>
407  public static IEnumerable<BaseData> DeserializeMessage(string serialized)
408  {
409  var deserialized = JsonConvert.DeserializeObject(serialized, JsonSerializerSettings);
410 
411  var enumerable = deserialized as IEnumerable<BaseData>;
412  if (enumerable != null)
413  {
414  return enumerable;
415  }
416 
417  var data = deserialized as BaseData;
418  if (data != null)
419  {
420  return new[] { data };
421  }
422 
423  return Enumerable.Empty<BaseData>();
424  }
425 
426  private static readonly JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings
427  {
428  TypeNameHandling = TypeNameHandling.All
429  };
430  }
431 }