Lean  $LEAN_TAG$
PredicateTimeProvider.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  /// Will generate time steps around the desired <see cref="ITimeProvider"/>
23  /// Provided step evaluator should return true when the next time step
24  /// is valid and time can advance
25  /// </summary>
27  {
28  private readonly ITimeProvider _underlyingTimeProvider;
29  private readonly Func<DateTime, bool> _customStepEvaluator;
30  private DateTime _currentUtc = DateTime.MinValue;
31 
32  /// <summary>
33  /// Creates a new instance
34  /// </summary>
35  /// <param name="underlyingTimeProvider">The timer provider instance to wrap</param>
36  /// <param name="customStepEvaluator">Function to evaluate whether or not
37  /// to advance time. Should return true if provided <see cref="DateTime"/> is a
38  /// valid new next time. False will avoid time advancing</param>
39  public PredicateTimeProvider(ITimeProvider underlyingTimeProvider,
40  Func<DateTime, bool> customStepEvaluator)
41  {
42  _underlyingTimeProvider = underlyingTimeProvider;
43  _customStepEvaluator = customStepEvaluator;
44  }
45 
46  /// <summary>
47  /// Gets the current utc time step
48  /// </summary>
49  public DateTime GetUtcNow()
50  {
51  if (_currentUtc == DateTime.MinValue)
52  {
53  Initialize();
54  }
55  var utcNow = _underlyingTimeProvider.GetUtcNow();
56 
57  // we check if we should advance time based on the provided custom step evaluator
58  if (_customStepEvaluator(utcNow))
59  {
60  _currentUtc = utcNow;
61  }
62  return _currentUtc;
63  }
64 
65  private void Initialize()
66  {
67  // to determine the current time we go backwards up to 2 days until we reach a valid time we don't want to start on an invalid time
68  var utcNow = _underlyingTimeProvider.GetUtcNow();
69  for (var i = 0; i < 48; i++)
70  {
71  var before = utcNow.AddHours(-1 * i);
72  if (_customStepEvaluator(before))
73  {
74  _currentUtc = before;
75  }
76  }
77  }
78  }
79 }