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  /// 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 var sunday);
251  Sunday = sunday;
252  SetSegmentsForDay(hours, DayOfWeek.Monday, out var monday);
253  Monday = monday;
254  SetSegmentsForDay(hours, DayOfWeek.Tuesday, out var tuesday);
255  Tuesday = tuesday;
256  SetSegmentsForDay(hours, DayOfWeek.Wednesday, out var wednesday);
257  Wednesday = wednesday;
258  SetSegmentsForDay(hours, DayOfWeek.Thursday, out var thursday);
259  Thursday = thursday;
260  SetSegmentsForDay(hours, DayOfWeek.Friday, out var friday);
261  Friday = friday;
262  SetSegmentsForDay(hours, DayOfWeek.Saturday, out var saturday);
263  Saturday = saturday;
264  Holidays = hours.Holidays.Select(x => x.ToString("M/d/yyyy", CultureInfo.InvariantCulture)).ToList();
265  EarlyCloses = entry.ExchangeHours.EarlyCloses.ToDictionary(pair => pair.Key.ToString("M/d/yyyy", CultureInfo.InvariantCulture), pair => pair.Value);
266  LateOpens = entry.ExchangeHours.LateOpens.ToDictionary(pair => pair.Key.ToString("M/d/yyyy", CultureInfo.InvariantCulture), pair => pair.Value);
267  }
268 
269  /// <summary>
270  /// Converts this json representation to the <see cref="MarketHoursDatabase.Entry"/> type
271  /// </summary>
272  /// <returns>A new instance of the <see cref="MarketHoursDatabase.Entry"/> class</returns>
274  {
275  var hours = new Dictionary<DayOfWeek, LocalMarketHours>
276  {
277  { DayOfWeek.Sunday, new LocalMarketHours(DayOfWeek.Sunday, Sunday) },
278  { DayOfWeek.Monday, new LocalMarketHours(DayOfWeek.Monday, Monday) },
279  { DayOfWeek.Tuesday, new LocalMarketHours(DayOfWeek.Tuesday, Tuesday) },
280  { DayOfWeek.Wednesday, new LocalMarketHours(DayOfWeek.Wednesday, Wednesday) },
281  { DayOfWeek.Thursday, new LocalMarketHours(DayOfWeek.Thursday, Thursday) },
282  { DayOfWeek.Friday, new LocalMarketHours(DayOfWeek.Friday, Friday) },
283  { DayOfWeek.Saturday, new LocalMarketHours(DayOfWeek.Saturday, Saturday) }
284  };
285  var holidayDates = Holidays.Select(x => DateTime.ParseExact(x, "M/d/yyyy", CultureInfo.InvariantCulture)).ToHashSet();
286  IReadOnlyDictionary<DateTime, TimeSpan> earlyCloses = EarlyCloses.ToDictionary(x => DateTime.ParseExact(x.Key, "M/d/yyyy", CultureInfo.InvariantCulture), x => x.Value);
287  IReadOnlyDictionary<DateTime, TimeSpan> lateOpens = LateOpens.ToDictionary(x => DateTime.ParseExact(x.Key, "M/d/yyyy", CultureInfo.InvariantCulture), x => x.Value);
288 
289  if(underlyingEntry != null)
290  {
291  // If we have no entries but the underlying does, let's use the underlyings
292  if (holidayDates.Count == 0)
293  {
294  holidayDates = underlyingEntry.ExchangeHours.Holidays;
295  }
296  if (earlyCloses.Count == 0)
297  {
298  earlyCloses = underlyingEntry.ExchangeHours.EarlyCloses;
299  }
300  if (lateOpens.Count == 0)
301  {
302  lateOpens = underlyingEntry.ExchangeHours.LateOpens;
303  }
304  }
305 
306  if(marketEntry != null)
307  {
308  if (marketEntry.ExchangeHours.Holidays.Count > 0)
309  {
310  holidayDates.UnionWith(marketEntry.ExchangeHours.Holidays);
311  }
312 
313  if (marketEntry.ExchangeHours.EarlyCloses.Count > 0 )
314  {
315  earlyCloses = MergeLateOpensAndEarlyCloses(marketEntry.ExchangeHours.EarlyCloses, earlyCloses);
316  }
317 
318  if (marketEntry.ExchangeHours.LateOpens.Count > 0)
319  {
320  lateOpens = MergeLateOpensAndEarlyCloses(marketEntry.ExchangeHours.LateOpens, lateOpens);
321  }
322  }
323 
324  var exchangeHours = new SecurityExchangeHours(DateTimeZoneProviders.Tzdb[ExchangeTimeZone], holidayDates, hours, earlyCloses, lateOpens);
325  return new MarketHoursDatabase.Entry(DateTimeZoneProviders.Tzdb[DataTimeZone], exchangeHours);
326  }
327 
328  private void SetSegmentsForDay(SecurityExchangeHours hours, DayOfWeek day, out List<MarketHoursSegment> segments)
329  {
330  LocalMarketHours local;
331  if (hours.MarketHours.TryGetValue(day, out local))
332  {
333  segments = local.Segments.ToList();
334  }
335  else
336  {
337  segments = new List<MarketHoursSegment>();
338  }
339  }
340 
341  /// <summary>
342  /// Merges the late opens or early closes from the common entry (with wildcards) with the specific entry
343  /// (e.g. Indices-usa-[*] with Indices-usa-VIX).
344  /// The specific entry takes precedence.
345  /// </summary>
346  private static Dictionary<DateTime, TimeSpan> MergeLateOpensAndEarlyCloses(IReadOnlyDictionary<DateTime, TimeSpan> common,
347  IReadOnlyDictionary<DateTime, TimeSpan> specific)
348  {
349  var result = common.ToDictionary();
350  foreach (var (key, value) in specific)
351  {
352  result[key] = value;
353  }
354 
355  return result;
356  }
357  }
358  }
359 }