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