Lean  $LEAN_TAG$
ArmsIndex.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;
18 
20 {
21  /// <summary>
22  /// The Arms Index, also called the Short-Term Trading Index (TRIN)
23  /// is a technical analysis indicator that compares the number of advancing
24  /// and declining stocks (AD Ratio) to advancing and declining volume (AD volume).
25  /// </summary>
27  {
28  private readonly IndicatorBase<IndicatorDataPoint> _arms;
29 
30  /// <summary>
31  /// Gets the Advance/Decline Ratio (ADR) indicator
32  /// </summary>
33  public AdvanceDeclineRatio ADRatio { get; }
34 
35  /// <summary>
36  /// Gets the Advance/Decline Volume Ratio (ADVR) indicator
37  /// </summary>
39 
40  /// <summary>
41  /// Initializes a new instance of the <see cref="ArmsIndex"/> class
42  /// </summary>
43  public ArmsIndex(string name) : base(name)
44  {
45  ADRatio = new AdvanceDeclineRatio(name + "_A/D Ratio");
46  ADVRatio = new AdvanceDeclineVolumeRatio(name + "_A/D Volume Ratio");
47 
48  _arms = ADRatio.Over(ADVRatio, name);
49  }
50 
51  /// <summary>
52  /// Gets a flag indicating when this indicator is ready and fully initialized
53  /// </summary>
54  public override bool IsReady => ADRatio.IsReady && ADVRatio.IsReady;
55 
56  /// <summary>
57  /// Required period, in data points, for the indicator to be ready and fully initialized.
58  /// </summary>
59  public int WarmUpPeriod => System.Math.Max(ADRatio.WarmUpPeriod, ADVRatio.WarmUpPeriod);
60 
61  /// <summary>
62  /// Computes the next value of this indicator from the given state
63  /// </summary>
64  /// <param name="input">The input given to the indicator</param>
65  /// <returns>A new value for this indicator</returns>
66  protected override decimal ComputeNextValue(TradeBar input)
67  {
68  ADRatio.Update(input);
69  ADVRatio.Update(input);
70 
71  if (ADVRatio == 0)
72  {
73  return 0;
74  }
75 
76  return _arms.Current.Value;
77  }
78 
79  /// <summary>
80  /// Resets this indicator to its initial state
81  /// </summary>
82  public override void Reset()
83  {
84  ADRatio.Reset();
85  ADVRatio.Reset();
86  _arms.Reset();
87 
88  base.Reset();
89  }
90 
91  /// <summary>
92  /// Add Tracking stock issue
93  /// </summary>
94  /// <param name="asset">the tracking stock issue</param>
95  public void Add(Symbol asset)
96  {
97  ADRatio.Add(asset);
98  ADVRatio.Add(asset);
99  }
100 
101  /// <summary>
102  /// Deprecated
103  /// </summary>
104  [Obsolete("Please use Add(asset)")]
105  public void AddStock(Symbol asset)
106  {
107  Add(asset);
108  }
109 
110  /// <summary>
111  /// Remove Tracking stock issue
112  /// </summary>
113  /// <param name="asset">the tracking stock issue</param>
114  public void Remove(Symbol asset)
115  {
116  ADRatio.Remove(asset);
117  ADVRatio.Remove(asset);
118  }
119 
120  /// <summary>
121  /// Deprecated
122  /// </summary>
123  [Obsolete("Please use Remove(asset)")]
124  public void RemoveStock(Symbol asset)
125  {
126  Remove(asset);
127  }
128  }
129 }