/* * 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; namespace QuantConnect.Data.Consolidators { /// /// Helper class that provides used to define consolidation calendar /// public static class Calendar { /// /// Computes the start of week (previous Monday) of given date/time /// public static Func Weekly { get { return dt => { var start = Expiry.EndOfWeek(dt).AddDays(-7); return new CalendarInfo(start, TimeSpan.FromDays(7)); }; } } /// /// Computes the start of month (1st of the current month) of given date/time /// public static Func Monthly { get { return dt => { var start = dt.AddDays(1 - dt.Day).Date; var end = Expiry.EndOfMonth(dt); return new CalendarInfo(start, end - start); }; } } /// /// Computes the start of quarter (1st of the starting month of current quarter) of given date/time /// public static Func Quarterly { get { return dt => { var nthQuarter = (dt.Month - 1) / 3; var firstMonthOfQuarter = nthQuarter * 3 + 1; var start = new DateTime(dt.Year, firstMonthOfQuarter, 1); var end = Expiry.EndOfQuarter(dt); return new CalendarInfo(start, end - start); }; } } /// /// Computes the start of year (1st of the current year) of given date/time /// public static Func Yearly { get { return dt => { var start = dt.AddDays(1 - dt.DayOfYear).Date; var end = Expiry.EndOfYear(dt); return new CalendarInfo(start, end - start); }; } } } /// /// Calendar Info for storing information related to the start and period of a consolidator /// public readonly struct CalendarInfo { /// /// Calendar Start /// public DateTime Start { get; init; } /// /// Consolidation Period /// public TimeSpan Period { get; init; } /// /// Calendar End /// public readonly DateTime End => Start + Period; /// /// Constructor for CalendarInfo; used for consolidation calendar /// /// Calendar Start /// Consolidation Period public CalendarInfo(DateTime start, TimeSpan period) { Start = start; Period = period; } /// /// Returns a string containing the Calendar start and the consolidation period /// public override string ToString() { return $"{Start} {Period}"; } /// /// Indicates whether the given object is equal to this object, this is, the Calendar start /// and consolidation period is the same for both /// public override bool Equals(object obj) { if (obj is not CalendarInfo other) { return false; } return Start == other.Start && Period == other.Period; } /// /// Returns the hash code for this object as an integer /// public override int GetHashCode() { unchecked { var hashCode = Start.GetHashCode(); return (hashCode * 397) ^ Period.GetHashCode(); } } /// /// Indicates whether the given object is equal to this object, this is, the Calendar start /// and consolidation period is the same for both /// public static bool operator ==(CalendarInfo left, CalendarInfo right) { return left.Equals(right); } /// /// Indicates whether the given object is equal to this object, this is, the Calendar start /// and consolidation period is the same for both /// public static bool operator !=(CalendarInfo left, CalendarInfo right) { return !(left == right); } } }