forked from QuantConnect/Lean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUniverseDefinitions.cs
355 lines (326 loc) · 17.2 KB
/
UniverseDefinitions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
using System.Linq;
using Python.Runtime;
using QuantConnect.Data;
using System.Collections.Generic;
using QuantConnect.Data.Fundamental;
using QuantConnect.Data.UniverseSelection;
namespace QuantConnect.Algorithm
{
/// <summary>
/// Provides helpers for defining universes in algorithms
/// </summary>
public class UniverseDefinitions
{
private readonly QCAlgorithm _algorithm;
/// <summary>
/// Gets a helper that provides methods for creating universes based on daily dollar volumes
/// </summary>
public DollarVolumeUniverseDefinitions DollarVolume { get; set; }
/// <summary>
/// Specifies that universe selection should not make changes on this iteration
/// </summary>
public Universe.UnchangedUniverse Unchanged => Universe.Unchanged;
/// <summary>
/// Initializes a new instance of the <see cref="UniverseDefinitions"/> class
/// </summary>
/// <param name="algorithm">The algorithm instance, used for obtaining the default <see cref="UniverseSettings"/></param>
public UniverseDefinitions(QCAlgorithm algorithm)
{
_algorithm = algorithm;
DollarVolume = new DollarVolumeUniverseDefinitions(algorithm);
}
/// <summary>
/// Creates a universe for the constituents of the provided <paramref name="etfTicker"/>
/// </summary>
/// <param name="etfTicker">Ticker of the ETF to get constituents for</param>
/// <param name="market">Market of the ETF</param>
/// <param name="universeSettings">Universe settings</param>
/// <param name="universeFilterFunc">Function to filter universe results</param>
/// <returns>New ETF constituents Universe</returns>
public Universe ETF(
string etfTicker,
string market,
UniverseSettings universeSettings,
Func<IEnumerable<ETFConstituentData>, IEnumerable<Symbol>> universeFilterFunc)
{
market ??= _algorithm.BrokerageModel.DefaultMarkets.TryGetValue(SecurityType.Equity, out var defaultMarket)
? defaultMarket
: throw new Exception("No default market set for security type: Equity");
var etfSymbol = new Symbol(
SecurityIdentifier.GenerateEquity(
etfTicker,
market,
true,
mappingResolveDate: _algorithm.Time.Date),
etfTicker);
return ETF(etfSymbol, universeSettings, universeFilterFunc);
}
/// <summary>
/// Creates a universe for the constituents of the provided <paramref name="etfTicker"/>
/// </summary>
/// <param name="etfTicker">Ticker of the ETF to get constituents for</param>
/// <param name="market">Market of the ETF</param>
/// <param name="universeFilterFunc">Function to filter universe results</param>
/// <returns>New ETF constituents Universe</returns>
public Universe ETF(string etfTicker, string market, Func<IEnumerable<ETFConstituentData>, IEnumerable<Symbol>> universeFilterFunc)
{
return ETF(etfTicker, market, null, universeFilterFunc);
}
/// <summary>
/// Creates a universe for the constituents of the provided <paramref name="etfTicker"/>
/// </summary>
/// <param name="etfTicker">Ticker of the ETF to get constituents for</param>
/// <param name="universeFilterFunc">Function to filter universe results</param>
/// <returns>New ETF constituents Universe</returns>
public Universe ETF(string etfTicker, Func<IEnumerable<ETFConstituentData>, IEnumerable<Symbol>> universeFilterFunc)
{
return ETF(etfTicker, null, null, universeFilterFunc);
}
/// <summary>
/// Creates a universe for the constituents of the provided <paramref name="etfTicker"/>
/// </summary>
/// <param name="etfTicker">Ticker of the ETF to get constituents for</param>
/// <param name="universeSettings">Universe settings</param>
/// <param name="universeFilterFunc">Function to filter universe results</param>
/// <returns>New ETF constituents Universe</returns>
public Universe ETF(
string etfTicker,
UniverseSettings universeSettings,
Func<IEnumerable<ETFConstituentData>, IEnumerable<Symbol>> universeFilterFunc)
{
return ETF(etfTicker, null, universeSettings, universeFilterFunc);
}
/// <summary>
/// Creates a universe for the constituents of the provided <paramref name="etfTicker"/>
/// </summary>
/// <param name="etfTicker">Ticker of the ETF to get constituents for</param>
/// <param name="market">Market of the ETF</param>
/// <param name="universeSettings">Universe settings</param>
/// <param name="universeFilterFunc">Function to filter universe results</param>
/// <returns>New ETF constituents Universe</returns>
public Universe ETF(
string etfTicker,
string market = null,
UniverseSettings universeSettings = null,
PyObject universeFilterFunc = null)
{
return ETF(etfTicker, market, universeSettings, universeFilterFunc?.ConvertPythonUniverseFilterFunction<ETFConstituentData>());
}
/// <summary>
/// Creates a universe for the constituents of the provided <paramref name="etfTicker"/>
/// </summary>
/// <param name="etfTicker">Ticker of the ETF to get constituents for</param>
/// <param name="universeSettings">Universe settings</param>
/// <param name="universeFilterFunc">Function to filter universe results</param>
/// <returns>New ETF constituents Universe</returns>
public Universe ETF(
string etfTicker,
UniverseSettings universeSettings,
PyObject universeFilterFunc)
{
return ETF(etfTicker, null, universeSettings, universeFilterFunc);
}
/// <summary>
/// Creates a universe for the constituents of the provided ETF <paramref name="symbol"/>
/// </summary>
/// <param name="symbol">ETF Symbol to get constituents for</param>
/// <param name="universeSettings">Universe settings</param>
/// <param name="universeFilterFunc">Function to filter universe results</param>
/// <returns>New ETF constituents Universe</returns>
public Universe ETF(Symbol symbol, UniverseSettings universeSettings,
Func<IEnumerable<ETFConstituentData>, IEnumerable<Symbol>> universeFilterFunc)
{
return new ETFConstituentsUniverse(symbol, universeSettings ?? _algorithm.UniverseSettings, universeFilterFunc);
}
/// <summary>
/// Creates a universe for the constituents of the provided ETF <paramref name="symbol"/>
/// </summary>
/// <param name="symbol">ETF Symbol to get constituents for</param>
/// <param name="universeFilterFunc">Function to filter universe results</param>
/// <returns>New ETF constituents Universe</returns>
public Universe ETF(Symbol symbol, Func<IEnumerable<ETFConstituentData>, IEnumerable<Symbol>> universeFilterFunc)
{
return ETF(symbol, null, universeFilterFunc);
}
/// <summary>
/// Creates a universe for the constituents of the provided ETF <paramref name="symbol"/>
/// </summary>
/// <param name="symbol">ETF Symbol to get constituents for</param>
/// <param name="universeSettings">Universe settings</param>
/// <param name="universeFilterFunc">Function to filter universe results</param>
/// <returns>New ETF constituents Universe</returns>
public Universe ETF(Symbol symbol, UniverseSettings universeSettings = null, PyObject universeFilterFunc = null)
{
return ETF(symbol, universeSettings ?? _algorithm.UniverseSettings,
universeFilterFunc?.ConvertPythonUniverseFilterFunction<ETFConstituentData>());
}
/// <summary>
/// Creates a universe for the constituents of the provided <paramref name="indexTicker"/>
/// </summary>
/// <param name="indexTicker">Ticker of the index to get constituents for</param>
/// <param name="market">Market of the index</param>
/// <param name="universeSettings">Universe settings</param>
/// <param name="universeFilterFunc">Function to filter universe results</param>
/// <returns>New index constituents Universe</returns>
public Universe Index(string indexTicker, string market, UniverseSettings universeSettings,
Func<IEnumerable<ETFConstituentData>, IEnumerable<Symbol>> universeFilterFunc)
{
market ??= _algorithm.BrokerageModel.DefaultMarkets.TryGetValue(SecurityType.Index, out var defaultMarket)
? defaultMarket
: throw new Exception("No default market set for security type: Index");
return Index(
Symbol.Create(indexTicker, SecurityType.Index, market),
universeSettings,
universeFilterFunc);
}
/// <summary>
/// Creates a universe for the constituents of the provided <paramref name="indexTicker"/>
/// </summary>
/// <param name="indexTicker">Ticker of the index to get constituents for</param>
/// <param name="market">Market of the index</param>
/// <param name="universeSettings">Universe settings</param>
/// <param name="universeFilterFunc">Function to filter universe results</param>
/// <returns>New index constituents Universe</returns>
public Universe Index(string indexTicker, string market, Func<IEnumerable<ETFConstituentData>, IEnumerable<Symbol>> universeFilterFunc)
{
return Index(indexTicker, market, null, universeFilterFunc);
}
/// <summary>
/// Creates a universe for the constituents of the provided <paramref name="indexTicker"/>
/// </summary>
/// <param name="indexTicker">Ticker of the index to get constituents for</param>
/// <param name="universeFilterFunc">Function to filter universe results</param>
/// <returns>New index constituents Universe</returns>
public Universe Index(string indexTicker, Func<IEnumerable<ETFConstituentData>, IEnumerable<Symbol>> universeFilterFunc)
{
return Index(indexTicker, null, null, universeFilterFunc);
}
/// <summary>
/// Creates a universe for the constituents of the provided <paramref name="indexTicker"/>
/// </summary>
/// <param name="indexTicker">Ticker of the index to get constituents for</param>
/// <param name="market">Market of the index</param>
/// <param name="universeSettings">Universe settings</param>
/// <param name="universeFilterFunc">Function to filter universe results</param>
/// <returns>New index constituents Universe</returns>
public Universe Index(string indexTicker, UniverseSettings universeSettings,
Func<IEnumerable<ETFConstituentData>, IEnumerable<Symbol>> universeFilterFunc)
{
return Index(indexTicker, null, universeSettings, universeFilterFunc);
}
/// <summary>
/// Creates a universe for the constituents of the provided <paramref name="indexTicker"/>
/// </summary>
/// <param name="indexTicker">Ticker of the index to get constituents for</param>
/// <param name="market">Market of the index</param>
/// <param name="universeSettings">Universe settings</param>
/// <param name="universeFilterFunc">Function to filter universe results</param>
/// <returns>New index constituents Universe</returns>
public Universe Index(
string indexTicker,
string market = null,
UniverseSettings universeSettings = null,
PyObject universeFilterFunc = null)
{
return Index(indexTicker, market, universeSettings, universeFilterFunc?.ConvertPythonUniverseFilterFunction<ETFConstituentData>());
}
/// <summary>
/// Creates a universe for the constituents of the provided <paramref name="indexTicker"/>
/// </summary>
/// <param name="indexTicker">Ticker of the index to get constituents for</param>
/// <param name="universeSettings">Universe settings</param>
/// <param name="universeFilterFunc">Function to filter universe results</param>
/// <returns>New index constituents Universe</returns>
public Universe Index(
string indexTicker,
UniverseSettings universeSettings,
PyObject universeFilterFunc)
{
return Index(indexTicker, null, universeSettings, universeFilterFunc);
}
/// <summary>
/// Creates a universe for the constituents of the provided <paramref name="indexSymbol"/>
/// </summary>
/// <param name="indexSymbol">Index Symbol to get constituents for</param>
/// <param name="universeSettings">Universe settings</param>
/// <param name="universeFilterFunc">Function to filter universe results</param>
/// <returns>New index constituents Universe</returns>
public Universe Index(Symbol indexSymbol, UniverseSettings universeSettings,
Func<IEnumerable<ETFConstituentData>, IEnumerable<Symbol>> universeFilterFunc)
{
return new ETFConstituentsUniverse(indexSymbol, universeSettings, universeFilterFunc);
}
/// <summary>
/// Creates a universe for the constituents of the provided <paramref name="indexSymbol"/>
/// </summary>
/// <param name="indexSymbol">Index Symbol to get constituents for</param>
/// <param name="universeFilterFunc">Function to filter universe results</param>
/// <returns>New index constituents Universe</returns>
public Universe Index(Symbol indexSymbol, Func<IEnumerable<ETFConstituentData>, IEnumerable<Symbol>> universeFilterFunc)
{
return Index(indexSymbol, null, universeFilterFunc);
}
/// <summary>
/// Creates a universe for the constituents of the provided <paramref name="indexSymbol"/>
/// </summary>
/// <param name="indexSymbol">Index Symbol to get constituents for</param>
/// <param name="universeSettings">Universe settings</param>
/// <param name="universeFilterFunc">Function to filter universe results</param>
/// <returns>New index constituents Universe</returns>
public Universe Index(
Symbol indexSymbol,
UniverseSettings universeSettings = null,
PyObject universeFilterFunc = null)
{
return Index(indexSymbol, universeSettings ?? _algorithm.UniverseSettings,
universeFilterFunc?.ConvertPythonUniverseFilterFunction<ETFConstituentData>());
}
/// <summary>
/// Creates a new fine universe that contains the constituents of QC500 index based onthe company fundamentals
/// The algorithm creates a default tradable and liquid universe containing 500 US equities
/// which are chosen at the first trading day of each month.
/// </summary>
/// <returns>A new coarse universe for the top count of stocks by dollar volume</returns>
public Universe QC500
{
get
{
return ETF(Symbol.Create("SPY", SecurityType.Equity, Market.USA));
}
}
/// <summary>
/// Creates a new coarse universe that contains the top count of stocks
/// by daily dollar volume
/// </summary>
/// <param name="count">The number of stock to select</param>
/// <param name="universeSettings">The settings for stocks added by this universe.
/// Defaults to <see cref="QCAlgorithm.UniverseSettings"/></param>
/// <returns>A new coarse universe for the top count of stocks by dollar volume</returns>
public Universe Top(int count, UniverseSettings universeSettings = null)
{
universeSettings ??= _algorithm.UniverseSettings;
var symbol = Symbol.Create("us-equity-dollar-volume-top-" + count, SecurityType.Equity, Market.USA);
return FundamentalUniverse.USA(selectionData => (
from c in selectionData
orderby c.DollarVolume descending
select c.Symbol).Take(count),
universeSettings);
}
}
}