/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using NodaTime; using QuantConnect.Logging; using QuantConnect.Securities; using QuantConnect.Securities.Option; namespace QuantConnect.Util { /// /// Provides json conversion for the class /// public class MarketHoursDatabaseJsonConverter : TypeChangeJsonConverter { /// /// Convert the input value to a value to be serialzied /// /// The input value to be converted before serialziation /// A new instance of TResult that is to be serialzied protected override MarketHoursDatabaseJson Convert(MarketHoursDatabase value) { return new MarketHoursDatabaseJson(value); } /// /// Converts the input value to be deserialized /// /// The deserialized value that needs to be converted to T /// The converted value protected override MarketHoursDatabase Convert(MarketHoursDatabaseJson value) { return value.Convert(); } /// /// Creates an instance of the un-projected type to be deserialized /// /// The input object type, this is the data held in the token /// The input data to be converted into a T /// A new instance of T that is to be serialized using default rules protected override MarketHoursDatabase Create(Type type, JToken token) { var jobject = (JObject) token; var instance = jobject.ToObject(); return Convert(instance); } /// /// Defines the json structure of the market-hours-database.json file /// [JsonObject(MemberSerialization.OptIn)] public class MarketHoursDatabaseJson { /// /// The entries in the market hours database, keyed by /// [JsonProperty("entries")] public Dictionary Entries { get; set; } /// /// Initializes a new instance of the class /// /// The database instance to copy public MarketHoursDatabaseJson(MarketHoursDatabase database) { if (database == null) return; Entries = new Dictionary(); foreach (var kvp in database.ExchangeHoursListing) { var key = kvp.Key; var entry = kvp.Value; Entries[key.ToString()] = new MarketHoursDatabaseEntryJson(entry); } } /// /// Converts this json representation to the type /// /// A new instance of the class public MarketHoursDatabase Convert() { // first we parse the entries keys so that later we can sort by security type var entries = new Dictionary(Entries.Count); foreach (var entry in Entries) { try { var key = SecurityDatabaseKey.Parse(entry.Key); if (key != null) { entries[key] = entry.Value; } } catch (Exception err) { Log.Error(err); } } var result = new Dictionary(Entries.Count); // we sort so we process generic entries and non options first foreach (var entry in entries.OrderBy(kvp => kvp.Key.Symbol != null ? 1 : 0).ThenBy(kvp => kvp.Key.SecurityType.IsOption() ? 1 : 0)) { try { result.TryGetValue(entry.Key.CreateCommonKey(), out var marketEntry); var underlyingEntry = GetUnderlyingEntry(entry.Key, result); result[entry.Key] = entry.Value.Convert(underlyingEntry, marketEntry); } catch (Exception err) { Log.Error(err); } } return new MarketHoursDatabase(result); } /// /// Helper method to get the already processed underlying entry for options /// private static MarketHoursDatabase.Entry GetUnderlyingEntry(SecurityDatabaseKey key, Dictionary result) { MarketHoursDatabase.Entry underlyingEntry = null; if (key.SecurityType.IsOption()) { // if option, let's get the underlyings entry var underlyingSecurityType = Symbol.GetUnderlyingFromOptionType(key.SecurityType); var underlying = OptionSymbol.MapToUnderlying(key.Symbol, key.SecurityType); var underlyingKey = new SecurityDatabaseKey(key.Market, underlying, underlyingSecurityType); if (!result.TryGetValue(underlyingKey, out underlyingEntry) // let's retry with the wildcard && underlying != SecurityDatabaseKey.Wildcard) { var underlyingKeyWildCard = new SecurityDatabaseKey(key.Market, SecurityDatabaseKey.Wildcard, underlyingSecurityType); result.TryGetValue(underlyingKeyWildCard, out underlyingEntry); } } return underlyingEntry; } } /// /// Defines the json structure of a single entry in the market-hours-database.json file /// [JsonObject(MemberSerialization.OptIn)] public class MarketHoursDatabaseEntryJson { /// /// The data's raw time zone /// [JsonProperty("dataTimeZone")] public string DataTimeZone { get; set; } /// /// The exchange's time zone id from the tzdb /// [JsonProperty("exchangeTimeZone")] public string ExchangeTimeZone { get; set; } /// /// Sunday market hours segments /// [JsonProperty("sunday")] public List Sunday { get; set; } /// /// Monday market hours segments /// [JsonProperty("monday")] public List Monday { get; set; } /// /// Tuesday market hours segments /// [JsonProperty("tuesday")] public List Tuesday { get; set; } /// /// Wednesday market hours segments /// [JsonProperty("wednesday")] public List Wednesday { get; set; } /// /// Thursday market hours segments /// [JsonProperty("thursday")] public List Thursday { get; set; } /// /// Friday market hours segments /// [JsonProperty("friday")] public List Friday { get; set; } /// /// Saturday market hours segments /// [JsonProperty("saturday")] public List Saturday { get; set; } /// /// Holiday date strings /// [JsonProperty("holidays")] public List Holidays { get; set; } = new(); /// /// Early closes by date /// [JsonProperty("earlyCloses")] public Dictionary EarlyCloses { get; set; } = new Dictionary(); /// /// Late opens by date /// [JsonProperty("lateOpens")] public Dictionary LateOpens { get; set; } = new Dictionary(); /// /// Bank holidays date strings /// [JsonProperty("bankHolidays")] public List BankHolidays { get; set; } = new(); /// /// Initializes a new instance of the class /// /// The entry instance to copy public MarketHoursDatabaseEntryJson(MarketHoursDatabase.Entry entry) { if (entry == null) return; DataTimeZone = entry.DataTimeZone.Id; var hours = entry.ExchangeHours; ExchangeTimeZone = hours.TimeZone.Id; SetSegmentsForDay(hours, DayOfWeek.Sunday, out var sunday); Sunday = sunday; SetSegmentsForDay(hours, DayOfWeek.Monday, out var monday); Monday = monday; SetSegmentsForDay(hours, DayOfWeek.Tuesday, out var tuesday); Tuesday = tuesday; SetSegmentsForDay(hours, DayOfWeek.Wednesday, out var wednesday); Wednesday = wednesday; SetSegmentsForDay(hours, DayOfWeek.Thursday, out var thursday); Thursday = thursday; SetSegmentsForDay(hours, DayOfWeek.Friday, out var friday); Friday = friday; SetSegmentsForDay(hours, DayOfWeek.Saturday, out var saturday); Saturday = saturday; Holidays = hours.Holidays.Select(x => x.ToString("M/d/yyyy", CultureInfo.InvariantCulture)).ToList(); EarlyCloses = entry.ExchangeHours.EarlyCloses.ToDictionary(pair => pair.Key.ToString("M/d/yyyy", CultureInfo.InvariantCulture), pair => pair.Value); LateOpens = entry.ExchangeHours.LateOpens.ToDictionary(pair => pair.Key.ToString("M/d/yyyy", CultureInfo.InvariantCulture), pair => pair.Value); } /// /// Converts this json representation to the type /// /// A new instance of the class public MarketHoursDatabase.Entry Convert(MarketHoursDatabase.Entry underlyingEntry, MarketHoursDatabase.Entry marketEntry) { var hours = new Dictionary { { DayOfWeek.Sunday, new LocalMarketHours(DayOfWeek.Sunday, Sunday) }, { DayOfWeek.Monday, new LocalMarketHours(DayOfWeek.Monday, Monday) }, { DayOfWeek.Tuesday, new LocalMarketHours(DayOfWeek.Tuesday, Tuesday) }, { DayOfWeek.Wednesday, new LocalMarketHours(DayOfWeek.Wednesday, Wednesday) }, { DayOfWeek.Thursday, new LocalMarketHours(DayOfWeek.Thursday, Thursday) }, { DayOfWeek.Friday, new LocalMarketHours(DayOfWeek.Friday, Friday) }, { DayOfWeek.Saturday, new LocalMarketHours(DayOfWeek.Saturday, Saturday) } }; var holidayDates = Holidays.Select(x => DateTime.ParseExact(x, "M/d/yyyy", CultureInfo.InvariantCulture)).ToHashSet(); var bankHolidayDates = BankHolidays.Select(x => DateTime.ParseExact(x, "M/d/yyyy", CultureInfo.InvariantCulture)).ToHashSet(); IReadOnlyDictionary earlyCloses = EarlyCloses.ToDictionary(x => DateTime.ParseExact(x.Key, "M/d/yyyy", CultureInfo.InvariantCulture), x => x.Value); IReadOnlyDictionary lateOpens = LateOpens.ToDictionary(x => DateTime.ParseExact(x.Key, "M/d/yyyy", CultureInfo.InvariantCulture), x => x.Value); if(underlyingEntry != null) { // If we have no entries but the underlying does, let's use the underlyings if (holidayDates.Count == 0) { holidayDates = underlyingEntry.ExchangeHours.Holidays; } if (bankHolidayDates.Count == 0) { bankHolidayDates = underlyingEntry.ExchangeHours.BankHolidays; } if (earlyCloses.Count == 0) { earlyCloses = underlyingEntry.ExchangeHours.EarlyCloses; } if (lateOpens.Count == 0) { lateOpens = underlyingEntry.ExchangeHours.LateOpens; } } if(marketEntry != null) { if (marketEntry.ExchangeHours.Holidays.Count > 0) { holidayDates.UnionWith(marketEntry.ExchangeHours.Holidays); } if (marketEntry.ExchangeHours.BankHolidays.Count > 0) { bankHolidayDates.UnionWith(marketEntry.ExchangeHours.BankHolidays); } if (marketEntry.ExchangeHours.EarlyCloses.Count > 0 ) { earlyCloses = MergeLateOpensAndEarlyCloses(marketEntry.ExchangeHours.EarlyCloses, earlyCloses); } if (marketEntry.ExchangeHours.LateOpens.Count > 0) { lateOpens = MergeLateOpensAndEarlyCloses(marketEntry.ExchangeHours.LateOpens, lateOpens); } } var exchangeHours = new SecurityExchangeHours(DateTimeZoneProviders.Tzdb[ExchangeTimeZone], holidayDates, hours, earlyCloses, lateOpens, bankHolidayDates); return new MarketHoursDatabase.Entry(DateTimeZoneProviders.Tzdb[DataTimeZone], exchangeHours); } private void SetSegmentsForDay(SecurityExchangeHours hours, DayOfWeek day, out List segments) { LocalMarketHours local; if (hours.MarketHours.TryGetValue(day, out local)) { segments = local.Segments.ToList(); } else { segments = new List(); } } /// /// Merges the late opens or early closes from the common entry (with wildcards) with the specific entry /// (e.g. Indices-usa-[*] with Indices-usa-VIX). /// The specific entry takes precedence. /// private static Dictionary MergeLateOpensAndEarlyCloses(IReadOnlyDictionary common, IReadOnlyDictionary specific) { var result = common.ToDictionary(); foreach (var (key, value) in specific) { result[key] = value; } return result; } } } }