Lean  $LEAN_TAG$
WilderAccumulativeSwingIndex.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  /// This indicator calculates the Accumulative Swing Index (ASI) as defined by
22  /// Welles Wilder in his book 'New Concepts in Technical Trading Systems'.
23  /// <para>
24  /// ASIₜ = ASIₜ₋₁ + SIₜ
25  /// </para>
26  /// <para>
27  /// Where:
28  /// <list type="bullet">
29  /// <item>
30  /// <term>ASIₜ₋₁</term>
31  /// <description>
32  /// The <see cref="WilderAccumulativeSwingIndex"/> for the previous period.
33  /// </description>
34  /// </item>
35  /// <item>
36  /// <term>SIₜ</term>
37  /// <description>
38  /// The <see cref="WilderSwingIndex"/> calculated for the current period.
39  /// </description>
40  /// </item>
41  /// </list>
42  /// </para>
43  /// </summary>
44  /// <seealso cref="WilderSwingIndex"/>
46  {
47  /// <summary>
48  /// The Swing Index (SI) used in calculating the Accumulative Swing Index.
49  /// </summary>
50  private readonly WilderSwingIndex _si;
51 
52  /// <summary>
53  /// Initializes a new instance of the <see cref="WilderAccumulativeSwingIndex"/> class using the specified name.
54  /// </summary>
55  /// <param name="limitMove">A decimal representing the limit move value for the period.</param>
56  public WilderAccumulativeSwingIndex(decimal limitMove)
57  : this ("ASI", limitMove)
58  {
59  }
60 
61  /// <summary>
62  /// Initializes a new instance of the <see cref="WilderAccumulativeSwingIndex"/> class using the specified name.
63  /// </summary>
64  /// <param name="name">The name of this indicator</param>
65  /// <param name="limitMove">A decimal representing the limit move value for the period.</param>
66  public WilderAccumulativeSwingIndex(string name, decimal limitMove)
67  : base (name)
68  {
69  _si = new WilderSwingIndex(limitMove);
70  }
71 
72  /// <summary>
73  /// Gets a flag indicating when this indicator is ready and fully initialized.
74  /// </summary>
75  public override bool IsReady => Samples > 1;
76 
77  /// <summary>
78  /// Required period, in data points, for the indicator to be ready and fully initialized.
79  /// </summary>
80  public int WarmUpPeriod => 2;
81 
82  /// <summary>
83  /// Computes the next value of this indicator from the given state
84  /// </summary>
85  /// <param name="input">The input given to the indicator</param>
86  /// <returns>A new value for this indicator</returns>
87  protected override decimal ComputeNextValue(TradeBar input)
88  {
89  var isReady = _si.Update(input);
90 
91  if (isReady)
92  {
93  return IsReady
94  ? Current.Value + _si.Current.Value
95  : _si.Current.Value;
96  }
97  else
98  {
99  return 0m;
100  }
101  }
102 
103  /// <summary>
104  /// Resets this indicator to its initial state.
105  /// </summary>
106  public override void Reset()
107  {
108  _si.Reset();
109  base.Reset();
110  }
111  }
112 }