Lean  $LEAN_TAG$
FutureFilterUniverse.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;
22 using QuantConnect.Util;
23 
25 {
26  /// <summary>
27  /// Represents futures symbols universe used in filtering.
28  /// </summary>
29  public class FutureFilterUniverse : ContractSecurityFilterUniverse<FutureFilterUniverse, FutureUniverse>
30  {
31  /// <summary>
32  /// Constructs FutureFilterUniverse
33  /// </summary>
34  public FutureFilterUniverse(IEnumerable<FutureUniverse> allData, DateTime localTime)
35  : base(allData, localTime)
36  {
37  }
38 
39  /// <summary>
40  /// Determine if the given Future contract symbol is standard
41  /// </summary>
42  /// <returns>True if contract is standard</returns>
43  protected override bool IsStandard(Symbol symbol)
44  {
45  return FutureSymbol.IsStandard(symbol);
46  }
47 
48  /// <summary>
49  /// Creates a new instance of the data type for the given symbol
50  /// </summary>
51  /// <returns>A data instance for the given symbol, which is just the symbol itself</returns>
52  protected override FutureUniverse CreateDataInstance(Symbol symbol)
53  {
54  return new FutureUniverse()
55  {
56  Symbol = symbol,
57  Time = LocalTime
58  };
59  }
60 
61  /// <summary>
62  /// Applies filter selecting futures contracts based on expiration cycles. See <see cref="FutureExpirationCycles"/> for details
63  /// </summary>
64  /// <param name="months">Months to select contracts from</param>
65  /// <returns>Universe with filter applied</returns>
67  {
68  var monthHashSet = months.ToHashSet();
69  return this.Where(x => monthHashSet.Contains(x.ID.Date.Month));
70  }
71  }
72 
73  /// <summary>
74  /// Extensions for Linq support
75  /// </summary>
76  public static class FutureFilterUniverseEx
77  {
78  /// <summary>
79  /// Filters universe
80  /// </summary>
81  /// <param name="universe">Universe to apply the filter too</param>
82  /// <param name="predicate">Bool function to determine which Symbol are filtered</param>
83  /// <returns><see cref="FutureFilterUniverse"/> with filter applied</returns>
84  public static FutureFilterUniverse Where(this FutureFilterUniverse universe, Func<FutureUniverse, bool> predicate)
85  {
86  universe.Data = universe.Data.Where(predicate).ToList();
87  return universe;
88  }
89 
90  /// <summary>
91  /// Maps universe
92  /// </summary>
93  /// <param name="universe">Universe to apply the filter too</param>
94  /// <param name="mapFunc">Symbol function to determine which Symbols are filtered</param>
95  /// <returns><see cref="FutureFilterUniverse"/> with filter applied</returns>
96  public static FutureFilterUniverse Select(this FutureFilterUniverse universe, Func<FutureUniverse, Symbol> mapFunc)
97  {
98  universe.AllSymbols = universe.Data.Select(mapFunc).ToList();
99  return universe;
100  }
101 
102  /// <summary>
103  /// Binds universe
104  /// </summary>
105  /// <param name="universe">Universe to apply the filter too</param>
106  /// <param name="mapFunc">Symbols function to determine which Symbols are filtered</param>
107  /// <returns><see cref="FutureFilterUniverse"/> with filter applied</returns>
108  public static FutureFilterUniverse SelectMany(this FutureFilterUniverse universe, Func<FutureUniverse, IEnumerable<Symbol>> mapFunc)
109  {
110  universe.AllSymbols = universe.Data.SelectMany(mapFunc).ToList();
111  return universe;
112  }
113  }
114 }