Lean  $LEAN_TAG$
CoarseFundamental.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.Globalization;
18 using System.IO;
19 using System.Linq;
20 using static QuantConnect.StringExtensions;
21 
23 {
24  /// <summary>
25  /// Defines summary information about a single symbol for a given date
26  /// </summary>
27  public class CoarseFundamental : BaseData
28  {
29  /// <summary>
30  /// Gets the market for this symbol
31  /// </summary>
32  public string Market { get; set; }
33 
34  /// <summary>
35  /// Gets the day's dollar volume for this symbol
36  /// </summary>
37  public decimal DollarVolume { get; set; }
38 
39  /// <summary>
40  /// Gets the day's total volume
41  /// </summary>
42  public long Volume { get; set; }
43 
44  /// <summary>
45  /// Returns whether the symbol has fundamental data for the given date
46  /// </summary>
47  public bool HasFundamentalData { get; set; }
48 
49  /// <summary>
50  /// Gets the price factor for the given date
51  /// </summary>
52  public decimal PriceFactor { get; set; } = 1m;
53 
54  /// <summary>
55  /// Gets the split factor for the given date
56  /// </summary>
57  public decimal SplitFactor { get; set; } = 1m;
58 
59  /// <summary>
60  /// Gets the combined factor used to create adjusted prices from raw prices
61  /// </summary>
63 
64  /// <summary>
65  /// Gets the split and dividend adjusted price
66  /// </summary>
67  public decimal AdjustedPrice => Price * PriceScaleFactor;
68 
69  /// <summary>
70  /// The end time of this data.
71  /// </summary>
72  public override DateTime EndTime
73  {
74  get { return Time + QuantConnect.Time.OneDay; }
75  set { Time = value - QuantConnect.Time.OneDay; }
76  }
77 
78  /// <summary>
79  /// Gets the raw price
80  /// </summary>
81  public override decimal Price => Value;
82 
83  /// <summary>
84  /// Initializes a new instance of the <see cref="CoarseFundamental"/> class
85  /// </summary>
87  {
88  DataType = MarketDataType.Auxiliary;
89  }
90 
91  /// <summary>
92  /// Return the URL string source of the file. This will be converted to a stream
93  /// </summary>
94  /// <param name="config">Configuration object</param>
95  /// <param name="date">Date of this source file</param>
96  /// <param name="isLiveMode">true if we're in live mode, false for backtesting mode</param>
97  /// <returns>String URL of source file.</returns>
98  public override SubscriptionDataSource GetSource(SubscriptionDataConfig config, DateTime date, bool isLiveMode)
99  {
100  var path = Path.Combine(Globals.DataFolder, "equity", config.Market, "fundamental", "coarse", Invariant($"{date:yyyyMMdd}.csv"));
101  return new SubscriptionDataSource(path, SubscriptionTransportMedium.LocalFile, FileFormat.Csv);
102  }
103 
104  /// <summary>
105  /// 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
106  /// each time it is called.
107  /// </summary>
108  /// <param name="config">Subscription data config setup object</param>
109  /// <param name="line">Line of the source document</param>
110  /// <param name="date">Date of the requested data</param>
111  /// <param name="isLiveMode">true if we're in live mode, false for backtesting mode</param>
112  /// <returns>Instance of the T:BaseData object generated by this line of the CSV</returns>
113  public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, bool isLiveMode)
114  {
115  try
116  {
117  var csv = line.Split(',');
118  var coarse = new CoarseFundamental
119  {
120  Symbol = new Symbol(SecurityIdentifier.Parse(csv[0]), csv[1]),
121  Time = date,
122  Market = config.Market,
123  Value = csv[2].ToDecimal(),
124  Volume = csv[3].ToInt64(),
125  DollarVolume = csv[4].ToDecimal()
126  };
127 
128  if (csv.Length > 5)
129  {
130  coarse.HasFundamentalData = csv[5].ConvertInvariant<bool>();
131  }
132 
133  if (csv.Length > 7)
134  {
135  coarse.PriceFactor = csv[6].ToDecimal();
136  coarse.SplitFactor = csv[7].ToDecimal();
137  }
138 
139  return coarse;
140  }
141  catch (Exception)
142  {
143  return null;
144  }
145  }
146 
147  /// <summary>
148  /// Return a new instance clone of this object, used in fill forward
149  /// </summary>
150  /// <returns>A clone of the current object</returns>
151  public override BaseData Clone()
152  {
153  return new CoarseFundamental
154  {
155  Symbol = Symbol,
156  Time = Time,
158  Market = Market,
159  Value = Value,
160  Volume = Volume,
161  DataType = MarketDataType.Auxiliary,
165  };
166  }
167 
168  /// <summary>
169  /// Creates the symbol used for coarse fundamental data
170  /// </summary>
171  /// <param name="market">The market</param>
172  /// <returns>A coarse universe symbol for the specified market</returns>
173  public static Symbol CreateUniverseSymbol(string market)
174  {
175  market = market.ToLowerInvariant();
176  var ticker = $"qc-universe-coarse-{market}-{Guid.NewGuid()}";
178  return new Symbol(sid, ticker);
179  }
180 
181  /// <summary>
182  /// Converts a given fundamental data point into row format
183  /// </summary>
184  public static string ToRow(CoarseFundamental coarse)
185  {
186  // sid,symbol,close,volume,dollar volume,has fundamental data,price factor,split factor
187  var values = new object[]
188  {
189  coarse.Symbol.ID,
190  coarse.Symbol.Value,
191  coarse.Value,
192  coarse.Volume,
193  coarse.DollarVolume,
194  coarse.HasFundamentalData,
195  coarse.PriceFactor,
196  coarse.SplitFactor
197  };
198 
199  return string.Join(",", values.Select(s => Convert.ToString(s, CultureInfo.InvariantCulture)));
200  }
201  }
202 }