Lean  $LEAN_TAG$
CompositeTimeRule.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 using System.Collections.Generic;
19 using System.Linq;
20 using QuantConnect.Util;
21 
23 {
24  /// <summary>
25  /// Combines multiple time rules into a single rule that emits for each rule
26  /// </summary>
28  {
29  /// <summary>
30  /// Gets the individual rules for this composite rule
31  /// </summary>
32  public readonly IReadOnlyList<ITimeRule> Rules;
33 
34  /// <summary>
35  /// Initializes a new instance of the <see cref="CompositeTimeRule"/> class
36  /// </summary>
37  /// <param name="timeRules">The time rules to compose</param>
38  public CompositeTimeRule(params ITimeRule[] timeRules)
39  : this((IEnumerable<ITimeRule>) timeRules)
40  {
41  }
42 
43  /// <summary>
44  /// Initializes a new instance of the <see cref="CompositeTimeRule"/> class
45  /// </summary>
46  /// <param name="timeRules">The time rules to compose</param>
47  public CompositeTimeRule(IEnumerable<ITimeRule> timeRules)
48  {
49  Rules = timeRules.ToList();
50  }
51 
52  /// <summary>
53  /// Gets a name for this rule
54  /// </summary>
55  public string Name
56  {
57  get { return string.Join(",", Rules.Select(x => x.Name)); }
58  }
59 
60  /// <summary>
61  /// Creates the event times for the specified dates in UTC
62  /// </summary>
63  /// <param name="dates">The dates to apply times to</param>
64  /// <returns>An enumerable of date times that is the result
65  /// of applying this rule to the specified dates</returns>
66  public IEnumerable<DateTime> CreateUtcEventTimes(IEnumerable<DateTime> dates)
67  {
68  foreach (var date in dates)
69  {
70  // make unqiue times and order the events before yielding
71  var enumerable = new[] {date};
72  var times = Rules.SelectMany(time => time.CreateUtcEventTimes(enumerable)).ToHashSet().OrderBy(x => x);
73  foreach (var time in times)
74  {
75  yield return time;
76  }
77  }
78  }
79  }
80 }