Lean  $LEAN_TAG$
ConstituentsUniverseData.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 System.IO;
18 using System.Collections.Generic;
19 
21 {
22  /// <summary>
23  /// Custom base data class used for <see cref="ConstituentsUniverse"/>
24  /// </summary>
26  {
27  /// <summary>
28  /// Initializes a new instance of the <see cref="CoarseFundamental"/> class
29  /// </summary>
31  {
32  DataType = MarketDataType.Auxiliary;
33  }
34 
35  /// <summary>
36  /// The end time of this data.
37  /// </summary>
38  public override DateTime EndTime
39  {
40  get { return Time + QuantConnect.Time.OneDay; }
41  set { Time = value - QuantConnect.Time.OneDay; }
42  }
43 
44  /// <summary>
45  /// Return the URL string source of the file. This will be converted to a stream
46  /// </summary>
47  /// <param name="config">Configuration object</param>
48  /// <param name="date">Date of this source file</param>
49  /// <param name="isLiveMode">true if we're in live mode, false for backtesting mode</param>
50  /// <returns>String URL of source file.</returns>
51  public override SubscriptionDataSource GetSource(SubscriptionDataConfig config, DateTime date, bool isLiveMode)
52  {
53  var universe = config.Symbol.ID.Symbol.Substring(config.MappedSymbol.LastIndexOf('-') + 1);
54 
55  if (isLiveMode)
56  {
57  date = date.AddDays(-1);
58  }
59  var path = Path.Combine(Globals.DataFolder,
60  config.SecurityType.SecurityTypeToLower(),
61  config.Market,
62  "universes",
63  config.Resolution.ResolutionToLower(),
64  universe,
65  $"{date:yyyyMMdd}.csv");
66  return new SubscriptionDataSource(path, SubscriptionTransportMedium.LocalFile, FileFormat.Csv);
67  }
68 
69  /// <summary>
70  /// 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
71  /// each time it is called.
72  /// </summary>
73  /// <param name="config">Subscription data config setup object</param>
74  /// <param name="line">Line of the source document</param>
75  /// <param name="date">Date of the requested data</param>
76  /// <param name="isLiveMode">true if we're in live mode, false for backtesting mode</param>
77  /// <returns>Instance of the T:BaseData object generated by this line of the CSV</returns>
78  public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, bool isLiveMode)
79  {
80  try
81  {
82  var csv = line.Split(',');
83  var preselected = new ConstituentsUniverseData
84  {
85  Symbol = new Symbol(SecurityIdentifier.Parse(csv[1]), csv[0]),
86  Time = isLiveMode ? date.AddDays(-1) : date
87  };
88 
89  return preselected;
90  }
91  catch
92  {
93  return null;
94  }
95  }
96 
97  /// <summary>
98  /// Indicates that the data set is expected to be sparse
99  /// </summary>
100  /// <remarks>Relies on the <see cref="Symbol"/> property value</remarks>
101  /// <remarks>This is a method and not a property so that python
102  /// custom data types can override it</remarks>
103  /// <returns>True if the data set represented by this type is expected to be sparse</returns>
104  public override bool IsSparseData()
105  {
106  return true;
107  }
108 
109  /// <summary>
110  /// Indicates if there is support for mapping
111  /// </summary>
112  /// <returns>True indicates mapping should be used</returns>
113  public override bool RequiresMapping()
114  {
115  return false;
116  }
117 
118  /// <summary>
119  /// Gets the default resolution for this data and security type
120  /// </summary>
121  /// <remarks>This is a method and not a property so that python
122  /// custom data types can override it</remarks>
123  public override Resolution DefaultResolution()
124  {
125  return Resolution.Daily;
126  }
127 
128  /// <summary>
129  /// Gets the supported resolution for this data and security type
130  /// </summary>
131  /// <remarks>Relies on the <see cref="Symbol"/> property value</remarks>
132  /// <remarks>This is a method and not a property so that python
133  /// custom data types can override it</remarks>
134  public override List<Resolution> SupportedResolutions()
135  {
136  return DailyResolution;
137  }
138  }
139 }