Lean  $LEAN_TAG$
ManualUniverse.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 QuantConnect.Data;
23 
25 {
26  /// <summary>
27  /// Defines a universe as a set of manually set symbols. This differs from <see cref="UserDefinedUniverse"/>
28  /// in that these securities were not added via AddSecurity.
29  /// </summary>
30  /// <remarks>Incompatible with multiple <see cref="Universe"/> selecting the same <see cref="Symbol"/>.
31  /// with different <see cref="SubscriptionDataConfig"/>. More information <see cref="GetSubscriptionRequests"/></remarks>
33  {
34  /// <summary>
35  /// Creates a new instance of the <see cref="ManualUniverse"/>
36  /// </summary>
37  public ManualUniverse(SubscriptionDataConfig configuration,
38  UniverseSettings universeSettings,
39  IEnumerable<Symbol> symbols)
40  : base(configuration, universeSettings, Time.MaxTimeSpan, symbols)
41  {
42  }
43 
44  /// <summary>
45  /// Creates a new instance of the <see cref="ManualUniverse"/>
46  /// </summary>
47  public ManualUniverse(SubscriptionDataConfig configuration,
48  UniverseSettings universeSettings,
49  Symbol[] symbols)
50  : base(configuration, universeSettings, Time.MaxTimeSpan, symbols)
51  {
52  }
53 
54  /// <summary>
55  /// Gets the subscription requests to be added for the specified security
56  /// </summary>
57  /// <param name="security">The security to get subscriptions for</param>
58  /// <param name="currentTimeUtc">The current time in utc. This is the frontier time of the algorithm</param>
59  /// <param name="maximumEndTimeUtc">The max end time</param>
60  /// <param name="subscriptionService">Instance which implements <see cref="ISubscriptionDataConfigService"/> interface</param>
61  /// <returns>All subscriptions required by this security</returns>
62  public override IEnumerable<SubscriptionRequest> GetSubscriptionRequests(Security security, DateTime currentTimeUtc, DateTime maximumEndTimeUtc,
63  ISubscriptionDataConfigService subscriptionService)
64  {
65  // ManualUniverse will return any existing SDC for the symbol, else will create new, using universe settings.
66  // This is for maintaining existing behavior and preventing breaking changes: Specifically motivated
67  // by usages of Algorithm.Securities.Keys as constructor parameter of the ManualUniverseSelectionModel.
68  // Symbols at Algorithm.Securities.Keys added by Addxxx() calls will already be added by the UserDefinedUniverse.
69 
70  var existingSubscriptionDataConfigs = subscriptionService.GetSubscriptionDataConfigs(security.Symbol);
71 
72  if (existingSubscriptionDataConfigs.Any())
73  {
74  return existingSubscriptionDataConfigs.Select(
75  config => new SubscriptionRequest(isUniverseSubscription: false,
76  universe: this,
77  security: security,
78  configuration: config,
79  startTimeUtc: currentTimeUtc,
80  endTimeUtc: maximumEndTimeUtc));
81  }
82  return base.GetSubscriptionRequests(security, currentTimeUtc, maximumEndTimeUtc, subscriptionService);
83  }
84  }
85 }