Lean  $LEAN_TAG$
IdentityDataConsolidator.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;
18 
20 {
21  /// <summary>
22  /// Represents the simplest DataConsolidator implementation, one that is defined
23  /// by a straight pass through of the data. No projection or aggregation is performed.
24  /// </summary>
25  /// <typeparam name="T">The type of data</typeparam>
27  where T : IBaseData
28  {
29  private static readonly bool IsTick = typeof(T) == typeof(Tick);
30 
31  private T _last;
32 
33  /// <summary>
34  /// Stores the timestamp of the last processed data item.
35  /// </summary>
36  private DateTime _lastTime;
37 
38  /// <summary>
39  /// Gets a clone of the data being currently consolidated
40  /// </summary>
41  public override IBaseData WorkingData
42  {
43  get { return _last == null ? null : _last.Clone(); }
44  }
45 
46  /// <summary>
47  /// Gets the type produced by this consolidator
48  /// </summary>
49  public override Type OutputType
50  {
51  get { return typeof(T); }
52  }
53 
54  /// <summary>
55  /// Updates this consolidator with the specified data
56  /// </summary>
57  /// <param name="data">The new data for the consolidator</param>
58  public override void Update(T data)
59  {
60  if (IsTick || _last == null || data.EndTime != _lastTime)
61  {
62  OnDataConsolidated(data);
63  _last = data;
64  _lastTime = data.EndTime;
65  }
66  }
67 
68  /// <summary>
69  /// Scans this consolidator to see if it should emit a bar due to time passing
70  /// </summary>
71  /// <param name="currentLocalTime">The current time in the local time zone (same as <see cref="BaseData.Time"/>)</param>
72  public override void Scan(DateTime currentLocalTime)
73  {
74  }
75 
76  /// <summary>
77  /// Resets the consolidator
78  /// </summary>
79  public override void Reset()
80  {
81  base.Reset();
82  _last = default(T);
83  }
84  }
85 }