Lean  $LEAN_TAG$
PremierStochasticOscillator.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  /// Premier Stochastic Oscillator (PSO) Indicator implementation.
23  /// This indicator combines a stochastic oscillator with exponential moving averages to provide
24  /// a normalized output between -1 and 1, which can be useful for identifying trends and
25  /// potential reversal points in the market.
26  /// </summary>
28  {
29  /// <summary>
30  /// Exponential Moving Averages (EMA) used in the calculation of the Premier Stochastic Oscillator (PSO).
31  /// firstSmoothingEma performs the first smoothing of the Normalized Stochastic (0.1 * (Fast%K - 50)),
32  /// and doubleSmoothingEma applies a second smoothing on the result of _ema1, resulting in the Double-Smoothed Normalized Stochastic
33  /// </summary>
34  private readonly ExponentialMovingAverage _firstSmoothingEma;
35  private readonly ExponentialMovingAverage _doubleSmoothingEma;
36 
37  /// <summary>
38  /// Stochastic oscillator used to calculate the K value.
39  /// </summary>
40  private readonly Stochastic _stochastic;
41 
42  /// <summary>
43  /// The warm-up period necessary before the PSO indicator is considered ready.
44  /// </summary>
45  public int WarmUpPeriod { get; }
46 
47  /// <summary>
48  /// Constructor for the Premier Stochastic Oscillator.
49  /// Initializes the Stochastic and EMA indicators and calculates the warm-up period.
50  /// </summary>
51  /// <param name="name">The name of the indicator.</param>
52  /// <param name="period">The period given to calculate FastK.</param>
53  /// <param name="emaPeriod">The period for EMA calculations.</param>
54  public PremierStochasticOscillator(string name, int period, int emaPeriod) : base(name)
55  {
56  _stochastic = new Stochastic(name, period, period, period);
57  _firstSmoothingEma = new ExponentialMovingAverage(emaPeriod);
58  _doubleSmoothingEma = _firstSmoothingEma.EMA(emaPeriod);
59  WarmUpPeriod = period + 2 * (emaPeriod - 1);
60  }
61 
62  /// <summary>
63  /// Overloaded constructor to facilitate instantiation with a default name format.
64  /// </summary>
65  /// <param name="period">The period given to calculate FastK.</param>
66  /// <param name="emaPeriod">The period for EMA calculations.</param>
67  public PremierStochasticOscillator(int period, int emaPeriod)
68  : this($"PSO({period},{emaPeriod})", period, emaPeriod)
69  {
70  }
71 
72  /// <summary>
73  /// Gets a flag indicating when this indicator is ready and fully initialized
74  /// </summary>
75  public override bool IsReady => _doubleSmoothingEma.IsReady;
76 
77  /// <summary>
78  /// Computes the Premier Stochastic Oscillator (PSO) based on the current input.
79  /// This calculation involves updating the stochastic oscillator and the EMAs,
80  /// followed by calculating the PSO using the formula:
81  /// PSO = (exp(EMA2) - 1) / (exp(EMA2) + 1)
82  /// </summary>
83  /// <param name="input">The current input bar containing market data.</param>
84  /// <returns>The computed value of the PSO.</returns>
85  protected override decimal ComputeNextValue(IBaseDataBar input)
86  {
87  _stochastic.Update(input);
88  if (!_stochastic.FastStoch.IsReady)
89  {
90  return decimal.Zero;
91  }
92 
93  var k = _stochastic.FastStoch.Current.Value;
94  var nsk = 0.1m * (k - 50);
95  if (!_firstSmoothingEma.Update(new IndicatorDataPoint(input.Time, nsk)))
96  {
97  return decimal.Zero;
98  }
99 
100  if (!_doubleSmoothingEma.IsReady)
101  {
102  return decimal.Zero;
103  }
104  var expss = (decimal)Math.Exp((double)_doubleSmoothingEma.Current.Value);
105  return (expss - 1) / (expss + 1);
106  }
107 
108  /// <summary>
109  /// Resets this indicator to its initial state
110  /// </summary>
111  public override void Reset()
112  {
113  _stochastic.Reset();
114  _firstSmoothingEma.Reset();
115  _doubleSmoothingEma.Reset();
116  base.Reset();
117  }
118  }
119 }