Lean  $LEAN_TAG$
FundamentalUniverseFactory.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;
21 
23 {
24  /// <summary>
25  /// Defines a universe that reads fundamental us equity data
26  /// </summary>
28  {
29  internal static readonly FundamentalUniverse SymbolFactory = new();
30 
31  private readonly Func<IEnumerable<Fundamental.Fundamental>, IEnumerable<Symbol>> _selector;
32 
33  /// <summary>
34  /// Initializes a new instance of the <see cref="FundamentalUniverseFactory"/> class
35  /// </summary>
36  /// <param name="market">The target market</param>
37  /// <param name="universeSettings">The settings used for new subscriptions generated by this universe</param>
38  /// <param name="selector">Returns the symbols that should be included in the universe</param>
39  public FundamentalUniverseFactory(string market, UniverseSettings universeSettings, Func<IEnumerable<Fundamental.Fundamental>, IEnumerable<Symbol>> selector)
40  : this(SymbolFactory.UniverseSymbol(market), universeSettings, selector)
41  {
42  }
43 
44  /// <summary>
45  /// Initializes a new instance of the <see cref="FundamentalUniverseFactory"/> class
46  /// </summary>
47  /// <param name="market">The target market</param>
48  /// <param name="universeSettings">The settings used for new subscriptions generated by this universe</param>
49  /// <param name="selector">Returns the symbols that should be included in the universe</param>
50  public FundamentalUniverseFactory(string market, UniverseSettings universeSettings, PyObject selector)
51  : this(market, universeSettings, selector.ConvertToDelegate<Func<IEnumerable<Fundamental.Fundamental>, object>>())
52  {
53  }
54 
55  /// <summary>
56  /// Initializes a new instance of the <see cref="FundamentalUniverseFactory"/> class
57  /// </summary>
58  /// <param name="market">The target market</param>
59  /// <param name="universeSettings">The settings used for new subscriptions generated by this universe</param>
60  /// <param name="selector">Returns the symbols that should be included in the universe</param>
61  public FundamentalUniverseFactory(string market, UniverseSettings universeSettings, Func<IEnumerable<Fundamental.Fundamental>, object> selector)
62  : this(market, universeSettings, selector.ConvertToUniverseSelectionSymbolDelegate())
63  {
64  }
65 
66  /// <summary>
67  /// Initializes a new instance of the <see cref="FundamentalUniverseFactory"/> class
68  /// </summary>
69  /// <param name="symbol">Defines the symbol to use for this universe</param>
70  /// <param name="universeSettings">The settings used for new subscriptions generated by this universe</param>
71  /// <param name="selector">Returns the symbols that should be included in the universe</param>
72  public FundamentalUniverseFactory(Symbol symbol, UniverseSettings universeSettings, Func<IEnumerable<Fundamental.Fundamental>, IEnumerable<Symbol>> selector)
73  : base(CreateConfiguration(symbol))
74  {
75  UniverseSettings = universeSettings;
76  _selector = selector;
77  }
78 
79  /// <summary>
80  /// Performs universe selection using the data specified
81  /// </summary>
82  /// <param name="utcTime">The current utc time</param>
83  /// <param name="data">The symbols to remain in the universe</param>
84  /// <returns>The data that passes the filter</returns>
85  public override IEnumerable<Symbol> SelectSymbols(DateTime utcTime, BaseDataCollection data)
86  {
87  return _selector(data.Data.OfType<Fundamental.Fundamental>());
88  }
89 
90  /// <summary>
91  /// Creates a <see cref="Fundamental.Fundamental"/> subscription configuration for the US-equity market
92  /// </summary>
93  /// <param name="symbol">The symbol used in the returned configuration</param>
94  /// <returns>A fundamental subscription configuration with the specified symbol</returns>
96  {
97  return new SubscriptionDataConfig(typeof(Fundamental.FundamentalUniverse),
98  symbol: symbol,
99  resolution: Resolution.Daily,
100  dataTimeZone: TimeZones.NewYork,
101  exchangeTimeZone: TimeZones.NewYork,
102  fillForward: false,
103  extendedHours: false,
104  isInternalFeed: true,
105  isCustom: false,
106  tickType: null,
107  isFilteredSubscription: false
108  );
109  }
110  }
111 }