Lean  $LEAN_TAG$
ForceIndex.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 
17 
19 {
20  /// <summary>
21  /// The Force Index is calculated by comparing the current market price with the previous market price
22  /// and multiplying its difference with the traded volume during a specific time period.
23  /// </summary>
25  {
26  private TradeBar _previousInput;
27 
28  /// <summary>
29  /// This indicator is used to smooth the ForceIndex computation
30  /// </summary>
31  /// <remarks>This is not exposed publicly since it is the same value as this indicator, meaning
32  /// that this '_smoother' computers the ForceIndex directly, so exposing it publicly would be duplication</remarks>
33  private readonly IndicatorBase<IndicatorDataPoint> _smoother;
34 
35 
36  /// <summary>
37  /// Gets a flag indicating when this indicator is ready and fully initialized
38  /// </summary>
39  public override bool IsReady => _smoother.IsReady;
40 
41  /// <summary>
42  /// Required period, in data points, for the indicator to be ready and fully initialized.
43  /// </summary>
44  public int WarmUpPeriod { get; }
45 
46  /// <summary>
47  /// Creates a new ForceIndex indicator using the specified period and moving average type
48  /// </summary>
49  /// <param name="name">The name of this indicator</param>
50  /// <param name="period">The smoothing period used to smooth the instantaneous force index values</param>
51  /// <param name="movingAverageType">The type of smoothing used to smooth the true range values</param>
52  public ForceIndex(string name, int period, MovingAverageType movingAverageType = MovingAverageType.Exponential)
53  : base(name)
54  {
55  _smoother = movingAverageType.AsIndicator($"{name}_{movingAverageType}", period);
56  WarmUpPeriod = period + 1;
57 
58  }
59 
60  /// <summary>
61  /// Creates a new ForceIndex indicator using the specified period and moving average type
62  /// </summary>
63  /// <param name="period">The smoothing period used to smooth the instantenous force index values</param>
64  /// <param name="movingAverageType">The type of smoothing used to smooth the instantenous force index values</param>
65  public ForceIndex(int period, MovingAverageType movingAverageType = MovingAverageType.Exponential)
66  : this($"FI({period})", period, movingAverageType)
67  {
68  }
69 
70  /// <summary>
71  /// Computes the next value of this indicator from the given state
72  /// </summary>
73  /// <param name="input">The input given to the indicator</param>
74  /// <returns>A new value for this indicator</returns>
75  protected override decimal ComputeNextValue(TradeBar input)
76  {
77  if (Samples < 2)
78  {
79  _previousInput = input;
80  return 0;
81  }
82  // compute the instantaneous force index and then send it to our smoother
83 
84  _smoother.Update(input.EndTime, (input.Close - _previousInput.Close) * input.Volume);
85 
86  _previousInput = input;
87 
88  return _smoother.Current.Value;
89  }
90 
91  /// <summary>
92  /// Resets this indicator to its initial state
93  /// </summary>
94  public override void Reset()
95  {
96  _previousInput = null;
97  _smoother.Reset();
98  base.Reset();
99  }
100  }
101 }