Lean  $LEAN_TAG$
LocalMarketHours.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.Linq;
18 using System.Collections.Generic;
19 using System.Collections.ObjectModel;
20 using static QuantConnect.StringExtensions;
21 
23 {
24  /// <summary>
25  /// Represents the market hours under normal conditions for an exchange and a specific day of the week in terms of local time
26  /// </summary>
27  public class LocalMarketHours
28  {
29  /// <summary>
30  /// Gets whether or not this exchange is closed all day
31  /// </summary>
32  public bool IsClosedAllDay { get; }
33 
34  /// <summary>
35  /// Gets whether or not this exchange is closed all day
36  /// </summary>
37  public bool IsOpenAllDay { get; }
38 
39  /// <summary>
40  /// Gets the day of week these hours apply to
41  /// </summary>
42  public DayOfWeek DayOfWeek { get; }
43 
44  /// <summary>
45  /// Gets the tradable time during the market day.
46  /// For a normal US equity trading day this is 6.5 hours.
47  /// This does NOT account for extended market hours and only
48  /// considers <see cref="MarketHoursState.Market"/>
49  /// </summary>
50  public TimeSpan MarketDuration { get; }
51 
52  /// <summary>
53  /// Gets the individual market hours segments that define the hours of operation for this day
54  /// </summary>
55  public ReadOnlyCollection<MarketHoursSegment> Segments { get; }
56 
57  /// <summary>
58  /// Initializes a new instance of the <see cref="LocalMarketHours"/> class
59  /// </summary>
60  /// <param name="day">The day of the week these hours are applicable</param>
61  /// <param name="segments">The open/close segments defining the market hours for one day</param>
62  public LocalMarketHours(DayOfWeek day, params MarketHoursSegment[] segments)
63  : this(day, (IEnumerable<MarketHoursSegment>) segments)
64  {
65  }
66 
67  /// <summary>
68  /// Initializes a new instance of the <see cref="LocalMarketHours"/> class
69  /// </summary>
70  /// <param name="day">The day of the week these hours are applicable</param>
71  /// <param name="segments">The open/close segments defining the market hours for one day</param>
72  public LocalMarketHours(DayOfWeek day, IEnumerable<MarketHoursSegment> segments)
73  {
74  DayOfWeek = day;
75  // filter out the closed states, we'll assume closed if no segment exists
76  Segments = new ReadOnlyCollection<MarketHoursSegment>((segments ?? Enumerable.Empty<MarketHoursSegment>()).Where(x => x.State != MarketHoursState.Closed).ToList());
77  IsClosedAllDay = Segments.Count == 0;
78  IsOpenAllDay = Segments.Count == 1
79  && Segments[0].Start == TimeSpan.Zero
80  && Segments[0].End == Time.OneDay
81  && Segments[0].State == MarketHoursState.Market;
82 
83  for (var i = 0; i < Segments.Count; i++)
84  {
85  var segment = Segments[i];
86  if (segment.State == MarketHoursState.Market)
87  {
88  MarketDuration += segment.End - segment.Start;
89  }
90  }
91  }
92 
93  /// <summary>
94  /// Initializes a new instance of the <see cref="LocalMarketHours"/> class from the specified open/close times
95  /// </summary>
96  /// <param name="day">The day of week these hours apply to</param>
97  /// <param name="extendedMarketOpen">The extended market open time</param>
98  /// <param name="marketOpen">The regular market open time, must be greater than or equal to the extended market open time</param>
99  /// <param name="marketClose">The regular market close time, must be greater than the regular market open time</param>
100  /// <param name="extendedMarketClose">The extended market close time, must be greater than or equal to the regular market close time</param>
101  public LocalMarketHours(DayOfWeek day, TimeSpan extendedMarketOpen, TimeSpan marketOpen, TimeSpan marketClose, TimeSpan extendedMarketClose)
102  : this(day, MarketHoursSegment.GetMarketHoursSegments(extendedMarketOpen, marketOpen, marketClose, extendedMarketClose))
103  {
104  }
105 
106  /// <summary>
107  /// Initializes a new instance of the <see cref="LocalMarketHours"/> class from the specified open/close times
108  /// using the market open as the extended market open and the market close as the extended market close, effectively
109  /// removing any 'extended' session from these exchange hours
110  /// </summary>
111  /// <param name="day">The day of week these hours apply to</param>
112  /// <param name="marketOpen">The regular market open time</param>
113  /// <param name="marketClose">The regular market close time, must be greater than the regular market open time</param>
114  public LocalMarketHours(DayOfWeek day, TimeSpan marketOpen, TimeSpan marketClose)
115  : this(day, marketOpen, marketOpen, marketClose, marketClose)
116  {
117  }
118 
119  /// <summary>
120  /// Gets the market opening time of day
121  /// </summary>
122  /// <param name="time">The reference time, the open returned will be the first open after the specified time if there are multiple market open segments</param>
123  /// <param name="extendedMarketHours">True to include extended market hours, false for regular market hours</param>
124  /// <param name="previousDayLastSegment">The previous days last segment. This is used when the potential next market open is the first segment of the day
125  /// so we need to check that segment is not part of previous day last segment. If null, it means there were no segments on the last day</param>
126  /// <returns>The market's opening time of day</returns>
127  public TimeSpan? GetMarketOpen(TimeSpan time, bool extendedMarketHours, TimeSpan? previousDayLastSegment = null)
128  {
129  var previousSegment = previousDayLastSegment;
130  bool prevSegmentIsFromPrevDay = true;
131  for (var i = 0; i < Segments.Count; i++)
132  {
133  var segment = Segments[i];
134  if (segment.State == MarketHoursState.Closed || segment.End <= time)
135  {
136  // update prev segment end time only if the current segment could have been taken into account
137  // (regular hours or, when enabled, extended hours segment)
138  if (segment.State == MarketHoursState.Market || extendedMarketHours)
139  {
140  previousSegment = segment.End;
141  prevSegmentIsFromPrevDay = false;
142  }
143 
144  continue;
145  }
146 
147  // let's try this segment if it's regular market hours or if it is extended market hours and extended market is allowed
148  if (segment.State == MarketHoursState.Market || extendedMarketHours)
149  {
150  if (!IsContinuousMarketOpen(previousSegment, segment.Start, prevSegmentIsFromPrevDay))
151  {
152  return segment.Start;
153  }
154 
155  previousSegment = segment.End;
156  prevSegmentIsFromPrevDay = false;
157  }
158  }
159 
160  // we couldn't locate an open segment after the specified time
161  return null;
162  }
163 
164  /// <summary>
165  /// Gets the market closing time of day
166  /// </summary>
167  /// <param name="time">The reference time, the close returned will be the first close after the specified time if there are multiple market open segments</param>
168  /// <param name="extendedMarketHours">True to include extended market hours, false for regular market hours</param>
169  /// <param name="nextDaySegmentStart">Next day first segment start. This is used when the potential next market close is
170  /// the last segment of the day so we need to check that segment is not continued on next day first segment.
171  /// If null, it means there are no segments on the next day</param>
172  /// <returns>The market's closing time of day</returns>
173  public TimeSpan? GetMarketClose(TimeSpan time, bool extendedMarketHours, TimeSpan? nextDaySegmentStart = null)
174  {
175  TimeSpan? nextSegment;
176  bool nextSegmentIsFromNextDay = false;
177  for (var i = 0; i < Segments.Count; i++)
178  {
179  var segment = Segments[i];
180  if (segment.State == MarketHoursState.Closed || segment.End <= time)
181  {
182  continue;
183  }
184 
185  if (i != Segments.Count - 1)
186  {
187  var potentialNextSegment = Segments[i+1];
188 
189  // Check whether we can consider PostMarket or not
190  if (potentialNextSegment.State != MarketHoursState.Market && !extendedMarketHours)
191  {
192  nextSegment = null;
193  }
194  else
195  {
196  nextSegment = Segments[i+1].Start;
197  }
198  }
199  else
200  {
201  nextSegment = nextDaySegmentStart;
202  nextSegmentIsFromNextDay = true;
203  }
204 
205  if ((segment.State == MarketHoursState.Market || extendedMarketHours) &&
206  !IsContinuousMarketOpen(segment.End, nextSegment, nextSegmentIsFromNextDay))
207  {
208  return segment.End;
209  }
210  }
211 
212  // we couldn't locate an open segment after the specified time
213  return null;
214  }
215 
216  /// <summary>
217  /// Determines if the exchange is open at the specified time
218  /// </summary>
219  /// <param name="time">The time of day to check</param>
220  /// <param name="extendedMarketHours">True to check exended market hours, false to check regular market hours</param>
221  /// <returns>True if the exchange is considered open, false otherwise</returns>
222  public bool IsOpen(TimeSpan time, bool extendedMarketHours)
223  {
224  for (var i = 0; i < Segments.Count; i++)
225  {
226  var segment = Segments[i];
227  if (segment.State == MarketHoursState.Closed)
228  {
229  continue;
230  }
231 
232  if (segment.Contains(time))
233  {
234  return extendedMarketHours || segment.State == MarketHoursState.Market;
235  }
236  }
237 
238  // if we didn't find a segment then we're closed
239  return false;
240  }
241 
242  /// <summary>
243  /// Determines if the exchange is open during the specified interval
244  /// </summary>
245  /// <param name="start">The start time of the interval</param>
246  /// <param name="end">The end time of the interval</param>
247  /// <param name="extendedMarketHours">True to check exended market hours, false to check regular market hours</param>
248  /// <returns>True if the exchange is considered open, false otherwise</returns>
249  public bool IsOpen(TimeSpan start, TimeSpan end, bool extendedMarketHours)
250  {
251  if (start == end)
252  {
253  return IsOpen(start, extendedMarketHours);
254  }
255 
256  for (var i = 0; i < Segments.Count; i++)
257  {
258  var segment = Segments[i];
259  if (segment.State == MarketHoursState.Closed)
260  {
261  continue;
262  }
263 
264  if (extendedMarketHours || segment.State == MarketHoursState.Market)
265  {
266  if (segment.Overlaps(start, end))
267  {
268  return true;
269  }
270  }
271  }
272 
273  // if we didn't find a segment then we're closed
274  return false;
275  }
276 
277  /// <summary>
278  /// Gets a <see cref="LocalMarketHours"/> instance that is always closed
279  /// </summary>
280  /// <param name="dayOfWeek">The day of week</param>
281  /// <returns>A <see cref="LocalMarketHours"/> instance that is always closed</returns>
282  public static LocalMarketHours ClosedAllDay(DayOfWeek dayOfWeek)
283  {
284  return new LocalMarketHours(dayOfWeek);
285  }
286 
287  /// <summary>
288  /// Gets a <see cref="LocalMarketHours"/> instance that is always open
289  /// </summary>
290  /// <param name="dayOfWeek">The day of week</param>
291  /// <returns>A <see cref="LocalMarketHours"/> instance that is always open</returns>
292  public static LocalMarketHours OpenAllDay(DayOfWeek dayOfWeek)
293  {
294  return new LocalMarketHours(dayOfWeek, new MarketHoursSegment(MarketHoursState.Market, TimeSpan.Zero, Time.OneDay));
295  }
296 
297  /// <summary>
298  /// Check the given segment is not part of the current previous segment
299  /// </summary>
300  /// <param name="previousSegmentEnd">Previous segment end time before the current segment</param>
301  /// <param name="nextSegmentStart">The next segment start time</param>
302  /// <param name="prevSegmentIsFromPrevDay">Indicated whether the previous segment is from the previous day or not
303  /// (then it is from the same day as the next segment). Defaults to true</param>
304  /// <returns>True if indeed the given segment is part of the last segment. False otherwise</returns>
305  public static bool IsContinuousMarketOpen(TimeSpan? previousSegmentEnd, TimeSpan? nextSegmentStart, bool prevSegmentIsFromPrevDay = true)
306  {
307  if (previousSegmentEnd != null && nextSegmentStart != null)
308  {
309  if (prevSegmentIsFromPrevDay)
310  {
311  // midnight passing to the next day
312  return previousSegmentEnd.Value == Time.OneDay && nextSegmentStart.Value == TimeSpan.Zero;
313  }
314 
315  // passing from one segment to another in the same day
316  return previousSegmentEnd.Value == nextSegmentStart.Value;
317  }
318  return false;
319  }
320 
321  /// <summary>
322  /// Returns a string that represents the current object.
323  /// </summary>
324  /// <returns>
325  /// A string that represents the current object.
326  /// </returns>
327  /// <filterpriority>2</filterpriority>
328  public override string ToString()
329  {
330  return Messages.LocalMarketHours.ToString(this);
331  }
332  }
333 }