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 { get; set; }
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 { get; set; }
173 
174  /// <summary>
175  /// The exchange's time zone id from the tzdb
176  /// </summary>
177  [JsonProperty("exchangeTimeZone")]
178  public string ExchangeTimeZone { get; set; }
179 
180  /// <summary>
181  /// Sunday market hours segments
182  /// </summary>
183  [JsonProperty("sunday")]
184  public List<MarketHoursSegment> Sunday { get; set; }
185 
186  /// <summary>
187  /// Monday market hours segments
188  /// </summary>
189  [JsonProperty("monday")]
190  public List<MarketHoursSegment> Monday { get; set; }
191 
192  /// <summary>
193  /// Tuesday market hours segments
194  /// </summary>
195  [JsonProperty("tuesday")]
196  public List<MarketHoursSegment> Tuesday { get; set; }
197 
198  /// <summary>
199  /// Wednesday market hours segments
200  /// </summary>
201  [JsonProperty("wednesday")]
202  public List<MarketHoursSegment> Wednesday { get; set; }
203 
204  /// <summary>
205  /// Thursday market hours segments
206  /// </summary>
207  [JsonProperty("thursday")]
208  public List<MarketHoursSegment> Thursday { get; set; }
209 
210  /// <summary>
211  /// Friday market hours segments
212  /// </summary>
213  [JsonProperty("friday")]
214  public List<MarketHoursSegment> Friday { get; set; }
215 
216  /// <summary>
217  /// Saturday market hours segments
218  /// </summary>
219  [JsonProperty("saturday")]
220  public List<MarketHoursSegment> Saturday { get; set; }
221 
222  /// <summary>
223  /// Holiday date strings
224  /// </summary>
225  [JsonProperty("holidays")]
226  public List<string> Holidays { get; set; } = new();
227 
228  /// <summary>
229  /// Early closes by date
230  /// </summary>
231  [JsonProperty("earlyCloses")]
232  public Dictionary<string, TimeSpan> EarlyCloses { get; set; } = new Dictionary<string, TimeSpan>();
233 
234  /// <summary>
235  /// Late opens by date
236  /// </summary>
237  [JsonProperty("lateOpens")]
238  public Dictionary<string, TimeSpan> LateOpens { get; set; } = new Dictionary<string, TimeSpan>();
239 
240  /// <summary>
241  /// Bank holidays date strings
242  /// </summary>
243  [JsonProperty("bankHolidays")]
244  public List<string> BankHolidays { get; set; } = new();
245 
246  /// <summary>
247  /// Initializes a new instance of the <see cref="MarketHoursDatabaseEntryJson"/> class
248  /// </summary>
249  /// <param name="entry">The entry instance to copy</param>
251  {
252  if (entry == null) return;
253  DataTimeZone = entry.DataTimeZone.Id;
254  var hours = entry.ExchangeHours;
255  ExchangeTimeZone = hours.TimeZone.Id;
256  SetSegmentsForDay(hours, DayOfWeek.Sunday, out var sunday);
257  Sunday = sunday;
258  SetSegmentsForDay(hours, DayOfWeek.Monday, out var monday);
259  Monday = monday;
260  SetSegmentsForDay(hours, DayOfWeek.Tuesday, out var tuesday);
261  Tuesday = tuesday;
262  SetSegmentsForDay(hours, DayOfWeek.Wednesday, out var wednesday);
263  Wednesday = wednesday;
264  SetSegmentsForDay(hours, DayOfWeek.Thursday, out var thursday);
265  Thursday = thursday;
266  SetSegmentsForDay(hours, DayOfWeek.Friday, out var friday);
267  Friday = friday;
268  SetSegmentsForDay(hours, DayOfWeek.Saturday, out var saturday);
269  Saturday = saturday;
270  Holidays = hours.Holidays.Select(x => x.ToString("M/d/yyyy", CultureInfo.InvariantCulture)).ToList();
271  EarlyCloses = entry.ExchangeHours.EarlyCloses.ToDictionary(pair => pair.Key.ToString("M/d/yyyy", CultureInfo.InvariantCulture), pair => pair.Value);
272  LateOpens = entry.ExchangeHours.LateOpens.ToDictionary(pair => pair.Key.ToString("M/d/yyyy", CultureInfo.InvariantCulture), pair => pair.Value);
273  }
274 
275  /// <summary>
276  /// Converts this json representation to the <see cref="MarketHoursDatabase.Entry"/> type
277  /// </summary>
278  /// <returns>A new instance of the <see cref="MarketHoursDatabase.Entry"/> class</returns>
280  {
281  var hours = new Dictionary<DayOfWeek, LocalMarketHours>
282  {
283  { DayOfWeek.Sunday, new LocalMarketHours(DayOfWeek.Sunday, Sunday) },
284  { DayOfWeek.Monday, new LocalMarketHours(DayOfWeek.Monday, Monday) },
285  { DayOfWeek.Tuesday, new LocalMarketHours(DayOfWeek.Tuesday, Tuesday) },
286  { DayOfWeek.Wednesday, new LocalMarketHours(DayOfWeek.Wednesday, Wednesday) },
287  { DayOfWeek.Thursday, new LocalMarketHours(DayOfWeek.Thursday, Thursday) },
288  { DayOfWeek.Friday, new LocalMarketHours(DayOfWeek.Friday, Friday) },
289  { DayOfWeek.Saturday, new LocalMarketHours(DayOfWeek.Saturday, Saturday) }
290  };
291  var holidayDates = Holidays.Select(x => DateTime.ParseExact(x, "M/d/yyyy", CultureInfo.InvariantCulture)).ToHashSet();
292  var bankHolidayDates = BankHolidays.Select(x => DateTime.ParseExact(x, "M/d/yyyy", CultureInfo.InvariantCulture)).ToHashSet();
293  IReadOnlyDictionary<DateTime, TimeSpan> earlyCloses = EarlyCloses.ToDictionary(x => DateTime.ParseExact(x.Key, "M/d/yyyy", CultureInfo.InvariantCulture), x => x.Value);
294  IReadOnlyDictionary<DateTime, TimeSpan> lateOpens = LateOpens.ToDictionary(x => DateTime.ParseExact(x.Key, "M/d/yyyy", CultureInfo.InvariantCulture), x => x.Value);
295 
296  if(underlyingEntry != null)
297  {
298  // If we have no entries but the underlying does, let's use the underlyings
299  if (holidayDates.Count == 0)
300  {
301  holidayDates = underlyingEntry.ExchangeHours.Holidays;
302  }
303  if (bankHolidayDates.Count == 0)
304  {
305  bankHolidayDates = underlyingEntry.ExchangeHours.BankHolidays;
306  }
307  if (earlyCloses.Count == 0)
308  {
309  earlyCloses = underlyingEntry.ExchangeHours.EarlyCloses;
310  }
311  if (lateOpens.Count == 0)
312  {
313  lateOpens = underlyingEntry.ExchangeHours.LateOpens;
314  }
315  }
316 
317  if(marketEntry != null)
318  {
319  if (marketEntry.ExchangeHours.Holidays.Count > 0)
320  {
321  holidayDates.UnionWith(marketEntry.ExchangeHours.Holidays);
322  }
323 
324  if (marketEntry.ExchangeHours.BankHolidays.Count > 0)
325  {
326  bankHolidayDates.UnionWith(marketEntry.ExchangeHours.BankHolidays);
327  }
328 
329  if (marketEntry.ExchangeHours.EarlyCloses.Count > 0 )
330  {
331  earlyCloses = MergeLateOpensAndEarlyCloses(marketEntry.ExchangeHours.EarlyCloses, earlyCloses);
332  }
333 
334  if (marketEntry.ExchangeHours.LateOpens.Count > 0)
335  {
336  lateOpens = MergeLateOpensAndEarlyCloses(marketEntry.ExchangeHours.LateOpens, lateOpens);
337  }
338  }
339 
340  var exchangeHours = new SecurityExchangeHours(DateTimeZoneProviders.Tzdb[ExchangeTimeZone], holidayDates, hours, earlyCloses, lateOpens, bankHolidayDates);
341  return new MarketHoursDatabase.Entry(DateTimeZoneProviders.Tzdb[DataTimeZone], exchangeHours);
342  }
343 
344  private void SetSegmentsForDay(SecurityExchangeHours hours, DayOfWeek day, out List<MarketHoursSegment> segments)
345  {
346  LocalMarketHours local;
347  if (hours.MarketHours.TryGetValue(day, out local))
348  {
349  segments = local.Segments.ToList();
350  }
351  else
352  {
353  segments = new List<MarketHoursSegment>();
354  }
355  }
356 
357  /// <summary>
358  /// Merges the late opens or early closes from the common entry (with wildcards) with the specific entry
359  /// (e.g. Indices-usa-[*] with Indices-usa-VIX).
360  /// The specific entry takes precedence.
361  /// </summary>
362  private static Dictionary<DateTime, TimeSpan> MergeLateOpensAndEarlyCloses(IReadOnlyDictionary<DateTime, TimeSpan> common,
363  IReadOnlyDictionary<DateTime, TimeSpan> specific)
364  {
365  var result = common.ToDictionary();
366  foreach (var (key, value) in specific)
367  {
368  result[key] = value;
369  }
370 
371  return result;
372  }
373  }
374  }
375 }