/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Linq; namespace QuantConnect.Indicators { /// /// Stochastic RSI, or simply StochRSI, is a technical analysis indicator used to determine whether /// an asset is overbought or oversold, as well as to identify current market trends. /// As the name suggests, the StochRSI is a derivative of the standard Relative Strength Index (RSI) and, /// as such, is considered an indicator of an indicator. /// It is a type of oscillator, meaning that it fluctuates above and below a center line. /// public class StochasticRelativeStrengthIndex : Indicator, IIndicatorWarmUpPeriodProvider { private readonly RelativeStrengthIndex _rsi; private readonly RollingWindow _recentRSIValues; /// /// Gets the %K output /// public IndicatorBase K { get; } /// /// Gets the %D output /// public IndicatorBase D { get; } /// /// Gets a flag indicating when this indicator is ready and fully initialized /// public override bool IsReady => Samples >= WarmUpPeriod; /// /// Required period, in data points, for the indicator to be ready and fully initialized. /// public int WarmUpPeriod { get; } /// /// Initializes a new instance of the StochasticRelativeStrengthIndex class /// /// The period of the relative strength index /// The period of the stochastic indicator /// The smoothing period of K output (aka %K) /// The smoothing period of D output (aka %D) /// The type of moving average to be used for k and d public StochasticRelativeStrengthIndex(int rsiPeriod, int stochPeriod, int kSmoothingPeriod, int dSmoothingPeriod, MovingAverageType movingAverageType = MovingAverageType.Simple) : this($"SRSI({rsiPeriod},{stochPeriod},{kSmoothingPeriod},{dSmoothingPeriod},{movingAverageType})", rsiPeriod, stochPeriod, kSmoothingPeriod, dSmoothingPeriod, movingAverageType) { } /// /// Initializes a new instance of the StochasticRelativeStrengthIndex class /// /// The name of this indicator /// The period of the relative strength index /// The period of the stochastic indicator /// The smoothing period of K output /// The smoothing period of D output /// The type of moving average to be used public StochasticRelativeStrengthIndex(string name, int rsiPeriod, int stochPeriod, int kSmoothingPeriod, int dSmoothingPeriod, MovingAverageType movingAverageType = MovingAverageType.Simple) : base(name) { _rsi = new RelativeStrengthIndex(rsiPeriod); _recentRSIValues = new RollingWindow(stochPeriod); K = movingAverageType.AsIndicator($"{name}_K_{movingAverageType}", kSmoothingPeriod); D = movingAverageType.AsIndicator($"{name}_D_{movingAverageType}", dSmoothingPeriod); WarmUpPeriod = rsiPeriod + stochPeriod + Math.Max(kSmoothingPeriod, dSmoothingPeriod); } /// /// Computes the next value of the following sub-indicators from the given state: /// K (%K) and D (%D) /// /// The input given to the indicator /// The input is returned unmodified. protected override decimal ComputeNextValue(IndicatorDataPoint input) { _rsi.Update(input); _recentRSIValues.Add(_rsi.Current.Value); if (!_recentRSIValues.IsReady) { return 0m; } var maxHigh = _recentRSIValues.Max(); var minLow = _recentRSIValues.Min(); decimal k = 100; if (maxHigh != minLow) { k = 100 * (_rsi.Current.Value - minLow) / (maxHigh - minLow); } K.Update(input.EndTime, k); D.Update(input.EndTime, K.Current.Value); return input.Value; } /// /// Resets this indicator and all sub-indicators /// public override void Reset() { _rsi.Reset(); _recentRSIValues.Reset(); K.Reset(); D.Reset(); base.Reset(); } } }