Lean  $LEAN_TAG$
QCAlgorithm.Indicators.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 QuantConnect.Data;
20 using System;
21 using System.Collections.Generic;
22 using System.Linq;
23 using QuantConnect.Util;
24 using static QuantConnect.StringExtensions;
25 
26 namespace QuantConnect.Algorithm
27 {
28  public partial class QCAlgorithm
29  {
30  /// <summary>
31  /// Gets whether or not WarmUpIndicator is allowed to warm up indicators/>
32  /// </summary>
33  [DocumentationAttribute(Indicators)]
34  public bool EnableAutomaticIndicatorWarmUp { get; set; } = false;
35 
36  /// <summary>
37  /// Creates a new Acceleration Bands indicator.
38  /// </summary>
39  /// <param name="symbol">The symbol whose Acceleration Bands we want.</param>
40  /// <param name="period">The period of the three moving average (middle, upper and lower band).</param>
41  /// <param name="width">A coefficient specifying the distance between the middle band and upper or lower bands.</param>
42  /// <param name="movingAverageType">Type of the moving average.</param>
43  /// <param name="resolution">The resolution.</param>
44  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar.</param>
45  /// <returns></returns>
46  [DocumentationAttribute(Indicators)]
47  public AccelerationBands ABANDS(Symbol symbol, int period, decimal width = 4, MovingAverageType movingAverageType = MovingAverageType.Simple,
48  Resolution? resolution = null, Func<IBaseData, TradeBar> selector = null)
49  {
50  var name = CreateIndicatorName(symbol, $"ABANDS({period},{width})", resolution);
51  var accelerationBands = new AccelerationBands(name, period, width, movingAverageType);
52  InitializeIndicator(symbol, accelerationBands, resolution, selector);
53 
54  return accelerationBands;
55  }
56 
57  /// <summary>
58  /// Creates a new AccumulationDistribution indicator.
59  /// </summary>
60  /// <param name="symbol">The symbol whose AD we want</param>
61  /// <param name="resolution">The resolution</param>
62  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
63  /// <returns>The AccumulationDistribution indicator for the requested symbol over the specified period</returns>
64  [DocumentationAttribute(Indicators)]
65  public AccumulationDistribution AD(Symbol symbol, Resolution? resolution = null, Func<IBaseData, TradeBar> selector = null)
66  {
67  var name = CreateIndicatorName(symbol, "AD", resolution);
68  var accumulationDistribution = new AccumulationDistribution(name);
69  InitializeIndicator(symbol, accumulationDistribution, resolution, selector);
70 
71  return accumulationDistribution;
72  }
73 
74  /// <summary>
75  /// Creates a new AccumulationDistributionOscillator indicator.
76  /// </summary>
77  /// <param name="symbol">The symbol whose ADOSC we want</param>
78  /// <param name="fastPeriod">The fast moving average period</param>
79  /// <param name="slowPeriod">The slow moving average period</param>
80  /// <param name="resolution">The resolution</param>
81  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
82  /// <returns>The AccumulationDistributionOscillator indicator for the requested symbol over the specified period</returns>
83  [DocumentationAttribute(Indicators)]
84  public AccumulationDistributionOscillator ADOSC(Symbol symbol, int fastPeriod, int slowPeriod, Resolution? resolution = null, Func<IBaseData, TradeBar> selector = null)
85  {
86  var name = CreateIndicatorName(symbol, $"ADOSC({fastPeriod},{slowPeriod})", resolution);
87  var accumulationDistributionOscillator = new AccumulationDistributionOscillator(name, fastPeriod, slowPeriod);
88  InitializeIndicator(symbol, accumulationDistributionOscillator, resolution, selector);
89 
90  return accumulationDistributionOscillator;
91  }
92 
93  /// <summary>
94  /// Creates a Alpha indicator for the given target symbol in relation with the reference used.
95  /// The indicator will be automatically updated on the given resolution.
96  /// </summary>
97  /// <param name="target">The target symbol whose Alpha value we want</param>
98  /// <param name="reference">The reference symbol to compare with the target symbol</param>
99  /// <param name="alphaPeriod">The period of the Alpha indicator</param>
100  /// <param name="betaPeriod">The period of the Beta indicator</param>
101  /// <param name="resolution">The resolution</param>
102  /// <param name="riskFreeRate">The risk free rate</param>
103  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
104  /// <returns>The Alpha indicator for the given parameters</returns>
105  [DocumentationAttribute(Indicators)]
106  public Alpha A(Symbol target, Symbol reference, int alphaPeriod = 1, int betaPeriod = 252, Resolution? resolution = null, decimal? riskFreeRate = null, Func<IBaseData, IBaseDataBar> selector = null)
107  {
108  var baseBame = riskFreeRate.HasValue ? $"A({alphaPeriod},{betaPeriod},{riskFreeRate})" : $"A({alphaPeriod},{betaPeriod})";
109  var name = CreateIndicatorName(target, baseBame, resolution);
110 
111  // If risk free rate is not specified, use the default risk free rate model
112  IRiskFreeInterestRateModel riskFreeRateModel = riskFreeRate.HasValue
113  ? new ConstantRiskFreeRateInterestRateModel(riskFreeRate.Value)
115 
116  var alpha = new Alpha(name, target, reference, alphaPeriod, betaPeriod, riskFreeRateModel);
117  InitializeIndicator(target, alpha, resolution, selector);
118  InitializeIndicator(reference, alpha, resolution, selector);
119 
120  return alpha;
121  }
122 
123  /// <summary>
124  /// Creates a new ARIMA indicator.
125  /// </summary>
126  /// <param name="symbol">The symbol whose ARIMA indicator we want</param>
127  /// <param name="arOrder">AR order (p) -- defines the number of past values to consider in the AR component of the model.</param>
128  /// <param name="diffOrder">Difference order (d) -- defines how many times to difference the model before fitting parameters.</param>
129  /// <param name="maOrder">MA order (q) -- defines the number of past values to consider in the MA component of the model.</param>
130  /// <param name="period">Size of the rolling series to fit onto</param>
131  /// <param name="resolution">The resolution</param>
132  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
133  /// <returns>The ARIMA indicator for the requested symbol over the specified period</returns>
134  [DocumentationAttribute(Indicators)]
135  public AutoRegressiveIntegratedMovingAverage ARIMA(Symbol symbol, int arOrder, int diffOrder, int maOrder, int period,
136  Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
137  {
138  var name = CreateIndicatorName(symbol, $"ARIMA({arOrder},{diffOrder},{maOrder},{period})", resolution);
139  var arimaIndicator = new AutoRegressiveIntegratedMovingAverage(name, arOrder, diffOrder, maOrder, period);
140  InitializeIndicator(symbol, arimaIndicator, resolution, selector);
141 
142  return arimaIndicator;
143  }
144 
145  /// <summary>
146  /// Creates a new Average Directional Index indicator.
147  /// The indicator will be automatically updated on the given resolution.
148  /// </summary>
149  /// <param name="symbol">The symbol whose Average Directional Index we seek</param>
150  /// <param name="resolution">The resolution.</param>
151  /// <param name="period">The period over which to compute the Average Directional Index</param>
152  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
153  /// <returns>The Average Directional Index indicator for the requested symbol.</returns>
154  [DocumentationAttribute(Indicators)]
155  public AverageDirectionalIndex ADX(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, IBaseDataBar> selector = null)
156  {
157  var name = CreateIndicatorName(symbol, $"ADX({period})", resolution);
158  var averageDirectionalIndex = new AverageDirectionalIndex(name, period);
159  InitializeIndicator(symbol, averageDirectionalIndex, resolution, selector);
160 
161  return averageDirectionalIndex;
162  }
163 
164  /// <summary>
165  /// Creates a new Awesome Oscillator from the specified periods.
166  /// </summary>
167  /// <param name="symbol">The symbol whose Awesome Oscillator we seek</param>
168  /// <param name="resolution">The resolution.</param>
169  /// <param name="fastPeriod">The period of the fast moving average associated with the AO</param>
170  /// <param name="slowPeriod">The period of the slow moving average associated with the AO</param>
171  /// <param name="type">The type of moving average used when computing the fast and slow term. Defaults to simple moving average.</param>
172  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
173  [DocumentationAttribute(Indicators)]
174  public AwesomeOscillator AO(Symbol symbol, int slowPeriod, int fastPeriod, MovingAverageType type, Resolution? resolution = null, Func<IBaseData, IBaseDataBar> selector = null)
175  {
176  var name = CreateIndicatorName(symbol, $"AO({fastPeriod},{slowPeriod},{type})", resolution);
177  var awesomeOscillator = new AwesomeOscillator(name, fastPeriod, slowPeriod, type);
178  InitializeIndicator(symbol, awesomeOscillator, resolution, selector);
179 
180  return awesomeOscillator;
181  }
182 
183  /// <summary>
184  /// Creates a new AverageDirectionalMovementIndexRating indicator.
185  /// </summary>
186  /// <param name="symbol">The symbol whose ADXR we want</param>
187  /// <param name="period">The period over which to compute the ADXR</param>
188  /// <param name="resolution">The resolution.</param>
189  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
190  /// <returns>The AverageDirectionalMovementIndexRating indicator for the requested symbol over the specified period</returns>
191  [DocumentationAttribute(Indicators)]
192  public AverageDirectionalMovementIndexRating ADXR(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, IBaseDataBar> selector = null)
193  {
194  var name = CreateIndicatorName(symbol, $"ADXR({period})", resolution);
195  var averageDirectionalMovementIndexRating = new AverageDirectionalMovementIndexRating(name, period);
196  InitializeIndicator(symbol, averageDirectionalMovementIndexRating, resolution, selector);
197 
198  return averageDirectionalMovementIndexRating;
199  }
200 
201  /// <summary>
202  /// Creates a new ArnaudLegouxMovingAverage indicator.
203  /// </summary>
204  /// <param name="symbol">The symbol whose ALMA we want</param>
205  /// <param name="period">int - the number of periods to calculate the ALMA</param>
206  /// <param name="sigma"> int - this parameter is responsible for the shape of the curve coefficients.
207  /// </param>
208  /// <param name="offset">
209  /// decimal - This parameter allows regulating the smoothness and high sensitivity of the
210  /// Moving Average. The range for this parameter is [0, 1].
211  /// </param>
212  /// <param name="resolution">The resolution</param>
213  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
214  /// <returns>The ArnaudLegouxMovingAverage indicator for the requested symbol over the specified period</returns>
215  [DocumentationAttribute(Indicators)]
216  public ArnaudLegouxMovingAverage ALMA(Symbol symbol, int period, int sigma = 6, decimal offset = 0.85m, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
217  {
218  var name = CreateIndicatorName(symbol, $"ALMA({period},{sigma},{offset})", resolution);
219  var arnaudLegouxMovingAverage = new ArnaudLegouxMovingAverage(name, period, sigma, offset);
220  InitializeIndicator(symbol, arnaudLegouxMovingAverage, resolution, selector);
221 
222  return arnaudLegouxMovingAverage;
223  }
224 
225  /// <summary>
226  /// Creates a new AbsolutePriceOscillator indicator.
227  /// </summary>
228  /// <param name="symbol">The symbol whose APO we want</param>
229  /// <param name="fastPeriod">The fast moving average period</param>
230  /// <param name="slowPeriod">The slow moving average period</param>
231  /// <param name="movingAverageType">The type of moving average to use</param>
232  /// <param name="resolution">The resolution</param>
233  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
234  /// <returns>The AbsolutePriceOscillator indicator for the requested symbol over the specified period</returns>
235  [DocumentationAttribute(Indicators)]
236  public AbsolutePriceOscillator APO(Symbol symbol, int fastPeriod, int slowPeriod, MovingAverageType movingAverageType, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
237  {
238  var name = CreateIndicatorName(symbol, $"APO({fastPeriod},{slowPeriod})", resolution);
239  var absolutePriceOscillator = new AbsolutePriceOscillator(name, fastPeriod, slowPeriod, movingAverageType);
240  InitializeIndicator(symbol, absolutePriceOscillator, resolution, selector);
241 
242  return absolutePriceOscillator;
243  }
244 
245  /// <summary>
246  /// Creates a new AroonOscillator indicator which will compute the AroonUp and AroonDown (as well as the delta)
247  /// </summary>
248  /// <param name="symbol">The symbol whose Aroon we seek</param>
249  /// <param name="period">The look back period for computing number of periods since maximum and minimum</param>
250  /// <param name="resolution">The resolution</param>
251  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
252  /// <returns>An AroonOscillator configured with the specified periods</returns>
253  [DocumentationAttribute(Indicators)]
254  public AroonOscillator AROON(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, IBaseDataBar> selector = null)
255  {
256  return AROON(symbol, period, period, resolution, selector);
257  }
258 
259  /// <summary>
260  /// Creates a new AroonOscillator indicator which will compute the AroonUp and AroonDown (as well as the delta)
261  /// </summary>
262  /// <param name="symbol">The symbol whose Aroon we seek</param>
263  /// <param name="upPeriod">The look back period for computing number of periods since maximum</param>
264  /// <param name="downPeriod">The look back period for computing number of periods since minimum</param>
265  /// <param name="resolution">The resolution</param>
266  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
267  /// <returns>An AroonOscillator configured with the specified periods</returns>
268  [DocumentationAttribute(Indicators)]
269  public AroonOscillator AROON(Symbol symbol, int upPeriod, int downPeriod, Resolution? resolution = null, Func<IBaseData, IBaseDataBar> selector = null)
270  {
271  var name = CreateIndicatorName(symbol, $"AROON({upPeriod},{downPeriod})", resolution);
272  var aroonOscillator = new AroonOscillator(name, upPeriod, downPeriod);
273  InitializeIndicator(symbol, aroonOscillator, resolution, selector);
274 
275  return aroonOscillator;
276  }
277 
278  /// <summary>
279  /// Creates a new AverageTrueRange indicator for the symbol. The indicator will be automatically
280  /// updated on the given resolution.
281  /// </summary>
282  /// <param name="symbol">The symbol whose ATR we want</param>
283  /// <param name="period">The smoothing period used to smooth the computed TrueRange values</param>
284  /// <param name="type">The type of smoothing to use</param>
285  /// <param name="resolution">The resolution</param>
286  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
287  /// <returns>A new AverageTrueRange indicator with the specified smoothing type and period</returns>
288  [DocumentationAttribute(Indicators)]
289  public AverageTrueRange ATR(Symbol symbol, int period, MovingAverageType type = MovingAverageType.Simple, Resolution? resolution = null, Func<IBaseData, IBaseDataBar> selector = null)
290  {
291  var name = CreateIndicatorName(symbol, $"ATR({period})", resolution);
292  var averageTrueRange = new AverageTrueRange(name, period, type);
293  InitializeIndicator(symbol, averageTrueRange, resolution, selector);
294 
295  return averageTrueRange;
296  }
297 
298  /// <summary>
299  /// Creates an AugenPriceSpike indicator for the symbol. The indicator will be automatically
300  /// updated on the given resolution.
301  /// </summary>
302  /// <param name="symbol">The symbol whose APS we want</param>
303  /// <param name="period">The period of the APS</param>
304  /// <param name="resolution">The resolution</param>
305  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
306  /// <returns>The AugenPriceSpike indicator for the given parameters</returns>
307  [DocumentationAttribute(Indicators)]
308  public AugenPriceSpike APS(Symbol symbol, int period = 3, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
309  {
310  var name = CreateIndicatorName(symbol, $"APS({period})", resolution);
311  var augenPriceSpike = new AugenPriceSpike(name, period);
312  InitializeIndicator(symbol, augenPriceSpike, resolution, selector);
313 
314  return augenPriceSpike;
315  }
316 
317  /// <summary>
318  /// Creates a new BollingerBands indicator which will compute the MiddleBand, UpperBand, LowerBand, and StandardDeviation
319  /// </summary>
320  /// <param name="symbol">The symbol whose BollingerBands we seek</param>
321  /// <param name="period">The period of the standard deviation and moving average (middle band)</param>
322  /// <param name="k">The number of standard deviations specifying the distance between the middle band and upper or lower bands</param>
323  /// <param name="movingAverageType">The type of moving average to be used</param>
324  /// <param name="resolution">The resolution</param>
325  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
326  /// <returns>A BollingerBands configured with the specified period</returns>
327  [DocumentationAttribute(Indicators)]
328  public BollingerBands BB(Symbol symbol, int period, decimal k, MovingAverageType movingAverageType = MovingAverageType.Simple,
329  Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
330  {
331  var name = CreateIndicatorName(symbol, $"BB({period},{k})", resolution);
332  var bollingerBands = new BollingerBands(name, period, k, movingAverageType);
333  InitializeIndicator(symbol, bollingerBands, resolution, selector);
334 
335  return bollingerBands;
336  }
337 
338  /// <summary>
339  /// Creates a Beta indicator for the given target symbol in relation with the reference used.
340  /// The indicator will be automatically updated on the given resolution.
341  /// </summary>
342  /// <param name="target">The target symbol whose Beta value we want</param>
343  /// <param name="reference">The reference symbol to compare with the target symbol</param>
344  /// <param name="period">The period of the Beta indicator</param>
345  /// <param name="resolution">The resolution</param>
346  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
347  /// <returns>The Beta indicator for the given parameters</returns>
348  [DocumentationAttribute(Indicators)]
349  public Beta B(Symbol target, Symbol reference, int period, Resolution? resolution = null, Func<IBaseData, IBaseDataBar> selector = null)
350  {
351  var name = CreateIndicatorName(QuantConnect.Symbol.None, $"B({period})", resolution);
352  var beta = new Beta(name, target, reference, period);
353  InitializeIndicator(target, beta, resolution, selector);
354  InitializeIndicator(reference, beta, resolution, selector);
355 
356  return beta;
357  }
358 
359  /// <summary>
360  /// Creates a new Balance Of Power indicator.
361  /// The indicator will be automatically updated on the given resolution.
362  /// </summary>
363  /// <param name="symbol">The symbol whose Balance Of Power we seek</param>
364  /// <param name="resolution">The resolution.</param>
365  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
366  /// <returns>The Balance Of Power indicator for the requested symbol.</returns>
367  [DocumentationAttribute(Indicators)]
368  public BalanceOfPower BOP(Symbol symbol, Resolution? resolution = null, Func<IBaseData, IBaseDataBar> selector = null)
369  {
370  var name = CreateIndicatorName(symbol, "BOP", resolution);
371  var balanceOfPower = new BalanceOfPower(name);
372  InitializeIndicator(symbol, balanceOfPower, resolution, selector);
373 
374  return balanceOfPower;
375  }
376 
377  /// <summary>
378  /// Initializes a new instance of the <see cref="CoppockCurve"/> indicator
379  /// </summary>
380  /// <param name="symbol">The symbol whose Coppock Curve we want</param>
381  /// <param name="shortRocPeriod">The period for the short ROC</param>
382  /// <param name="longRocPeriod">The period for the long ROC</param>
383  /// <param name="lwmaPeriod">The period for the LWMA</param>
384  /// <param name="resolution">The resolution</param>
385  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
386  /// <returns>The Coppock Curve indicator for the requested symbol over the specified period</returns>
387  [DocumentationAttribute(Indicators)]
388  public CoppockCurve CC(Symbol symbol, int shortRocPeriod = 11, int longRocPeriod = 14, int lwmaPeriod = 10, Resolution? resolution = null,
389  Func<IBaseData, decimal> selector = null)
390  {
391  var name = CreateIndicatorName(symbol, $"CC({shortRocPeriod},{longRocPeriod},{lwmaPeriod})", resolution);
392  var coppockCurve = new CoppockCurve(name, shortRocPeriod, longRocPeriod, lwmaPeriod);
393  InitializeIndicator(symbol, coppockCurve, resolution, selector);
394 
395  return coppockCurve;
396  }
397 
398  /// <summary>
399  /// Creates a Correlation indicator for the given target symbol in relation with the reference used.
400  /// The indicator will be automatically updated on the given resolution.
401  /// </summary>
402  /// <param name="target">The target symbol of this indicator</param>
403  /// <param name="reference">The reference symbol of this indicator</param>
404  /// <param name="period">The period of this indicator</param>
405  /// <param name="correlationType">Correlation type</param>
406  /// <param name="resolution">The resolution</param>
407  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
408  /// <returns>The Correlation indicator for the given parameters</returns>
409  [DocumentationAttribute(Indicators)]
410  public Correlation C(Symbol target, Symbol reference, int period, CorrelationType correlationType = CorrelationType.Pearson, Resolution? resolution = null, Func<IBaseData, IBaseDataBar> selector = null)
411  {
412  var name = CreateIndicatorName(QuantConnect.Symbol.None, $"C({period})", resolution);
413  var correlation = new Correlation(name, target, reference, period);
414  InitializeIndicator(target, correlation, resolution, selector);
415  InitializeIndicator(reference, correlation, resolution, selector);
416 
417  return correlation;
418  }
419 
420  /// <summary>
421  /// Creates a new CommodityChannelIndex indicator. The indicator will be automatically
422  /// updated on the given resolution.
423  /// </summary>
424  /// <param name="symbol">The symbol whose CCI we want</param>
425  /// <param name="period">The period over which to compute the CCI</param>
426  /// <param name="movingAverageType">The type of moving average to use in computing the typical price average</param>
427  /// <param name="resolution">The resolution</param>
428  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
429  /// <returns>The CommodityChannelIndex indicator for the requested symbol over the specified period</returns>
430  [DocumentationAttribute(Indicators)]
431  public CommodityChannelIndex CCI(Symbol symbol, int period, MovingAverageType movingAverageType = MovingAverageType.Simple, Resolution? resolution = null, Func<IBaseData, IBaseDataBar> selector = null)
432  {
433  var name = CreateIndicatorName(symbol, $"CCI({period})", resolution);
434  var commodityChannelIndex = new CommodityChannelIndex(name, period, movingAverageType);
435  InitializeIndicator(symbol, commodityChannelIndex, resolution, selector);
436 
437  return commodityChannelIndex;
438  }
439 
440  /// <summary>
441  /// Creates a new ChaikinMoneyFlow indicator.
442  /// </summary>
443  /// <param name="symbol">The symbol whose CMF we want</param>
444  /// <param name="period">The period over which to compute the CMF</param>
445  /// <param name="resolution">The resolution</param>
446  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
447  /// <returns>The ChaikinMoneyFlow indicator for the requested symbol over the specified period</returns>
448  [DocumentationAttribute(Indicators)]
449  public ChaikinMoneyFlow CMF(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, TradeBar> selector = null)
450  {
451  var name = CreateIndicatorName(symbol, $"CMF({period})", resolution);
452  var chaikinMoneyFlow = new ChaikinMoneyFlow(name, period);
453  InitializeIndicator(symbol, chaikinMoneyFlow, resolution, selector);
454 
455  return chaikinMoneyFlow;
456 
457  }
458 
459  /// <summary>
460  /// Creates a new ChandeMomentumOscillator indicator.
461  /// </summary>
462  /// <param name="symbol">The symbol whose CMO we want</param>
463  /// <param name="period">The period over which to compute the CMO</param>
464  /// <param name="resolution">The resolution</param>
465  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
466  /// <returns>The ChandeMomentumOscillator indicator for the requested symbol over the specified period</returns>
467  [DocumentationAttribute(Indicators)]
468  public ChandeMomentumOscillator CMO(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
469  {
470  var name = CreateIndicatorName(symbol, $"CMO({period})", resolution);
471  var chandeMomentumOscillator = new ChandeMomentumOscillator(name, period);
472  InitializeIndicator(symbol, chandeMomentumOscillator, resolution, selector);
473 
474  return chandeMomentumOscillator;
475  }
476 
477  ///<summary>
478  /// Creates a new DeMarker Indicator (DEM), an oscillator-type indicator measuring changes in terms of an asset's
479  /// High and Low tradebar values.
480  ///</summary>
481  /// <param name="symbol">The symbol whose DEM we seek.</param>
482  /// <param name="period">The period of the moving average implemented</param>
483  /// <param name="type">Specifies the type of moving average to be used</param>
484  /// <param name="resolution">The resolution.</param>
485  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
486  /// <returns>The DeMarker indicator for the requested symbol.</returns>
487  [DocumentationAttribute(Indicators)]
488  public DeMarkerIndicator DEM(Symbol symbol, int period, MovingAverageType type, Resolution? resolution = null, Func<IBaseData, TradeBar> selector = null)
489  {
490  var name = CreateIndicatorName(symbol, $"DEM({period},{type})", resolution);
491  var deMarkerIndicator = new DeMarkerIndicator(name, period, type);
492  InitializeIndicator(symbol, deMarkerIndicator, resolution, selector);
493  return deMarkerIndicator;
494  }
495 
496  /// <summary>
497  /// Creates a new Donchian Channel indicator which will compute the Upper Band and Lower Band.
498  /// The indicator will be automatically updated on the given resolution.
499  /// </summary>
500  /// <param name="symbol">The symbol whose Donchian Channel we seek.</param>
501  /// <param name="upperPeriod">The period over which to compute the upper Donchian Channel.</param>
502  /// <param name="lowerPeriod">The period over which to compute the lower Donchian Channel.</param>
503  /// <param name="resolution">The resolution.</param>
504  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
505  /// <returns>The Donchian Channel indicator for the requested symbol.</returns>
506  [DocumentationAttribute(Indicators)]
507  public DonchianChannel DCH(Symbol symbol, int upperPeriod, int lowerPeriod, Resolution? resolution = null, Func<IBaseData, IBaseDataBar> selector = null)
508  {
509  var name = CreateIndicatorName(symbol, $"DCH({upperPeriod},{lowerPeriod})", resolution);
510  var donchianChannel = new DonchianChannel(name, upperPeriod, lowerPeriod);
511  InitializeIndicator(symbol, donchianChannel, resolution, selector);
512 
513  return donchianChannel;
514  }
515 
516  /// <summary>
517  /// Overload shorthand to create a new symmetric Donchian Channel indicator which
518  /// has the upper and lower channels set to the same period length.
519  /// </summary>
520  /// <param name="symbol">The symbol whose Donchian Channel we seek.</param>
521  /// <param name="period">The period over which to compute the Donchian Channel.</param>
522  /// <param name="resolution">The resolution.</param>
523  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
524  /// <returns>The Donchian Channel indicator for the requested symbol.</returns>
525  [DocumentationAttribute(Indicators)]
526  public DonchianChannel DCH(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, IBaseDataBar> selector = null)
527  {
528  return DCH(symbol, period, period, resolution, selector);
529  }
530 
531  /// <summary>
532  /// Creates a new Delta indicator for the symbol The indicator will be automatically
533  /// updated on the symbol's subscription resolution
534  /// </summary>
535  /// <param name="symbol">The option symbol whose values we want as an indicator</param>
536  /// <param name="mirrorOption">The mirror option for parity calculation</param>
537  /// <param name="riskFreeRate">The risk free rate</param>
538  /// <param name="dividendYield">The dividend yield</param>
539  /// <param name="optionModel">The option pricing model used to estimate Delta</param>
540  /// <param name="ivModel">The option pricing model used to estimate IV</param>
541  /// <param name="resolution">The desired resolution of the data</param>
542  /// <returns>A new Delta indicator for the specified symbol</returns>
543  [DocumentationAttribute(Indicators)]
544  public Delta D(Symbol symbol, Symbol mirrorOption = null, decimal? riskFreeRate = null, decimal? dividendYield = null, OptionPricingModelType optionModel = OptionPricingModelType.BlackScholes,
545  OptionPricingModelType? ivModel = null, Resolution? resolution = null)
546  {
547  var name = InitializeOptionIndicator<Delta>(symbol, out var riskFreeRateModel, out var dividendYieldModel, riskFreeRate, dividendYield, optionModel, resolution);
548 
549  var delta = new Delta(name, symbol, riskFreeRateModel, dividendYieldModel, mirrorOption, optionModel, ivModel);
550  RegisterIndicator(symbol, delta, ResolveConsolidator(symbol, resolution, typeof(QuoteBar)));
551  RegisterIndicator(symbol.Underlying, delta, ResolveConsolidator(symbol.Underlying, resolution));
552  if (mirrorOption != null)
553  {
554  RegisterIndicator(mirrorOption, delta, ResolveConsolidator(mirrorOption, resolution, typeof(QuoteBar)));
555  }
556  return delta;
557  }
558 
559  /// <summary>
560  /// Creates a new Delta indicator for the symbol The indicator will be automatically
561  /// updated on the symbol's subscription resolution
562  /// </summary>
563  /// <param name="symbol">The option symbol whose values we want as an indicator</param>
564  /// <param name="mirrorOption">The mirror option for parity calculation</param>
565  /// <param name="riskFreeRate">The risk free rate</param>
566  /// <param name="dividendYield">The dividend yield</param>
567  /// <param name="optionModel">The option pricing model used to estimate Delta</param>
568  /// <param name="ivModel">The option pricing model used to estimate IV</param>
569  /// <param name="resolution">The desired resolution of the data</param>
570  /// <returns>A new Delta indicator for the specified symbol</returns>
571  [DocumentationAttribute(Indicators)]
572  public Delta Δ(Symbol symbol, Symbol mirrorOption = null, decimal? riskFreeRate = null, decimal? dividendYield = null, OptionPricingModelType optionModel = OptionPricingModelType.BlackScholes,
573  OptionPricingModelType? ivModel = null, Resolution? resolution = null)
574  {
575  return D(symbol, mirrorOption, riskFreeRate, dividendYield, optionModel, ivModel, resolution);
576  }
577 
578  /// <summary>
579  /// Creates a new DoubleExponentialMovingAverage indicator.
580  /// </summary>
581  /// <param name="symbol">The symbol whose DEMA we want</param>
582  /// <param name="period">The period over which to compute the DEMA</param>
583  /// <param name="resolution">The resolution</param>
584  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
585  /// <returns>The DoubleExponentialMovingAverage indicator for the requested symbol over the specified period</returns>
586  [DocumentationAttribute(Indicators)]
587  public DoubleExponentialMovingAverage DEMA(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
588  {
589  var name = CreateIndicatorName(symbol, $"DEMA({period})", resolution);
590  var doubleExponentialMovingAverage = new DoubleExponentialMovingAverage(name, period);
591  InitializeIndicator(symbol, doubleExponentialMovingAverage, resolution, selector);
592 
593  return doubleExponentialMovingAverage;
594  }
595 
596  /// <summary>
597  /// Creates a new <see cref="DetrendedPriceOscillator"/> indicator.
598  /// </summary>
599  /// <param name="symbol">The symbol whose DPO we want</param>
600  /// <param name="period">The period over which to compute the DPO</param>
601  /// <param name="resolution">The resolution</param>
602  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
603  /// <returns>A new registered DetrendedPriceOscillator indicator for the requested symbol over the specified period</returns>
604  [DocumentationAttribute(Indicators)]
605  public DetrendedPriceOscillator DPO(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
606  {
607  var name = CreateIndicatorName(symbol, $"DPO({period})", resolution);
608  var detrendedPriceOscillator = new DetrendedPriceOscillator(name, period);
609  InitializeIndicator(symbol, detrendedPriceOscillator, resolution, selector);
610 
611  return detrendedPriceOscillator;
612  }
613 
614  /// <summary>
615  /// Creates an ExponentialMovingAverage indicator for the symbol. The indicator will be automatically
616  /// updated on the given resolution.
617  /// </summary>
618  /// <param name="symbol">The symbol whose EMA we want</param>
619  /// <param name="period">The period of the EMA</param>
620  /// <param name="resolution">The resolution</param>
621  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
622  /// <returns>The ExponentialMovingAverage for the given parameters</returns>
623  [DocumentationAttribute(Indicators)]
624  public ExponentialMovingAverage EMA(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
625  {
626  return EMA(symbol, period, ExponentialMovingAverage.SmoothingFactorDefault(period), resolution, selector);
627  }
628 
629  /// <summary>
630  /// Creates an ExponentialMovingAverage indicator for the symbol. The indicator will be automatically
631  /// updated on the given resolution.
632  /// </summary>
633  /// <param name="symbol">The symbol whose EMA we want</param>
634  /// <param name="period">The period of the EMA</param>
635  /// <param name="smoothingFactor">The percentage of data from the previous value to be carried into the next value</param>
636  /// <param name="resolution">The resolution</param>
637  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
638  /// <returns>The ExponentialMovingAverage for the given parameters</returns>
639  [DocumentationAttribute(Indicators)]
640  public ExponentialMovingAverage EMA(Symbol symbol, int period, decimal smoothingFactor, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
641  {
642  var name = CreateIndicatorName(symbol, $"EMA({period})", resolution);
643  var exponentialMovingAverage = new ExponentialMovingAverage(name, period, smoothingFactor);
644  InitializeIndicator(symbol, exponentialMovingAverage, resolution, selector);
645 
646  return exponentialMovingAverage;
647  }
648 
649  /// <summary>
650  /// Creates an EaseOfMovementValue indicator for the symbol. The indicator will be automatically
651  /// updated on the given resolution.
652  /// </summary>
653  /// <param name="symbol">The symbol whose EMV we want</param>
654  /// <param name="period">The period of the EMV</param>
655  /// <param name="scale">The length of the outputed value</param>
656  /// <param name="resolution">The resolution</param>
657  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
658  /// <returns>The EaseOfMovementValue indicator for the given parameters</returns>
659  [DocumentationAttribute(Indicators)]
660  public EaseOfMovementValue EMV(Symbol symbol, int period = 1, int scale = 10000, Resolution? resolution = null, Func<IBaseData, TradeBar> selector = null)
661  {
662  var name = CreateIndicatorName(symbol, $"EMV({period}, {scale})", resolution);
663  var easeOfMovementValue = new EaseOfMovementValue(name, period, scale);
664  InitializeIndicator(symbol, easeOfMovementValue, resolution, selector);
665 
666  return easeOfMovementValue;
667  }
668 
669  /// <summary>
670  /// Creates a new FilteredIdentity indicator for the symbol The indicator will be automatically
671  /// updated on the symbol's subscription resolution
672  /// </summary>
673  /// <param name="symbol">The symbol whose values we want as an indicator</param>
674  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
675  /// <param name="filter">Filters the IBaseData send into the indicator, if null defaults to true (x => true) which means no filter</param>
676  /// <param name="fieldName">The name of the field being selected</param>
677  /// <returns>A new FilteredIdentity indicator for the specified symbol and selector</returns>
678  [DocumentationAttribute(Indicators)]
679  public FilteredIdentity FilteredIdentity(Symbol symbol, Func<IBaseData, IBaseDataBar> selector = null, Func<IBaseData, bool> filter = null, string fieldName = null)
680  {
681  var resolution = GetSubscription(symbol).Resolution;
682  return FilteredIdentity(symbol, resolution, selector, filter, fieldName);
683  }
684 
685  /// <summary>
686  /// Creates a new FilteredIdentity indicator for the symbol The indicator will be automatically
687  /// updated on the symbol's subscription resolution
688  /// </summary>
689  /// <param name="symbol">The symbol whose values we want as an indicator</param>
690  /// <param name="resolution">The desired resolution of the data</param>
691  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
692  /// <param name="filter">Filters the IBaseData send into the indicator, if null defaults to true (x => true) which means no filter</param>
693  /// <param name="fieldName">The name of the field being selected</param>
694  /// <returns>A new FilteredIdentity indicator for the specified symbol and selector</returns>
695  [DocumentationAttribute(Indicators)]
696  public FilteredIdentity FilteredIdentity(Symbol symbol, Resolution resolution, Func<IBaseData, IBaseDataBar> selector = null, Func<IBaseData, bool> filter = null, string fieldName = null)
697  {
698  var name = CreateIndicatorName(symbol, fieldName ?? "close", resolution);
699  var filteredIdentity = new FilteredIdentity(name, filter);
700  RegisterIndicator<IBaseData>(symbol, filteredIdentity, resolution, selector);
701  return filteredIdentity;
702  }
703 
704  /// <summary>
705  /// Creates a new FilteredIdentity indicator for the symbol The indicator will be automatically
706  /// updated on the symbol's subscription resolution
707  /// </summary>
708  /// <param name="symbol">The symbol whose values we want as an indicator</param>
709  /// <param name="resolution">The desired resolution of the data</param>
710  /// <param name="selector">Selects a value from the BaseData, if null defaults to the .Value property (x => x.Value)</param>
711  /// <param name="filter">Filters the IBaseData send into the indicator, if null defaults to true (x => true) which means no filter</param>
712  /// <param name="fieldName">The name of the field being selected</param>
713  /// <returns>A new FilteredIdentity indicator for the specified symbol and selector</returns>
714  [DocumentationAttribute(Indicators)]
715  public FilteredIdentity FilteredIdentity(Symbol symbol, TimeSpan resolution, Func<IBaseData, IBaseDataBar> selector = null, Func<IBaseData, bool> filter = null, string fieldName = null)
716  {
717  var name = Invariant($"{symbol}({fieldName ?? "close"}_{resolution})");
718  var filteredIdentity = new FilteredIdentity(name, filter);
719  RegisterIndicator<IBaseData>(symbol, filteredIdentity, ResolveConsolidator(symbol, resolution), selector);
720  return filteredIdentity;
721  }
722 
723  /// <summary>
724  /// Creates an FisherTransform indicator for the symbol.
725  /// The indicator will be automatically updated on the given resolution.
726  /// </summary>
727  /// <param name="symbol">The symbol whose FisherTransform we want</param>
728  /// <param name="period">The period of the FisherTransform</param>
729  /// <param name="resolution">The resolution</param>
730  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
731  /// <returns>The FisherTransform for the given parameters</returns>
732  [DocumentationAttribute(Indicators)]
733  public FisherTransform FISH(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, TradeBar> selector = null)
734  {
735  var name = CreateIndicatorName(symbol, $"FISH({period})", resolution);
736  var fisherTransform = new FisherTransform(name, period);
737  InitializeIndicator(symbol, fisherTransform, resolution, selector);
738 
739  return fisherTransform;
740  }
741 
742  /// <summary>
743  /// Creates an FractalAdaptiveMovingAverage (FRAMA) indicator for the symbol. The indicator will be automatically
744  /// updated on the given resolution.
745  /// </summary>
746  /// <param name="symbol">The symbol whose FRAMA we want</param>
747  /// <param name="period">The period of the FRAMA</param>
748  /// <param name="longPeriod">The long period of the FRAMA</param>
749  /// <param name="resolution">The resolution</param>
750  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
751  /// <returns>The FRAMA for the given parameters</returns>
752  [DocumentationAttribute(Indicators)]
753  public FractalAdaptiveMovingAverage FRAMA(Symbol symbol, int period, int longPeriod = 198, Resolution? resolution = null, Func<IBaseData, IBaseDataBar> selector = null)
754  {
755  var name = CreateIndicatorName(symbol, $"FRAMA({period},{longPeriod})", resolution);
756  var fractalAdaptiveMovingAverage = new FractalAdaptiveMovingAverage(name, period, longPeriod);
757  InitializeIndicator(symbol, fractalAdaptiveMovingAverage, resolution, selector);
758 
759  return fractalAdaptiveMovingAverage;
760  }
761 
762  /// <summary>
763  /// Creates a new Gamma indicator for the symbol The indicator will be automatically
764  /// updated on the symbol's subscription resolution
765  /// </summary>
766  /// <param name="symbol">The option symbol whose values we want as an indicator</param>
767  /// <param name="mirrorOption">The mirror option for parity calculation</param>
768  /// <param name="riskFreeRate">The risk free rate</param>
769  /// <param name="dividendYield">The dividend yield</param>
770  /// <param name="optionModel">The option pricing model used to estimate Gamma</param>
771  /// <param name="ivModel">The option pricing model used to estimate IV</param>
772  /// <param name="resolution">The desired resolution of the data</param>
773  /// <returns>A new Gamma indicator for the specified symbol</returns>
774  [DocumentationAttribute(Indicators)]
775  public Gamma G(Symbol symbol, Symbol mirrorOption = null, decimal? riskFreeRate = null, decimal? dividendYield = null, OptionPricingModelType optionModel = OptionPricingModelType.BlackScholes,
776  OptionPricingModelType? ivModel = null, Resolution? resolution = null)
777  {
778  var name = InitializeOptionIndicator<Gamma>(symbol, out var riskFreeRateModel, out var dividendYieldModel, riskFreeRate, dividendYield, optionModel, resolution);
779 
780  var gamma = new Gamma(name, symbol, riskFreeRateModel, dividendYieldModel, mirrorOption, optionModel, ivModel);
781  RegisterIndicator(symbol, gamma, ResolveConsolidator(symbol, resolution, typeof(QuoteBar)));
782  RegisterIndicator(symbol.Underlying, gamma, ResolveConsolidator(symbol.Underlying, resolution));
783  if (mirrorOption != null)
784  {
785  RegisterIndicator(mirrorOption, gamma, ResolveConsolidator(mirrorOption, resolution, typeof(QuoteBar)));
786  }
787  return gamma;
788  }
789 
790  /// <summary>
791  /// Creates a new Gamma indicator for the symbol The indicator will be automatically
792  /// updated on the symbol's subscription resolution
793  /// </summary>
794  /// <param name="symbol">The option symbol whose values we want as an indicator</param>
795  /// <param name="mirrorOption">The mirror option for parity calculation</param>
796  /// <param name="riskFreeRate">The risk free rate</param>
797  /// <param name="dividendYield">The dividend yield</param>
798  /// <param name="optionModel">The option pricing model used to estimate Gamma</param>
799  /// <param name="ivModel">The option pricing model used to estimate IV</param>
800  /// <param name="resolution">The desired resolution of the data</param>
801  /// <returns>A new Gamma indicator for the specified symbol</returns>
802  [DocumentationAttribute(Indicators)]
803  public Gamma Γ(Symbol symbol, Symbol mirrorOption = null, decimal? riskFreeRate = null, decimal? dividendYield = null, OptionPricingModelType optionModel = OptionPricingModelType.BlackScholes,
804  OptionPricingModelType? ivModel = null, Resolution? resolution = null)
805  {
806  return G(symbol, mirrorOption, riskFreeRate, dividendYield, optionModel, ivModel, resolution);
807  }
808 
809  /// <summary>
810  /// Creates a new Heikin-Ashi indicator.
811  /// </summary>
812  /// <param name="symbol">The symbol whose Heikin-Ashi we want</param>
813  /// <param name="resolution">The resolution</param>
814  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
815  /// <returns>The Heikin-Ashi indicator for the requested symbol over the specified period</returns>
816  [DocumentationAttribute(Indicators)]
817  public HeikinAshi HeikinAshi(Symbol symbol, Resolution? resolution = null, Func<IBaseData, TradeBar> selector = null)
818  {
819  var name = CreateIndicatorName(symbol, "HA", resolution);
820  var heikinAshi = new HeikinAshi(name);
821  InitializeIndicator(symbol, heikinAshi, resolution, selector);
822 
823  return heikinAshi;
824  }
825 
826  /// <summary>
827  /// Creates a new Hilbert Transform indicator
828  /// </summary>
829  /// <param name="symbol">The symbol whose Hilbert transform we want</param>
830  /// <param name="length">The length of the FIR filter used in the calculation of the Hilbert Transform.
831  /// This parameter determines the number of filter coefficients in the FIR filter.</param>
832  /// <param name="inPhaseMultiplicationFactor">The multiplication factor used in the calculation of the in-phase component
833  /// of the Hilbert Transform. This parameter adjusts the sensitivity and responsiveness of
834  /// the transform to changes in the input signal.</param>
835  /// <param name="quadratureMultiplicationFactor">The multiplication factor used in the calculation of the quadrature component of
836  /// the Hilbert Transform. This parameter also adjusts the sensitivity and responsiveness of the
837  /// transform to changes in the input signal.</param>
838  /// <param name="resolution">The resolution</param>
839  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
840  [DocumentationAttribute(Indicators)]
841  public HilbertTransform HT(Symbol symbol, int length, decimal inPhaseMultiplicationFactor, decimal quadratureMultiplicationFactor, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
842  {
843  var name = CreateIndicatorName(symbol, $"HT({length}, {inPhaseMultiplicationFactor}, {quadratureMultiplicationFactor})", resolution);
844  var hilbertTransform = new HilbertTransform(length, inPhaseMultiplicationFactor, quadratureMultiplicationFactor);
845  InitializeIndicator(symbol, hilbertTransform, resolution, selector);
846 
847  return hilbertTransform;
848  }
849 
850  /// <summary>
851  /// Creates a new HullMovingAverage indicator. The Hull moving average is a series of nested weighted moving averages, is fast and smooth.
852  /// </summary>
853  /// <param name="symbol">The symbol whose Hull moving average we want</param>
854  /// <param name="period">The period over which to compute the Hull moving average</param>
855  /// <param name="resolution">The resolution</param>
856  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
857  /// <returns></returns>
858  [DocumentationAttribute(Indicators)]
859  public HullMovingAverage HMA(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
860  {
861  var name = CreateIndicatorName(symbol, $"HMA({period})", resolution);
862  var hullMovingAverage = new HullMovingAverage(name, period);
863  InitializeIndicator(symbol, hullMovingAverage, resolution, selector);
864 
865  return hullMovingAverage;
866  }
867 
868  /// <summary>
869  /// Creates a new IchimokuKinkoHyo indicator for the symbol. The indicator will be automatically
870  /// updated on the given resolution.
871  /// </summary>
872  /// <param name="symbol">The symbol whose ICHIMOKU we want</param>
873  /// <param name="tenkanPeriod">The period to calculate the Tenkan-sen period</param>
874  /// <param name="kijunPeriod">The period to calculate the Kijun-sen period</param>
875  /// <param name="senkouAPeriod">The period to calculate the Tenkan-sen period</param>
876  /// <param name="senkouBPeriod">The period to calculate the Tenkan-sen period</param>
877  /// <param name="senkouADelayPeriod">The period to calculate the Tenkan-sen period</param>
878  /// <param name="senkouBDelayPeriod">The period to calculate the Tenkan-sen period</param>
879  /// <param name="resolution">The resolution</param>
880  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
881  /// <returns>A new IchimokuKinkoHyo indicator with the specified periods and delays</returns>
882  [DocumentationAttribute(Indicators)]
883  public IchimokuKinkoHyo ICHIMOKU(Symbol symbol, int tenkanPeriod, int kijunPeriod, int senkouAPeriod, int senkouBPeriod,
884  int senkouADelayPeriod, int senkouBDelayPeriod, Resolution? resolution = null, Func<IBaseData, TradeBar> selector = null)
885  {
886  var name = CreateIndicatorName(symbol, $"ICHIMOKU({tenkanPeriod},{kijunPeriod},{senkouAPeriod},{senkouBPeriod},{senkouADelayPeriod},{senkouBDelayPeriod})", resolution);
887  var ichimokuKinkoHyo = new IchimokuKinkoHyo(name, tenkanPeriod, kijunPeriod, senkouAPeriod, senkouBPeriod, senkouADelayPeriod, senkouBDelayPeriod);
888  InitializeIndicator(symbol, ichimokuKinkoHyo, resolution, selector);
889 
890  return ichimokuKinkoHyo;
891  }
892 
893  /// <summary>
894  /// Creates a new Identity indicator for the symbol The indicator will be automatically
895  /// updated on the symbol's subscription resolution
896  /// </summary>
897  /// <param name="symbol">The symbol whose values we want as an indicator</param>
898  /// <param name="selector">Selects a value from the BaseData, if null defaults to the .Value property (x => x.Value)</param>
899  /// <param name="fieldName">The name of the field being selected</param>
900  /// <returns>A new Identity indicator for the specified symbol and selector</returns>
901  [DocumentationAttribute(Indicators)]
902  public Identity Identity(Symbol symbol, Func<IBaseData, decimal> selector = null, string fieldName = null)
903  {
904  var resolution = GetSubscription(symbol).Resolution;
905  return Identity(symbol, resolution, selector, fieldName);
906  }
907 
908  /// <summary>
909  /// Creates a new Identity indicator for the symbol The indicator will be automatically
910  /// updated on the symbol's subscription resolution
911  /// </summary>
912  /// <param name="symbol">The symbol whose values we want as an indicator</param>
913  /// <param name="resolution">The desired resolution of the data</param>
914  /// <param name="selector">Selects a value from the BaseData, if null defaults to the .Value property (x => x.Value)</param>
915  /// <param name="fieldName">The name of the field being selected</param>
916  /// <returns>A new Identity indicator for the specified symbol and selector</returns>
917  [DocumentationAttribute(Indicators)]
918  public Identity Identity(Symbol symbol, Resolution resolution, Func<IBaseData, decimal> selector = null, string fieldName = null)
919  {
920  var name = CreateIndicatorName(symbol, fieldName ?? "close", resolution);
921  var identity = new Identity(name);
922  RegisterIndicator(symbol, identity, resolution, selector);
923  return identity;
924  }
925 
926  /// <summary>
927  /// Creates a new Identity indicator for the symbol The indicator will be automatically
928  /// updated on the symbol's subscription resolution
929  /// </summary>
930  /// <param name="symbol">The symbol whose values we want as an indicator</param>
931  /// <param name="resolution">The desired resolution of the data</param>
932  /// <param name="selector">Selects a value from the BaseData, if null defaults to the .Value property (x => x.Value)</param>
933  /// <param name="fieldName">The name of the field being selected</param>
934  /// <returns>A new Identity indicator for the specified symbol and selector</returns>
935  [DocumentationAttribute(Indicators)]
936  public Identity Identity(Symbol symbol, TimeSpan resolution, Func<IBaseData, decimal> selector = null, string fieldName = null)
937  {
938  var name = Invariant($"{symbol}({fieldName ?? "close"},{resolution})");
939  var identity = new Identity(name);
940  RegisterIndicator(symbol, identity, ResolveConsolidator(symbol, resolution), selector);
941  return identity;
942  }
943 
944  /// <summary>
945  /// Creates a new ImpliedVolatility indicator for the symbol The indicator will be automatically
946  /// updated on the symbol's subscription resolution
947  /// </summary>
948  /// <param name="symbol">The option symbol whose values we want as an indicator</param>
949  /// <param name="mirrorOption">The mirror option contract used for parity type calculation</param>
950  /// <param name="riskFreeRate">The risk free rate</param>
951  /// <param name="dividendYield">The dividend yield</param>
952  /// <param name="optionModel">The option pricing model used to estimate IV</param>
953  /// <param name="period">The lookback period of historical volatility</param>
954  /// <param name="resolution">The desired resolution of the data</param>
955  /// <returns>A new ImpliedVolatility indicator for the specified symbol</returns>
956  [DocumentationAttribute(Indicators)]
957  public ImpliedVolatility IV(Symbol symbol, Symbol mirrorOption = null, decimal? riskFreeRate = null, decimal? dividendYield = null,
958  OptionPricingModelType optionModel = OptionPricingModelType.BlackScholes, int period = 252, Resolution? resolution = null)
959  {
960  var name = InitializeOptionIndicator<ImpliedVolatility>(symbol, out var riskFreeRateModel, out var dividendYieldModel, riskFreeRate, dividendYield, optionModel, resolution);
961 
962  var iv = new ImpliedVolatility(name, symbol, riskFreeRateModel, dividendYieldModel, mirrorOption, optionModel);
963  RegisterIndicator(symbol, iv, ResolveConsolidator(symbol, resolution, typeof(QuoteBar)));
964  RegisterIndicator(symbol.Underlying, iv, ResolveConsolidator(symbol.Underlying, resolution));
965  if (mirrorOption != null)
966  {
967  RegisterIndicator(mirrorOption, iv, ResolveConsolidator(mirrorOption, resolution, typeof(QuoteBar)));
968  }
969  return iv;
970  }
971 
972  /// <summary>
973  /// Creates a new KaufmanAdaptiveMovingAverage indicator.
974  /// </summary>
975  /// <param name="symbol">The symbol whose KAMA we want</param>
976  /// <param name="period">The period of the Efficiency Ratio (ER) of KAMA</param>
977  /// <param name="resolution">The resolution</param>
978  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
979  /// <returns>The KaufmanAdaptiveMovingAverage indicator for the requested symbol over the specified period</returns>
980  [DocumentationAttribute(Indicators)]
981  public KaufmanAdaptiveMovingAverage KAMA(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
982  {
983  return KAMA(symbol, period, 2, 30, resolution, selector);
984  }
985 
986  /// <summary>
987  /// Creates a new KaufmanAdaptiveMovingAverage indicator.
988  /// </summary>
989  /// <param name="symbol">The symbol whose KAMA we want</param>
990  /// <param name="period">The period of the Efficiency Ratio (ER)</param>
991  /// <param name="fastEmaPeriod">The period of the fast EMA used to calculate the Smoothing Constant (SC)</param>
992  /// <param name="slowEmaPeriod">The period of the slow EMA used to calculate the Smoothing Constant (SC)</param>
993  /// <param name="resolution">The resolution</param>
994  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
995  /// <returns>The KaufmanAdaptiveMovingAverage indicator for the requested symbol over the specified period</returns>
996  [DocumentationAttribute(Indicators)]
997  public KaufmanAdaptiveMovingAverage KAMA(Symbol symbol, int period, int fastEmaPeriod, int slowEmaPeriod, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
998  {
999  var name = CreateIndicatorName(symbol, $"KAMA({period},{fastEmaPeriod},{slowEmaPeriod})", resolution);
1000  var kaufmanAdaptiveMovingAverage = new KaufmanAdaptiveMovingAverage(name, period, fastEmaPeriod, slowEmaPeriod);
1001  InitializeIndicator(symbol, kaufmanAdaptiveMovingAverage, resolution, selector);
1002 
1003  return kaufmanAdaptiveMovingAverage;
1004  }
1005 
1006  /// <summary>
1007  /// Creates an KaufmanEfficiencyRatio indicator for the symbol. The indicator will be automatically
1008  /// updated on the given resolution.
1009  /// </summary>
1010  /// <param name="symbol">The symbol whose EF we want</param>
1011  /// <param name="period">The period of the EF</param>
1012  /// <param name="resolution">The resolution</param>
1013  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
1014  /// <returns>The KaufmanEfficiencyRatio indicator for the given parameters</returns>
1015  [DocumentationAttribute(Indicators)]
1016  public KaufmanEfficiencyRatio KER(Symbol symbol, int period = 2, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
1017  {
1018  var name = CreateIndicatorName(symbol, $"KER({period})", resolution);
1019  var kaufmanEfficiencyRatio = new KaufmanEfficiencyRatio(name, period);
1020  InitializeIndicator(symbol, kaufmanEfficiencyRatio, resolution, selector);
1021 
1022  return kaufmanEfficiencyRatio;
1023  }
1024 
1025  /// <summary>
1026  /// Creates a new Keltner Channels indicator.
1027  /// The indicator will be automatically updated on the given resolution.
1028  /// </summary>
1029  /// <param name="symbol">The symbol whose Keltner Channel we seek</param>
1030  /// <param name="period">The period over which to compute the Keltner Channels</param>
1031  /// <param name="k">The number of multiples of the <see cref="AverageTrueRange"/> from the middle band of the Keltner Channels</param>
1032  /// <param name="movingAverageType">Specifies the type of moving average to be used as the middle line of the Keltner Channel</param>
1033  /// <param name="resolution">The resolution.</param>
1034  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
1035  /// <returns>The Keltner Channel indicator for the requested symbol.</returns>
1036  [DocumentationAttribute(Indicators)]
1037  public KeltnerChannels KCH(Symbol symbol, int period, decimal k, MovingAverageType movingAverageType = MovingAverageType.Simple, Resolution? resolution = null, Func<IBaseData, IBaseDataBar> selector = null)
1038  {
1039  var name = CreateIndicatorName(symbol, $"KCH({period},{k})", resolution);
1040  var keltnerChannels = new KeltnerChannels(name, period, k, movingAverageType);
1041  InitializeIndicator(symbol, keltnerChannels, resolution, selector);
1042 
1043  return keltnerChannels;
1044  }
1045 
1046  /// <summary>
1047  /// Creates a new LogReturn indicator.
1048  /// </summary>
1049  /// <param name="symbol">The symbol whose log return we seek</param>
1050  /// <param name="period">The period of the log return.</param>
1051  /// <param name="resolution">The resolution.</param>
1052  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar.</param>
1053  /// <returns>log return indicator for the requested symbol.</returns>
1054  [DocumentationAttribute(Indicators)]
1055  public LogReturn LOGR(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
1056  {
1057  var name = CreateIndicatorName(symbol, $"LOGR({period})", resolution);
1058  var logReturn = new LogReturn(name, period);
1059  InitializeIndicator(symbol, logReturn, resolution, selector);
1060 
1061  return logReturn;
1062  }
1063 
1064  /// <summary>
1065  /// Creates and registers a new Least Squares Moving Average instance.
1066  /// </summary>
1067  /// <param name="symbol">The symbol whose LSMA we seek.</param>
1068  /// <param name="period">The LSMA period. Normally 14.</param>
1069  /// <param name="resolution">The resolution.</param>
1070  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar.</param>
1071  /// <returns>A LeastSquaredMovingAverage configured with the specified period</returns>
1072  [DocumentationAttribute(Indicators)]
1073  public LeastSquaresMovingAverage LSMA(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
1074  {
1075  var name = CreateIndicatorName(symbol, $"LSMA({period})", resolution);
1076  var leastSquaresMovingAverage = new LeastSquaresMovingAverage(name, period);
1077  InitializeIndicator(symbol, leastSquaresMovingAverage, resolution, selector);
1078 
1079  return leastSquaresMovingAverage;
1080  }
1081 
1082  /// <summary>
1083  /// Creates a new LinearWeightedMovingAverage indicator. This indicator will linearly distribute
1084  /// the weights across the periods.
1085  /// </summary>
1086  /// <param name="symbol">The symbol whose LWMA we want</param>
1087  /// <param name="period">The period over which to compute the LWMA</param>
1088  /// <param name="resolution">The resolution</param>
1089  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
1090  /// <returns></returns>
1091  [DocumentationAttribute(Indicators)]
1092  public LinearWeightedMovingAverage LWMA(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
1093  {
1094  var name = CreateIndicatorName(symbol, $"LWMA({period})", resolution);
1095  var linearWeightedMovingAverage = new LinearWeightedMovingAverage(name, period);
1096  InitializeIndicator(symbol, linearWeightedMovingAverage, resolution, selector);
1097 
1098  return linearWeightedMovingAverage;
1099  }
1100 
1101  /// <summary>
1102  /// Creates a MACD indicator for the symbol. The indicator will be automatically updated on the given resolution.
1103  /// </summary>
1104  /// <param name="symbol">The symbol whose MACD we want</param>
1105  /// <param name="fastPeriod">The period for the fast moving average</param>
1106  /// <param name="slowPeriod">The period for the slow moving average</param>
1107  /// <param name="signalPeriod">The period for the signal moving average</param>
1108  /// <param name="type">The type of moving average to use for the MACD</param>
1109  /// <param name="resolution">The resolution</param>
1110  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
1111  /// <returns>The moving average convergence divergence between the fast and slow averages</returns>
1112  [DocumentationAttribute(Indicators)]
1113  public MovingAverageConvergenceDivergence MACD(Symbol symbol, int fastPeriod, int slowPeriod, int signalPeriod, MovingAverageType type = MovingAverageType.Exponential, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
1114  {
1115  var name = CreateIndicatorName(symbol, $"MACD({fastPeriod},{slowPeriod},{signalPeriod})", resolution);
1116  var movingAverageConvergenceDivergence = new MovingAverageConvergenceDivergence(name, fastPeriod, slowPeriod, signalPeriod, type);
1117  InitializeIndicator(symbol, movingAverageConvergenceDivergence, resolution, selector);
1118 
1119  return movingAverageConvergenceDivergence;
1120  }
1121 
1122  /// <summary>
1123  /// Creates a new MeanAbsoluteDeviation indicator.
1124  /// </summary>
1125  /// <param name="symbol">The symbol whose MeanAbsoluteDeviation we want</param>
1126  /// <param name="period">The period over which to compute the MeanAbsoluteDeviation</param>
1127  /// <param name="resolution">The resolution</param>
1128  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
1129  /// <returns>The MeanAbsoluteDeviation indicator for the requested symbol over the specified period</returns>
1130  [DocumentationAttribute(Indicators)]
1131  public MeanAbsoluteDeviation MAD(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
1132  {
1133  var name = CreateIndicatorName(symbol, $"MAD({period})", resolution);
1134  var meanAbsoluteDeviation = new MeanAbsoluteDeviation(name, period);
1135  InitializeIndicator(symbol, meanAbsoluteDeviation, resolution, selector);
1136 
1137  return meanAbsoluteDeviation;
1138  }
1139 
1140  /// <summary>
1141  /// Creates an Market Profile indicator for the symbol with Volume Profile (VOL) mode. The indicator will be automatically
1142  /// updated on the given resolution.
1143  /// </summary>
1144  /// <param name="symbol">The symbol whose VP we want</param>
1145  /// <param name="period">The period of the VP</param>
1146  /// <param name="valueAreaVolumePercentage">The percentage of volume contained in the value area</param>
1147  /// <param name="priceRangeRoundOff">How many digits you want to round and the precision. i.e 0.01 round to two digits exactly.</param>
1148  /// <param name="resolution">The resolution</param>
1149  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
1150  /// <returns>The Volume Profile indicator for the given parameters</returns>
1151  [DocumentationAttribute(Indicators)]
1152  public VolumeProfile VP(Symbol symbol, int period = 2, decimal valueAreaVolumePercentage = 0.70m, decimal priceRangeRoundOff = 0.05m, Resolution resolution = Resolution.Daily, Func<IBaseData, TradeBar> selector = null)
1153  {
1154  var name = CreateIndicatorName(symbol, $"VP({period})", resolution);
1155  var marketProfile = new VolumeProfile(name, period, valueAreaVolumePercentage, priceRangeRoundOff);
1156  InitializeIndicator(symbol, marketProfile, resolution, selector);
1157 
1158  return marketProfile;
1159  }
1160 
1161  /// <summary>
1162  /// Creates an Market Profile indicator for the symbol with Time Price Opportunity (TPO) mode. The indicator will be automatically
1163  /// updated on the given resolution.
1164  /// </summary>
1165  /// <param name="symbol">The symbol whose TP we want</param>
1166  /// <param name="period">The period of the TP</param>
1167  /// <param name="valueAreaVolumePercentage">The percentage of volume contained in the value area</param>
1168  /// <param name="priceRangeRoundOff">How many digits you want to round and the precision. i.e 0.01 round to two digits exactly.</param>
1169  /// <param name="resolution">The resolution</param>
1170  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
1171  /// <returns>The Time Profile indicator for the given parameters</returns>
1172  [DocumentationAttribute(Indicators)]
1173  public TimeProfile TP(Symbol symbol, int period = 2, decimal valueAreaVolumePercentage = 0.70m, decimal priceRangeRoundOff = 0.05m, Resolution resolution = Resolution.Daily, Func<IBaseData, TradeBar> selector = null)
1174  {
1175  var name = CreateIndicatorName(symbol, $"TP({period})", resolution);
1176  var marketProfile = new TimeProfile(name, period, valueAreaVolumePercentage, priceRangeRoundOff);
1177  InitializeIndicator(symbol, marketProfile, resolution, selector);
1178 
1179  return marketProfile;
1180  }
1181 
1182  /// <summary>
1183  /// Creates a new Time Series Forecast indicator
1184  /// </summary>
1185  /// <param name="symbol">The symbol whose TSF we want</param>
1186  /// <param name="period">The period of the TSF</param>
1187  /// <param name="resolution">The resolution</param>
1188  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to Value property of BaseData (x => x.Value)</param>
1189  /// <returns>The TimeSeriesForecast indicator for the requested symbol over the specified period</returns>
1190  [DocumentationAttribute(Indicators)]
1191  public TimeSeriesForecast TSF(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
1192  {
1193  var name = CreateIndicatorName(symbol, $"TSF({period})", resolution);
1194  var timeSeriesForecast = new TimeSeriesForecast(name, period);
1195  InitializeIndicator(symbol, timeSeriesForecast, resolution, selector);
1196 
1197  return timeSeriesForecast;
1198  }
1199 
1200  /// <summary>
1201  /// Creates a new Maximum indicator to compute the maximum value
1202  /// </summary>
1203  /// <param name="symbol">The symbol whose max we want</param>
1204  /// <param name="period">The look back period over which to compute the max value</param>
1205  /// <param name="resolution">The resolution</param>
1206  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null and the symbol is of type TradeBar defaults to the High property,
1207  /// otherwise it defaults to Value property of BaseData (x => x.Value)</param>
1208  /// <returns>A Maximum indicator that compute the max value and the periods since the max value</returns>
1209  [DocumentationAttribute(Indicators)]
1210  public Maximum MAX(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
1211  {
1212  var name = CreateIndicatorName(symbol, $"MAX({period})", resolution);
1213  var maximum = new Maximum(name, period);
1214 
1215  // assign a default value for the selector function
1216  if (selector == null)
1217  {
1218  var subscription = GetSubscription(symbol);
1219  if (typeof(TradeBar).IsAssignableFrom(subscription.Type))
1220  {
1221  // if we have trade bar data we'll use the High property, if not x => x.Value will be set in RegisterIndicator
1222  selector = x => ((TradeBar)x).High;
1223  }
1224  }
1225 
1226  RegisterIndicator(symbol, maximum, ResolveConsolidator(symbol, resolution), selector);
1227 
1229  {
1230  WarmUpIndicator(symbol, maximum, resolution, selector);
1231  }
1232 
1233  return maximum;
1234  }
1235 
1236  /// <summary>
1237  /// Creates a new MoneyFlowIndex indicator. The indicator will be automatically
1238  /// updated on the given resolution.
1239  /// </summary>
1240  /// <param name="symbol">The symbol whose MFI we want</param>
1241  /// <param name="period">The period over which to compute the MFI</param>
1242  /// <param name="resolution">The resolution</param>
1243  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
1244  /// <returns>The MoneyFlowIndex indicator for the requested symbol over the specified period</returns>
1245  [DocumentationAttribute(Indicators)]
1246  public MoneyFlowIndex MFI(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, TradeBar> selector = null)
1247  {
1248  var name = CreateIndicatorName(symbol, $"MFI({period})", resolution);
1249  var moneyFlowIndex = new MoneyFlowIndex(name, period);
1250  InitializeIndicator(symbol, moneyFlowIndex, resolution, selector);
1251 
1252  return moneyFlowIndex;
1253  }
1254 
1255  /// <summary>
1256  /// Creates a new Mass Index indicator. The indicator will be automatically
1257  /// updated on the given resolution.
1258  /// </summary>
1259  /// <param name="symbol">The symbol whose Mass Index we want.</param>
1260  /// <param name="emaPeriod">The period used by both EMA.</param>
1261  /// <param name="sumPeriod">The sum period.</param>
1262  /// <param name="resolution">The resolution.</param>
1263  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
1264  /// <returns>The Mass Index indicator for the requested symbol over the specified period</returns>
1265  [DocumentationAttribute(Indicators)]
1266  public MassIndex MASS(Symbol symbol, int emaPeriod = 9, int sumPeriod = 25, Resolution? resolution = null, Func<IBaseData, TradeBar> selector = null)
1267  {
1268  var name = CreateIndicatorName(symbol, $"MASS({emaPeriod},{sumPeriod})", resolution);
1269  var massIndex = new MassIndex(name, emaPeriod, sumPeriod);
1270  InitializeIndicator(symbol, massIndex, resolution, selector);
1271 
1272  return massIndex;
1273  }
1274 
1275  /// <summary>
1276  /// Creates a new MidPoint indicator.
1277  /// </summary>
1278  /// <param name="symbol">The symbol whose MIDPOINT we want</param>
1279  /// <param name="period">The period over which to compute the MIDPOINT</param>
1280  /// <param name="resolution">The resolution</param>
1281  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
1282  /// <returns>The MidPoint indicator for the requested symbol over the specified period</returns>
1283  [DocumentationAttribute(Indicators)]
1284  public MidPoint MIDPOINT(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
1285  {
1286  var name = CreateIndicatorName(symbol, $"MIDPOINT({period})", resolution);
1287  var midPoint = new MidPoint(name, period);
1288  InitializeIndicator(symbol, midPoint, resolution, selector);
1289 
1290  return midPoint;
1291  }
1292 
1293  /// <summary>
1294  /// Creates a new MidPrice indicator.
1295  /// </summary>
1296  /// <param name="symbol">The symbol whose MIDPRICE we want</param>
1297  /// <param name="period">The period over which to compute the MIDPRICE</param>
1298  /// <param name="resolution">The resolution</param>
1299  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
1300  /// <returns>The MidPrice indicator for the requested symbol over the specified period</returns>
1301  [DocumentationAttribute(Indicators)]
1302  public MidPrice MIDPRICE(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, IBaseDataBar> selector = null)
1303  {
1304  var name = CreateIndicatorName(symbol, $"MIDPRICE({period})", resolution);
1305  var midPrice = new MidPrice(name, period);
1306  InitializeIndicator(symbol, midPrice, resolution, selector);
1307 
1308  return midPrice;
1309  }
1310 
1311  /// <summary>
1312  /// Creates a new Minimum indicator to compute the minimum value
1313  /// </summary>
1314  /// <param name="symbol">The symbol whose min we want</param>
1315  /// <param name="period">The look back period over which to compute the min value</param>
1316  /// <param name="resolution">The resolution</param>
1317  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null and the symbol is of type TradeBar defaults to the Low property,
1318  /// otherwise it defaults to Value property of BaseData (x => x.Value)</param>
1319  /// <returns>A Minimum indicator that compute the in value and the periods since the min value</returns>
1320  [DocumentationAttribute(Indicators)]
1321  public Minimum MIN(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
1322  {
1323  var name = CreateIndicatorName(symbol, $"MIN({period})", resolution);
1324  var minimum = new Minimum(name, period);
1325 
1326  // assign a default value for the selector function
1327  if (selector == null)
1328  {
1329  var subscription = GetSubscription(symbol);
1330  if (typeof(TradeBar).IsAssignableFrom(subscription.Type))
1331  {
1332  // if we have trade bar data we'll use the Low property, if not x => x.Value will be set in RegisterIndicator
1333  selector = x => ((TradeBar)x).Low;
1334  }
1335  }
1336 
1337  RegisterIndicator(symbol, minimum, ResolveConsolidator(symbol, resolution), selector);
1338 
1340  {
1341  WarmUpIndicator(symbol, minimum, resolution, selector);
1342  }
1343 
1344  return minimum;
1345  }
1346 
1347  /// <summary>
1348  /// Creates a new Momentum indicator. This will compute the absolute n-period change in the security.
1349  /// The indicator will be automatically updated on the given resolution.
1350  /// </summary>
1351  /// <param name="symbol">The symbol whose momentum we want</param>
1352  /// <param name="period">The period over which to compute the momentum</param>
1353  /// <param name="resolution">The resolution</param>
1354  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
1355  /// <returns>The momentum indicator for the requested symbol over the specified period</returns>
1356  [DocumentationAttribute(Indicators)]
1357  public Momentum MOM(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
1358  {
1359  var name = CreateIndicatorName(symbol, $"MOM({period})", resolution);
1360  var momentum = new Momentum(name, period);
1361  InitializeIndicator(symbol, momentum, resolution, selector);
1362 
1363  return momentum;
1364  }
1365 
1366  /// <summary>
1367  /// Creates a new Momersion indicator.
1368  /// </summary>
1369  /// <param name="symbol">The symbol whose Momersion we want</param>
1370  /// <param name="minPeriod">The minimum period over which to compute the Momersion. Must be greater than 3. If null, only full period will be used in computations.</param>
1371  /// <param name="fullPeriod">The full period over which to compute the Momersion</param>
1372  /// <param name="resolution">The resolution</param>
1373  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
1374  /// <returns>The Momersion indicator for the requested symbol over the specified period</returns>
1375  [DocumentationAttribute(Indicators)]
1376  public MomersionIndicator MOMERSION(Symbol symbol, int? minPeriod, int fullPeriod, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
1377  {
1378  var name = CreateIndicatorName(symbol, $"MOMERSION({minPeriod},{fullPeriod})", resolution);
1379  var momersion = new MomersionIndicator(name, minPeriod, fullPeriod);
1380  InitializeIndicator(symbol, momersion, resolution, selector);
1381 
1382  return momersion;
1383  }
1384 
1385  /// <summary>
1386  /// Creates a new MomentumPercent indicator. This will compute the n-period percent change in the security.
1387  /// The indicator will be automatically updated on the given resolution.
1388  /// </summary>
1389  /// <param name="symbol">The symbol whose momentum we want</param>
1390  /// <param name="period">The period over which to compute the momentum</param>
1391  /// <param name="resolution">The resolution</param>
1392  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
1393  /// <returns>The momentum indicator for the requested symbol over the specified period</returns>
1394  [DocumentationAttribute(Indicators)]
1395  public MomentumPercent MOMP(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
1396  {
1397  var name = CreateIndicatorName(symbol, $"MOMP({period})", resolution);
1398  var momentumPercent = new MomentumPercent(name, period);
1399  InitializeIndicator(symbol, momentumPercent, resolution, selector);
1400 
1401  return momentumPercent;
1402  }
1403 
1404  /// <summary>
1405  /// Creates a new NormalizedAverageTrueRange indicator.
1406  /// </summary>
1407  /// <param name="symbol">The symbol whose NATR we want</param>
1408  /// <param name="period">The period over which to compute the NATR</param>
1409  /// <param name="resolution">The resolution</param>
1410  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
1411  /// <returns>The NormalizedAverageTrueRange indicator for the requested symbol over the specified period</returns>
1412  [DocumentationAttribute(Indicators)]
1413  public NormalizedAverageTrueRange NATR(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, IBaseDataBar> selector = null)
1414  {
1415  var name = CreateIndicatorName(symbol, $"NATR({period})", resolution);
1416  var normalizedAverageTrueRange = new NormalizedAverageTrueRange(name, period);
1417  InitializeIndicator(symbol, normalizedAverageTrueRange, resolution, selector);
1418 
1419  return normalizedAverageTrueRange;
1420  }
1421 
1422  /// <summary>
1423  /// Creates a new On Balance Volume indicator. This will compute the cumulative total volume
1424  /// based on whether the close price being higher or lower than the previous period.
1425  /// The indicator will be automatically updated on the given resolution.
1426  /// </summary>
1427  /// <param name="symbol">The symbol whose On Balance Volume we seek</param>
1428  /// <param name="resolution">The resolution.</param>
1429  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
1430  /// <returns>The On Balance Volume indicator for the requested symbol.</returns>
1431  [DocumentationAttribute(Indicators)]
1432  public OnBalanceVolume OBV(Symbol symbol, Resolution? resolution = null, Func<IBaseData, TradeBar> selector = null)
1433  {
1434  var name = CreateIndicatorName(symbol, "OBV", resolution);
1435  var onBalanceVolume = new OnBalanceVolume(name);
1436  InitializeIndicator(symbol, onBalanceVolume, resolution, selector);
1437 
1438  return onBalanceVolume;
1439  }
1440 
1441  /// <summary>
1442  /// Creates a new PivotPointsHighLow indicator
1443  /// </summary>
1444  /// <param name="symbol">The symbol whose PPHL we seek</param>
1445  /// <param name="lengthHigh">The number of surrounding bars whose high values should be less than the current bar's for the bar high to be marked as high pivot point</param>
1446  /// <param name="lengthLow">The number of surrounding bars whose low values should be more than the current bar's for the bar low to be marked as low pivot point</param>
1447  /// <param name="lastStoredValues">The number of last stored indicator values</param>
1448  /// <param name="resolution">The resolution</param>
1449  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
1450  /// <returns>The PivotPointsHighLow indicator for the requested symbol.</returns>
1451  [DocumentationAttribute(Indicators)]
1452  public PivotPointsHighLow PPHL(Symbol symbol, int lengthHigh, int lengthLow, int lastStoredValues = 100, Resolution? resolution = null, Func<IBaseData, IBaseDataBar> selector = null)
1453  {
1454  var name = CreateIndicatorName(symbol, $"PPHL({lengthHigh},{lengthLow})", resolution);
1455  var pivotPointsHighLow = new PivotPointsHighLow(name, lengthHigh, lengthLow, lastStoredValues);
1456  InitializeIndicator(symbol, pivotPointsHighLow, resolution, selector);
1457 
1458  return pivotPointsHighLow;
1459  }
1460 
1461  /// <summary>
1462  /// Creates a new PercentagePriceOscillator indicator.
1463  /// </summary>
1464  /// <param name="symbol">The symbol whose PPO we want</param>
1465  /// <param name="fastPeriod">The fast moving average period</param>
1466  /// <param name="slowPeriod">The slow moving average period</param>
1467  /// <param name="movingAverageType">The type of moving average to use</param>
1468  /// <param name="resolution">The resolution</param>
1469  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
1470  /// <returns>The PercentagePriceOscillator indicator for the requested symbol over the specified period</returns>
1471  [DocumentationAttribute(Indicators)]
1472  public PercentagePriceOscillator PPO(Symbol symbol, int fastPeriod, int slowPeriod, MovingAverageType movingAverageType, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
1473  {
1474  var name = CreateIndicatorName(symbol, $"PPO({fastPeriod},{slowPeriod})", resolution);
1475  var percentagePriceOscillator = new PercentagePriceOscillator(name, fastPeriod, slowPeriod, movingAverageType);
1476  InitializeIndicator(symbol, percentagePriceOscillator, resolution, selector);
1477 
1478  return percentagePriceOscillator;
1479  }
1480 
1481  /// <summary>
1482  /// Creates a new Parabolic SAR indicator
1483  /// </summary>
1484  /// <param name="symbol">The symbol whose PSAR we seek</param>
1485  /// <param name="afStart">Acceleration factor start value. Normally 0.02</param>
1486  /// <param name="afIncrement">Acceleration factor increment value. Normally 0.02</param>
1487  /// <param name="afMax">Acceleration factor max value. Normally 0.2</param>
1488  /// <param name="resolution">The resolution</param>
1489  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
1490  /// <returns>A ParabolicStopAndReverse configured with the specified periods</returns>
1491  [DocumentationAttribute(Indicators)]
1492  public ParabolicStopAndReverse PSAR(Symbol symbol, decimal afStart = 0.02m, decimal afIncrement = 0.02m, decimal afMax = 0.2m, Resolution? resolution = null, Func<IBaseData, IBaseDataBar> selector = null)
1493  {
1494  var name = CreateIndicatorName(symbol, $"PSAR({afStart},{afIncrement},{afMax})", resolution);
1495  var parabolicStopAndReverse = new ParabolicStopAndReverse(name, afStart, afIncrement, afMax);
1496  InitializeIndicator(symbol, parabolicStopAndReverse, resolution, selector);
1497 
1498  return parabolicStopAndReverse;
1499  }
1500 
1501  /// <summary>
1502  /// Creates a new RegressionChannel indicator which will compute the LinearRegression, UpperChannel and LowerChannel lines, the intercept and slope
1503  /// </summary>
1504  /// <param name="symbol">The symbol whose RegressionChannel we seek</param>
1505  /// <param name="period">The period of the standard deviation and least square moving average (linear regression line)</param>
1506  /// <param name="k">The number of standard deviations specifying the distance between the linear regression and upper or lower channel lines</param>
1507  /// <param name="resolution">The resolution</param>
1508  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
1509  /// <returns>A Regression Channel configured with the specified period and number of standard deviation</returns>
1510  [DocumentationAttribute(Indicators)]
1511  public RegressionChannel RC(Symbol symbol, int period, decimal k, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
1512  {
1513  var name = CreateIndicatorName(symbol, $"RC({period},{k})", resolution);
1514  var regressionChannel = new RegressionChannel(name, period, k);
1515  InitializeIndicator(symbol, regressionChannel, resolution, selector);
1516 
1517  return regressionChannel;
1518  }
1519 
1520  /// <summary>
1521  /// Creates a new Relative Moving Average indicator for the symbol. The indicator will be automatically updated on the given resolution.
1522  /// </summary>
1523  /// <param name="symbol">The symbol whose relative moving average we seek</param>
1524  /// <param name="period">The period of the relative moving average</param>
1525  /// <param name="resolution">The resolution</param>
1526  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
1527  /// <returns>A relative moving average configured with the specified period and number of standard deviation</returns>
1528  [DocumentationAttribute(Indicators)]
1529  public RelativeMovingAverage RMA(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
1530  {
1531  var name = CreateIndicatorName(symbol, $"RMA({period})", resolution);
1532  var relativeMovingAverage = new RelativeMovingAverage(name, period);
1533  InitializeIndicator(symbol, relativeMovingAverage, resolution, selector);
1534 
1535  return relativeMovingAverage;
1536  }
1537 
1538 
1539  /// <summary>
1540  /// Creates a new RateOfChange indicator. This will compute the n-period rate of change in the security.
1541  /// The indicator will be automatically updated on the given resolution.
1542  /// </summary>
1543  /// <param name="symbol">The symbol whose RateOfChange we want</param>
1544  /// <param name="period">The period over which to compute the RateOfChange</param>
1545  /// <param name="resolution">The resolution</param>
1546  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
1547  /// <returns>The RateOfChange indicator for the requested symbol over the specified period</returns>
1548  [DocumentationAttribute(Indicators)]
1549  public RateOfChange ROC(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
1550  {
1551  var name = CreateIndicatorName(symbol, $"ROC({period})", resolution);
1552  var rateOfChange = new RateOfChange(name, period);
1553  InitializeIndicator(symbol, rateOfChange, resolution, selector);
1554 
1555  return rateOfChange;
1556  }
1557 
1558  /// <summary>
1559  /// Creates a new RateOfChangePercent indicator. This will compute the n-period percentage rate of change in the security.
1560  /// The indicator will be automatically updated on the given resolution.
1561  /// </summary>
1562  /// <param name="symbol">The symbol whose RateOfChangePercent we want</param>
1563  /// <param name="period">The period over which to compute the RateOfChangePercent</param>
1564  /// <param name="resolution">The resolution</param>
1565  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
1566  /// <returns>The RateOfChangePercent indicator for the requested symbol over the specified period</returns>
1567  [DocumentationAttribute(Indicators)]
1568  public RateOfChangePercent ROCP(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
1569  {
1570  var name = CreateIndicatorName(symbol, $"ROCP({period})", resolution);
1571  var rateOfChangePercent = new RateOfChangePercent(name, period);
1572  InitializeIndicator(symbol, rateOfChangePercent, resolution, selector);
1573 
1574  return rateOfChangePercent;
1575  }
1576 
1577  /// <summary>
1578  /// Creates a new RateOfChangeRatio indicator.
1579  /// </summary>
1580  /// <param name="symbol">The symbol whose ROCR we want</param>
1581  /// <param name="period">The period over which to compute the ROCR</param>
1582  /// <param name="resolution">The resolution</param>
1583  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
1584  /// <returns>The RateOfChangeRatio indicator for the requested symbol over the specified period</returns>
1585  [DocumentationAttribute(Indicators)]
1586  public RateOfChangeRatio ROCR(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
1587  {
1588  var name = CreateIndicatorName(symbol, $"ROCR({period})", resolution);
1589  var rateOfChangeRatio = new RateOfChangeRatio(name, period);
1590  InitializeIndicator(symbol, rateOfChangeRatio, resolution, selector);
1591 
1592  return rateOfChangeRatio;
1593  }
1594 
1595  /// <summary>
1596  /// Creates a new RelativeStrengthIndex indicator. This will produce an oscillator that ranges from 0 to 100 based
1597  /// on the ratio of average gains to average losses over the specified period.
1598  /// </summary>
1599  /// <param name="symbol">The symbol whose RSI we want</param>
1600  /// <param name="period">The period over which to compute the RSI</param>
1601  /// <param name="movingAverageType">The type of moving average to use in computing the average gain/loss values</param>
1602  /// <param name="resolution">The resolution</param>
1603  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
1604  /// <returns>The RelativeStrengthIndex indicator for the requested symbol over the specified period</returns>
1605  [DocumentationAttribute(Indicators)]
1606  public RelativeStrengthIndex RSI(Symbol symbol, int period, MovingAverageType movingAverageType = MovingAverageType.Wilders, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
1607  {
1608  var name = CreateIndicatorName(symbol, $"RSI({period},{movingAverageType})", resolution);
1609  var relativeStrengthIndex = new RelativeStrengthIndex(name, period, movingAverageType);
1610  InitializeIndicator(symbol, relativeStrengthIndex, resolution, selector);
1611 
1612  return relativeStrengthIndex;
1613  }
1614  /// <summary>
1615  /// Creates a new RelativeVigorIndex indicator.
1616  /// </summary>
1617  /// <param name="symbol">The symbol whose RVI we want</param>
1618  /// <param name="period">The period over which to compute the RVI</param>
1619  /// <param name="movingAverageType">The type of moving average to use</param>
1620  /// <param name="resolution">The resolution</param>
1621  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
1622  /// <returns>The RelativeVigorIndex indicator for the requested symbol over the specified period</returns>
1623  [DocumentationAttribute(Indicators)]
1624  public RelativeVigorIndex RVI(Symbol symbol, int period, MovingAverageType movingAverageType = MovingAverageType.Simple, Resolution? resolution = null, Func<IBaseData, TradeBar> selector = null)
1625  {
1626  var name = CreateIndicatorName(symbol, $"RVI({period},{movingAverageType})", resolution);
1627  var relativeVigorIndex = new RelativeVigorIndex(name, period, movingAverageType);
1628  InitializeIndicator(symbol, relativeVigorIndex, resolution, selector);
1629 
1630  return relativeVigorIndex;
1631  }
1632 
1633  /// <summary>
1634  /// Creates an RelativeDailyVolume indicator for the symbol. The indicator will be automatically
1635  /// updated on the given resolution.
1636  /// </summary>
1637  /// <param name="symbol">The symbol whose RDV we want</param>
1638  /// <param name="period">The period of the RDV</param>
1639  /// <param name="resolution">The resolution</param>
1640  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
1641  /// <returns>The Relative Volume indicator for the given parameters</returns>
1642  [DocumentationAttribute(Indicators)]
1643  public RelativeDailyVolume RDV(Symbol symbol, int period = 2, Resolution resolution = Resolution.Daily, Func<IBaseData, TradeBar> selector = null)
1644  {
1645  var name = CreateIndicatorName(symbol, $"RDV({period})", resolution);
1646  var relativeDailyVolume = new RelativeDailyVolume(name, period);
1647  RegisterIndicator(symbol, relativeDailyVolume, resolution, selector);
1648 
1649  return relativeDailyVolume;
1650  }
1651 
1652  /// <summary>
1653  /// Creates a new Rho indicator for the symbol The indicator will be automatically
1654  /// updated on the symbol's subscription resolution
1655  /// </summary>
1656  /// <param name="symbol">The option symbol whose values we want as an indicator</param>
1657  /// <param name="mirrorOption">The mirror option for parity calculation</param>
1658  /// <param name="riskFreeRate">The risk free rate</param>
1659  /// <param name="dividendYield">The dividend yield</param>
1660  /// <param name="optionModel">The option pricing model used to estimate Rho</param>
1661  /// <param name="ivModel">The option pricing model used to estimate IV</param>
1662  /// <param name="resolution">The desired resolution of the data</param>
1663  /// <returns>A new Rho indicator for the specified symbol</returns>
1664  [DocumentationAttribute(Indicators)]
1665  public Rho R(Symbol symbol, Symbol mirrorOption = null, decimal? riskFreeRate = null, decimal? dividendYield = null, OptionPricingModelType optionModel = OptionPricingModelType.BlackScholes,
1666  OptionPricingModelType? ivModel = null, Resolution? resolution = null)
1667  {
1668  var name = InitializeOptionIndicator<Rho>(symbol, out var riskFreeRateModel, out var dividendYieldModel, riskFreeRate, dividendYield, optionModel, resolution);
1669 
1670  var rho = new Rho(name, symbol, riskFreeRateModel, dividendYieldModel, mirrorOption, optionModel, ivModel);
1671  RegisterIndicator(symbol, rho, ResolveConsolidator(symbol, resolution, typeof(QuoteBar)));
1672  RegisterIndicator(symbol.Underlying, rho, ResolveConsolidator(symbol.Underlying, resolution));
1673  if (mirrorOption != null)
1674  {
1675  RegisterIndicator(mirrorOption, rho, ResolveConsolidator(mirrorOption, resolution, typeof(QuoteBar)));
1676  }
1677  return rho;
1678  }
1679 
1680  /// <summary>
1681  /// Creates a new Rho indicator for the symbol The indicator will be automatically
1682  /// updated on the symbol's subscription resolution
1683  /// </summary>
1684  /// <param name="symbol">The option symbol whose values we want as an indicator</param>
1685  /// <param name="mirrorOption">The mirror option for parity calculation</param>
1686  /// <param name="riskFreeRate">The risk free rate</param>
1687  /// <param name="dividendYield">The dividend yield</param>
1688  /// <param name="optionModel">The option pricing model used to estimate Rho</param>
1689  /// <param name="ivModel">The option pricing model used to estimate IV</param>
1690  /// <param name="resolution">The desired resolution of the data</param>
1691  /// <returns>A new Rho indicator for the specified symbol</returns>
1692  [DocumentationAttribute(Indicators)]
1693  public Rho ρ(Symbol symbol, Symbol mirrorOption = null, decimal? riskFreeRate = null, decimal? dividendYield = null, OptionPricingModelType optionModel = OptionPricingModelType.BlackScholes,
1694  OptionPricingModelType? ivModel = null, Resolution? resolution = null)
1695  {
1696  return R(symbol, mirrorOption, riskFreeRate, dividendYield, optionModel, ivModel, resolution);
1697  }
1698 
1699  /// <summary>
1700  /// Creates a new SuperTrend indicator.
1701  /// </summary>
1702  /// <param name="symbol">The symbol whose SuperTrend indicator we want.</param>
1703  /// <param name="period">The smoothing period for average true range.</param>
1704  /// <param name="multiplier">Multiplier to calculate basic upper and lower bands width.</param>
1705  /// <param name="movingAverageType">Smoother type for average true range, defaults to Wilders.</param>
1706  /// <param name="resolution">The resolution.</param>
1707  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
1708  [DocumentationAttribute(Indicators)]
1709  public SuperTrend STR(Symbol symbol, int period, decimal multiplier, MovingAverageType movingAverageType = MovingAverageType.Wilders,
1710  Resolution? resolution = null, Func<IBaseData, IBaseDataBar> selector = null)
1711  {
1712  var name = CreateIndicatorName(symbol, $"STR({period},{multiplier})", resolution);
1713  var strend = new SuperTrend(name, period, multiplier, movingAverageType);
1714  InitializeIndicator(symbol, strend, resolution, selector);
1715 
1716  return strend;
1717  }
1718 
1719  /// <summary>
1720  /// Creates a new SharpeRatio indicator.
1721  /// </summary>
1722  /// <param name="symbol">The symbol whose RSR we want</param>
1723  /// <param name="sharpePeriod">Period of historical observation for sharpe ratio calculation</param>
1724  /// <param name="riskFreeRate">
1725  /// Risk-free rate for sharpe ratio calculation. If not specified, it will use the algorithms' <see cref="RiskFreeInterestRateModel"/>
1726  /// </param>
1727  /// <param name="resolution">The resolution</param>
1728  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
1729  /// <returns>The SharpeRatio indicator for the requested symbol over the specified period</returns>
1730  [DocumentationAttribute(Indicators)]
1731  public SharpeRatio SR(Symbol symbol, int sharpePeriod, decimal? riskFreeRate = null, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
1732  {
1733  var baseBame = riskFreeRate.HasValue ? $"SR({sharpePeriod},{riskFreeRate})" : $"SR({sharpePeriod})";
1734  var name = CreateIndicatorName(symbol, baseBame, resolution);
1735  IRiskFreeInterestRateModel riskFreeRateModel = riskFreeRate.HasValue
1736  ? new ConstantRiskFreeRateInterestRateModel(riskFreeRate.Value)
1737  // Make it a function so it's lazily evaluated: SetRiskFreeInterestRateModel can be called after this method
1739  var sharpeRatio = new SharpeRatio(name, sharpePeriod, riskFreeRateModel);
1740  InitializeIndicator(symbol, sharpeRatio, resolution, selector);
1741 
1742  return sharpeRatio;
1743  }
1744 
1745  /// <summary>
1746  /// Creates a new Sortino indicator.
1747  /// </summary>
1748  /// <param name="symbol">The symbol whose Sortino we want</param>
1749  /// <param name="sortinoPeriod">Period of historical observation for Sortino ratio calculation</param>
1750  /// <param name="minimumAcceptableReturn">Minimum acceptable return (eg risk-free rate) for the Sortino ratio calculation</param>
1751  /// <param name="resolution">The resolution</param>
1752  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
1753  /// <returns>The SortinoRatio indicator for the requested symbol over the specified period</returns>
1754  [DocumentationAttribute(Indicators)]
1755  public SortinoRatio SORTINO(Symbol symbol, int sortinoPeriod, double minimumAcceptableReturn = 0.0, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
1756  {
1757  var name = CreateIndicatorName(symbol, $"SORTINO({sortinoPeriod},{minimumAcceptableReturn})", resolution);
1758  var sortinoRatio = new SortinoRatio(name, sortinoPeriod, minimumAcceptableReturn);
1759  InitializeIndicator(symbol, sortinoRatio, resolution, selector);
1760 
1761  return sortinoRatio;
1762  }
1763 
1764 
1765  /// <summary>
1766  /// Creates an SimpleMovingAverage indicator for the symbol. The indicator will be automatically
1767  /// updated on the given resolution.
1768  /// </summary>
1769  /// <param name="symbol">The symbol whose SMA we want</param>
1770  /// <param name="period">The period of the SMA</param>
1771  /// <param name="resolution">The resolution</param>
1772  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
1773  /// <returns>The SimpleMovingAverage for the given parameters</returns>
1774  [DocumentationAttribute(Indicators)]
1775  public SimpleMovingAverage SMA(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
1776  {
1777  var name = CreateIndicatorName(symbol, $"SMA({period})", resolution);
1778  var simpleMovingAverage = new SimpleMovingAverage(name, period);
1779  InitializeIndicator(symbol, simpleMovingAverage, resolution, selector);
1780 
1781  return simpleMovingAverage;
1782  }
1783 
1784  /// <summary>
1785  /// Creates a new Schaff Trend Cycle indicator
1786  /// </summary>
1787  /// <param name="symbol">The symbol for the indicator to track</param>
1788  /// <param name="fastPeriod">The fast moving average period</param>
1789  /// <param name="slowPeriod">The slow moving average period</param>
1790  /// <param name="cyclePeriod">The signal period</param>
1791  /// <param name="movingAverageType">The type of moving average to use</param>
1792  /// <param name="resolution">The resolution</param>
1793  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
1794  /// <returns>The SchaffTrendCycle indicator for the requested symbol over the specified period</returns>
1795  [DocumentationAttribute(Indicators)]
1796  public SchaffTrendCycle STC(Symbol symbol, int cyclePeriod, int fastPeriod, int slowPeriod, MovingAverageType movingAverageType = MovingAverageType.Exponential, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
1797  {
1798  var name = CreateIndicatorName(symbol, $"STC({cyclePeriod},{fastPeriod},{slowPeriod})", resolution);
1799  var schaffTrendCycle = new SchaffTrendCycle(name, cyclePeriod, fastPeriod, slowPeriod, movingAverageType);
1800  InitializeIndicator(symbol, schaffTrendCycle, resolution, selector);
1801 
1802  return schaffTrendCycle;
1803  }
1804 
1805  /// <summary>
1806  /// Creates a new StandardDeviation indicator. This will return the population standard deviation of samples over the specified period.
1807  /// </summary>
1808  /// <param name="symbol">The symbol whose STD we want</param>
1809  /// <param name="period">The period over which to compute the STD</param>
1810  /// <param name="resolution">The resolution</param>
1811  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
1812  /// <returns>The StandardDeviation indicator for the requested symbol over the specified period</returns>
1813  [DocumentationAttribute(Indicators)]
1814  public StandardDeviation STD(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
1815  {
1816  var name = CreateIndicatorName(symbol, $"STD({period})", resolution);
1817  var standardDeviation = new StandardDeviation(name, period);
1818  InitializeIndicator(symbol, standardDeviation, resolution, selector);
1819 
1820  return standardDeviation;
1821  }
1822 
1823  /// <summary>
1824  /// Creates a new TargetDownsideDeviation indicator. The target downside deviation is defined as the root-mean-square, or RMS, of the deviations of the
1825  /// realized return’s underperformance from the target return where all returns above the target return are treated as underperformance of 0.
1826  /// </summary>
1827  /// <param name="symbol">The symbol whose TDD we want</param>
1828  /// <param name="period">The period over which to compute the TDD</param>
1829  /// <param name="resolution">The resolution</param>
1830  /// <param name="minimumAcceptableReturn">Minimum acceptable return (MAR) for the target downside deviation calculation</param>
1831  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
1832  /// <returns>The TargetDownsideDeviation indicator for the requested symbol over the specified period</returns>
1833  [DocumentationAttribute(Indicators)]
1834  public TargetDownsideDeviation TDD(Symbol symbol, int period, double minimumAcceptableReturn = 0, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
1835  {
1836  var name = CreateIndicatorName(symbol, $"TDD({period},{minimumAcceptableReturn})", resolution);
1837  var targetDownsideDeviation = new TargetDownsideDeviation(name, period, minimumAcceptableReturn);
1838  InitializeIndicator(symbol, targetDownsideDeviation, resolution, selector);
1839 
1840  return targetDownsideDeviation;
1841  }
1842 
1843  /// <summary>
1844  /// Creates a new Stochastic indicator.
1845  /// </summary>
1846  /// <param name="symbol">The symbol whose stochastic we seek</param>
1847  /// <param name="period">The period of the stochastic. Normally 14</param>
1848  /// <param name="kPeriod">The sum period of the stochastic. Normally 14</param>
1849  /// <param name="dPeriod">The sum period of the stochastic. Normally 3</param>
1850  /// <param name="resolution">The resolution.</param>
1851  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
1852  /// <returns>Stochastic indicator for the requested symbol.</returns>
1853  [DocumentationAttribute(Indicators)]
1854  public Stochastic STO(Symbol symbol, int period, int kPeriod, int dPeriod, Resolution? resolution = null,
1855  Func<IBaseData, TradeBar> selector = null)
1856  {
1857  var name = CreateIndicatorName(symbol, $"STO({period},{kPeriod},{dPeriod})", resolution);
1858  var stochastic = new Stochastic(name, period, kPeriod, dPeriod);
1859  InitializeIndicator(symbol, stochastic, resolution, selector);
1860 
1861  return stochastic;
1862  }
1863 
1864  /// <summary>
1865  /// Overload short hand to create a new Stochastic indicator; defaulting to the 3 period for dStoch
1866  /// </summary>
1867  /// <param name="symbol">The symbol whose stochastic we seek</param>
1868  /// <param name="resolution">The resolution.</param>
1869  /// <param name="period">The period of the stochastic. Normally 14</param>
1870  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
1871  /// <returns>Stochastic indicator for the requested symbol.</returns>
1872  [DocumentationAttribute(Indicators)]
1873  public Stochastic STO(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, TradeBar> selector = null)
1874  {
1875  return STO(symbol, period, period, 3, resolution, selector);
1876  }
1877 
1878  /// <summary>
1879  /// Creates a new Sum indicator.
1880  /// </summary>
1881  /// <param name="symbol">The symbol whose Sum we want</param>
1882  /// <param name="period">The period over which to compute the Sum</param>
1883  /// <param name="resolution">The resolution</param>
1884  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
1885  /// <returns>The Sum indicator for the requested symbol over the specified period</returns>
1886  [DocumentationAttribute(Indicators)]
1887  public Sum SUM(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
1888  {
1889  var name = CreateIndicatorName(symbol, $"SUM({period})", resolution);
1890  var sum = new Sum(name, period);
1891  InitializeIndicator(symbol, sum, resolution, selector);
1892 
1893  return sum;
1894  }
1895 
1896  /// <summary>
1897  /// Creates Swiss Army Knife transformation for the symbol. The indicator will be automatically
1898  /// updated on the given resolution.
1899  /// </summary>
1900  /// <param name="symbol">The symbol to use for calculations</param>
1901  /// <param name="period">The period of the calculation</param>
1902  /// <param name="delta">The delta scale of the BandStop or BandPass</param>
1903  /// <param name="tool">The tool os the Swiss Army Knife</param>
1904  /// <param name="resolution">The resolution</param>
1905  /// <param name="selector">elects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
1906  /// <returns>The calculation using the given tool</returns>
1907  [DocumentationAttribute(Indicators)]
1908  public SwissArmyKnife SWISS(Symbol symbol, int period, double delta, SwissArmyKnifeTool tool, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
1909  {
1910  var name = CreateIndicatorName(symbol, $"SWISS({period},{delta},{tool})", resolution);
1911  var swissArmyKnife = new SwissArmyKnife(name, period, delta, tool);
1912  InitializeIndicator(symbol, swissArmyKnife, resolution, selector);
1913 
1914  return swissArmyKnife;
1915  }
1916 
1917  /// <summary>
1918  /// Creates a new Theta indicator for the symbol The indicator will be automatically
1919  /// updated on the symbol's subscription resolution
1920  /// </summary>
1921  /// <param name="symbol">The option symbol whose values we want as an indicator</param>
1922  /// <param name="mirrorOption">The mirror option for parity calculation</param>
1923  /// <param name="riskFreeRate">The risk free rate</param>
1924  /// <param name="dividendYield">The dividend yield</param>
1925  /// <param name="optionModel">The option pricing model used to estimate Theta</param>
1926  /// <param name="ivModel">The option pricing model used to estimate IV</param>
1927  /// <param name="resolution">The desired resolution of the data</param>
1928  /// <returns>A new Theta indicator for the specified symbol</returns>
1929  [DocumentationAttribute(Indicators)]
1930  public Theta T(Symbol symbol, Symbol mirrorOption = null, decimal? riskFreeRate = null, decimal? dividendYield = null, OptionPricingModelType optionModel = OptionPricingModelType.BlackScholes,
1931  OptionPricingModelType? ivModel = null, Resolution? resolution = null)
1932  {
1933  var name = InitializeOptionIndicator<Theta>(symbol, out var riskFreeRateModel, out var dividendYieldModel, riskFreeRate, dividendYield, optionModel, resolution);
1934 
1935  var theta = new Theta(name, symbol, riskFreeRateModel, dividendYieldModel, mirrorOption, optionModel, ivModel);
1936  RegisterIndicator(symbol, theta, ResolveConsolidator(symbol, resolution, typeof(QuoteBar)));
1937  RegisterIndicator(symbol.Underlying, theta, ResolveConsolidator(symbol.Underlying, resolution));
1938  if (mirrorOption != null)
1939  {
1940  RegisterIndicator(mirrorOption, theta, ResolveConsolidator(mirrorOption, resolution, typeof(QuoteBar)));
1941  }
1942  return theta;
1943  }
1944 
1945  /// <summary>
1946  /// Creates a new Theta indicator for the symbol The indicator will be automatically
1947  /// updated on the symbol's subscription resolution
1948  /// </summary>
1949  /// <param name="symbol">The option symbol whose values we want as an indicator</param>
1950  /// <param name="mirrorOption">The mirror option for parity calculation</param>
1951  /// <param name="riskFreeRate">The risk free rate</param>
1952  /// <param name="dividendYield">The dividend yield</param>
1953  /// <param name="optionModel">The option pricing model used to estimate Theta</param>
1954  /// <param name="ivModel">The option pricing model used to estimate IV</param>
1955  /// <param name="resolution">The desired resolution of the data</param>
1956  /// <returns>A new Theta indicator for the specified symbol</returns>
1957  [DocumentationAttribute(Indicators)]
1958  public Theta Θ(Symbol symbol, Symbol mirrorOption = null, decimal? riskFreeRate = null, decimal? dividendYield = null, OptionPricingModelType optionModel = OptionPricingModelType.BlackScholes,
1959  OptionPricingModelType? ivModel = null, Resolution? resolution = null)
1960  {
1961  return T(symbol, mirrorOption, riskFreeRate, dividendYield, optionModel, ivModel, resolution);
1962  }
1963 
1964  /// <summary>
1965  /// Creates a new T3MovingAverage indicator.
1966  /// </summary>
1967  /// <param name="symbol">The symbol whose T3 we want</param>
1968  /// <param name="period">The period over which to compute the T3</param>
1969  /// <param name="volumeFactor">The volume factor to be used for the T3 (value must be in the [0,1] range, defaults to 0.7)</param>
1970  /// <param name="resolution">The resolution</param>
1971  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
1972  /// <returns>The T3MovingAverage indicator for the requested symbol over the specified period</returns>
1973  [DocumentationAttribute(Indicators)]
1974  public T3MovingAverage T3(Symbol symbol, int period, decimal volumeFactor = 0.7m, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
1975  {
1976  var name = CreateIndicatorName(symbol, $"T3({period},{volumeFactor})", resolution);
1977  var t3MovingAverage = new T3MovingAverage(name, period, volumeFactor);
1978  InitializeIndicator(symbol, t3MovingAverage, resolution, selector);
1979 
1980  return t3MovingAverage;
1981  }
1982 
1983  /// <summary>
1984  /// Creates a new TripleExponentialMovingAverage indicator.
1985  /// </summary>
1986  /// <param name="symbol">The symbol whose TEMA we want</param>
1987  /// <param name="period">The period over which to compute the TEMA</param>
1988  /// <param name="resolution">The resolution</param>
1989  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
1990  /// <returns>The TripleExponentialMovingAverage indicator for the requested symbol over the specified period</returns>
1991  [DocumentationAttribute(Indicators)]
1992  public TripleExponentialMovingAverage TEMA(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
1993  {
1994  var name = CreateIndicatorName(symbol, $"TEMA({period})", resolution);
1995  var tripleExponentialMovingAverage = new TripleExponentialMovingAverage(name, period);
1996  InitializeIndicator(symbol, tripleExponentialMovingAverage, resolution, selector);
1997 
1998  return tripleExponentialMovingAverage;
1999  }
2000 
2001  /// <summary>
2002  /// Creates a TrueStrengthIndex indicator for the symbol. The indicator will be automatically
2003  /// updated on the given resolution.
2004  /// </summary>
2005  /// <param name="symbol">The symbol whose TSI we want</param>
2006  /// <param name="shortTermPeriod">Period used for the first price change smoothing</param>
2007  /// <param name="longTermPeriod">Period used for the second (double) price change smoothing</param>
2008  /// <param name="signalPeriod">The signal period</param>
2009  /// <param name="signalType">The type of moving average to use for the signal</param>
2010  /// <param name="resolution">The resolution</param>
2011  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
2012  /// <returns>The TrueStrengthIndex indicator for the given parameters</returns>
2013  [DocumentationAttribute(Indicators)]
2014  public TrueStrengthIndex TSI(Symbol symbol, int longTermPeriod = 25, int shortTermPeriod = 13, int signalPeriod = 7,
2015  MovingAverageType signalType = MovingAverageType.Exponential, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
2016  {
2017  var name = CreateIndicatorName(symbol, $"TSI({longTermPeriod},{shortTermPeriod},{signalPeriod})", resolution);
2018  var trueStrengthIndex = new TrueStrengthIndex(name, longTermPeriod, shortTermPeriod, signalPeriod, signalType);
2019  InitializeIndicator(symbol, trueStrengthIndex, resolution, selector);
2020 
2021  return trueStrengthIndex;
2022  }
2023 
2024  /// <summary>
2025  /// Creates a new TrueRange indicator.
2026  /// </summary>
2027  /// <param name="symbol">The symbol whose TR we want</param>
2028  /// <param name="resolution">The resolution</param>
2029  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
2030  /// <returns>The TrueRange indicator for the requested symbol.</returns>
2031  [DocumentationAttribute(Indicators)]
2032  public TrueRange TR(Symbol symbol, Resolution? resolution = null, Func<IBaseData, IBaseDataBar> selector = null)
2033  {
2034  var name = CreateIndicatorName(symbol, "TR", resolution);
2035  var trueRange = new TrueRange(name);
2036  InitializeIndicator(symbol, trueRange, resolution, selector);
2037 
2038  return trueRange;
2039  }
2040 
2041  /// <summary>
2042  /// Creates a new TriangularMovingAverage indicator.
2043  /// </summary>
2044  /// <param name="symbol">The symbol whose TRIMA we want</param>
2045  /// <param name="period">The period over which to compute the TRIMA</param>
2046  /// <param name="resolution">The resolution</param>
2047  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
2048  /// <returns>The TriangularMovingAverage indicator for the requested symbol over the specified period</returns>
2049  [DocumentationAttribute(Indicators)]
2050  public TriangularMovingAverage TRIMA(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
2051  {
2052  var name = CreateIndicatorName(symbol, $"TRIMA({period})", resolution);
2053  var triangularMovingAverage = new TriangularMovingAverage(name, period);
2054  InitializeIndicator(symbol, triangularMovingAverage, resolution, selector);
2055 
2056  return triangularMovingAverage;
2057  }
2058 
2059  /// <summary>
2060  /// Creates a new Trix indicator.
2061  /// </summary>
2062  /// <param name="symbol">The symbol whose TRIX we want</param>
2063  /// <param name="period">The period over which to compute the TRIX</param>
2064  /// <param name="resolution">The resolution</param>
2065  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
2066  /// <returns>The Trix indicator for the requested symbol over the specified period</returns>
2067  [DocumentationAttribute(Indicators)]
2068  public Trix TRIX(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
2069  {
2070  var name = CreateIndicatorName(symbol, $"TRIX({period})", resolution);
2071  var trix = new Trix(name, period);
2072  InitializeIndicator(symbol, trix, resolution, selector);
2073 
2074  return trix;
2075  }
2076 
2077  /// <summary>
2078  /// Creates a new UltimateOscillator indicator.
2079  /// </summary>
2080  /// <param name="symbol">The symbol whose ULTOSC we want</param>
2081  /// <param name="period1">The first period over which to compute the ULTOSC</param>
2082  /// <param name="period2">The second period over which to compute the ULTOSC</param>
2083  /// <param name="period3">The third period over which to compute the ULTOSC</param>
2084  /// <param name="resolution">The resolution</param>
2085  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
2086  /// <returns>The UltimateOscillator indicator for the requested symbol over the specified period</returns>
2087  [DocumentationAttribute(Indicators)]
2088  public UltimateOscillator ULTOSC(Symbol symbol, int period1, int period2, int period3, Resolution? resolution = null, Func<IBaseData, IBaseDataBar> selector = null)
2089  {
2090  var name = CreateIndicatorName(symbol, $"ULTOSC({period1},{period2},{period3})", resolution);
2091  var ultimateOscillator = new UltimateOscillator(name, period1, period2, period3);
2092  InitializeIndicator(symbol, ultimateOscillator, resolution, selector);
2093 
2094  return ultimateOscillator;
2095  }
2096 
2097  /// <summary>
2098  /// Creates a new Vega indicator for the symbol The indicator will be automatically
2099  /// updated on the symbol's subscription resolution
2100  /// </summary>
2101  /// <param name="symbol">The option symbol whose values we want as an indicator</param>
2102  /// <param name="mirrorOption">The mirror option for parity calculation</param>
2103  /// <param name="riskFreeRate">The risk free rate</param>
2104  /// <param name="dividendYield">The dividend yield</param>
2105  /// <param name="optionModel">The option pricing model used to estimate Vega</param>
2106  /// <param name="ivModel">The option pricing model used to estimate IV</param>
2107  /// <param name="resolution">The desired resolution of the data</param>
2108  /// <returns>A new Vega indicator for the specified symbol</returns>
2109  [DocumentationAttribute(Indicators)]
2110  public Vega V(Symbol symbol, Symbol mirrorOption = null, decimal? riskFreeRate = null, decimal? dividendYield = null, OptionPricingModelType optionModel = OptionPricingModelType.BlackScholes,
2111  OptionPricingModelType? ivModel = null, Resolution? resolution = null)
2112  {
2113  var name = InitializeOptionIndicator<Vega>(symbol, out var riskFreeRateModel, out var dividendYieldModel, riskFreeRate, dividendYield, optionModel, resolution);
2114 
2115  var vega = new Vega(name, symbol, riskFreeRateModel, dividendYieldModel, mirrorOption, optionModel, ivModel);
2116  RegisterIndicator(symbol, vega, ResolveConsolidator(symbol, resolution, typeof(QuoteBar)));
2117  RegisterIndicator(symbol.Underlying, vega, ResolveConsolidator(symbol.Underlying, resolution));
2118  if (mirrorOption != null)
2119  {
2120  RegisterIndicator(mirrorOption, vega, ResolveConsolidator(mirrorOption, resolution, typeof(QuoteBar)));
2121  }
2122  return vega;
2123  }
2124 
2125  /// <summary>
2126  /// Creates a new Chande's Variable Index Dynamic Average indicator.
2127  /// </summary>
2128  /// <param name="symbol">The symbol whose VIDYA we want</param>
2129  /// <param name="period">The period over which to compute the VIDYA</param>
2130  /// <param name="resolution">The resolution</param>
2131  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
2132  /// <returns>The VariableIndexDynamicAverage indicator for the requested symbol over the specified period</returns>
2133  [DocumentationAttribute(Indicators)]
2134  public VariableIndexDynamicAverage VIDYA(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
2135  {
2136  var name = CreateIndicatorName(symbol, $"VIDYA({period})", resolution);
2137  var variableIndexDynamicAverage = new VariableIndexDynamicAverage(name, period);
2138  InitializeIndicator(symbol, variableIndexDynamicAverage, resolution, selector);
2139 
2140  return variableIndexDynamicAverage;
2141  }
2142 
2143  /// <summary>
2144  /// Creates a new Variance indicator. This will return the population variance of samples over the specified period.
2145  /// </summary>
2146  /// <param name="symbol">The symbol whose VAR we want</param>
2147  /// <param name="period">The period over which to compute the VAR</param>
2148  /// <param name="resolution">The resolution</param>
2149  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
2150  /// <returns>The Variance indicator for the requested symbol over the specified period</returns>
2151  [DocumentationAttribute(Indicators)]
2152  [Obsolete("'VAR' is obsolete please use 'V' instead")]
2153  public Variance VAR(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
2154  {
2155  return V(symbol, period, resolution, selector);
2156  }
2157 
2158  /// <summary>
2159  /// Creates a new Variance indicator. This will return the population variance of samples over the specified period.
2160  /// </summary>
2161  /// <param name="symbol">The symbol whose variance we want</param>
2162  /// <param name="period">The period over which to compute the variance</param>
2163  /// <param name="resolution">The resolution</param>
2164  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
2165  /// <returns>The Variance indicator for the requested symbol over the specified period</returns>
2166  [DocumentationAttribute(Indicators)]
2167  public Variance V(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
2168  {
2169  var name = CreateIndicatorName(symbol, $"V({period})", resolution);
2170  var variance = new Variance(name, period);
2171  InitializeIndicator(symbol, variance, resolution, selector);
2172 
2173  return variance;
2174  }
2175 
2176  /// <summary>
2177  /// Creates a new ValueAtRisk indicator.
2178  /// </summary>
2179  /// <param name="symbol">The symbol whose VAR we want</param>
2180  /// <param name="period">The period over which to compute the VAR</param>
2181  /// <param name="confidenceLevel">The confidence level for Value at risk calculation</param>
2182  /// <param name="resolution">The resolution</param>
2183  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
2184  /// <returns>The ValueAtRisk indicator for the requested Symbol, lookback period, and confidence level</returns>
2185  public ValueAtRisk VAR(Symbol symbol, int period, double confidenceLevel, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
2186  {
2187  var name = CreateIndicatorName(symbol, $"VAR({period},{confidenceLevel})", resolution);
2188  var valueAtRisk = new ValueAtRisk(name, period, confidenceLevel);
2189  InitializeIndicator(symbol, valueAtRisk, resolution, selector);
2190 
2191  return valueAtRisk;
2192  }
2193 
2194  /// <summary>
2195  /// Creates an VolumeWeightedAveragePrice (VWAP) indicator for the symbol. The indicator will be automatically
2196  /// updated on the given resolution.
2197  /// </summary>
2198  /// <param name="symbol">The symbol whose VWAP we want</param>
2199  /// <param name="period">The period of the VWAP</param>
2200  /// <param name="resolution">The resolution</param>
2201  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
2202  /// <returns>The VolumeWeightedAveragePrice for the given parameters</returns>
2203  [DocumentationAttribute(Indicators)]
2204  public VolumeWeightedAveragePriceIndicator VWAP(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, TradeBar> selector = null)
2205  {
2206  var name = CreateIndicatorName(symbol, $"VWAP({period})", resolution);
2207  var volumeWeightedAveragePriceIndicator = new VolumeWeightedAveragePriceIndicator(name, period);
2208  InitializeIndicator(symbol, volumeWeightedAveragePriceIndicator, resolution, selector);
2209 
2210  return volumeWeightedAveragePriceIndicator;
2211  }
2212 
2213  /// <summary>
2214  /// Creates the canonical VWAP indicator that resets each day. The indicator will be automatically
2215  /// updated on the security's configured resolution.
2216  /// </summary>
2217  /// <param name="symbol">The symbol whose VWAP we want</param>
2218  /// <returns>The IntradayVWAP for the specified symbol</returns>
2219  [DocumentationAttribute(Indicators)]
2220  public IntradayVwap VWAP(Symbol symbol)
2221  {
2222  var name = CreateIndicatorName(symbol, "VWAP", null);
2223  var intradayVwap = new IntradayVwap(name);
2224  RegisterIndicator(symbol, intradayVwap);
2225  return intradayVwap;
2226  }
2227 
2228  /// <summary>
2229  /// Creates a new Williams %R indicator. This will compute the percentage change of
2230  /// the current closing price in relation to the high and low of the past N periods.
2231  /// The indicator will be automatically updated on the given resolution.
2232  /// </summary>
2233  /// <param name="symbol">The symbol whose Williams %R we want</param>
2234  /// <param name="period">The period over which to compute the Williams %R</param>
2235  /// <param name="resolution">The resolution</param>
2236  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
2237  /// <returns>The Williams %R indicator for the requested symbol over the specified period</returns>
2238  [DocumentationAttribute(Indicators)]
2239  public WilliamsPercentR WILR(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, IBaseDataBar> selector = null)
2240  {
2241  var name = CreateIndicatorName(symbol, $"WILR({period})", resolution);
2242  var williamsPercentR = new WilliamsPercentR(name, period);
2243  InitializeIndicator(symbol, williamsPercentR, resolution, selector);
2244 
2245  return williamsPercentR;
2246  }
2247 
2248  /// <summary>
2249  /// Creates a WilderMovingAverage indicator for the symbol.
2250  /// The indicator will be automatically updated on the given resolution.
2251  /// </summary>
2252  /// <param name="symbol">The symbol whose WMA we want</param>
2253  /// <param name="period">The period of the WMA</param>
2254  /// <param name="resolution">The resolution</param>
2255  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
2256  /// <returns>The WilderMovingAverage for the given parameters</returns>
2257  /// <remarks>WWMA for Welles Wilder Moving Average</remarks>
2258  [DocumentationAttribute(Indicators)]
2259  public WilderMovingAverage WWMA(Symbol symbol, int period, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
2260  {
2261  var name = CreateIndicatorName(symbol, $"WWMA({period})", resolution);
2262  var wilderMovingAverage = new WilderMovingAverage(name, period);
2263  InitializeIndicator(symbol, wilderMovingAverage, resolution, selector);
2264 
2265  return wilderMovingAverage;
2266  }
2267 
2268  /// <summary>
2269  /// Creates a Wilder Swing Index (SI) indicator for the symbol.
2270  /// The indicator will be automatically updated on the given resolution.
2271  /// </summary>
2272  /// <param name="symbol">The symbol whose SI we want</param>
2273  /// <param name="limitMove">The maximum daily change in price for the SI</param>
2274  /// <param name="resolution">The resolution</param>
2275  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
2276  /// <returns>The WilderSwingIndex for the given parameters</returns>
2277  /// <remarks>SI for Wilder Swing Index</remarks>
2278  [DocumentationAttribute(Indicators)]
2279  public WilderSwingIndex SI(Symbol symbol, decimal limitMove, Resolution? resolution = Resolution.Daily,
2280  Func<IBaseData, TradeBar> selector = null)
2281  {
2282  var name = CreateIndicatorName(symbol, "SI", resolution);
2283  var si = new WilderSwingIndex(name, limitMove);
2284  InitializeIndicator(symbol, si, resolution, selector);
2285 
2286  return si;
2287  }
2288 
2289  /// <summary>
2290  /// Creates a Wilder Accumulative Swing Index (ASI) indicator for the symbol.
2291  /// The indicator will be automatically updated on the given resolution.
2292  /// </summary>
2293  /// <param name="symbol">The symbol whose ASI we want</param>
2294  /// <param name="limitMove">The maximum daily change in price for the ASI</param>
2295  /// <param name="resolution">The resolution</param>
2296  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
2297  /// <returns>The WilderAccumulativeSwingIndex for the given parameters</returns>
2298  /// <remarks>ASI for Wilder Accumulative Swing Index</remarks>
2299  [DocumentationAttribute(Indicators)]
2300  public WilderAccumulativeSwingIndex ASI(Symbol symbol, decimal limitMove, Resolution? resolution = Resolution.Daily,
2301  Func<IBaseData, TradeBar> selector = null)
2302  {
2303  var name = CreateIndicatorName(symbol, "ASI", resolution);
2304  var asi = new WilderAccumulativeSwingIndex(name, limitMove);
2305  InitializeIndicator(symbol, asi, resolution, selector);
2306 
2307  return asi;
2308  }
2309 
2310  /// <summary>
2311  /// Creates a new Arms Index indicator
2312  /// </summary>
2313  /// <param name="symbols">The symbols whose Arms Index we want</param>
2314  /// <param name="resolution">The resolution</param>
2315  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
2316  /// <returns>The Arms Index indicator for the requested symbol over the specified period</returns>
2317  [DocumentationAttribute(Indicators)]
2318  public ArmsIndex TRIN(IEnumerable<Symbol> symbols, Resolution? resolution = null, Func<IBaseData, TradeBar> selector = null)
2319  {
2320  return TRIN(symbols.ToArray(), resolution, selector);
2321  }
2322 
2323  /// <summary>
2324  /// Creates a new Arms Index indicator
2325  /// </summary>
2326  /// <param name="symbols">The symbols whose Arms Index we want</param>
2327  /// <param name="resolution">The resolution</param>
2328  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
2329  /// <returns>The Arms Index indicator for the requested symbol over the specified period</returns>
2330  [DocumentationAttribute(Indicators)]
2331  public ArmsIndex TRIN(Symbol[] symbols, Resolution? resolution = null, Func<IBaseData, TradeBar> selector = null)
2332  {
2333  var name = CreateIndicatorName(QuantConnect.Symbol.None, "TRIN", resolution ?? GetSubscription(symbols.First()).Resolution);
2334  var trin = new ArmsIndex(name);
2335  foreach (var symbol in symbols)
2336  {
2337  trin.Add(symbol);
2338  InitializeIndicator(symbol, trin, resolution, selector);
2339  }
2340 
2341  return trin;
2342  }
2343 
2344  /// <summary>
2345  /// Creates a new Advance/Decline Ratio indicator
2346  /// </summary>
2347  /// <param name="symbols">The symbols whose A/D Ratio we want</param>
2348  /// <param name="resolution">The resolution</param>
2349  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
2350  /// <returns>The Advance/Decline Ratio indicator for the requested symbol over the specified period</returns>
2351  [DocumentationAttribute(Indicators)]
2352  public AdvanceDeclineRatio ADR(IEnumerable<Symbol> symbols, Resolution? resolution = null, Func<IBaseData, TradeBar> selector = null)
2353  {
2354  var name = CreateIndicatorName(QuantConnect.Symbol.None, "A/D Ratio", resolution ?? GetSubscription(symbols.First()).Resolution);
2355  var adr = new AdvanceDeclineRatio(name);
2356  foreach (var symbol in symbols)
2357  {
2358  adr.Add(symbol);
2359  InitializeIndicator(symbol, adr, resolution, selector);
2360  }
2361 
2362  return adr;
2363  }
2364 
2365  /// <summary>
2366  /// Creates a new Advance/Decline Volume Ratio indicator
2367  /// </summary>
2368  /// <param name="symbols">The symbol whose A/D Volume Rate we want</param>
2369  /// <param name="resolution">The resolution</param>
2370  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
2371  /// <returns>The Advance/Decline Volume Ratio indicator for the requested symbol over the specified period</returns>
2372  [DocumentationAttribute(Indicators)]
2373  public AdvanceDeclineVolumeRatio ADVR(IEnumerable<Symbol> symbols, Resolution? resolution = null, Func<IBaseData, TradeBar> selector = null)
2374  {
2375  var name = CreateIndicatorName(QuantConnect.Symbol.None, "A/D Volume Rate", resolution ?? GetSubscription(symbols.First()).Resolution);
2376  var advr = new AdvanceDeclineVolumeRatio(name);
2377  foreach (var symbol in symbols)
2378  {
2379  advr.Add(symbol);
2380  InitializeIndicator(symbol, advr, resolution, selector);
2381  }
2382 
2383  return advr;
2384  }
2385 
2386  /// <summary>
2387  /// Creates a new Advance/Decline Difference indicator
2388  /// </summary>
2389  /// <param name="symbols">The symbols whose A/D Difference we want</param>
2390  /// <param name="resolution">The resolution</param>
2391  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
2392  /// <returns>The Advance/Decline Difference indicator for the requested symbol over the specified period</returns>
2393  [DocumentationAttribute(Indicators)]
2394  public AdvanceDeclineDifference ADDIFF(IEnumerable<Symbol> symbols, Resolution? resolution = null, Func<IBaseData, TradeBar> selector = null)
2395  {
2396  var name = CreateIndicatorName(QuantConnect.Symbol.None, "A/D Difference", resolution ?? GetSubscription(symbols.First()).Resolution);
2397  var adDiff = new AdvanceDeclineDifference(name);
2398  foreach (var symbol in symbols)
2399  {
2400  adDiff.Add(symbol);
2401  InitializeIndicator(symbol, adDiff, resolution, selector);
2402  }
2403 
2404  return adDiff;
2405  }
2406 
2407  /// <summary>
2408  /// Creates a new McClellan Oscillator indicator
2409  /// </summary>
2410  /// <param name="symbols">The symbols whose McClellan Oscillator we want</param>
2411  /// <param name="fastPeriod">Fast period EMA of advance decline difference</param>
2412  /// <param name="slowPeriod">Slow period EMA of advance decline difference</param>
2413  /// <param name="resolution">The resolution</param>
2414  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
2415  /// <returns>The McClellan Oscillator indicator for the requested symbol over the specified period</returns>
2416  [DocumentationAttribute(Indicators)]
2417  public McClellanOscillator MOSC(IEnumerable<Symbol> symbols, int fastPeriod = 19, int slowPeriod = 39, Resolution? resolution = null, Func<IBaseData, TradeBar> selector = null)
2418  {
2419  return MOSC(symbols.ToArray(), fastPeriod, slowPeriod, resolution, selector);
2420  }
2421 
2422  /// <summary>
2423  /// Creates a new McClellan Oscillator indicator
2424  /// </summary>
2425  /// <param name="symbols">The symbols whose McClellan Oscillator we want</param>
2426  /// <param name="fastPeriod">Fast period EMA of advance decline difference</param>
2427  /// <param name="slowPeriod">Slow period EMA of advance decline difference</param>
2428  /// <param name="resolution">The resolution</param>
2429  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
2430  /// <returns>The McClellan Oscillator indicator for the requested symbol over the specified period</returns>
2431  [DocumentationAttribute(Indicators)]
2432  public McClellanOscillator MOSC(Symbol[] symbols, int fastPeriod = 19, int slowPeriod = 39, Resolution? resolution = null, Func<IBaseData, TradeBar> selector = null)
2433  {
2434  var name = CreateIndicatorName(QuantConnect.Symbol.None, $"MO({fastPeriod},{slowPeriod})", resolution ?? GetSubscription(symbols.First()).Resolution);
2435  var mosc = new McClellanOscillator(name, fastPeriod, slowPeriod);
2436  foreach (var symbol in symbols)
2437  {
2438  mosc.Add(symbol);
2439  InitializeIndicator(symbol, mosc, resolution, selector);
2440  }
2441 
2442  return mosc;
2443  }
2444 
2445  /// <summary>
2446  /// Creates a new McClellan Summation Index indicator
2447  /// </summary>
2448  /// <param name="symbols">The symbols whose McClellan Summation Index we want</param>
2449  /// <param name="fastPeriod">Fast period EMA of advance decline difference</param>
2450  /// <param name="slowPeriod">Slow period EMA of advance decline difference</param>
2451  /// <param name="resolution">The resolution</param>
2452  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
2453  /// <returns>The McClellan Summation Index indicator for the requested symbol over the specified period</returns>
2454  [DocumentationAttribute(Indicators)]
2455  public McClellanSummationIndex MSI(IEnumerable<Symbol> symbols, int fastPeriod = 19, int slowPeriod = 39, Resolution? resolution = null, Func<IBaseData, TradeBar> selector = null)
2456  {
2457  return MSI(symbols.ToArray(), fastPeriod, slowPeriod, resolution, selector);
2458  }
2459 
2460  /// <summary>
2461  /// Creates a new McClellan Summation Index indicator
2462  /// </summary>
2463  /// <param name="symbols">The symbols whose McClellan Summation Index we want</param>
2464  /// <param name="fastPeriod">Fast period EMA of advance decline difference</param>
2465  /// <param name="slowPeriod">Slow period EMA of advance decline difference</param>
2466  /// <param name="resolution">The resolution</param>
2467  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar</param>
2468  /// <returns>The McClellan Summation Index indicator for the requested symbol over the specified period</returns>
2469  [DocumentationAttribute(Indicators)]
2470  public McClellanSummationIndex MSI(Symbol[] symbols, int fastPeriod = 19, int slowPeriod = 39, Resolution? resolution = null, Func<IBaseData, TradeBar> selector = null)
2471  {
2472  var name = CreateIndicatorName(QuantConnect.Symbol.None, $"MSI({fastPeriod},{slowPeriod})", resolution ?? GetSubscription(symbols.First()).Resolution);
2473  var msi = new McClellanSummationIndex(name, fastPeriod, slowPeriod);
2474  foreach (var symbol in symbols)
2475  {
2476  msi.Add(symbol);
2477  InitializeIndicator(symbol, msi, resolution, selector);
2478  }
2479 
2480  return msi;
2481  }
2482 
2483  /// <summary>
2484  /// Creates a new name for an indicator created with the convenience functions (SMA, EMA, ect...)
2485  /// </summary>
2486  /// <param name="symbol">The symbol this indicator is registered to</param>
2487  /// <param name="type">The indicator type, for example, 'SMA(5)'</param>
2488  /// <param name="resolution">The resolution requested</param>
2489  /// <returns>A unique for the given parameters</returns>
2490  [DocumentationAttribute(Indicators)]
2491  public string CreateIndicatorName(Symbol symbol, FormattableString type, Resolution? resolution)
2492  {
2493  return CreateIndicatorName(symbol, Invariant(type), resolution);
2494  }
2495 
2496  /// <summary>
2497  /// Creates a new name for an indicator created with the convenience functions (SMA, EMA, ect...)
2498  /// </summary>
2499  /// <param name="symbol">The symbol this indicator is registered to</param>
2500  /// <param name="type">The indicator type, for example, 'SMA(5)'</param>
2501  /// <param name="resolution">The resolution requested</param>
2502  /// <returns>A unique for the given parameters</returns>
2503  [DocumentationAttribute(Indicators)]
2504  public string CreateIndicatorName(Symbol symbol, string type, Resolution? resolution)
2505  {
2506  var symbolIsNotEmpty = symbol != QuantConnect.Symbol.None && symbol != QuantConnect.Symbol.Empty;
2507 
2508  if (!resolution.HasValue && symbolIsNotEmpty)
2509  {
2510  resolution = GetSubscription(symbol).Resolution;
2511  }
2512 
2513  var res = string.Empty;
2514  switch (resolution)
2515  {
2516  case Resolution.Tick:
2517  res = "tick";
2518  break;
2519 
2520  case Resolution.Second:
2521  res = "sec";
2522  break;
2523 
2524  case Resolution.Minute:
2525  res = "min";
2526  break;
2527 
2528  case Resolution.Hour:
2529  res = "hr";
2530  break;
2531 
2532  case Resolution.Daily:
2533  res = "day";
2534  break;
2535 
2536  case null:
2537  break;
2538 
2539  default:
2540  throw new ArgumentOutOfRangeException(nameof(resolution), resolution, "resolution parameter is out of range.");
2541  }
2542 
2543  var parts = new List<string>();
2544 
2545  if (symbolIsNotEmpty)
2546  {
2547  parts.Add(symbol.ToString());
2548  }
2549  parts.Add(res);
2550 
2551  return Invariant($"{type}({string.Join("_", parts)})").Replace(")(", ",");
2552  }
2553 
2554  /// <summary>
2555  /// Gets the SubscriptionDataConfig for the specified symbol and tick type
2556  /// </summary>
2557  /// <exception cref="InvalidOperationException">Thrown if no configuration is found for the requested symbol</exception>
2558  /// <param name="symbol">The symbol to retrieve configuration for</param>
2559  /// <param name="tickType">The tick type of the subscription to get. If null, will use the first ordered by TickType</param>
2560  /// <returns>The SubscriptionDataConfig for the specified symbol</returns>
2561  private SubscriptionDataConfig GetSubscription(Symbol symbol, TickType? tickType = null)
2562  {
2563  SubscriptionDataConfig subscription;
2564  try
2565  {
2566  // deterministic ordering is required here
2568  .GetSubscriptionDataConfigs(symbol)
2569  // make sure common lean data types are at the bottom
2570  .OrderByDescending(x => LeanData.IsCommonLeanDataType(x.Type))
2571  .ThenBy(x => x.TickType)
2572  .ToList();
2573 
2574  // find our subscription
2575  subscription = subscriptions.FirstOrDefault(x => tickType == null || tickType == x.TickType);
2576  if (subscription == null)
2577  {
2578  // if we can't locate the exact subscription by tick type just grab the first one we find
2579  subscription = subscriptions.First();
2580  }
2581  }
2582  catch (InvalidOperationException)
2583  {
2584  // this will happen if we did not find the subscription, let's give the user a decent error message
2585  throw new Exception($"Please register to receive data for symbol \'{symbol}\' using the AddSecurity() function.");
2586  }
2587  return subscription;
2588  }
2589 
2590  /// <summary>
2591  /// Creates and registers a new consolidator to receive automatic updates at the specified resolution as well as configures
2592  /// the indicator to receive updates from the consolidator.
2593  /// </summary>
2594  /// <param name="symbol">The symbol to register against</param>
2595  /// <param name="indicator">The indicator to receive data from the consolidator</param>
2596  /// <param name="resolution">The resolution at which to send data to the indicator, null to use the same resolution as the subscription</param>
2597  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
2598  [DocumentationAttribute(ConsolidatingData)]
2599  [DocumentationAttribute(Indicators)]
2600  public void RegisterIndicator(Symbol symbol, IndicatorBase<IndicatorDataPoint> indicator, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
2601  {
2602  RegisterIndicator(symbol, indicator, ResolveConsolidator(symbol, resolution), selector ?? (x => x.Value));
2603  }
2604 
2605  /// <summary>
2606  /// Creates and registers a new consolidator to receive automatic updates at the specified resolution as well as configures
2607  /// the indicator to receive updates from the consolidator.
2608  /// </summary>
2609  /// <param name="symbol">The symbol to register against</param>
2610  /// <param name="indicator">The indicator to receive data from the consolidator</param>
2611  /// <param name="resolution">The resolution at which to send data to the indicator, null to use the same resolution as the subscription</param>
2612  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
2613  [DocumentationAttribute(ConsolidatingData)]
2614  [DocumentationAttribute(Indicators)]
2615  public void RegisterIndicator(Symbol symbol, IndicatorBase<IndicatorDataPoint> indicator, TimeSpan? resolution = null, Func<IBaseData, decimal> selector = null)
2616  {
2617  RegisterIndicator(symbol, indicator, ResolveConsolidator(symbol, resolution), selector ?? (x => x.Value));
2618  }
2619 
2620  /// <summary>
2621  /// Registers the consolidator to receive automatic updates as well as configures the indicator to receive updates
2622  /// from the consolidator.
2623  /// </summary>
2624  /// <param name="symbol">The symbol to register against</param>
2625  /// <param name="indicator">The indicator to receive data from the consolidator</param>
2626  /// <param name="consolidator">The consolidator to receive raw subscription data</param>
2627  /// <param name="selector">Selects a value from the BaseData send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
2628  [DocumentationAttribute(ConsolidatingData)]
2629  [DocumentationAttribute(Indicators)]
2630  public void RegisterIndicator(Symbol symbol, IndicatorBase<IndicatorDataPoint> indicator, IDataConsolidator consolidator, Func<IBaseData, decimal> selector = null)
2631  {
2632  // default our selector to the Value property on BaseData
2633  selector = selector ?? (x => x.Value);
2634 
2635  RegisterConsolidator(indicator, symbol, consolidator);
2636 
2637  // attach to the DataConsolidated event so it updates our indicator
2638  consolidator.DataConsolidated += (sender, consolidated) =>
2639  {
2640  var value = selector(consolidated);
2641  indicator.Update(new IndicatorDataPoint(consolidated.Symbol, consolidated.EndTime, value));
2642  };
2643  }
2644 
2645  /// <summary>
2646  /// Registers the consolidator to receive automatic updates as well as configures the indicator to receive updates
2647  /// from the consolidator.
2648  /// </summary>
2649  /// <param name="symbol">The symbol to register against</param>
2650  /// <param name="indicator">The indicator to receive data from the consolidator</param>
2651  /// <param name="resolution">The resolution at which to send data to the indicator, null to use the same resolution as the subscription</param>
2652  [DocumentationAttribute(ConsolidatingData)]
2653  [DocumentationAttribute(Indicators)]
2654  public void RegisterIndicator<T>(Symbol symbol, IndicatorBase<T> indicator, Resolution? resolution = null)
2655  where T : IBaseData
2656  {
2657  RegisterIndicator(symbol, indicator, ResolveConsolidator(symbol, resolution, typeof(T)));
2658  }
2659 
2660  /// <summary>
2661  /// Registers the consolidator to receive automatic updates as well as configures the indicator to receive updates
2662  /// from the consolidator.
2663  /// </summary>
2664  /// <param name="symbol">The symbol to register against</param>
2665  /// <param name="indicator">The indicator to receive data from the consolidator</param>
2666  /// <param name="resolution">The resolution at which to send data to the indicator, null to use the same resolution as the subscription</param>
2667  /// <param name="selector">Selects a value from the BaseData send into the indicator, if null defaults to a cast (x => (T)x)</param>
2668  [DocumentationAttribute(ConsolidatingData)]
2669  [DocumentationAttribute(Indicators)]
2670  public void RegisterIndicator<T>(Symbol symbol, IndicatorBase<T> indicator, Resolution? resolution, Func<IBaseData, T> selector)
2671  where T : IBaseData
2672  {
2673  RegisterIndicator(symbol, indicator, ResolveConsolidator(symbol, resolution, typeof(T)), selector);
2674  }
2675 
2676  /// <summary>
2677  /// Registers the consolidator to receive automatic updates as well as configures the indicator to receive updates
2678  /// from the consolidator.
2679  /// </summary>
2680  /// <param name="symbol">The symbol to register against</param>
2681  /// <param name="indicator">The indicator to receive data from the consolidator</param>
2682  /// <param name="resolution">The resolution at which to send data to the indicator, null to use the same resolution as the subscription</param>
2683  /// <param name="selector">Selects a value from the BaseData send into the indicator, if null defaults to a cast (x => (T)x)</param>
2684  [DocumentationAttribute(ConsolidatingData)]
2685  [DocumentationAttribute(Indicators)]
2686  public void RegisterIndicator<T>(Symbol symbol, IndicatorBase<T> indicator, TimeSpan? resolution, Func<IBaseData, T> selector = null)
2687  where T : IBaseData
2688  {
2689  RegisterIndicator(symbol, indicator, ResolveConsolidator(symbol, resolution, typeof(T)), selector);
2690  }
2691 
2692  /// <summary>
2693  /// Registers the consolidator to receive automatic updates as well as configures the indicator to receive updates
2694  /// from the consolidator.
2695  /// </summary>
2696  /// <param name="symbol">The symbol to register against</param>
2697  /// <param name="indicator">The indicator to receive data from the consolidator</param>
2698  /// <param name="consolidator">The consolidator to receive raw subscription data</param>
2699  /// <param name="selector">Selects a value from the BaseData send into the indicator, if null defaults to a cast (x => (T)x)</param>
2700  [DocumentationAttribute(ConsolidatingData)]
2701  [DocumentationAttribute(Indicators)]
2702  public void RegisterIndicator<T>(Symbol symbol, IndicatorBase<T> indicator, IDataConsolidator consolidator, Func<IBaseData, T> selector = null)
2703  where T : IBaseData
2704  {
2705  // assign default using cast
2706  var selectorToUse = selector ?? (x => (T)x);
2707 
2708  RegisterConsolidator(indicator, symbol, consolidator);
2709 
2710  // check the output type of the consolidator and verify we can assign it to T
2711  var type = typeof(T);
2712  if (!type.IsAssignableFrom(consolidator.OutputType))
2713  {
2714  if (type == typeof(IndicatorDataPoint) && selector == null)
2715  {
2716  // if no selector was provided and the indicator input is of 'IndicatorDataPoint', common case, a selector with a direct cast will fail
2717  // so we use a smarter selector as in other API methods
2718  selectorToUse = consolidated => (T)(object)new IndicatorDataPoint(consolidated.Symbol, consolidated.EndTime, consolidated.Value);
2719  }
2720  else
2721  {
2722  throw new ArgumentException($"Type mismatch found between consolidator and indicator for symbol: {symbol}." +
2723  $"Consolidator outputs type {consolidator.OutputType.Name} but indicator expects input type {type.Name}"
2724  );
2725  }
2726  }
2727 
2728  // attach to the DataConsolidated event so it updates our indicator
2729  consolidator.DataConsolidated += (sender, consolidated) =>
2730  {
2731  var value = selectorToUse(consolidated);
2732  indicator.Update(value);
2733  };
2734  }
2735 
2736  /// <summary>
2737  /// Will unregister an indicator and it's associated consolidator instance so they stop receiving data updates
2738  /// </summary>
2739  /// <param name="indicator">The indicator instance to unregister</param>
2740  [DocumentationAttribute(ConsolidatingData)]
2741  [DocumentationAttribute(Indicators)]
2742  public void UnregisterIndicator(IndicatorBase indicator)
2743  {
2744  DeregisterIndicator(indicator);
2745  }
2746 
2747  /// <summary>
2748  /// Will deregister an indicator and it's associated consolidator instance so they stop receiving data updates
2749  /// </summary>
2750  /// <param name="indicator">The indicator instance to deregister</param>
2751  [DocumentationAttribute(ConsolidatingData)]
2752  [DocumentationAttribute(Indicators)]
2753  public void DeregisterIndicator(IndicatorBase indicator)
2754  {
2755  foreach (var consolidator in indicator.Consolidators)
2756  {
2757  SubscriptionManager.RemoveConsolidator(null, consolidator);
2758  }
2759 
2760  indicator.Consolidators.Clear();
2761  }
2762 
2763  /// <summary>
2764  /// Warms up a given indicator with historical data
2765  /// </summary>
2766  /// <param name="symbol">The symbol whose indicator we want</param>
2767  /// <param name="indicator">The indicator we want to warm up</param>
2768  /// <param name="resolution">The resolution</param>
2769  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
2770  [DocumentationAttribute(HistoricalData)]
2771  [DocumentationAttribute(Indicators)]
2772  public void WarmUpIndicator(Symbol symbol, IndicatorBase<IndicatorDataPoint> indicator, Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
2773  {
2774  resolution = GetResolution(symbol, resolution, null);
2775  var period = resolution.Value.ToTimeSpan();
2776  WarmUpIndicator(symbol, indicator, period, selector);
2777  }
2778 
2779  /// <summary>
2780  /// Warms up a given indicator with historical data
2781  /// </summary>
2782  /// <param name="symbol">The symbol whose indicator we want</param>
2783  /// <param name="indicator">The indicator we want to warm up</param>
2784  /// <param name="period">The necessary period to warm up the indicator</param>
2785  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
2786  [DocumentationAttribute(HistoricalData)]
2787  [DocumentationAttribute(Indicators)]
2788  public void WarmUpIndicator(Symbol symbol, IndicatorBase<IndicatorDataPoint> indicator, TimeSpan period, Func<IBaseData, decimal> selector = null)
2789  {
2790  var history = GetIndicatorWarmUpHistory(symbol, indicator, period, out var identityConsolidator);
2791  if (history == Enumerable.Empty<Slice>()) return;
2792 
2793  // assign default using cast
2794  selector ??= (x => x.Value);
2795 
2796  Action<IBaseData> onDataConsolidated = bar =>
2797  {
2798  var input = new IndicatorDataPoint(bar.Symbol, bar.EndTime, selector(bar));
2799  indicator.Update(input);
2800  };
2801 
2802  WarmUpIndicatorImpl(symbol, period, onDataConsolidated, history, identityConsolidator);
2803  }
2804 
2805  /// <summary>
2806  /// Warms up a given indicator with historical data
2807  /// </summary>
2808  /// <param name="symbol">The symbol whose indicator we want</param>
2809  /// <param name="indicator">The indicator we want to warm up</param>
2810  /// <param name="resolution">The resolution</param>
2811  /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
2812  [DocumentationAttribute(HistoricalData)]
2813  [DocumentationAttribute(Indicators)]
2814  public void WarmUpIndicator<T>(Symbol symbol, IndicatorBase<T> indicator, Resolution? resolution = null, Func<IBaseData, T> selector = null)
2815  where T : class, IBaseData
2816  {
2817  resolution = GetResolution(symbol, resolution, typeof(T));
2818  var period = resolution.Value.ToTimeSpan();
2819  WarmUpIndicator(symbol, indicator, period, selector);
2820  }
2821 
2822  /// <summary>
2823  /// Warms up a given indicator with historical data
2824  /// </summary>
2825  /// <param name="symbol">The symbol whose indicator we want</param>
2826  /// <param name="indicator">The indicator we want to warm up</param>
2827  /// <param name="period">The necessary period to warm up the indicator</param>
2828  /// <param name="selector">Selects a value from the BaseData send into the indicator, if null defaults to a cast (x => (T)x)</param>
2829  [DocumentationAttribute(HistoricalData)]
2830  [DocumentationAttribute(Indicators)]
2831  public void WarmUpIndicator<T>(Symbol symbol, IndicatorBase<T> indicator, TimeSpan period, Func<IBaseData, T> selector = null)
2832  where T : class, IBaseData
2833  {
2834  var history = GetIndicatorWarmUpHistory(symbol, indicator, period, out var identityConsolidator);
2835  if (history == Enumerable.Empty<Slice>()) return;
2836 
2837  // assign default using cast
2838  selector ??= (x => (T)x);
2839 
2840  // we expect T type as input
2841  Action<T> onDataConsolidated = bar =>
2842  {
2843  indicator.Update(selector(bar));
2844  };
2845 
2846  WarmUpIndicatorImpl(symbol, period, onDataConsolidated, history, identityConsolidator);
2847  }
2848 
2849  private IEnumerable<Slice> GetIndicatorWarmUpHistory(Symbol symbol, IIndicator indicator, TimeSpan timeSpan, out bool identityConsolidator)
2850  {
2851  identityConsolidator = false;
2852  var periods = (indicator as IIndicatorWarmUpPeriodProvider)?.WarmUpPeriod;
2853 
2854  if (periods.HasValue && periods != 0)
2855  {
2856  var resolution = timeSpan.ToHigherResolutionEquivalent(false);
2857  // if they are the same, means we can use an identity consolidator
2858  identityConsolidator = resolution.ToTimeSpan() == timeSpan;
2859  var resolutionTicks = resolution.ToTimeSpan().Ticks;
2860  if (resolutionTicks != 0)
2861  {
2862  periods *= (int)(timeSpan.Ticks / resolutionTicks);
2863  }
2864 
2865  try
2866  {
2867  return History(new[] { symbol }, periods.Value, resolution);
2868  }
2869  catch (ArgumentException e)
2870  {
2871  Debug($"{indicator.Name} could not be warmed up. Reason: {e.Message}");
2872  }
2873  }
2874  else if (!_isEmitWarmupInsightWarningSent)
2875  {
2876  Debug($"Warning: the 'WarmUpIndicator' feature only works with indicators which inherit from '{nameof(IIndicatorWarmUpPeriodProvider)}'" +
2877  $" and define a warm up period, setting property 'WarmUpPeriod' with a value > 0." +
2878  $" The provided indicator of type '{indicator.GetType().Name}' will not be warmed up.");
2879  _isEmitWarmupInsightWarningSent = true;
2880  }
2881 
2882  return Enumerable.Empty<Slice>();
2883  }
2884 
2885  private void WarmUpIndicatorImpl<T>(Symbol symbol, TimeSpan period, Action<T> handler, IEnumerable<Slice> history, bool identityConsolidator)
2886  where T : class, IBaseData
2887  {
2888  IDataConsolidator consolidator;
2889  if (identityConsolidator)
2890  {
2891  period = TimeSpan.Zero;
2892  }
2893  if (SubscriptionManager.SubscriptionDataConfigService.GetSubscriptionDataConfigs(symbol).Count > 0)
2894  {
2895  consolidator = Consolidate(symbol, period, handler);
2896  }
2897  else
2898  {
2899  var providedType = typeof(T);
2900  if (providedType.IsAbstract)
2901  {
2903  symbol.SecurityType,
2904  Resolution.Daily,
2905  // order by tick type so that behavior is consistent with 'GetSubscription()'
2906  symbol.IsCanonical())
2907  // make sure common lean data types are at the bottom
2908  .OrderByDescending(tuple => LeanData.IsCommonLeanDataType(tuple.Item1))
2909  .ThenBy(tuple => tuple.Item2).First();
2910 
2911  consolidator = CreateConsolidator(period, dataType.Item1, dataType.Item2);
2912  }
2913  else
2914  {
2915  // if the 'providedType' is not abstract we use it instead to determine which consolidator to use
2916  var tickType = LeanData.GetCommonTickTypeForCommonDataTypes(providedType, symbol.SecurityType);
2917  consolidator = CreateConsolidator(period, providedType, tickType);
2918  }
2919  consolidator.DataConsolidated += (s, bar) => handler((T)bar);
2920  }
2921 
2922  var consolidatorInputType = consolidator.InputType;
2923  IBaseData lastBar = null;
2924  foreach (var slice in history)
2925  {
2926  if (slice.TryGet(consolidatorInputType, symbol, out var data))
2927  {
2928  lastBar = data;
2929  consolidator.Update(lastBar);
2930  }
2931  }
2932 
2933  // Scan for time after we've pumped all the data through for this consolidator
2934  if (lastBar != null)
2935  {
2936  consolidator.Scan(lastBar.EndTime);
2937  }
2938 
2939  SubscriptionManager.RemoveConsolidator(symbol, consolidator);
2940  }
2941 
2942  /// <summary>
2943  /// Gets the default consolidator for the specified symbol and resolution
2944  /// </summary>
2945  /// <param name="symbol">The symbol whose data is to be consolidated</param>
2946  /// <param name="resolution">The resolution for the consolidator, if null, uses the resolution from subscription</param>
2947  /// <param name="dataType">The data type for this consolidator, if null, uses TradeBar over QuoteBar if present</param>
2948  /// <returns>The new default consolidator</returns>
2949  [DocumentationAttribute(ConsolidatingData)]
2950  [DocumentationAttribute(Indicators)]
2951  public IDataConsolidator ResolveConsolidator(Symbol symbol, Resolution? resolution, Type dataType = null)
2952  {
2953  TimeSpan? timeSpan = null;
2954  if (resolution.HasValue)
2955  {
2956  timeSpan = resolution.Value.ToTimeSpan();
2957  }
2958  return ResolveConsolidator(symbol, timeSpan, dataType);
2959  }
2960 
2961  /// <summary>
2962  /// Gets the default consolidator for the specified symbol and resolution
2963  /// </summary>
2964  /// <param name="symbol">The symbol whose data is to be consolidated</param>
2965  /// <param name="timeSpan">The requested time span for the consolidator, if null, uses the resolution from subscription</param>
2966  /// <param name="dataType">The data type for this consolidator, if null, uses TradeBar over QuoteBar if present</param>
2967  /// <returns>The new default consolidator</returns>
2968  [DocumentationAttribute(ConsolidatingData)]
2969  [DocumentationAttribute(Indicators)]
2970  public IDataConsolidator ResolveConsolidator(Symbol symbol, TimeSpan? timeSpan, Type dataType = null)
2971  {
2972  var tickType = dataType != null ? LeanData.GetCommonTickTypeForCommonDataTypes(dataType, symbol.SecurityType) : (TickType?)null;
2973  var subscription = GetSubscription(symbol, tickType);
2974  var subscriptionTimeSpan = subscription.Resolution.ToTimeSpan();
2975 
2976  // if not specified, default to the subscription resolution
2977  if (!timeSpan.HasValue)
2978  {
2979  timeSpan = subscriptionTimeSpan;
2980  }
2981 
2982  // verify this consolidator will give reasonable results, if someone asks for second consolidation but we have minute
2983  // data we won't be able to do anything good, we'll call it second, but it would really just be minute!
2984  if (timeSpan.Value < subscriptionTimeSpan)
2985  {
2986  throw new ArgumentException($"Unable to create {symbol} consolidator because {symbol} is registered for " +
2987  Invariant($"{subscription.Resolution.ToStringInvariant()} data. Consolidators require higher resolution data to produce lower resolution data.")
2988  );
2989  }
2990  else if (timeSpan.Value == subscriptionTimeSpan)
2991  {
2992  // input and expected output share the same time span, means we just want an identity consolidator
2993  timeSpan = TimeSpan.Zero;
2994  }
2995 
2996  return CreateConsolidator(timeSpan.Value, subscription.Type, subscription.TickType);
2997  }
2998 
2999  /// <summary>
3000  /// Creates a new consolidator for the specified period, generating the requested output type.
3001  /// </summary>
3002  /// <param name="period">The consolidation period</param>
3003  /// <param name="consolidatorInputType">The desired input type of the consolidator, such as TradeBar or QuoteBar</param>
3004  /// <param name="tickType">Trade or Quote. Optional, defaults to trade</param>
3005  /// <returns>A new consolidator matching the requested parameters</returns>
3006  [DocumentationAttribute(ConsolidatingData)]
3007  public static IDataConsolidator CreateConsolidator(TimeSpan period, Type consolidatorInputType, TickType? tickType = null)
3008  {
3009  // if our type can be used as a trade bar, then let's just make one of those
3010  // we use IsAssignableFrom instead of IsSubclassOf so that we can account for types that are able to be cast to TradeBar
3011  if (typeof(TradeBar).IsAssignableFrom(consolidatorInputType))
3012  {
3013  // Use IdentityDataConsolidator when data are not meant to consolidated into bars
3014  if (period.Ticks == 0)
3015  {
3017  }
3018  return new TradeBarConsolidator(period);
3019  }
3020 
3021  // if our type can be used as a quote bar, then let's just make one of those
3022  // we use IsAssignableFrom instead of IsSubclassOf so that we can account for types that are able to be cast to QuoteBar
3023  if (typeof(QuoteBar).IsAssignableFrom(consolidatorInputType))
3024  {
3025  // Use IdentityDataConsolidator when data are not meant to consolidated into bars
3026  if (period.Ticks == 0)
3027  {
3029  }
3030  return new QuoteBarConsolidator(period);
3031  }
3032 
3033  // if our type can be used as a tick then we'll use a consolidator that keeps the TickType
3034  // we use IsAssignableFrom instead of IsSubclassOf so that we can account for types that are able to be cast to Tick
3035  if (typeof(Tick).IsAssignableFrom(consolidatorInputType))
3036  {
3037  // Use IdentityDataConsolidator when ticks are not meant to consolidated into bars
3038  if (period.Ticks == 0)
3039  {
3040  return new IdentityDataConsolidator<Tick>();
3041  }
3042 
3043  switch (tickType)
3044  {
3045  case TickType.OpenInterest:
3046  return new OpenInterestConsolidator(period);
3047 
3048  case TickType.Quote:
3049  return new TickQuoteBarConsolidator(period);
3050 
3051  default:
3052  return new TickConsolidator(period);
3053  }
3054  }
3055 
3056  // if our type can be used as a DynamicData then we'll use the DynamicDataConsolidator
3057  if (typeof(DynamicData).IsAssignableFrom(consolidatorInputType))
3058  {
3059  // Use IdentityDataConsolidator when data are not meant to consolidated into bars
3060  if (period.Ticks == 0)
3061  {
3062  return new DynamicDataConsolidator(1);
3063  }
3064  return new DynamicDataConsolidator(period);
3065  }
3066 
3067  // Use IdentityDataConsolidator when data are not meant to consolidated into bars
3068  if (period.Ticks == 0)
3069  {
3071  }
3072  // no matter what we can always consolidate based on the time-value pair of BaseData
3073  return new BaseDataConsolidator(period);
3074  }
3075 
3076  /// <summary>
3077  /// Registers the <paramref name="handler"/> to receive consolidated data for the specified symbol
3078  /// </summary>
3079  /// <param name="symbol">The symbol who's data is to be consolidated</param>
3080  /// <param name="period">The consolidation period</param>
3081  /// <param name="handler">Data handler receives new consolidated data when generated</param>
3082  /// <returns>A new consolidator matching the requested parameters with the handler already registered</returns>
3083  [DocumentationAttribute(ConsolidatingData)]
3084  public IDataConsolidator Consolidate(Symbol symbol, Resolution period, Action<TradeBar> handler)
3085  {
3086  return Consolidate(symbol, period.ToTimeSpan(), TickType.Trade, handler);
3087  }
3088 
3089  /// <summary>
3090  /// Registers the <paramref name="handler"/> to receive consolidated data for the specified symbol
3091  /// </summary>
3092  /// <param name="symbol">The symbol who's data is to be consolidated</param>
3093  /// <param name="period">The consolidation period</param>
3094  /// <param name="handler">Data handler receives new consolidated data when generated</param>
3095  /// <returns>A new consolidator matching the requested parameters with the handler already registered</returns>
3096  [DocumentationAttribute(ConsolidatingData)]
3097  public IDataConsolidator Consolidate(Symbol symbol, TimeSpan period, Action<TradeBar> handler)
3098  {
3099  return Consolidate(symbol, period, TickType.Trade, handler);
3100  }
3101 
3102  /// <summary>
3103  /// Registers the <paramref name="handler"/> to receive consolidated data for the specified symbol
3104  /// </summary>
3105  /// <param name="symbol">The symbol who's data is to be consolidated</param>
3106  /// <param name="period">The consolidation period</param>
3107  /// <param name="handler">Data handler receives new consolidated data when generated</param>
3108  /// <returns>A new consolidator matching the requested parameters with the handler already registered</returns>
3109  [DocumentationAttribute(ConsolidatingData)]
3110  public IDataConsolidator Consolidate(Symbol symbol, Resolution period, Action<QuoteBar> handler)
3111  {
3112  return Consolidate(symbol, period.ToTimeSpan(), TickType.Quote, handler);
3113  }
3114 
3115  /// <summary>
3116  /// Registers the <paramref name="handler"/> to receive consolidated data for the specified symbol
3117  /// </summary>
3118  /// <param name="symbol">The symbol who's data is to be consolidated</param>
3119  /// <param name="period">The consolidation period</param>
3120  /// <param name="handler">Data handler receives new consolidated data when generated</param>
3121  /// <returns>A new consolidator matching the requested parameters with the handler already registered</returns>
3122  [DocumentationAttribute(ConsolidatingData)]
3123  public IDataConsolidator Consolidate(Symbol symbol, TimeSpan period, Action<QuoteBar> handler)
3124  {
3125  return Consolidate(symbol, period, TickType.Quote, handler);
3126  }
3127 
3128  /// <summary>
3129  /// Registers the <paramref name="handler"/> to receive consolidated data for the specified symbol and tick type.
3130  /// The handler and tick type must match.
3131  /// </summary>
3132  /// <param name="symbol">The symbol who's data is to be consolidated</param>
3133  /// <param name="period">The consolidation period</param>
3134  /// <param name="handler">Data handler receives new consolidated data when generated</param>
3135  /// <returns>A new consolidator matching the requested parameters with the handler already registered</returns>
3136  [DocumentationAttribute(ConsolidatingData)]
3137  public IDataConsolidator Consolidate<T>(Symbol symbol, TimeSpan period, Action<T> handler)
3138  where T : class, IBaseData
3139  {
3140  // only infer TickType from T if it's not abstract (for example IBaseData, BaseData), else if will end up being TradeBar let's not take that
3141  // decision here (default type), it will be taken later by 'GetSubscription' so we keep it centralized
3142  // This could happen when a user passes in a generic 'Action<BaseData>' handler
3143  var tickType = typeof(T).IsAbstract ? (TickType?)null : LeanData.GetCommonTickTypeForCommonDataTypes(typeof(T), symbol.SecurityType);
3144  return Consolidate(symbol, period, tickType, handler);
3145  }
3146 
3147  /// <summary>
3148  /// Registers the <paramref name="handler"/> to receive consolidated data for the specified symbol and tick type.
3149  /// The handler and tick type must match.
3150  /// </summary>
3151  /// <param name="symbol">The symbol who's data is to be consolidated</param>
3152  /// <param name="period">The consolidation period</param>
3153  /// <param name="tickType">The tick type of subscription used as data source for consolidator. Specify null to use first subscription found.</param>
3154  /// <param name="handler">Data handler receives new consolidated data when generated</param>
3155  /// <returns>A new consolidator matching the requested parameters with the handler already registered</returns>
3156  [DocumentationAttribute(ConsolidatingData)]
3157  public IDataConsolidator Consolidate<T>(Symbol symbol, Resolution period, TickType? tickType, Action<T> handler)
3158  where T : class, IBaseData
3159  {
3160  return Consolidate(symbol, period.ToTimeSpan(), tickType, handler);
3161  }
3162 
3163  /// <summary>
3164  /// Registers the <paramref name="handler"/> to receive consolidated data for the specified symbol and tick type.
3165  /// The handler and tick type must match.
3166  /// </summary>
3167  /// <param name="symbol">The symbol who's data is to be consolidated</param>
3168  /// <param name="period">The consolidation period</param>
3169  /// <param name="tickType">The tick type of subscription used as data source for consolidator. Specify null to use first subscription found.</param>
3170  /// <param name="handler">Data handler receives new consolidated data when generated</param>
3171  /// <returns>A new consolidator matching the requested parameters with the handler already registered</returns>
3172  [DocumentationAttribute(ConsolidatingData)]
3173  public IDataConsolidator Consolidate<T>(Symbol symbol, TimeSpan period, TickType? tickType, Action<T> handler)
3174  where T : class, IBaseData
3175  {
3176  // resolve consolidator input subscription
3177  var subscription = GetSubscription(symbol, tickType);
3178 
3179  // create requested consolidator
3180  var consolidator = CreateConsolidator(period, subscription.Type, subscription.TickType);
3181 
3182  AddConsolidator(symbol, consolidator, handler, tickType);
3183  return consolidator;
3184  }
3185 
3186  /// <summary>
3187  /// Registers the <paramref name="handler"/> to receive consolidated data for the specified symbol
3188  /// </summary>
3189  /// <param name="symbol">The symbol who's data is to be consolidated</param>
3190  /// <param name="calendar">The consolidation calendar</param>
3191  /// <param name="handler">Data handler receives new consolidated data when generated</param>
3192  /// <returns>A new consolidator matching the requested parameters with the handler already registered</returns>
3193  [DocumentationAttribute(ConsolidatingData)]
3194  public IDataConsolidator Consolidate(Symbol symbol, Func<DateTime, CalendarInfo> calendar, Action<QuoteBar> handler)
3195  {
3196  return Consolidate(symbol, calendar, TickType.Quote, handler);
3197  }
3198 
3199  /// <summary>
3200  /// Registers the <paramref name="handler"/> to receive consolidated data for the specified symbol
3201  /// </summary>
3202  /// <param name="symbol">The symbol who's data is to be consolidated</param>
3203  /// <param name="calendar">The consolidation calendar</param>
3204  /// <param name="handler">Data handler receives new consolidated data when generated</param>
3205  /// <returns>A new consolidator matching the requested parameters with the handler already registered</returns>
3206  [DocumentationAttribute(ConsolidatingData)]
3207  public IDataConsolidator Consolidate(Symbol symbol, Func<DateTime, CalendarInfo> calendar, Action<TradeBar> handler)
3208  {
3209  return Consolidate(symbol, calendar, TickType.Trade, handler);
3210  }
3211 
3212  /// <summary>
3213  /// Registers the <paramref name="handler"/> to receive consolidated data for the specified symbol and tick type.
3214  /// The handler and tick type must match.
3215  /// </summary>
3216  /// <param name="symbol">The symbol who's data is to be consolidated</param>
3217  /// <param name="calendar">The consolidation calendar</param>
3218  /// <param name="handler">Data handler receives new consolidated data when generated</param>
3219  /// <returns>A new consolidator matching the requested parameters with the handler already registered</returns>
3220  [DocumentationAttribute(ConsolidatingData)]
3221  public IDataConsolidator Consolidate<T>(Symbol symbol, Func<DateTime, CalendarInfo> calendar, Action<T> handler)
3222  where T : class, IBaseData
3223  {
3224  // only infer TickType from T if it's not abstract (for example IBaseData, BaseData), else if will end up being TradeBar let's not take that
3225  // decision here (default type), it will be taken later by 'GetSubscription' so we keep it centralized
3226  // This could happen when a user passes in a generic 'Action<BaseData>' handler
3227  var tickType = typeof(T).IsAbstract ? (TickType?)null : LeanData.GetCommonTickTypeForCommonDataTypes(typeof(T), symbol.SecurityType);
3228  return Consolidate(symbol, calendar, tickType, handler);
3229  }
3230 
3231  /// <summary>
3232  /// Registers the <paramref name="handler"/> to receive consolidated data for the specified symbol and tick type.
3233  /// The handler and tick type must match.
3234  /// </summary>
3235  /// <param name="symbol">The symbol who's data is to be consolidated</param>
3236  /// <param name="calendar">The consolidation calendar</param>
3237  /// <param name="tickType">The tick type of subscription used as data source for consolidator. Specify null to use first subscription found.</param>
3238  /// <param name="handler">Data handler receives new consolidated data when generated</param>
3239  /// <returns>A new consolidator matching the requested parameters with the handler already registered</returns>
3240  [DocumentationAttribute(ConsolidatingData)]
3241  public IDataConsolidator Consolidate<T>(Symbol symbol, Func<DateTime, CalendarInfo> calendar, TickType? tickType, Action<T> handler)
3242  where T : class, IBaseData
3243  {
3244  // resolve consolidator input subscription
3245  var subscription = GetSubscription(symbol, tickType);
3246 
3247  // create requested consolidator
3248  var consolidator = CreateConsolidator(calendar, subscription.Type, subscription.TickType);
3249 
3250  AddConsolidator(symbol, consolidator, handler);
3251  return consolidator;
3252  }
3253 
3254  /// <summary>
3255  /// Adds the provided consolidator and asserts the handler T type is assignable from the consolidator output,
3256  /// if not will throw <see cref="ArgumentException"/>
3257  /// </summary>
3258  [DocumentationAttribute(ConsolidatingData)]
3259  private void AddConsolidator<T>(Symbol symbol, IDataConsolidator consolidator, Action<T> handler, TickType? tickType = null)
3260  {
3261  if (!typeof(T).IsAssignableFrom(consolidator.OutputType))
3262  {
3263  // special case downgrading of QuoteBar -> TradeBar
3264  if (typeof(T) == typeof(TradeBar) && consolidator.OutputType == typeof(QuoteBar))
3265  {
3266  // collapse quote bar into trade bar (ignore the funky casting, required due to generics)
3267  consolidator.DataConsolidated += (sender, consolidated) => handler((T)(object)((QuoteBar)consolidated).Collapse());
3268  }
3269 
3270  throw new ArgumentException(
3271  $"Unable to consolidate with the specified handler because the consolidator's output type " +
3272  $"is {consolidator.OutputType.Name} but the handler's input type is {typeof(T).Name}.");
3273  }
3274 
3275  // register user-defined handler to receive consolidated data events
3276  consolidator.DataConsolidated += (sender, consolidated) => handler((T)consolidated);
3277 
3278  // register the consolidator for automatic updates via SubscriptionManager
3279  SubscriptionManager.AddConsolidator(symbol, consolidator, tickType);
3280  }
3281 
3282  [DocumentationAttribute(ConsolidatingData)]
3283  private IDataConsolidator CreateConsolidator(Func<DateTime, CalendarInfo> calendar, Type consolidatorInputType, TickType tickType)
3284  {
3285  // if our type can be used as a trade bar, then let's just make one of those
3286  // we use IsAssignableFrom instead of IsSubclassOf so that we can account for types that are able to be cast to TradeBar
3287  if (typeof(TradeBar).IsAssignableFrom(consolidatorInputType))
3288  {
3289  return new TradeBarConsolidator(calendar);
3290  }
3291 
3292  // if our type can be used as a quote bar, then let's just make one of those
3293  // we use IsAssignableFrom instead of IsSubclassOf so that we can account for types that are able to be cast to QuoteBar
3294  if (typeof(QuoteBar).IsAssignableFrom(consolidatorInputType))
3295  {
3296  return new QuoteBarConsolidator(calendar);
3297  }
3298 
3299  // if our type can be used as a tick then we'll use a consolidator that keeps the TickType
3300  // we use IsAssignableFrom instead of IsSubclassOf so that we can account for types that are able to be cast to Tick
3301  if (typeof(Tick).IsAssignableFrom(consolidatorInputType))
3302  {
3303  if (tickType == TickType.Quote)
3304  {
3305  return new TickQuoteBarConsolidator(calendar);
3306  }
3307  return new TickConsolidator(calendar);
3308  }
3309 
3310  // if our type can be used as a DynamicData then we'll use the DynamicDataConsolidator
3311  if (typeof(DynamicData).IsAssignableFrom(consolidatorInputType))
3312  {
3313  return new DynamicDataConsolidator(calendar);
3314  }
3315 
3316  // no matter what we can always consolidate based on the time-value pair of BaseData
3317  return new BaseDataConsolidator(calendar);
3318  }
3319 
3320  /// <summary>
3321  /// Registers and warms up (if EnableAutomaticIndicatorWarmUp is set) the indicator
3322  /// </summary>
3323  private void InitializeIndicator(Symbol symbol, IndicatorBase<IndicatorDataPoint> indicator, Resolution? resolution = null,
3324  Func<IBaseData, decimal> selector = null)
3325  {
3326  RegisterIndicator(symbol, indicator, resolution, selector);
3327 
3329  {
3330  WarmUpIndicator(symbol, indicator, resolution, selector);
3331  }
3332  }
3333 
3334  private void InitializeIndicator<T>(Symbol symbol, IndicatorBase<T> indicator, Resolution? resolution = null,
3335  Func<IBaseData, T> selector = null)
3336  where T : class, IBaseData
3337  {
3338  RegisterIndicator(symbol, indicator, resolution, selector);
3339 
3341  {
3342  WarmUpIndicator(symbol, indicator, resolution, selector);
3343  }
3344  }
3345 
3346  private string InitializeOptionIndicator<T>(Symbol symbol, out IRiskFreeInterestRateModel riskFreeRateModel, out IDividendYieldModel dividendYieldModel,
3347  decimal? riskFreeRate = null, decimal? dividendYield = null, OptionPricingModelType optionModel = OptionPricingModelType.BlackScholes, Resolution? resolution = null)
3348  where T : OptionIndicatorBase
3349  {
3350  var name = CreateIndicatorName(symbol, $"{typeof(T).Name}({riskFreeRate},{dividendYield},{optionModel})", resolution);
3351 
3352  riskFreeRateModel = riskFreeRate.HasValue
3353  ? new ConstantRiskFreeRateInterestRateModel(riskFreeRate.Value)
3354  // Make it a function so it's lazily evaluated: SetRiskFreeInterestRateModel can be called after this method
3355  : new FuncRiskFreeRateInterestRateModel((datetime) => RiskFreeInterestRateModel.GetInterestRate(datetime));
3356 
3357  if (dividendYield.HasValue)
3358  {
3359  dividendYieldModel = new ConstantDividendYieldModel(dividendYield.Value);
3360  }
3361  else if (symbol.ID.SecurityType == SecurityType.FutureOption || symbol.ID.SecurityType == SecurityType.IndexOption)
3362  {
3363  dividendYieldModel = new ConstantDividendYieldModel(0m);
3364  }
3365  else
3366  {
3367  dividendYieldModel = new DividendYieldProvider(symbol.Underlying);
3368  }
3369 
3370  return name;
3371  }
3372 
3373  private void RegisterConsolidator(IndicatorBase indicatorBase, Symbol symbol, IDataConsolidator consolidator)
3374  {
3375  // keep a reference of the consolidator so we can unregister it later using only a reference to the indicator
3376  indicatorBase.Consolidators.Add(consolidator);
3377 
3378  // register the consolidator for automatic updates via SubscriptionManager
3379  SubscriptionManager.AddConsolidator(symbol, consolidator);
3380  }
3381  }
3382 }