Lean  $LEAN_TAG$
MarketHoursDatabaseJsonConverter.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.Collections.Generic;
18 using System.Globalization;
19 using System.Linq;
20 using Newtonsoft.Json;
21 using Newtonsoft.Json.Linq;
22 using NodaTime;
23 using QuantConnect.Logging;
26 
27 namespace QuantConnect.Util
28 {
29  /// <summary>
30  /// Provides json conversion for the <see cref="MarketHoursDatabase"/> class
31  /// </summary>
32  public class MarketHoursDatabaseJsonConverter : TypeChangeJsonConverter<MarketHoursDatabase, MarketHoursDatabaseJsonConverter.MarketHoursDatabaseJson>
33  {
34  /// <summary>
35  /// Convert the input value to a value to be serialzied
36  /// </summary>
37  /// <param name="value">The input value to be converted before serialziation</param>
38  /// <returns>A new instance of TResult that is to be serialzied</returns>
40  {
41  return new MarketHoursDatabaseJson(value);
42  }
43 
44  /// <summary>
45  /// Converts the input value to be deserialized
46  /// </summary>
47  /// <param name="value">The deserialized value that needs to be converted to T</param>
48  /// <returns>The converted value</returns>
50  {
51  return value.Convert();
52  }
53 
54  /// <summary>
55  /// Creates an instance of the un-projected type to be deserialized
56  /// </summary>
57  /// <param name="type">The input object type, this is the data held in the token</param>
58  /// <param name="token">The input data to be converted into a T</param>
59  /// <returns>A new instance of T that is to be serialized using default rules</returns>
60  protected override MarketHoursDatabase Create(Type type, JToken token)
61  {
62  var jobject = (JObject) token;
63  var instance = jobject.ToObject<MarketHoursDatabaseJson>();
64  return Convert(instance);
65  }
66 
67  /// <summary>
68  /// Defines the json structure of the market-hours-database.json file
69  /// </summary>
70  [JsonObject(MemberSerialization.OptIn)]
72  {
73  /// <summary>
74  /// The entries in the market hours database, keyed by <see cref="SecurityDatabaseKey"/>
75  /// </summary>
76  [JsonProperty("entries")]
77  public Dictionary<string, MarketHoursDatabaseEntryJson> Entries;
78 
79  /// <summary>
80  /// Initializes a new instance of the <see cref="MarketHoursDatabaseJson"/> class
81  /// </summary>
82  /// <param name="database">The database instance to copy</param>
84  {
85  if (database == null) return;
86  Entries = new Dictionary<string, MarketHoursDatabaseEntryJson>();
87  foreach (var kvp in database.ExchangeHoursListing)
88  {
89  var key = kvp.Key;
90  var entry = kvp.Value;
91  Entries[key.ToString()] = new MarketHoursDatabaseEntryJson(entry);
92  }
93  }
94 
95  /// <summary>
96  /// Converts this json representation to the <see cref="MarketHoursDatabase"/> type
97  /// </summary>
98  /// <returns>A new instance of the <see cref="MarketHoursDatabase"/> class</returns>
100  {
101  // first we parse the entries keys so that later we can sort by security type
102  var entries = new Dictionary<SecurityDatabaseKey, MarketHoursDatabaseEntryJson>(Entries.Count);
103  foreach (var entry in Entries)
104  {
105  try
106  {
107  var key = SecurityDatabaseKey.Parse(entry.Key);
108  if (key != null)
109  {
110  entries[key] = entry.Value;
111  }
112  }
113  catch (Exception err)
114  {
115  Log.Error(err);
116  }
117  }
118 
119  var result = new Dictionary<SecurityDatabaseKey, MarketHoursDatabase.Entry>(Entries.Count);
120  // we sort so we process generic entries and non options first
121  foreach (var entry in entries.OrderBy(kvp => kvp.Key.Symbol != null ? 1 : 0).ThenBy(kvp => kvp.Key.SecurityType.IsOption() ? 1 : 0))
122  {
123  try
124  {
125  result.TryGetValue(entry.Key.CreateCommonKey(), out var marketEntry);
126  var underlyingEntry = GetUnderlyingEntry(entry.Key, result);
127  result[entry.Key] = entry.Value.Convert(underlyingEntry, marketEntry);
128  }
129  catch (Exception err)
130  {
131  Log.Error(err);
132  }
133  }
134  return new MarketHoursDatabase(result);
135  }
136 
137  /// <summary>
138  /// Helper method to get the already processed underlying entry for options
139  /// </summary>
140  private static MarketHoursDatabase.Entry GetUnderlyingEntry(SecurityDatabaseKey key, Dictionary<SecurityDatabaseKey, MarketHoursDatabase.Entry> result)
141  {
142  MarketHoursDatabase.Entry underlyingEntry = null;
143  if (key.SecurityType.IsOption())
144  {
145  // if option, let's get the underlyings entry
146  var underlyingSecurityType = Symbol.GetUnderlyingFromOptionType(key.SecurityType);
147  var underlying = OptionSymbol.MapToUnderlying(key.Symbol, key.SecurityType);
148  var underlyingKey = new SecurityDatabaseKey(key.Market, underlying, underlyingSecurityType);
149 
150  if (!result.TryGetValue(underlyingKey, out underlyingEntry)
151  // let's retry with the wildcard
152  && underlying != SecurityDatabaseKey.Wildcard)
153  {
154  var underlyingKeyWildCard = new SecurityDatabaseKey(key.Market, SecurityDatabaseKey.Wildcard, underlyingSecurityType);
155  result.TryGetValue(underlyingKeyWildCard, out underlyingEntry);
156  }
157  }
158  return underlyingEntry;
159  }
160  }
161 
162  /// <summary>
163  /// Defines the json structure of a single entry in the market-hours-database.json file
164  /// </summary>
165  [JsonObject(MemberSerialization.OptIn)]
167  {
168  /// <summary>
169  /// The data's raw time zone
170  /// </summary>
171  [JsonProperty("dataTimeZone")]
172  public string DataTimeZone;
173 
174  /// <summary>
175  /// The exchange's time zone id from the tzdb
176  /// </summary>
177  [JsonProperty("exchangeTimeZone")]
178  public string ExchangeTimeZone;
179 
180  /// <summary>
181  /// Sunday market hours segments
182  /// </summary>
183  [JsonProperty("sunday")]
184  public List<MarketHoursSegment> Sunday;
185 
186  /// <summary>
187  /// Monday market hours segments
188  /// </summary>
189  [JsonProperty("monday")]
190  public List<MarketHoursSegment> Monday;
191 
192  /// <summary>
193  /// Tuesday market hours segments
194  /// </summary>
195  [JsonProperty("tuesday")]
196  public List<MarketHoursSegment> Tuesday;
197 
198  /// <summary>
199  /// Wednesday market hours segments
200  /// </summary>
201  [JsonProperty("wednesday")]
202  public List<MarketHoursSegment> Wednesday;
203 
204  /// <summary>
205  /// Thursday market hours segments
206  /// </summary>
207  [JsonProperty("thursday")]
208  public List<MarketHoursSegment> Thursday;
209 
210  /// <summary>
211  /// Friday market hours segments
212  /// </summary>
213  [JsonProperty("friday")]
214  public List<MarketHoursSegment> Friday;
215 
216  /// <summary>
217  /// Saturday market hours segments
218  /// </summary>
219  [JsonProperty("saturday")]
220  public List<MarketHoursSegment> Saturday;
221 
222  /// <summary>
223  /// Holiday date strings
224  /// </summary>
225  [JsonProperty("holidays")]
226  public List<string> Holidays = new();
227 
228  /// <summary>
229  /// Early closes by date
230  /// </summary>
231  [JsonProperty("earlyCloses")]
232  public Dictionary<string, TimeSpan> EarlyCloses = new Dictionary<string, TimeSpan>();
233 
234  /// <summary>
235  /// Late opens by date
236  /// </summary>
237  [JsonProperty("lateOpens")]
238  public Dictionary<string, TimeSpan> LateOpens = new Dictionary<string, TimeSpan>();
239 
240  /// <summary>
241  /// Initializes a new instance of the <see cref="MarketHoursDatabaseEntryJson"/> class
242  /// </summary>
243  /// <param name="entry">The entry instance to copy</param>
245  {
246  if (entry == null) return;
247  DataTimeZone = entry.DataTimeZone.Id;
248  var hours = entry.ExchangeHours;
249  ExchangeTimeZone = hours.TimeZone.Id;
250  SetSegmentsForDay(hours, DayOfWeek.Sunday, out Sunday);
251  SetSegmentsForDay(hours, DayOfWeek.Monday, out Monday);
252  SetSegmentsForDay(hours, DayOfWeek.Tuesday, out Tuesday);
253  SetSegmentsForDay(hours, DayOfWeek.Wednesday, out Wednesday);
254  SetSegmentsForDay(hours, DayOfWeek.Thursday, out Thursday);
255  SetSegmentsForDay(hours, DayOfWeek.Friday, out Friday);
256  SetSegmentsForDay(hours, DayOfWeek.Saturday, out Saturday);
257  Holidays = hours.Holidays.Select(x => x.ToString("M/d/yyyy", CultureInfo.InvariantCulture)).ToList();
258  EarlyCloses = entry.ExchangeHours.EarlyCloses.ToDictionary(pair => pair.Key.ToString("M/d/yyyy", CultureInfo.InvariantCulture), pair => pair.Value);
259  LateOpens = entry.ExchangeHours.LateOpens.ToDictionary(pair => pair.Key.ToString("M/d/yyyy", CultureInfo.InvariantCulture), pair => pair.Value);
260  }
261 
262  /// <summary>
263  /// Converts this json representation to the <see cref="MarketHoursDatabase.Entry"/> type
264  /// </summary>
265  /// <returns>A new instance of the <see cref="MarketHoursDatabase.Entry"/> class</returns>
267  {
268  var hours = new Dictionary<DayOfWeek, LocalMarketHours>
269  {
270  { DayOfWeek.Sunday, new LocalMarketHours(DayOfWeek.Sunday, Sunday) },
271  { DayOfWeek.Monday, new LocalMarketHours(DayOfWeek.Monday, Monday) },
272  { DayOfWeek.Tuesday, new LocalMarketHours(DayOfWeek.Tuesday, Tuesday) },
273  { DayOfWeek.Wednesday, new LocalMarketHours(DayOfWeek.Wednesday, Wednesday) },
274  { DayOfWeek.Thursday, new LocalMarketHours(DayOfWeek.Thursday, Thursday) },
275  { DayOfWeek.Friday, new LocalMarketHours(DayOfWeek.Friday, Friday) },
276  { DayOfWeek.Saturday, new LocalMarketHours(DayOfWeek.Saturday, Saturday) }
277  };
278  var holidayDates = Holidays.Select(x => DateTime.ParseExact(x, "M/d/yyyy", CultureInfo.InvariantCulture)).ToHashSet();
279  IReadOnlyDictionary<DateTime, TimeSpan> earlyCloses = EarlyCloses.ToDictionary(x => DateTime.ParseExact(x.Key, "M/d/yyyy", CultureInfo.InvariantCulture), x => x.Value);
280  IReadOnlyDictionary<DateTime, TimeSpan> lateOpens = LateOpens.ToDictionary(x => DateTime.ParseExact(x.Key, "M/d/yyyy", CultureInfo.InvariantCulture), x => x.Value);
281 
282  if(underlyingEntry != null)
283  {
284  // If we have no entries but the underlying does, let's use the underlyings
285  if (holidayDates.Count == 0)
286  {
287  holidayDates = underlyingEntry.ExchangeHours.Holidays;
288  }
289  if (earlyCloses.Count == 0)
290  {
291  earlyCloses = underlyingEntry.ExchangeHours.EarlyCloses;
292  }
293  if (lateOpens.Count == 0)
294  {
295  lateOpens = underlyingEntry.ExchangeHours.LateOpens;
296  }
297  }
298 
299  if(marketEntry != null)
300  {
301  if (marketEntry.ExchangeHours.Holidays.Count > 0)
302  {
303  holidayDates.UnionWith(marketEntry.ExchangeHours.Holidays);
304  }
305 
306  if (marketEntry.ExchangeHours.EarlyCloses.Count > 0 )
307  {
308  earlyCloses = MergeLateOpensAndEarlyCloses(marketEntry.ExchangeHours.EarlyCloses, earlyCloses);
309  }
310 
311  if (marketEntry.ExchangeHours.LateOpens.Count > 0)
312  {
313  lateOpens = MergeLateOpensAndEarlyCloses(marketEntry.ExchangeHours.LateOpens, lateOpens);
314  }
315  }
316 
317  var exchangeHours = new SecurityExchangeHours(DateTimeZoneProviders.Tzdb[ExchangeTimeZone], holidayDates, hours, earlyCloses, lateOpens);
318  return new MarketHoursDatabase.Entry(DateTimeZoneProviders.Tzdb[DataTimeZone], exchangeHours);
319  }
320 
321  private void SetSegmentsForDay(SecurityExchangeHours hours, DayOfWeek day, out List<MarketHoursSegment> segments)
322  {
323  LocalMarketHours local;
324  if (hours.MarketHours.TryGetValue(day, out local))
325  {
326  segments = local.Segments.ToList();
327  }
328  else
329  {
330  segments = new List<MarketHoursSegment>();
331  }
332  }
333 
334  /// <summary>
335  /// Merges the late opens or early closes from the common entry (with wildcards) with the specific entry
336  /// (e.g. Indices-usa-[*] with Indices-usa-VIX).
337  /// The specific entry takes precedence.
338  /// </summary>
339  private static Dictionary<DateTime, TimeSpan> MergeLateOpensAndEarlyCloses(IReadOnlyDictionary<DateTime, TimeSpan> common,
340  IReadOnlyDictionary<DateTime, TimeSpan> specific)
341  {
342  var result = common.ToDictionary();
343  foreach (var (key, value) in specific)
344  {
345  result[key] = value;
346  }
347 
348  return result;
349  }
350  }
351  }
352 }