Stochastic Oscillator
Created by George Lane, the Stochastic Oscillator, also known as KDJ Index, is a momentum oscillator that compares current price with recent highs and lows and is presented on a scale of 0 to 100. [Discuss] 💬
// C# usage syntax (standard)
IReadOnlyList<StochResult> results =
bars.ToStoch(lookbackPeriods, signalPeriods, smoothPeriods);
// advanced customization
IReadOnlyList<StochResult> results =
bars.ToStoch(lookbackPeriods, signalPeriods, smoothPeriods,
kFactor, dFactor, movingAverageType);Create a buffer list and add each bar as it arrives:
StochList stochList = new(lookbackPeriods, signalPeriods, smoothPeriods);
// call from your WebSocket or SSE message handler
void OnBarReceived(IBar bar) => stochList.Add(bar);
// the list itself holds the results; fills as the handler runs
IReadOnlyList<StochResult> results = stochList;See Buffer lists for the full usage guide.
Subscribe to a BarHub and add each bar as it arrives:
BarHub barHub = new();
StochHub observer = barHub.ToStochHub(lookbackPeriods, signalPeriods, smoothPeriods);
// call from your WebSocket or SSE message handler
void OnBarReceived(IBar bar) => barHub.Add(bar);
// results fill as the handler runs
IReadOnlyList<StochResult> results = observer.Results;See Stream hubs for the full usage guide.
Parameters
| param | description |
|---|---|
lookbackPeriodsint | Lookback period (N) for the oscillator (%K). Must be greater than 0. Default is 14. |
signalPeriodsint | Smoothing period for the signal (%D). Must be greater than 0. Default is 3. |
smoothPeriodsint | Smoothing period (S) for the Oscillator (%K). "Slow" stochastic uses 3, "Fast" stochastic uses 1. Must be greater than 0. Default is 3. |
kFactordouble | Optional. Weight of %K in the %J calculation. Must be greater than 0. Default is 3. |
dFactordouble | Optional. Weight of %D in the %J calculation. Must be greater than 0. Default is 2. |
movingAverageTypeMaType | Optional. Type of moving average (SMA or SMMA) used for smoothing. See MaType enum options below. Default is MaType.SMA. |
Historical price bars requirements
You must have at least N+S periods of bars to cover the warmup and convergence periods.
bars is a collection of generic TBar historical price bars. It should have a consistent frequency (day, hour, minute, etc). See the Guide for more information.
MaType enum options
These are the supported moving average types:
| enum value | moving average |
|---|---|
MaType.SMA | Simple Moving Average (default) |
MaType.SMMA | Smoothed Moving Average |
Response
IReadOnlyList<StochResult>- This method returns a time series of all available indicator values for the
barsprovided. - It always returns the same number of elements as there are in the historical price bars.
- It does not return a single incremental indicator value.
- The first
N+S-2periods will havenullOscillator values since there's not enough data to calculate.
StochList : IReadOnlyList<StochResult>- The list itself holds the results, one for each bar or value added, in timestamp order.
- It keeps up to 100,000 results by default and drops the oldest beyond that; set
MaxListSizeto keep fewer. - The first
N+S-2periods will havenullOscillator values since there's not enough data to calculate.
IReadOnlyList<StochResult> results = observer.Results;observer.Resultsholds the results, one for each bar in theBarHubcache, in timestamp order.- Results update as bars arrive, including bars that arrive late or are revised.
- The cache keeps up to 100,000 results by default; set
maxCacheSizeon theBarHubto keep fewer. - The first
N+S-2periods will havenullOscillator values since there's not enough data to calculate.
🚩 ⚞ Convergence warning
The first N+100 periods will have decreasing magnitude, convergence-related precision errors that can be as high as ~5% deviation in indicator values for earlier periods when using MaType.SMMA. Standard use of MaType.SMA does not have convergence-related precision errors.
StochResult
| property | description |
|---|---|
TimestampDateTime | Date from evaluated TBar |
Oscillator or Kdouble? | %K Oscillator |
Signal or Ddouble? | %D Simple moving average of Oscillator |
PercentJ or Jdouble? | %J is the weighted divergence of %K and %D: %J = kFactor × %K - dFactor × %D |
Note: aliases of K, D, and J are also provided. They can be used interchangeably with the standard outputs.
Chaining
This indicator must be generated from bars and cannot be generated from results of another chain-enabled indicator or method.
Results can be further processed on Oscillator with additional chain-enabled indicators.
// example
var results = bars
.ToStoch(..)
.ToSlope(..);See Chaining indicators for more.
Add each new result to another chain-enabled buffer list to process Oscillator further.
// example
StochList stochList = new(..);
SlopeList slopeList = new(..);
void OnBarReceived(IBar bar)
{
stochList.Add(bar);
slopeList.Add(stochList[^1]);
}See Chaining buffer lists for more.
Stream hub results cannot be further chained with additional hubs.
Utilities
Call these on the list itself, as in stochList.Find(lookupDate). .Condense() returns a new list and leaves stochList unchanged.
Warmup removal is left out. Once the list is full it drops its oldest results, so trimming by position would remove valid values.
Call these on observer.Results, as in observer.Results.Find(lookupDate). .Condense() returns a new list and leaves the hub's results unchanged.
Warmup removal is left out. Once the cache is full the hub drops its oldest results, so trimming by position would remove valid values.
See Utilities and helpers for more information.