Lean  $LEAN_TAG$
FineFundamentalUniverseSelectionModel.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 Python.Runtime;
21 
23 {
24  /// <summary>
25  /// Portfolio selection model that uses coarse/fine selectors. For US equities only.
26  /// </summary>
28  {
29  private readonly Func<IEnumerable<CoarseFundamental>, IEnumerable<Symbol>> _coarseSelector;
30  private readonly Func<IEnumerable<FineFundamental>, IEnumerable<Symbol>> _fineSelector;
31 
32  /// <summary>
33  /// Initializes a new instance of the <see cref="FineFundamentalUniverseSelectionModel"/> class
34  /// </summary>
35  /// <param name="coarseSelector">Selects symbols from the provided coarse data set</param>
36  /// <param name="fineSelector">Selects symbols from the provided fine data set (this set has already been filtered according to the coarse selection)</param>
37  /// <param name="universeSettings">Universe settings define attributes of created subscriptions, such as their resolution and the minimum time in universe before they can be removed</param>
39  Func<IEnumerable<CoarseFundamental>, IEnumerable<Symbol>> coarseSelector,
40  Func<IEnumerable<FineFundamental>, IEnumerable<Symbol>> fineSelector,
41  UniverseSettings universeSettings = null)
42  : base(true, universeSettings)
43  {
44  _coarseSelector = coarseSelector;
45  _fineSelector = fineSelector;
46  }
47 
48  /// <summary>
49  /// Initializes a new instance of the <see cref="FineFundamentalUniverseSelectionModel"/> class
50  /// </summary>
51  /// <param name="coarseSelector">Selects symbols from the provided coarse data set</param>
52  /// <param name="fineSelector">Selects symbols from the provided fine data set (this set has already been filtered according to the coarse selection)</param>
53  /// <param name="universeSettings">Universe settings define attributes of created subscriptions, such as their resolution and the minimum time in universe before they can be removed</param>
55  PyObject coarseSelector,
56  PyObject fineSelector,
57  UniverseSettings universeSettings = null
58  )
59  : base(true, universeSettings)
60  {
61  Func<IEnumerable<FineFundamental>, object> fineFunc;
62  Func<IEnumerable<CoarseFundamental>, object> coarseFunc;
63  if (fineSelector.TryConvertToDelegate(out fineFunc) &&
64  coarseSelector.TryConvertToDelegate(out coarseFunc))
65  {
66  _fineSelector = fineFunc.ConvertToUniverseSelectionSymbolDelegate();
67  _coarseSelector = coarseFunc.ConvertToUniverseSelectionSymbolDelegate();
68  }
69  }
70 
71  /// <inheritdoc />
72  public override IEnumerable<Symbol> SelectCoarse(QCAlgorithm algorithm, IEnumerable<CoarseFundamental> coarse)
73  {
74  return _coarseSelector(coarse);
75  }
76 
77  /// <inheritdoc />
78  public override IEnumerable<Symbol> SelectFine(QCAlgorithm algorithm, IEnumerable<FineFundamental> fine)
79  {
80  return _fineSelector(fine);
81  }
82  }
83 }