Click or drag to resize

Stochastic Oscillator

The Stochastic compares where a security’s price closed relative to its price range over a given time period. The Stochastic is displayed as two lines. The main line is called %K. The second line, called %D, is a Moving Average of %K. The %K line is usually displayed as a solid line and the %D line is usually displayed as a dotted line.

Market Signals

Buy when the Oscillator (either %K or %D) falls below a specific level (e.g., 20) and then rises above that level. Sell when the Oscillator rises above a specific level (e.g., 80) and then falls below that level.

Calculation

Stochastic Oscillator 001

Stochastic Oscillator 002

Stochastic Oscillator 003

Chart Example

Stochastic Oscillator 004

Implementation and Usage

To initialize Stochastic indicator use one of the following constructors:

Stochastic – sets default values: rawPeriod = 14, kPeriod = 3, dPeriod = 3

Stochastic(Int32, Int32, Int32) – sets values for periods

Stochastic(Int32, IAverager, IAverager) – sets raw periods and the type of averaging

Use

RawStochastic

PercentK

PercentD

properties to get current value

Example
C#
 1// Create new instance
 2Stochastic stochastic = new Stochastic(28, 9, 6);
 3
 4// Number of stored values
 5stochastic.HistoryCapacity = 2;
 6
 7// Add new data point
 8stochastic.Add(Bars.Current.Open, Bars.Current.High, Bars.Current.Low, Bars.Current.Close, Bars.Current.Volume);
 9
10// Get indicator value
11double IndicatorValue = stochastic.PercentK;
12double IndicatorValue = stochastic.PercentD;
13// Get previous value
14if (stochastic.HistoryCount == 2)
15{
16    double IndicatorPrevValue = stochastic[1].PercentK;
17    double IndicatorPrevValue = stochastic[1].PercentD;
18}

Stochastic with custom averaging

C#
 1// Create new instance
 2// Sets raw period = 28
 3// and EMA instead SMA
 4Stochastic stochastic = new Stochastic(28, new Ema(9), new Ema(6));
 5
 6// Add new data point
 7stochastic.Add(Bars.Current.Open, Bars.Current.High, Bars.Current.Low, Bars.Current.Close, Bars.Current.Volume);
 8
 9// Get indicator value
10double IndicatorValue = stochastic.PercentK;
11double IndicatorValue = stochastic.PercentD;