Skip to content

Commit

Permalink
Refactor PSO
Browse files Browse the repository at this point in the history
- Changed "STO" to "PSO"
- Removed unused imports
- Updated private fields
- Minor code cleanup
  • Loading branch information
JosueNina committed Nov 12, 2024
1 parent 789725d commit 42ba2d9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Algorithm/QCAlgorithm.Indicators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2019,7 +2019,7 @@ public Stochastic STO(Symbol symbol, int period, Resolution? resolution = null,
[DocumentationAttribute(Indicators)]
public PremierStochasticOscillator PSO(Symbol symbol, int period, int emaPeriod, Resolution? resolution = null, Func<IBaseData, TradeBar> selector = null)
{
var name = CreateIndicatorName(symbol, $"STO({period},{emaPeriod})", resolution);
var name = CreateIndicatorName(symbol, $"PSO({period},{emaPeriod})", resolution);
var premierStochasticOscillator = new PremierStochasticOscillator(name, period, emaPeriod);
InitializeIndicator(premierStochasticOscillator, resolution, selector, symbol);
return premierStochasticOscillator;
Expand Down
35 changes: 15 additions & 20 deletions Indicators/PremierStochasticOscillator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using QuantConnect.Data.Market;

namespace QuantConnect.Indicators
Expand All @@ -34,13 +31,13 @@ public class PremierStochasticOscillator : BarIndicator, IIndicatorWarmUpPeriodP
/// firstSmoothingEma performs the first smoothing of the Normalized Stochastic (0.1 * (Fast%K - 50)),
/// and doubleSmoothingEma applies a second smoothing on the result of _ema1, resulting in the Double-Smoothed Normalized Stochastic
/// </summary>
private readonly ExponentialMovingAverage firstSmoothingEma;
private readonly ExponentialMovingAverage doubleSmoothingEma;
private readonly ExponentialMovingAverage _firstSmoothingEma;
private readonly ExponentialMovingAverage _doubleSmoothingEma;

/// <summary>
/// Stochastic oscillator used to calculate the K value.
/// </summary>
private readonly Stochastic stochastic;
private readonly Stochastic _stochastic;

/// <summary>
/// The warm-up period necessary before the PSO indicator is considered ready.
Expand All @@ -56,9 +53,9 @@ public class PremierStochasticOscillator : BarIndicator, IIndicatorWarmUpPeriodP
/// <param name="emaPeriod">The period for EMA calculations.</param>
public PremierStochasticOscillator(string name, int period, int emaPeriod) : base(name)
{
stochastic = new Stochastic(name, period, period, period);
firstSmoothingEma = new ExponentialMovingAverage(emaPeriod);
doubleSmoothingEma = firstSmoothingEma.EMA(emaPeriod);
_stochastic = new Stochastic(name, period, period, period);
_firstSmoothingEma = new ExponentialMovingAverage(emaPeriod);
_doubleSmoothingEma = _firstSmoothingEma.EMA(emaPeriod);
WarmUpPeriod = period + 2 * (emaPeriod - 1);
}

Expand All @@ -75,7 +72,7 @@ public PremierStochasticOscillator(int period, int emaPeriod)
/// <summary>
/// Gets a flag indicating when this indicator is ready and fully initialized
/// </summary>
public override bool IsReady => doubleSmoothingEma.IsReady;
public override bool IsReady => _doubleSmoothingEma.IsReady;

/// <summary>
/// Computes the Premier Stochastic Oscillator (PSO) based on the current input.
Expand All @@ -87,25 +84,23 @@ public PremierStochasticOscillator(int period, int emaPeriod)
/// <returns>The computed value of the PSO.</returns>
protected override decimal ComputeNextValue(IBaseDataBar input)
{
stochastic.Update(input);
if (!stochastic.IsReady)
if (!_stochastic.Update(input))
{
return decimal.Zero;
}

var k = stochastic.FastStoch.Current.Value;
var k = _stochastic.FastStoch.Current.Value;
var nsk = 0.1m * (k - 50);
firstSmoothingEma.Update(new IndicatorDataPoint(input.Time, nsk));
if (!firstSmoothingEma.IsReady)
if (!_firstSmoothingEma.Update(new IndicatorDataPoint(input.Time, nsk)))
{
return decimal.Zero;
}

if (!doubleSmoothingEma.IsReady)
if (!_doubleSmoothingEma.IsReady)
{
return decimal.Zero;
}
var expss = (decimal)Math.Exp((double)doubleSmoothingEma.Current.Value);
var expss = (decimal)Math.Exp((double)_doubleSmoothingEma.Current.Value);
return (expss - 1) / (expss + 1);
}

Expand All @@ -114,9 +109,9 @@ protected override decimal ComputeNextValue(IBaseDataBar input)
/// </summary>
public override void Reset()
{
stochastic.Reset();
firstSmoothingEma.Reset();
doubleSmoothingEma.Reset();
_stochastic.Reset();
_firstSmoothingEma.Reset();
_doubleSmoothingEma.Reset();
base.Reset();
}
}
Expand Down
4 changes: 0 additions & 4 deletions Tests/Indicators/PremierStochasticOscillatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,9 @@
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;
using QuantConnect.Data.Market;
using QuantConnect.Indicators;
using QuantConnect.ToolBox.RandomDataGenerator;

namespace QuantConnect.Tests.Indicators
{
Expand Down

0 comments on commit 42ba2d9

Please sign in to comment.