Lean
$LEAN_TAG$
SequentialConsolidator.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
18
namespace
QuantConnect.Data.Consolidators
19
{
20
/// <summary>
21
/// This consolidator wires up the events on its First and Second consolidators
22
/// such that data flows from the First to Second consolidator. It's output comes
23
/// from the Second.
24
/// </summary>
25
public
class
SequentialConsolidator
:
IDataConsolidator
26
{
27
/// <summary>
28
/// Gets the first consolidator to receive data
29
/// </summary>
30
public
IDataConsolidator
First
31
{
32
get
;
private
set
;
33
}
34
35
/// <summary>
36
/// Gets the second consolidator that ends up receiving data produced
37
/// by the first
38
/// </summary>
39
public
IDataConsolidator
Second
40
{
41
get
;
private
set
;
42
}
43
44
/// <summary>
45
/// Gets the most recently consolidated piece of data. This will be null if this consolidator
46
/// has not produced any data yet.
47
///
48
/// For a SequentialConsolidator, this is the output from the 'Second' consolidator.
49
/// </summary>
50
public
IBaseData
Consolidated
51
{
52
get
{
return
Second
.
Consolidated
; }
53
}
54
55
/// <summary>
56
/// Gets a clone of the data being currently consolidated
57
/// </summary>
58
public
IBaseData
WorkingData
59
{
60
get
{
return
Second
.
WorkingData
; }
61
}
62
63
/// <summary>
64
/// Gets the type consumed by this consolidator
65
/// </summary>
66
public
Type
InputType
67
{
68
get
{
return
First
.
InputType
; }
69
}
70
71
/// <summary>
72
/// Gets the type produced by this consolidator
73
/// </summary>
74
public
Type
OutputType
75
{
76
get
{
return
Second
.
OutputType
; }
77
}
78
79
/// <summary>
80
/// Updates this consolidator with the specified data
81
/// </summary>
82
/// <param name="data">The new data for the consolidator</param>
83
public
void
Update
(
IBaseData
data)
84
{
85
First
.
Update
(data);
86
}
87
88
/// <summary>
89
/// Scans this consolidator to see if it should emit a bar due to time passing
90
/// </summary>
91
/// <param name="currentLocalTime">The current time in the local time zone (same as <see cref="BaseData.Time"/>)</param>
92
public
void
Scan
(DateTime currentLocalTime)
93
{
94
First
.
Scan
(currentLocalTime);
95
}
96
97
/// <summary>
98
/// Event handler that fires when a new piece of data is produced
99
/// </summary>
100
public
event
DataConsolidatedHandler
DataConsolidated
;
101
102
/// <summary>
103
/// Creates a new consolidator that will pump date through the first, and then the output
104
/// of the first into the second. This enables 'wrapping' or 'composing' of consolidators
105
/// </summary>
106
/// <param name="first">The first consolidator to receive data</param>
107
/// <param name="second">The consolidator to receive first's output</param>
108
public
SequentialConsolidator
(
IDataConsolidator
first,
IDataConsolidator
second)
109
{
110
if
(!second.
InputType
.IsAssignableFrom(first.
OutputType
))
111
{
112
throw
new
ArgumentException(
"first.OutputType must equal second.OutputType!"
);
113
}
114
First
= first;
115
Second
= second;
116
117
// wire up the second one to get data from the first
118
first.
DataConsolidated
+= (sender, consolidated) => second.
Update
(consolidated);
119
120
// wire up the second one's events to also fire this consolidator's event so consumers
121
// can attach
122
second.
DataConsolidated
+= (sender, consolidated) =>
OnDataConsolidated
(consolidated);
123
}
124
125
/// <summary>
126
/// Event invocator for the DataConsolidated event. This should be invoked
127
/// by derived classes when they have consolidated a new piece of data.
128
/// </summary>
129
/// <param name="consolidated">The newly consolidated data</param>
130
protected
virtual
void
OnDataConsolidated
(
IBaseData
consolidated)
131
{
132
var handler =
DataConsolidated
;
133
if
(handler !=
null
) handler(
this
, consolidated);
134
}
135
136
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
137
/// <filterpriority>2</filterpriority>
138
public
void
Dispose
()
139
{
140
First
.Dispose();
141
Second
.Dispose();
142
DataConsolidated
=
null
;
143
}
144
145
/// <summary>
146
/// Resets the consolidator
147
/// </summary>
148
public
void
Reset
()
149
{
150
First
.
Reset
();
151
Second
.
Reset
();
152
}
153
}
154
}
Common
Data
Consolidators
SequentialConsolidator.cs
Generated by
1.8.17