-
Notifications
You must be signed in to change notification settings - Fork 2
/
CompositeIndicator.cs
183 lines (165 loc) · 8.02 KB
/
CompositeIndicator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using QuantConnect.Data;
using QuantConnect.Indicators.BaseData;
namespace QuantConnect.Indicators
{
/// <summary>
/// This indicator is capable of wiring up two separate indicators into a single indicator
/// such that the output of each will be sent to a user specified function.
/// </summary>
/// <remarks>
/// This type is initialized such that there is no need to call the Update function. This indicator
/// will have its values automatically updated each time a new piece of data is received from both
/// the left and right indicators.
/// </remarks>
/// <typeparam name="T">The type of data input into this indicator</typeparam>
public class CompositeIndicator<T> : IndicatorBase<IndicatorDataPoint>
where T : IBaseData
{
/// <summary>
/// Delegate type used to compose the output of two indicators into a new value.
/// </summary>
/// <remarks>
/// A simple example would be to compute the difference between the two indicators (such as with MACD)
/// (left, right) => left - right
/// </remarks>
/// <param name="left">The left indicator</param>
/// <param name="right">The right indicator</param>
/// <returns>And indicator result representing the composition of the two indicators</returns>
public delegate IndicatorResult IndicatorComposer(IndicatorBase<T> left, IndicatorBase<T> right);
/// <summary>function used to compose the individual indicators</summary>
private readonly IndicatorComposer _composer;
/// <summary>
/// Gets the 'left' indicator for the delegate
/// </summary>
public IndicatorBase<T> Left { get; private set; }
/// <summary>
/// Gets the 'right' indicator for the delegate
/// </summary>
public IndicatorBase<T> Right { get; private set; }
/// <summary>
/// Gets a flag indicating when this indicator is ready and fully initialized
/// </summary>
public override bool IsReady
{
get { return Left.IsReady && Right.IsReady; }
}
/// <summary>
/// Resets this indicator to its initial state
/// </summary>
public override void Reset() {
Left.Reset();
Right.Reset();
base.Reset();
}
/// <summary>
/// Creates a new CompositeIndicator capable of taking the output from the left and right indicators
/// and producing a new value via the composer delegate specified
/// </summary>
/// <param name="name">The name of this indicator</param>
/// <param name="left">The left indicator for the 'composer'</param>
/// <param name="right">The right indidcator for the 'composoer'</param>
/// <param name="composer">Function used to compose the left and right indicators</param>
public CompositeIndicator(string name, IndicatorBase<T> left, IndicatorBase<T> right, IndicatorComposer composer)
: base(name)
{
_composer = composer;
Left = left;
Right = right;
ConfigureEventHandlers();
}
/// <summary>
/// Creates a new CompositeIndicator capable of taking the output from the left and right indicators
/// and producing a new value via the composer delegate specified
/// </summary>
/// <param name="left">The left indicator for the 'composer'</param>
/// <param name="right">The right indidcator for the 'composoer'</param>
/// <param name="composer">Function used to compose the left and right indicators</param>
public CompositeIndicator(IndicatorBase<T> left, IndicatorBase<T> right, IndicatorComposer composer)
: this(string.Format("COMPOSE({0},{1})", left.Name, right.Name), left, right, composer)
{ }
/// <summary>
/// Computes the next value of this indicator from the given state
/// and returns an instance of the <see cref="IndicatorResult"/> class
/// </summary>
/// <param name="input">The input given to the indicator</param>
/// <returns>An IndicatorResult object including the status of the indicator</returns>
protected override IndicatorResult ValidateAndComputeNextValue(IndicatorDataPoint input)
{
return _composer.Invoke(Left, Right);
}
/// <summary>
/// Computes the next value of this indicator from the given state
/// </summary>
/// <remarks>
/// Since this class overrides <see cref="ValidateAndComputeNextValue"/>, this method is a no-op
/// </remarks>
/// <param name="input">The input given to the indicator</param>
/// <returns>A new value for this indicator</returns>
protected override decimal ComputeNextValue(IndicatorDataPoint input)
{
// this should never actually be invoked
return _composer.Invoke(Left, Right).Value;
}
/// <summary>
/// Configures the event handlers for Left.Updated and Right.Updated to update this instance when
/// they both have new data.
/// </summary>
private void ConfigureEventHandlers()
{
// if either of these are constants then there's no reason
bool leftIsConstant = Left.GetType().IsSubclassOfGeneric(typeof (ConstantIndicator<>));
bool rightIsConstant = Right.GetType().IsSubclassOfGeneric(typeof (ConstantIndicator<>));
// wire up the Updated events such that when we get a new piece of data from both left and right
// we'll call update on this indicator. It's important to note that the CompositeIndicator only uses
// the timestamp that gets passed into the Update function, his compuation is soley a function
// of the left and right indicator via '_composer'
IndicatorDataPoint newLeftData = null;
IndicatorDataPoint newRightData = null;
Left.Updated += (sender, updated) =>
{
newLeftData = updated;
// if we have left and right data (or if right is a constant) then we need to update
if (newRightData != null || rightIsConstant)
{
var dataPoint = new IndicatorDataPoint { Time = MaxTime(updated) };
Update(dataPoint);
// reset these to null after each update
newLeftData = null;
newRightData = null;
}
};
Right.Updated += (sender, updated) =>
{
newRightData = updated;
// if we have left and right data (or if left is a constant) then we need to update
if (newLeftData != null || leftIsConstant)
{
var dataPoint = new IndicatorDataPoint { Time = MaxTime(updated) };
Update(dataPoint);
// reset these to null after each update
newLeftData = null;
newRightData = null;
}
};
}
private DateTime MaxTime(IndicatorDataPoint updated)
{
return new DateTime(Math.Max(updated.Time.Ticks, Math.Max(Right.Current.Time.Ticks, Left.Current.Time.Ticks)));
}
}
}