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 QuantConnect.Data;
20 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>
30  {
31  /// <summary>
32  /// Constructs FutureFilterUniverse
33  /// </summary>
34  public FutureFilterUniverse(IEnumerable<Symbol> allSymbols, DateTime localTime)
35  : base(allSymbols, 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  /// Applies filter selecting futures contracts based on expiration cycles. See <see cref="FutureExpirationCycles"/> for details
50  /// </summary>
51  /// <param name="months">Months to select contracts from</param>
52  /// <returns>Universe with filter applied</returns>
54  {
55  var monthHashSet = months.ToHashSet();
56  return this.Where(x => monthHashSet.Contains(x.ID.Date.Month));
57  }
58  }
59 
60  /// <summary>
61  /// Extensions for Linq support
62  /// </summary>
63  public static class FutureFilterUniverseEx
64  {
65  /// <summary>
66  /// Filters universe
67  /// </summary>
68  /// <param name="universe">Universe to apply the filter too</param>
69  /// <param name="predicate">Bool function to determine which Symbol are filtered</param>
70  /// <returns><see cref="FutureFilterUniverse"/> with filter applied</returns>
71  public static FutureFilterUniverse Where(this FutureFilterUniverse universe, Func<Symbol, bool> predicate)
72  {
73  universe.AllSymbols = universe.AllSymbols.Where(predicate).ToList();
74  return universe;
75  }
76 
77  /// <summary>
78  /// Maps universe
79  /// </summary>
80  /// <param name="universe">Universe to apply the filter too</param>
81  /// <param name="mapFunc">Symbol function to determine which Symbols are filtered</param>
82  /// <returns><see cref="FutureFilterUniverse"/> with filter applied</returns>
83  public static FutureFilterUniverse Select(this FutureFilterUniverse universe, Func<Symbol, Symbol> mapFunc)
84  {
85  universe.AllSymbols = universe.AllSymbols.Select(mapFunc).ToList();
86  return universe;
87  }
88 
89  /// <summary>
90  /// Binds universe
91  /// </summary>
92  /// <param name="universe">Universe to apply the filter too</param>
93  /// <param name="mapFunc">Symbols function to determine which Symbols are filtered</param>
94  /// <returns><see cref="FutureFilterUniverse"/> with filter applied</returns>
95  public static FutureFilterUniverse SelectMany(this FutureFilterUniverse universe, Func<Symbol, IEnumerable<Symbol>> mapFunc)
96  {
97  universe.AllSymbols = universe.AllSymbols.SelectMany(mapFunc).ToList();
98  return universe;
99  }
100  }
101 }