Lean  $LEAN_TAG$
Expiry.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 
17 using System;
18 
19 namespace QuantConnect
20 {
21  /// <summary>
22  /// Provides static functions that can be used to compute a future <see cref="DateTime"/> (expiry) given a <see cref="DateTime"/>.
23  /// </summary>
24  public static class Expiry
25  {
26  /// <summary>
27  /// Computes a date/time one month after a given date/time (nth day to nth day)
28  /// </summary>
29  public static Func<DateTime, DateTime> OneMonth => dt => dt.AddMonths(1);
30 
31  /// <summary>
32  /// Computes a date/time one quarter after a given date/time (nth day to nth day)
33  /// </summary>
34  public static Func<DateTime, DateTime> OneQuarter => dt => dt.AddMonths(3);
35 
36  /// <summary>
37  /// Computes a date/time one year after a given date/time (nth day to nth day)
38  /// </summary>
39  public static Func<DateTime, DateTime> OneYear => dt => dt.AddYears(1);
40 
41  /// <summary>
42  /// Computes the end of day (mid-night of the next day) of given date/time
43  /// </summary>
44  public static Func<DateTime, DateTime> EndOfDay => dt => dt.AddDays(1).Date;
45 
46  /// <summary>
47  /// Computes the end of week (next Monday) of given date/time
48  /// </summary>
49  public static Func<DateTime, DateTime> EndOfWeek
50  {
51  get
52  {
53  return dt =>
54  {
55  var value = 8 - (int)dt.DayOfWeek;
56  if (value == 8) value = 1; // Sunday
57  return dt.AddDays(value).Date;
58  };
59  }
60  }
61 
62  /// <summary>
63  /// Computes the end of month (1st of the next month) of given date/time
64  /// </summary>
65  public static Func<DateTime, DateTime> EndOfMonth
66  {
67  get
68  {
69  return dt =>
70  {
71  var value = OneMonth(dt);
72  return new DateTime(value.Year, value.Month, 1);
73  };
74  }
75  }
76 
77  /// <summary>
78  /// Computes the end of quarter (1st of the starting month of next quarter) of given date/time
79  /// </summary>
80  public static Func<DateTime, DateTime> EndOfQuarter
81  {
82  get
83  {
84  return dt =>
85  {
86  var nthQuarter = (dt.Month - 1) / 3;
87  var firstMonthOfQuarter = nthQuarter * 3 + 1;
88  return OneQuarter(new DateTime(dt.Year, firstMonthOfQuarter, 1));
89  };
90  }
91  }
92 
93  /// <summary>
94  /// Computes the end of year (1st of the next year) of given date/time
95  /// </summary>
96  public static Func<DateTime, DateTime> EndOfYear => dt => new DateTime(dt.Year + 1, 1, 1);
97  }
98 }