Lean  $LEAN_TAG$
DataConsolidator.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  /// Represents a type that consumes BaseData instances and fires an event with consolidated
23  /// and/or aggregated data.
24  /// </summary>
25  /// <typeparam name="TInput">The type consumed by the consolidator</typeparam>
26  public abstract class DataConsolidator<TInput> : IDataConsolidator
27  where TInput : IBaseData
28  {
29  /// <summary>
30  /// Updates this consolidator with the specified data
31  /// </summary>
32  /// <param name="data">The new data for the consolidator</param>
33  public void Update(IBaseData data)
34  {
35  if (!(data is TInput))
36  {
37  throw new ArgumentNullException(nameof(data),
38  $"Received type of {data.GetType().Name} but expected {typeof(TInput).Name}"
39  );
40  }
41  Update((TInput)data);
42  }
43 
44  /// <summary>
45  /// Scans this consolidator to see if it should emit a bar due to time passing
46  /// </summary>
47  /// <param name="currentLocalTime">The current time in the local time zone (same as <see cref="BaseData.Time"/>)</param>
48  public abstract void Scan(DateTime currentLocalTime);
49 
50  /// <summary>
51  /// Event handler that fires when a new piece of data is produced
52  /// </summary>
54 
55  /// <summary>
56  /// Gets the most recently consolidated piece of data. This will be null if this consolidator
57  /// has not produced any data yet.
58  /// </summary>
59  public IBaseData Consolidated
60  {
61  get; private set;
62  }
63 
64  /// <summary>
65  /// Gets a clone of the data being currently consolidated
66  /// </summary>
67  public abstract IBaseData WorkingData
68  {
69  get;
70  }
71 
72  /// <summary>
73  /// Gets the type consumed by this consolidator
74  /// </summary>
75  public Type InputType
76  {
77  get { return typeof (TInput); }
78  }
79 
80  /// <summary>
81  /// Gets the type produced by this consolidator
82  /// </summary>
83  public abstract Type OutputType
84  {
85  get;
86  }
87 
88  /// <summary>
89  /// Updates this consolidator with the specified data. This method is
90  /// responsible for raising the DataConsolidated event
91  /// </summary>
92  /// <param name="data">The new data for the consolidator</param>
93  public abstract void Update(TInput data);
94 
95  /// <summary>
96  /// Event invocator for the DataConsolidated event. This should be invoked
97  /// by derived classes when they have consolidated a new piece of data.
98  /// </summary>
99  /// <param name="consolidated">The newly consolidated data</param>
100  protected virtual void OnDataConsolidated(IBaseData consolidated)
101  {
102  var handler = DataConsolidated;
103  if (handler != null) handler(this, consolidated);
104 
105  // assign the Consolidated property after the event handlers are fired,
106  // this allows the event handlers to look at the new consolidated data
107  // and the previous consolidated data at the same time without extra bookkeeping
108  Consolidated = consolidated;
109  }
110 
111  /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
112  /// <filterpriority>2</filterpriority>
113  public void Dispose()
114  {
115  DataConsolidated = null;
116  }
117  }
118 }