Skip to content

Commit

Permalink
Implements new indicator StochRSI - Stochastic Relative Strength Index (
Browse files Browse the repository at this point in the history
#8163)

* tests passing except ResetProperly and WarmsUpProperly

* doc

* minor fixes

* doc - return

* fix InitializeIndicator call

* workaround ResetsProperly

* fix WarmsUpProperly test

* remove WriteLine

* remove WriteLine

* cr

* fix data

* open high low defaults to close when these columns don't exist into data

* simplify using ternary operator

* better fix for ResetsProperly

* fix some code conventions issues

* fix some review issues

* Update StochasticRelativeStrengthIndex.cs
  • Loading branch information
femtotrader authored Jul 19, 2024
1 parent 3bdde74 commit 72a1bd4
Show file tree
Hide file tree
Showing 6 changed files with 714 additions and 2 deletions.
23 changes: 23 additions & 0 deletions Algorithm/QCAlgorithm.Indicators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1744,6 +1744,29 @@ public Rho ρ(Symbol symbol, Symbol mirrorOption = null, decimal? riskFreeRate =
return R(symbol, mirrorOption, riskFreeRate, dividendYield, optionModel, ivModel, resolution);
}


/// <summary>
/// Creates a new Stochastic RSI indicator which will compute the %K and %D
/// </summary>
/// <param name="symbol">The symbol whose Stochastic RSI we seek</param>
/// <param name="rsiPeriod">The period of the relative strength index</param>
/// <param name="stochPeriod">The period of the stochastic indicator</param>
/// <param name="kSmoothingPeriod">The smoothing period of K output</param>
/// <param name="dSmoothingPeriod">The smoothing period of D output</param>
/// <param name="movingAverageType">The type of moving average to be used</param>
/// <param name="resolution">The resolution</param>
/// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
/// <returns>A StochasticRelativeStrengthIndex configured with the specified periods and moving average type</returns>
[DocumentationAttribute(Indicators)]
public StochasticRelativeStrengthIndex SRSI(Symbol symbol, int rsiPeriod, int stochPeriod, int kSmoothingPeriod, int dSmoothingPeriod, MovingAverageType movingAverageType = MovingAverageType.Simple,
Resolution? resolution = null, Func<IBaseData, decimal> selector = null)
{
var name = CreateIndicatorName(symbol, $"SRSI({rsiPeriod},{stochPeriod},{kSmoothingPeriod},{dSmoothingPeriod})", resolution);
var indicator = new StochasticRelativeStrengthIndex(name, rsiPeriod, stochPeriod, kSmoothingPeriod, dSmoothingPeriod, movingAverageType);
InitializeIndicator(indicator, resolution, selector, symbol);
return indicator;
}

/// <summary>
/// Creates a new SuperTrend indicator.
/// </summary>
Expand Down
130 changes: 130 additions & 0 deletions Indicators/StochasticRelativeStrengthIndex.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* 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
{
/// <summary>
/// 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.
/// </summary>
public class StochasticRelativeStrengthIndex : Indicator, IIndicatorWarmUpPeriodProvider
{
private readonly RelativeStrengthIndex _rsi;
private readonly RollingWindow<decimal> _recentRSIValues;

/// <summary>
/// Gets the %K output
/// </summary>
public IndicatorBase<IndicatorDataPoint> K { get; }

/// <summary>
/// Gets the %D output
/// </summary>
public IndicatorBase<IndicatorDataPoint> D { get; }

/// <summary>
/// Gets a flag indicating when this indicator is ready and fully initialized
/// </summary>
public override bool IsReady => Samples >= WarmUpPeriod;

/// <summary>
/// Required period, in data points, for the indicator to be ready and fully initialized.
/// </summary>
public int WarmUpPeriod { get; }

/// <summary>
/// Initializes a new instance of the StochasticRelativeStrengthIndex class
/// </summary>
/// <param name="rsiPeriod">The period of the relative strength index</param>
/// <param name="stochPeriod">The period of the stochastic indicator</param>
/// <param name="kSmoothingPeriod">The smoothing period of K output (aka %K)</param>
/// <param name="dSmoothingPeriod">The smoothing period of D output (aka %D)</param>
/// <param name="movingAverageType">The type of moving average to be used for k and d</param>
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)
{
}

/// <summary>
/// Initializes a new instance of the StochasticRelativeStrengthIndex class
/// </summary>
/// <param name="name">The name of this indicator</param>
/// <param name="rsiPeriod">The period of the relative strength index</param>
/// <param name="stochPeriod">The period of the stochastic indicator</param>
/// <param name="kSmoothingPeriod">The smoothing period of K output</param>
/// <param name="dSmoothingPeriod">The smoothing period of D output</param>
/// <param name="movingAverageType">The type of moving average to be used</param>
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<decimal>(stochPeriod);

K = movingAverageType.AsIndicator($"{name}_K_{movingAverageType}", kSmoothingPeriod);
D = movingAverageType.AsIndicator($"{name}_D_{movingAverageType}", dSmoothingPeriod);

WarmUpPeriod = rsiPeriod + stochPeriod + Math.Max(kSmoothingPeriod, dSmoothingPeriod);
}

/// <summary>
/// Computes the next value of the following sub-indicators from the given state:
/// K (%K) and D (%D)
/// </summary>
/// <param name="input">The input given to the indicator</param>
/// <returns>The input is returned unmodified.</returns>
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;
}

/// <summary>
/// Resets this indicator and all sub-indicators
/// </summary>
public override void Reset()
{
_rsi.Reset();
_recentRSIValues.Reset();
K.Reset();
D.Reset();
base.Reset();
}
}
}
50 changes: 50 additions & 0 deletions Tests/Indicators/StochasticRelativeStrengthIndexTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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 NUnit.Framework;
using QuantConnect.Indicators;

namespace QuantConnect.Tests.Indicators
{
[TestFixture]
public class StochasticRelativeStrengthIndexTests : CommonIndicatorTests<IndicatorDataPoint>
{
protected override IndicatorBase<IndicatorDataPoint> CreateIndicator()
{
return new StochasticRelativeStrengthIndex(14, 14, 3, 3);
}

protected override string TestFileName => "spy_with_StochRSI.csv";

protected override string TestColumnName => "k";

protected override Action<IndicatorBase<IndicatorDataPoint>, double> Assertion =>
(indicator, expected) =>
Assert.AreEqual(expected, (double) ((StochasticRelativeStrengthIndex) indicator).K.Current.Value, 1e-3);

[Test]
public void ComparesWithExternalDataColumnD()
{
TestHelper.TestIndicator(
CreateIndicator() as StochasticRelativeStrengthIndex,
TestFileName,
"d",
ind => (double) ind.D.Current.Value
);
}

}
}
9 changes: 7 additions & 2 deletions Tests/Indicators/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,14 @@ public static void TestIndicatorReset(IndicatorBase<IndicatorDataPoint> indicato
{
var date = DateTime.Today;

foreach (var data in GetTradeBarStream(externalDataFilename, false))
foreach (var parts in GetCsvFileStream(externalDataFilename))
{
indicator.Update(date, data.Close);
if (!(parts.ContainsKey("Close")))
{
Assert.Fail("Didn't find column 'Close'");
break;
}
indicator.Update(date, parts.GetCsvValue("close").ToDecimal());
}

Assert.IsTrue(indicator.IsReady);
Expand Down
3 changes: 3 additions & 0 deletions Tests/QuantConnect.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,9 @@
<None Update="TestData\spy_with_McGinleyDynamic.csv">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestData\spy_with_StochRSI.csv">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestData\spy_with_vwma.csv">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
Loading

0 comments on commit 72a1bd4

Please sign in to comment.