Lean  $LEAN_TAG$
CoarseFundamentalUniverse.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 using System;
17 using System.Linq;
18 using Python.Runtime;
19 using System.Collections.Generic;
20 
22 {
23  /// <summary>
24  /// Defines a universe that reads coarse us equity data
25  /// </summary>
27  {
28  private readonly Func<IEnumerable<CoarseFundamental>, IEnumerable<Symbol>> _selector;
29 
30  /// <summary>
31  /// Initializes a new instance of the <see cref="CoarseFundamentalUniverse"/> class
32  /// </summary>
33  /// <param name="universeSettings">The settings used for new subscriptions generated by this universe</param>
34  /// <param name="selector">Returns the symbols that should be included in the universe</param>
35  public CoarseFundamentalUniverse(UniverseSettings universeSettings, Func<IEnumerable<CoarseFundamental>, IEnumerable<Symbol>> selector)
36  : base(CreateConfiguration(FundamentalUniverseFactory.SymbolFactory.UniverseSymbol()))
37  {
38  UniverseSettings = universeSettings;
39  _selector = selector;
40  }
41 
42  /// <summary>
43  /// Initializes a new instance of the <see cref="CoarseFundamentalUniverse"/> class
44  /// </summary>
45  /// <param name="universeSettings">The settings used for new subscriptions generated by this universe</param>
46  /// <param name="selector">Returns the symbols that should be included in the universe</param>
47  public CoarseFundamentalUniverse(UniverseSettings universeSettings, PyObject selector)
48  : this(FundamentalUniverseFactory.SymbolFactory.UniverseSymbol(), universeSettings, selector)
49  {
50  }
51 
52  /// <summary>
53  /// Initializes a new instance of the <see cref="CoarseFundamentalUniverse"/> class
54  /// </summary>
55  /// <param name="symbol">Defines the symbol to use for this universe</param>
56  /// <param name="universeSettings">The settings used for new subscriptions generated by this universe</param>
57  /// <param name="selector">Returns the symbols that should be included in the universe</param>
58  public CoarseFundamentalUniverse(Symbol symbol, UniverseSettings universeSettings, Func<IEnumerable<CoarseFundamental>, IEnumerable<Symbol>> selector)
59  : base(CreateConfiguration(symbol))
60  {
61  UniverseSettings = universeSettings;
62  _selector = selector;
63  }
64 
65  /// <summary>
66  /// Initializes a new instance of the <see cref="CoarseFundamentalUniverse"/> class
67  /// </summary>
68  /// <param name="symbol">Defines the symbol to use for this universe</param>
69  /// <param name="universeSettings">The settings used for new subscriptions generated by this universe</param>
70  /// <param name="selector">Returns the symbols that should be included in the universe</param>
71  public CoarseFundamentalUniverse(Symbol symbol, UniverseSettings universeSettings, PyObject selector)
72  : base(CreateConfiguration(symbol))
73  {
74  UniverseSettings = universeSettings;
75  Func<IEnumerable<CoarseFundamental>, object> func;
76  if (selector.TryConvertToDelegate(out func))
77  {
78  _selector = func.ConvertToUniverseSelectionSymbolDelegate();
79  }
80  }
81 
82  /// <summary>
83  /// Performs universe selection using the data specified
84  /// </summary>
85  /// <param name="utcTime">The current utc time</param>
86  /// <param name="data">The symbols to remain in the universe</param>
87  /// <returns>The data that passes the filter</returns>
88  public override IEnumerable<Symbol> SelectSymbols(DateTime utcTime, BaseDataCollection data)
89  {
90  return _selector(data.Data.OfType<CoarseFundamental>());
91  }
92 
93  /// <summary>
94  /// Creates a <see cref="CoarseFundamental"/> subscription configuration for the US-equity market
95  /// </summary>
96  /// <param name="symbol">The symbol used in the returned configuration</param>
97  /// <returns>A coarse fundamental subscription configuration with the specified symbol</returns>
99  {
101  }
102  }
103 }