-
Notifications
You must be signed in to change notification settings - Fork 2
/
IndicatorExtensions.cs
338 lines (314 loc) · 16.6 KB
/
IndicatorExtensions.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* 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 QuantConnect.Data;
using System;
using System.Globalization;
namespace QuantConnect.Indicators
{
/// <summary>
/// Provides extension methods for Indicator
/// </summary>
public static class IndicatorExtensions
{
/// <summary>
/// Updates the state of this indicator with the given value and returns true
/// if this indicator is ready, false otherwise
/// </summary>
/// <param name="indicator">The indicator to be updated</param>
/// <param name="time">The time associated with the value</param>
/// <param name="value">The value to use to update this indicator</param>
/// <returns>True if this indicator is ready, false otherwise</returns>
public static bool Update(this IndicatorBase<IndicatorDataPoint> indicator, DateTime time, decimal value)
{
return indicator.Update(new IndicatorDataPoint(time, value));
}
/// <summary>
/// Configures the second indicator to receive automatic updates from the first by attaching an event handler
/// to first.DataConsolidated
/// </summary>
/// <param name="second">The indicator that receives data from the first</param>
/// <param name="first">The indicator that sends data via DataConsolidated even to the second</param>
/// <param name="waitForFirstToReady">True to only send updates to the second if first.IsReady returns true, false to alway send updates to second</param>
/// <returns>The reference to the second indicator to allow for method chaining</returns>
public static T Of<T>(this T second, IIndicator first, bool waitForFirstToReady = true)
where T : IIndicator
{
first.Updated += (sender, consolidated) =>
{
// only send the data along if we're ready
if (!waitForFirstToReady || first.IsReady)
{
second.Update(consolidated);
}
};
return second;
}
/// <summary>
/// Creates a new CompositeIndicator such that the result will be average of a first indicator weighted by a second one
/// </summary>
/// <param name="value">Indicator that will be averaged</param>
/// <param name="weight">Indicator that provides the average weights</param>
/// <param name="period">Average period</param>
/// <returns>Indicator that results of the average of first by weights given by second</returns>
public static CompositeIndicator<IndicatorDataPoint> WeightedBy<T, TWeight>(this IndicatorBase<T> value, TWeight weight, int period)
where T : IBaseData
where TWeight : IndicatorBase<IndicatorDataPoint>
{
var x = new WindowIdentity(period);
var y = new WindowIdentity(period);
var numerator = new Sum("Sum_xy", period);
var denominator = new Sum("Sum_y", period);
value.Updated += (sender, consolidated) =>
{
x.Update(consolidated);
if (x.Samples == y.Samples)
{
numerator.Update(consolidated.Time, consolidated.Value * y.Current.Value);
}
};
weight.Updated += (sender, consolidated) =>
{
y.Update(consolidated);
if (x.Samples == y.Samples)
{
numerator.Update(consolidated.Time, consolidated.Value * x.Current.Value);
}
denominator.Update(consolidated);
};
return numerator.Over(denominator);
}
/// <summary>
/// Creates a new CompositeIndicator such that the result will be the sum of the left and the constant
/// </summary>
/// <remarks>
/// value = left + constant
/// </remarks>
/// <param name="left">The left indicator</param>
/// <param name="constant">The addend</param>
/// <returns>The sum of the left and right indicators</returns>
public static CompositeIndicator<T> Plus<T>(this IndicatorBase<T> left, decimal constant)
where T : IBaseData
{
var constantIndicator = new ConstantIndicator<T>(constant.ToString(CultureInfo.InvariantCulture), constant);
return left.Plus(constantIndicator);
}
/// <summary>
/// Creates a new CompositeIndicator such that the result will be the sum of the left and right
/// </summary>
/// <remarks>
/// value = left + right
/// </remarks>
/// <param name="left">The left indicator</param>
/// <param name="right">The right indicator</param>
/// <returns>The sum of the left and right indicators</returns>
public static CompositeIndicator<T> Plus<T>(this IndicatorBase<T> left, IndicatorBase<T> right)
where T : IBaseData
{
return new CompositeIndicator<T>(left, right, (l, r) => l + r);
}
/// <summary>
/// Creates a new CompositeIndicator such that the result will be the sum of the left and right
/// </summary>
/// <remarks>
/// value = left + right
/// </remarks>
/// <param name="left">The left indicator</param>
/// <param name="right">The right indicator</param>
/// <param name="name">The name of this indicator</param>
/// <returns>The sum of the left and right indicators</returns>
public static CompositeIndicator<T> Plus<T>(this IndicatorBase<T> left, IndicatorBase<T> right, string name)
where T : IBaseData
{
return new CompositeIndicator<T>(name, left, right, (l, r) => l + r);
}
/// <summary>
/// Creates a new CompositeIndicator such that the result will be the difference of the left and constant
/// </summary>
/// <remarks>
/// value = left - constant
/// </remarks>
/// <param name="left">The left indicator</param>
/// <param name="constant">The subtrahend</param>
/// <returns>The difference of the left and right indicators</returns>
public static CompositeIndicator<T> Minus<T>(this IndicatorBase<T> left, decimal constant)
where T : IBaseData
{
var constantIndicator = new ConstantIndicator<T>(constant.ToString(CultureInfo.InvariantCulture), constant);
return left.Minus(constantIndicator);
}
/// <summary>
/// Creates a new CompositeIndicator such that the result will be the difference of the left and right
/// </summary>
/// <remarks>
/// value = left - right
/// </remarks>
/// <param name="left">The left indicator</param>
/// <param name="right">The right indicator</param>
/// <returns>The difference of the left and right indicators</returns>
public static CompositeIndicator<T> Minus<T>(this IndicatorBase<T> left, IndicatorBase<T> right)
where T : IBaseData
{
return new CompositeIndicator<T>(left, right, (l, r) => l - r);
}
/// <summary>
/// Creates a new CompositeIndicator such that the result will be the difference of the left and right
/// </summary>
/// <remarks>
/// value = left - right
/// </remarks>
/// <param name="left">The left indicator</param>
/// <param name="right">The right indicator</param>
/// <param name="name">The name of this indicator</param>
/// <returns>The difference of the left and right indicators</returns>
public static CompositeIndicator<T> Minus<T>(this IndicatorBase<T> left, IndicatorBase<T> right, string name)
where T : IBaseData
{
return new CompositeIndicator<T>(name, left, right, (l, r) => l - r);
}
/// <summary>
/// Creates a new CompositeIndicator such that the result will be the ratio of the left to the constant
/// </summary>
/// <remarks>
/// value = left/constant
/// </remarks>
/// <param name="left">The left indicator</param>
/// <param name="constant">The constant value denominator</param>
/// <returns>The ratio of the left to the right indicator</returns>
public static CompositeIndicator<T> Over<T>(this IndicatorBase<T> left, decimal constant)
where T : IBaseData
{
var constantIndicator = new ConstantIndicator<T>(constant.ToString(CultureInfo.InvariantCulture), constant);
return left.Over(constantIndicator);
}
/// <summary>
/// Creates a new CompositeIndicator such that the result will be the ratio of the left to the right
/// </summary>
/// <remarks>
/// value = left/right
/// </remarks>
/// <param name="left">The left indicator</param>
/// <param name="right">The right indicator</param>
/// <returns>The ratio of the left to the right indicator</returns>
public static CompositeIndicator<T> Over<T>(this IndicatorBase<T> left, IndicatorBase<T> right)
where T : IBaseData
{
return new CompositeIndicator<T>(left, right, (l, r) => r == 0m ? new IndicatorResult(0m, IndicatorStatus.MathError) : new IndicatorResult(l / r));
}
/// <summary>
/// Creates a new CompositeIndicator such that the result will be the ratio of the left to the right
/// </summary>
/// <remarks>
/// value = left/right
/// </remarks>
/// <param name="left">The left indicator</param>
/// <param name="right">The right indicator</param>
/// <param name="name">The name of this indicator</param>
/// <returns>The ratio of the left to the right indicator</returns>
public static CompositeIndicator<T> Over<T>(this IndicatorBase<T> left, IndicatorBase<T> right, string name)
where T : IBaseData
{
return new CompositeIndicator<T>(name, left, right, (l, r) => r == 0m ? new IndicatorResult(0m, IndicatorStatus.MathError) : new IndicatorResult(l / r));
}
/// <summary>
/// Creates a new CompositeIndicator such that the result will be the product of the left and the constant
/// </summary>
/// <remarks>
/// value = left*constant
/// </remarks>
/// <param name="left">The left indicator</param>
/// <param name="constant">The constant value to multiple by</param>
/// <returns>The product of the left to the right indicators</returns>
public static CompositeIndicator<T> Times<T>(this IndicatorBase<T> left, decimal constant)
where T : IBaseData
{
var constantIndicator = new ConstantIndicator<T>(constant.ToString(CultureInfo.InvariantCulture), constant);
return left.Times(constantIndicator);
}
/// <summary>
/// Creates a new CompositeIndicator such that the result will be the product of the left to the right
/// </summary>
/// <remarks>
/// value = left*right
/// </remarks>
/// <param name="left">The left indicator</param>
/// <param name="right">The right indicator</param>
/// <returns>The product of the left to the right indicators</returns>
public static CompositeIndicator<T> Times<T>(this IndicatorBase<T> left, IndicatorBase<T> right)
where T : IBaseData
{
return new CompositeIndicator<T>(left, right, (l, r) => l * r);
}
/// <summary>
/// Creates a new CompositeIndicator such that the result will be the product of the left to the right
/// </summary>
/// <remarks>
/// value = left*right
/// </remarks>
/// <param name="left">The left indicator</param>
/// <param name="right">The right indicator</param>
/// <param name="name">The name of this indicator</param>
/// <returns>The product of the left to the right indicators</returns>
public static CompositeIndicator<T> Times<T>(this IndicatorBase<T> left, IndicatorBase<T> right, string name)
where T : IBaseData
{
return new CompositeIndicator<T>(name, left, right, (l, r) => l * r);
}
/// <summary>Creates a new ExponentialMovingAverage indicator with the specified period and smoothingFactor from the left indicator
/// </summary>
/// <param name="left">The ExponentialMovingAverage indicator will be created using the data from left</param>
/// <param name="period">The period of the ExponentialMovingAverage indicators</param>
/// <param name="smoothingFactor">The percentage of data from the previous value to be carried into the next value</param>
/// <param name="waitForFirstToReady">True to only send updates to the second if left.IsReady returns true, false to alway send updates</param>
/// <returns>A reference to the ExponentialMovingAverage indicator to allow for method chaining</returns>
public static ExponentialMovingAverage EMA<T>(this IndicatorBase<T> left, int period, decimal? smoothingFactor = null, bool waitForFirstToReady = true)
where T : IBaseData
{
decimal k = smoothingFactor.HasValue ? k = smoothingFactor.Value : ExponentialMovingAverage.SmoothingFactorDefault(period);
return new ExponentialMovingAverage($"EMA{period}_Of_{left.Name}", period, k).Of(left, waitForFirstToReady);
}
/// <summary>Creates a new Maximum indicator with the specified period from the left indicator
/// </summary>
/// <param name="left">The Maximum indicator will be created using the data from left</param>
/// <param name="period">The period of the Maximum indicator</param>
/// <param name="waitForFirstToReady">True to only send updates to the second if left.IsReady returns true, false to alway send updates</param>
/// <returns>A reference to the Maximum indicator to allow for method chaining</returns>
public static Maximum MAX(this IIndicator left, int period, bool waitForFirstToReady = true)
{
return new Maximum($"MAX{period}_Of_{left.Name}", period).Of(left, waitForFirstToReady);
}
/// <summary>Creates a new Minimum indicator with the specified period from the left indicator
/// </summary>
/// <param name="left">The Minimum indicator will be created using the data from left</param>
/// <param name="period">The period of the Minimum indicator</param>
/// <param name="waitForFirstToReady">True to only send updates to the second if left.IsReady returns true, false to alway send updates</param>
/// <returns>A reference to the Minimum indicator to allow for method chaining</returns>
public static Minimum MIN<T>(this IndicatorBase<T> left, int period, bool waitForFirstToReady = true)
where T : IBaseData
{
return new Minimum($"MIN{period}_Of_{left.Name}", period).Of(left, waitForFirstToReady);
}
/// <summary>Initializes a new instance of the SimpleMovingAverage class with the specified name and period from the left indicator
/// </summary>
/// <param name="left">The SimpleMovingAverage indicator will be created using the data from left</param>
/// <param name="period">The period of the SMA</param>
/// <param name="waitForFirstToReady">True to only send updates to the second if first.IsReady returns true, false to alway send updates to second</param>
/// <returns>The reference to the SimpleMovingAverage indicator to allow for method chaining</returns>
public static SimpleMovingAverage SMA<T>(this IndicatorBase<T> left, int period, bool waitForFirstToReady = true)
where T : IBaseData
{
return new SimpleMovingAverage($"SMA{period}_Of_{left.Name}", period).Of(left, waitForFirstToReady);
}
}
}