/* * 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 Newtonsoft.Json; namespace QuantConnect.Securities { /// /// Represents the state of an exchange during a specified time range /// [JsonObject(MemberSerialization.OptIn)] public class MarketHoursSegment { /// /// Gets the start time for this segment /// [JsonProperty("start")] public TimeSpan Start { get; private set; } /// /// Gets the end time for this segment /// [JsonProperty("end")] public TimeSpan End { get; private set; } /// /// Gets the market hours state for this segment /// [JsonProperty("state")] public MarketHoursState State { get; private set; } /// /// Initializes a new instance of the class /// /// The state of the market during the specified times /// The start time of the segment /// The end time of the segment public MarketHoursSegment(MarketHoursState state, TimeSpan start, TimeSpan end) { Start = start; End = end; State = state; } /// /// Gets a new market hours segment representing being open all day /// public static MarketHoursSegment OpenAllDay() { return new MarketHoursSegment(MarketHoursState.Market, TimeSpan.Zero, Time.OneDay); } /// /// Gets a new market hours segment representing being open all day /// public static MarketHoursSegment ClosedAllDay() { return new MarketHoursSegment(MarketHoursState.Closed, TimeSpan.Zero, Time.OneDay); } /// /// Creates the market hours segments for the specified market open/close times /// /// The extended market open time. If no pre market, set to market open /// The regular market open time /// The regular market close time /// The extended market close time. If no post market, set to market close /// An array of representing the specified market open/close times public static MarketHoursSegment[] GetMarketHoursSegments( TimeSpan extendedMarketOpen, TimeSpan marketOpen, TimeSpan marketClose, TimeSpan extendedMarketClose ) { // perform some sanity checks if (marketOpen < extendedMarketOpen) { throw new ArgumentException(Messages.MarketHoursSegment.InvalidExtendedMarketOpenTime); } if (marketClose < marketOpen) { throw new ArgumentException(Messages.MarketHoursSegment.InvalidMarketCloseTime); } if (extendedMarketClose < marketClose) { throw new ArgumentException(Messages.MarketHoursSegment.InvalidExtendedMarketCloseTime); } var segments = new List(); if (extendedMarketOpen != marketOpen) { segments.Add(new MarketHoursSegment(MarketHoursState.PreMarket, extendedMarketOpen, marketOpen)); } if (marketOpen != TimeSpan.Zero || marketClose != TimeSpan.Zero) { segments.Add(new MarketHoursSegment(MarketHoursState.Market, marketOpen, marketClose)); } if (marketClose != extendedMarketClose) { segments.Add(new MarketHoursSegment(MarketHoursState.PostMarket, marketClose, extendedMarketClose)); } return segments.ToArray(); } /// /// Determines whether or not the specified time is contained within this segment /// /// The time to check /// True if this segment contains the specified time, false otherwise public bool Contains(TimeSpan time) { return time >= Start && time < End; } /// /// Determines whether or not the specified time range overlaps with this segment /// /// The start of the range /// The end of the range /// True if the specified range overlaps this time segment, false otherwise public bool Overlaps(TimeSpan start, TimeSpan end) { return Start < end && End > start; } /// /// Returns a string that represents the current object. /// /// /// A string that represents the current object. /// public override string ToString() { return Messages.MarketHoursSegment.ToString(this); } } }