Lean  $LEAN_TAG$
FuncUniverse.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.Collections.Generic;
18 using System.Linq;
19 using Python.Runtime;
20 
22 {
23  /// <summary>
24  /// Provides a functional implementation of <see cref="Universe"/>
25  /// </summary>
26  /// <typeparam name="T">The BaseData type to provide to the user defined universe filter</typeparam>
27  public class FuncUniverse<T> : Universe
28  where T : BaseData
29  {
30  private readonly Func<IEnumerable<T>, IEnumerable<Symbol>> _universeSelector;
31 
32  /// <summary>
33  /// Initializes a new instance of the <see cref="FuncUniverse{T}"/> class
34  /// </summary>
35  /// <param name="configuration">The configuration used to resolve the data for universe selection</param>
36  /// <param name="universeSettings">The settings used for new subscriptions generated by this universe</param>
37  /// <param name="universeSelector">Returns the symbols that should be included in the universe</param>
38  public FuncUniverse(SubscriptionDataConfig configuration, UniverseSettings universeSettings, Func<IEnumerable<T>, IEnumerable<Symbol>> universeSelector)
39  : base(configuration)
40  {
41  _universeSelector = universeSelector;
42  _universeSelector ??= (dataPoints) => dataPoints.Select(x => x.Symbol);
43  UniverseSettings = universeSettings;
44  }
45 
46  /// <summary>
47  /// Initializes a new instance of the <see cref="FuncUniverse{T}"/> class for a filter function loaded from Python
48  /// </summary>
49  /// <param name="configuration">The configuration used to resolve the data for universe selection</param>
50  /// <param name="universeSettings">The settings used for new subscriptions generated by this universe</param>
51  /// <param name="universeSelector">Function that returns the symbols that should be included in the universe</param>
52  public FuncUniverse(SubscriptionDataConfig configuration, UniverseSettings universeSettings, PyObject universeSelector)
53  : this(configuration, universeSettings, universeSelector.ConvertPythonUniverseFilterFunction<T>())
54  {
55  }
56 
57  /// <summary>
58  /// Performs an initial, coarse filter
59  /// </summary>
60  /// <param name="utcTime">The current utc time</param>
61  /// <param name="data">The coarse fundamental data</param>
62  /// <returns>The data that passes the filter</returns>
63  public override IEnumerable<Symbol> SelectSymbols(DateTime utcTime, BaseDataCollection data)
64  {
65  return _universeSelector(data.Data.Cast<T>());
66  }
67  }
68 
69  /// <summary>
70  /// Provides a functional implementation of <see cref="Universe"/>
71  /// </summary>
72  public class FuncUniverse : FuncUniverse<BaseData>
73  {
74  /// <summary>
75  /// Initializes a new instance of the <see cref="FuncUniverse"/> class
76  /// </summary>
77  /// <param name="configuration">The configuration used to resolve the data for universe selection</param>
78  /// <param name="universeSettings">The settings used for new subscriptions generated by this universe</param>
79  /// <param name="universeSelector">Returns the symbols that should be included in the universe</param>
80  public FuncUniverse(SubscriptionDataConfig configuration, UniverseSettings universeSettings, Func<IEnumerable<BaseData>, IEnumerable<Symbol>> universeSelector)
81  : base(configuration, universeSettings, universeSelector)
82  {
83  }
84 
85  /// <summary>
86  /// Initializes a new instance of the <see cref="FuncUniverse"/> class
87  /// </summary>
88  /// <param name="configuration">The configuration used to resolve the data for universe selection</param>
89  /// <param name="universeSettings">The settings used for new subscriptions generated by this universe</param>
90  /// <param name="universeSelector">Returns the symbols that should be included in the universe</param>
91  public FuncUniverse(SubscriptionDataConfig configuration, UniverseSettings universeSettings, PyObject universeSelector)
92  : base(configuration, universeSettings, universeSelector)
93  {
94  }
95  }
96 }