Lean  $LEAN_TAG$
UniverseDecorator.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.Concurrent;
18 using System.Collections.Generic;
19 using System.Linq;
22 
24 {
25  /// <summary>
26  /// Provides an implementation of <see cref="UniverseSelection.Universe"/> that redirects all calls to a
27  /// wrapped (or decorated) universe. This provides scaffolding for other decorators who
28  /// only need to override one or two methods.
29  /// </summary>
30  /// <remarks> Requires special handling due to `this != this.Universe`
31  /// <see cref="GetSubscriptionRequests(Security, DateTime, DateTime, ISubscriptionDataConfigService)"/></remarks>
32  public abstract class UniverseDecorator : Universe
33  {
34  /// <summary>
35  /// The decorated universe instance
36  /// </summary>
37  protected readonly Universe Universe;
38 
39  /// <summary>
40  /// Gets the settings used for subscriptions added for this universe
41  /// </summary>
42  public override UniverseSettings UniverseSettings
43  {
44  get
45  {
47  }
48  set
49  {
50  Universe.UniverseSettings = value;
51  }
52  }
53 
54  /// <summary>
55  /// Gets the internal security collection used to define membership in this universe
56  /// </summary>
57  public override ConcurrentDictionary<Symbol, Member> Securities
58  {
59  get { return Universe.Securities; }
60  }
61 
62  /// <summary>
63  /// Initializes a new instance of the <see cref="UniverseDecorator"/> class
64  /// </summary>
65  /// <param name="universe">The decorated universe. All overridable methods delegate to this instance.</param>
66  protected UniverseDecorator(Universe universe)
67  : base(universe.Configuration)
68  {
69  Universe = universe;
70  }
71 
72  /// <summary>
73  /// Gets the subscription requests to be added for the specified security
74  /// </summary>
75  /// <param name="security">The security to get subscriptions for</param>
76  /// <param name="currentTimeUtc">The current time in utc. This is the frontier time of the algorithm</param>
77  /// <param name="maximumEndTimeUtc">The max end time</param>
78  /// <returns>All subscriptions required by this security</returns>
79  [Obsolete("This overload is obsolete and will not be called. It was not capable of creating new SubscriptionDataConfig due to lack of information")]
80  public override IEnumerable<SubscriptionRequest> GetSubscriptionRequests(Security security, DateTime currentTimeUtc, DateTime maximumEndTimeUtc)
81  {
82  return Universe.GetSubscriptionRequests(security, currentTimeUtc, maximumEndTimeUtc);
83  }
84 
85  /// <summary>
86  /// Gets the subscription requests to be added for the specified security
87  /// </summary>
88  /// <param name="security">The security to get subscriptions for</param>
89  /// <param name="currentTimeUtc">The current time in utc. This is the frontier time of the algorithm</param>
90  /// <param name="maximumEndTimeUtc">The max end time</param>
91  /// <param name="subscriptionService">Instance which implements <see cref="ISubscriptionDataConfigService"/> interface</param>
92  /// <returns>All subscriptions required by this security</returns>
93  public override IEnumerable<SubscriptionRequest> GetSubscriptionRequests(Security security,
94  DateTime currentTimeUtc,
95  DateTime maximumEndTimeUtc,
96  ISubscriptionDataConfigService subscriptionService)
97  {
98  var result = Universe.GetSubscriptionRequests(
99  security,
100  currentTimeUtc,
101  maximumEndTimeUtc,
102  subscriptionService).ToList();
103 
104  for (var i = 0; i < result.Count; i++)
105  {
106  // This is required because the system tracks which universe
107  // is requesting to add or remove each SubscriptionRequest.
108  // UniverseDecorator is a special case because
109  // `this != UniverseDecorator.Universe`
110  result[i] =
111  new SubscriptionRequest(result[i], universe: this);
112  }
113 
114  return result;
115  }
116 
117  /// <summary>
118  /// Determines whether or not the specified security can be removed from
119  /// this universe. This is useful to prevent securities from being taken
120  /// out of a universe before the algorithm has had enough time to make
121  /// decisions on the security
122  /// </summary>
123  /// <param name="utcTime">The current utc time</param>
124  /// <param name="security">The security to check if its ok to remove</param>
125  /// <returns>True if we can remove the security, false otherwise</returns>
126  public override bool CanRemoveMember(DateTime utcTime, Security security)
127  {
128  return Universe.CanRemoveMember(utcTime, security);
129  }
130 
131  /// <summary>
132  /// Creates and configures a security for the specified symbol
133  /// </summary>
134  /// <param name="symbol">The symbol of the security to be created</param>
135  /// <param name="algorithm">The algorithm instance</param>
136  /// <param name="marketHoursDatabase">The market hours database</param>
137  /// <param name="symbolPropertiesDatabase">The symbol properties database</param>
138  /// <returns>The newly initialized security object</returns>
139  [Obsolete("CreateSecurity is obsolete and will not be called. The system will create the required Securities based on selected symbols")]
140  public override Security CreateSecurity(Symbol symbol, IAlgorithm algorithm, MarketHoursDatabase marketHoursDatabase, SymbolPropertiesDatabase symbolPropertiesDatabase)
141  {
142  return Universe.CreateSecurity(symbol, algorithm, marketHoursDatabase, symbolPropertiesDatabase);
143  }
144 
145  /// <summary>
146  /// Performs universe selection using the data specified
147  /// </summary>
148  /// <param name="utcTime">The current utc time</param>
149  /// <param name="data">The symbols to remain in the universe</param>
150  /// <returns>The data that passes the filter</returns>
151  public override IEnumerable<Symbol> SelectSymbols(DateTime utcTime, BaseDataCollection data)
152  {
153  return Universe.SelectSymbols(utcTime, data);
154  }
155  }
156 }