Lean  $LEAN_TAG$
SubscriptionFrontierTimeProvider.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 
17 using System;
18 
20 {
21  /// <summary>
22  /// A time provider which updates 'now' time based on the current data emit time of all subscriptions
23  /// </summary>
24  /// <remarks>This class is not thread safe but there is no need for it to be since it's only consumed by the
25  /// <see cref="SubscriptionSynchronizer"/></remarks>
27  {
28  private static readonly long MaxDateTimeTicks = DateTime.MaxValue.Ticks;
29  private DateTime _utcNow;
30  private readonly IDataFeedSubscriptionManager _subscriptionManager;
31 
32  /// <summary>
33  /// Creates a new instance of the SubscriptionFrontierTimeProvider
34  /// </summary>
35  /// <param name="utcNow">Initial UTC now time</param>
36  /// <param name="subscriptionManager">Subscription manager. Will be used to obtain current subscriptions</param>
37  public SubscriptionFrontierTimeProvider(DateTime utcNow, IDataFeedSubscriptionManager subscriptionManager)
38  {
39  _utcNow = utcNow;
40  _subscriptionManager = subscriptionManager;
41  }
42 
43  /// <summary>
44  /// Gets the current time in UTC
45  /// </summary>
46  /// <returns>The current time in UTC</returns>
47  public DateTime GetUtcNow()
48  {
49  UpdateCurrentTime();
50  return _utcNow;
51  }
52 
53  /// <summary>
54  /// Sets the current time calculated as the minimum current data emit time of all the subscriptions.
55  /// If there are no subscriptions current time will remain unchanged
56  /// </summary>
57  private void UpdateCurrentTime()
58  {
59  long earlyBirdTicks = MaxDateTimeTicks;
60  foreach (var subscription in _subscriptionManager.DataFeedSubscriptions)
61  {
62  // this if should just be 'subscription.Current == null' but its affected by GH issue 3914
63  if (// this is a data subscription we just added
64  // lets move it next to find the initial emit time
65  subscription.Current == null
66  && !subscription.IsUniverseSelectionSubscription
67  && subscription.UtcStartTime == _utcNow
68  ||
69  // UserDefinedUniverse, through the AddData calls
70  // will add new universe selection data points when is has too
71  // so lets move it next to check if there is any
72  subscription.Current == null
73  && subscription.IsUniverseSelectionSubscription
74  && subscription.UtcStartTime != _utcNow)
75  {
76  subscription.MoveNext();
77  }
78 
79  if (subscription.Current != null)
80  {
81  if (earlyBirdTicks == MaxDateTimeTicks)
82  {
83  earlyBirdTicks = subscription.Current.EmitTimeUtc.Ticks;
84  }
85  else
86  {
87  // take the earliest between the next piece of data or the current earliest bird
88  earlyBirdTicks = Math.Min(earlyBirdTicks, subscription.Current.EmitTimeUtc.Ticks);
89  }
90  }
91  }
92 
93  if (earlyBirdTicks != MaxDateTimeTicks)
94  {
95  _utcNow = new DateTime(Math.Max(earlyBirdTicks, _utcNow.Ticks), DateTimeKind.Utc);
96  }
97  }
98  }
99 }