forked from QuantConnect/Lean
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements new indicator StochRSI - Stochastic Relative Strength Index (
QuantConnect#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
1 parent
2f9290b
commit 7c78835
Showing
6 changed files
with
714 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.