Lean  $LEAN_TAG$
TrueStrengthIndex.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 {
19  /// <summary>
20  /// This indicator computes the True Strength Index (TSI).
21  /// The True Strength Index is calculated as explained here:
22  /// https://school.stockcharts.com/doku.php?id=technical_indicators:true_strength_index
23  ///
24  /// Briefly, the calculation has three steps:
25  /// 1. Smooth the momentum and the absolute momentum by getting an EMA of them (typically of period 25)
26  /// 2. Double smooth the momentum and the absolute momentum by getting an EMA of their EMA (typically of period 13)
27  /// 3. The TSI formula itself: divide the double-smoothed momentum over the double-smoothed absolute momentum and multiply by 100
28  ///
29  /// The signal is typically a 7-to-12-EMA of the TSI.
30  /// </summary>
32  {
33  private decimal _prevClose;
34 
35  private readonly ExponentialMovingAverage _priceChangeEma;
36 
37  private readonly ExponentialMovingAverage _priceChangeEmaEma;
38 
39  private readonly ExponentialMovingAverage _absPriceChangeEma;
40 
41  private readonly ExponentialMovingAverage _absPriceChangeEmaEma;
42 
43  private readonly IndicatorBase<IndicatorDataPoint> _tsi;
44 
45  /// <summary>
46  /// Gets the signal line for the TSI indicator
47  /// </summary>
49 
50  /// <summary>
51  /// Required period, in data points, for the indicator to be ready and fully initialized.
52  /// </summary>
53  public int WarmUpPeriod { get; }
54 
55  /// <summary>
56  /// Gets a flag indicating when this indicator is ready and fully initialized
57  /// </summary>
58  public override bool IsReady => Samples >= WarmUpPeriod;
59 
60  /// <summary>
61  /// Initializes a new instance of the <see cref="TrueStrengthIndex"/> class using the specified short and long term smoothing periods, and the signal period and type.
62  /// </summary>
63  /// <param name="shortTermPeriod">Period used for the first price change smoothing</param>
64  /// <param name="longTermPeriod">Period used for the second (double) price change smoothing</param>
65  /// <param name="signalPeriod">The signal period</param>
66  /// <param name="signalType">The type of moving average to use for the signal</param>
67  public TrueStrengthIndex(int longTermPeriod = 25, int shortTermPeriod = 13, int signalPeriod = 7, MovingAverageType signalType = MovingAverageType.Exponential)
68  : this($"TSI({longTermPeriod},{shortTermPeriod},{signalPeriod})", longTermPeriod, shortTermPeriod, signalPeriod, signalType)
69  {
70  }
71 
72  /// <summary>
73  /// Initializes a new instance of the <see cref="TrueStrengthIndex"/> class using the specified name, the short and long term smoothing periods, and the signal period and type.
74  /// </summary>
75  /// <param name="name">The name of the indicator</param>
76  /// <param name="shortTermPeriod">Period used for the first price change smoothing</param>
77  /// <param name="longTermPeriod">Period used for the second (double) price change smoothing</param>
78  /// <param name="signalPeriod">The signal period</param>
79  /// <param name="signalType">The type of moving average to use for the signal</param>
80  public TrueStrengthIndex(string name, int longTermPeriod = 25, int shortTermPeriod = 13, int signalPeriod = 7, MovingAverageType signalType = MovingAverageType.Exponential)
81  : base(name)
82  {
83  _priceChangeEma = new ExponentialMovingAverage(name + "_PC_EMA", longTermPeriod);
84  _absPriceChangeEma = new ExponentialMovingAverage(name + "_APC_EMA", longTermPeriod);
85  _priceChangeEmaEma = new ExponentialMovingAverage(name + "_PC_EMA_EMA", shortTermPeriod).Of(_priceChangeEma, true);
86  _absPriceChangeEmaEma = new ExponentialMovingAverage(name + "_APC_EMA_EMA", shortTermPeriod).Of(_absPriceChangeEma, true);
87  _tsi = _priceChangeEmaEma.Over(_absPriceChangeEmaEma).Times(100m);
88  Signal = signalType.AsIndicator(name + "_Signal", signalPeriod).Of(_tsi, true);
89  WarmUpPeriod = longTermPeriod + shortTermPeriod;
90  }
91 
92  /// <summary>
93  /// Computes the next value of this indicator from the given state
94  /// </summary>
95  /// <param name="input">The input given to the indicator</param>
96  /// <returns>A new value for this indicator</returns>
97  protected override decimal ComputeNextValue(IndicatorDataPoint input)
98  {
99  if (Samples == 1)
100  {
101  _prevClose = input.Price;
102  return 0m;
103  }
104 
105  var priceChange = input.Price - _prevClose;
106  _prevClose = input.Price;
107  _priceChangeEma.Update(input.Time, priceChange);
108  _absPriceChangeEma.Update(input.Time, Math.Abs(priceChange));
109 
110  return _tsi.Current.Value;
111  }
112 
113  /// <summary>
114  /// Resets this indicator to its initial state
115  /// </summary>
116  public override void Reset()
117  {
118  _prevClose = 0;
119  _priceChangeEma.Reset();
120  _priceChangeEmaEma.Reset();
121  _absPriceChangeEma.Reset();
122  _absPriceChangeEmaEma.Reset();
123  _tsi.Reset();
124  Signal.Reset();
125  base.Reset();
126  }
127  }
128 }