Lean  $LEAN_TAG$
FundamentalUniverse.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;
20 
22 {
23  /// <summary>
24  /// Lean fundamentals universe data class
25  /// </summary>
26  [Obsolete("'Fundamentals' was renamed to 'FundamentalUniverse'")]
27  public class Fundamentals : FundamentalUniverse { }
28 
29  /// <summary>
30  /// Lean fundamentals universe data class
31  /// </summary>
33  {
34  private static readonly Fundamental _factory = new();
35 
36  /// <summary>
37  /// Creates a new instance
38  /// </summary>
40  {
41  }
42 
43  /// <summary>
44  /// Creates a new instance
45  /// </summary>
46  /// <param name="time">The current time</param>
47  /// <param name="symbol">The associated symbol</param>
48  public FundamentalUniverse(DateTime time, Symbol symbol) : base(time, symbol)
49  {
50  }
51 
52  /// <summary>
53  /// Return the URL string source of the file. This will be converted to a stream
54  /// </summary>
55  public override SubscriptionDataSource GetSource(SubscriptionDataConfig config, DateTime date, bool isLiveMode)
56  {
57  var path = _factory.GetSource(config, date, isLiveMode).Source;
58  return new SubscriptionDataSource(path, SubscriptionTransportMedium.LocalFile, FileFormat.FoldingCollection);
59  }
60 
61  /// <summary>
62  /// Will read a new instance from the given line
63  /// </summary>
64  /// <param name="config">The associated requested configuration</param>
65  /// <param name="line">The line to parse</param>
66  /// <param name="date">The current time</param>
67  /// <param name="isLiveMode">True if live mode</param>
68  /// <returns>A new instance or null</returns>
69  public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, bool isLiveMode)
70  {
71  try
72  {
73  var csv = line.Split(',');
74  var symbol = new Symbol(SecurityIdentifier.Parse(csv[0]), csv[1]);
75  return new Fundamental(date, symbol);
76  }
77  catch (Exception)
78  {
79  return null;
80  }
81  }
82 
83  /// <summary>
84  /// Will clone the current instance
85  /// </summary>
86  /// <returns>The cloned instance</returns>
87  public override BaseData Clone()
88  {
89  return new FundamentalUniverse(Time, Symbol) { Data = Data, EndTime = EndTime };
90  }
91 
92  /// <summary>
93  /// Gets the default resolution for this data and security type
94  /// </summary>
95  /// <remarks>This is a method and not a property so that python
96  /// custom data types can override it</remarks>
97  public override Resolution DefaultResolution()
98  {
99  return Resolution.Daily;
100  }
101 
102  /// <summary>
103  /// Creates the universe symbol for the target market
104  /// </summary>
105  /// <returns>The universe symbol to use</returns>
106  public override Symbol UniverseSymbol(string market = null)
107  {
108  market ??= QuantConnect.Market.USA;
109  var ticker = $"{GetType().Name}-{market}-{Guid.NewGuid()}";
110  return Symbol.Create(ticker, SecurityType.Equity, market, baseDataType: GetType());
111  }
112 
113  /// <summary>
114  /// Creates a new fundamental universe for the USA market
115  /// </summary>
116  /// <param name="selector">The selector function</param>
117  /// <param name="universeSettings">The universe settings to use, will default to algorithms if not provided</param>
118  /// <returns>A configured new universe instance</returns>
119  public static FundamentalUniverseFactory USA(Func<IEnumerable<Fundamental>, IEnumerable<Symbol>> selector, UniverseSettings universeSettings = null)
120  {
121  return new FundamentalUniverseFactory(QuantConnect.Market.USA, universeSettings, selector);
122  }
123 
124  /// <summary>
125  /// Creates a new fundamental universe for the USA market
126  /// </summary>
127  /// <param name="selector">The selector function</param>
128  /// <param name="universeSettings">The universe settings to use, will default to algorithms if not provided</param>
129  /// <returns>A configured new universe instance</returns>
130  public static FundamentalUniverseFactory USA(PyObject selector, UniverseSettings universeSettings = null)
131  {
132  return new FundamentalUniverseFactory(QuantConnect.Market.USA, universeSettings, selector);
133  }
134 
135  /// <summary>
136  /// Creates a new fundamental universe for the USA market
137  /// </summary>
138  /// <param name="selector">The selector function</param>
139  /// <param name="universeSettings">The universe settings to use, will default to algorithms if not provided</param>
140  /// <returns>A configured new universe instance</returns>
141  public static FundamentalUniverseFactory USA(Func<IEnumerable<Fundamental>, object> selector, UniverseSettings universeSettings = null)
142  {
143  return new FundamentalUniverseFactory(QuantConnect.Market.USA, universeSettings, selector);
144  }
145  }
146 }