Lean  $LEAN_TAG$
FunctionalIndicator.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using System;
17 using QuantConnect.Data;
18 
20 {
21  /// <summary>
22  /// The functional indicator is used to lift any function into an indicator. This can be very useful
23  /// when trying to combine output of several indicators, or for expression a mathematical equation
24  /// </summary>
25  /// <typeparam name="T">The input type for this indicator</typeparam>
26  public class FunctionalIndicator<T> : IndicatorBase<T>
27  where T : IBaseData
28  {
29  /// <summary>function implementation of the IndicatorBase.IsReady property</summary>
30  private readonly Func<IndicatorBase<T>, bool> _isReady;
31  /// <summary>Action used to reset this indicator completely along with any indicators this one is dependent on</summary>
32  private readonly Action _reset;
33  /// <summary>function implementation of the IndicatorBase.ComputeNextValue method</summary>
34  private readonly Func<T, decimal> _computeNextValue;
35 
36  /// <summary>
37  /// Creates a new FunctionalIndicator using the specified functions as its implementation.
38  /// </summary>
39  /// <param name="name">The name of this indicator</param>
40  /// <param name="computeNextValue">A function accepting the input value and returning this indicator's output value</param>
41  /// <param name="isReady">A function accepting this indicator and returning true if the indicator is ready, false otherwise</param>
42  public FunctionalIndicator(string name, Func<T, decimal> computeNextValue, Func<IndicatorBase<T>, bool> isReady)
43  : base(name)
44  {
45  _computeNextValue = computeNextValue;
46  _isReady = isReady;
47  }
48 
49  /// <summary>
50  /// Creates a new FunctionalIndicator using the specified functions as its implementation.
51  /// </summary>
52  /// <param name="name">The name of this indicator</param>
53  /// <param name="computeNextValue">A function accepting the input value and returning this indicator's output value</param>
54  /// <param name="isReady">A function accepting this indicator and returning true if the indicator is ready, false otherwise</param>
55  /// <param name="reset">Function called to reset this indicator and any indicators this is dependent on</param>
56  public FunctionalIndicator(string name, Func<T, decimal> computeNextValue, Func<IndicatorBase<T>, bool> isReady, Action reset)
57  : base(name)
58  {
59  _computeNextValue = computeNextValue;
60  _isReady = isReady;
61  _reset = reset;
62  }
63 
64  /// <summary>
65  /// Gets a flag indicating when this indicator is ready and fully initialized
66  /// </summary>
67  public override bool IsReady
68  {
69  get { return _isReady(this); }
70  }
71 
72  /// <summary>
73  /// Computes the next value of this indicator from the given state
74  /// </summary>
75  /// <param name="input">The input given to the indicator</param>
76  /// <returns>A new value for this indicator</returns>
77  protected override decimal ComputeNextValue(T input)
78  {
79  return _computeNextValue(input);
80  }
81 
82  /// <summary>
83  /// Resets this indicator to its initial state, optionally using the reset action passed via the constructor
84  /// </summary>
85  public override void Reset()
86  {
87  if (_reset != null)
88  {
89  // if a reset function was specified then use that
90  _reset.Invoke();
91  }
92  base.Reset();
93  }
94  }
95 }