Stochastic Oscillator

Technical Analysis Indicator: stoch

Fork me on GitHub

Function Prototype

/* Stochastic Oscillator */
/* Type: indicator */
/* Input arrays: 3    Options: 3    Output arrays: 2 */
/* Inputs: high, low, close */
/* Options: %k period, %k slowing period, %d period */
/* Outputs: stoch_k, stoch_d */
int ti_stoch_start(TI_REAL const *options);
int ti_stoch(int size,
      TI_REAL const *const *inputs,
      TI_REAL const *options,
      TI_REAL *const *outputs);

Description

This documentation is still a work in progress. It has omissions, and it probably has errors too. If you see any issues, or have any general feedback, please get in touch.

The Stochastic Oscillator indicator calculates two values, %k and %d.

It takes three options: the %k period n, the %k slowing period m, and the %d period p.

The calculation is as follows:

$$max_{t} = maximum(high_{t}, high_{t-1}, ..., high_{t-n+1}) $$
$$min_{t} = minimum(low_{t}, low_{t-1}, ..., low_{t-n+1}) $$
$$fastk_{t} = 100 \frac{close_{t} - min_{t}}{max_{t} - min_{t}}$$

Then the first output, %k, is a Simple Moving Average of period m applied to fastk. The second output, %d, is a Simple Moving Average of period p applied to %k.

$$k_{t} = \frac{1}{m} \sum_{i=0}^{m-1} fastk_{t-i}$$
$$d_{t} = \frac{1}{p} \sum_{i=0}^{p-1} k_{t-i}$$

See Also

References

Example Usage

Calling From C

/* Example usage of Stochastic Oscillator */
/* Assuming that 'high', 'low', and 'close' are pre-loaded arrays of size 'in_size'. */
TI_REAL *inputs[] = {high, low, close};
TI_REAL options[] = {5, 3, 3}; /* %k period, %k slowing period, %d period */
TI_REAL *outputs[2]; /* stoch_k, stoch_d */

/* Determine how large the output size is for our options. */
const int out_size = in_size - ti_stoch_start(options);

/* Allocate memory for each output. */
outputs[0] = malloc(sizeof(TI_REAL) * out_size); assert(outputs[0] != 0); /* stoch_k */
outputs[1] = malloc(sizeof(TI_REAL) * out_size); assert(outputs[1] != 0); /* stoch_d */

/* Run the actual calculation. */
const int ret = ti_stoch(in_size, inputs, options, outputs);
assert(ret == TI_OKAY);

Calling From Lua (with Tulip Chart bindings)

-- Example usage of Stochastic Oscillator
stoch_k, stoch_d = ti.stoch(high, low, close, 5, 3, 3)

Example Calculation

%k period = 5, %k slowing period = 3, %d period = 3

datehighlowclosestoch_kstoch_d
2005-11-0182.1581.2981.59
2005-11-0281.8980.6481.06
2005-11-0383.0381.3182.87
2005-11-0483.3082.6583.00
2005-11-0783.8583.0783.61
2005-11-0883.9083.1183.15
2005-11-0983.3382.4982.84
2005-11-1084.3082.3083.99
2005-11-1184.8484.1584.5577.3975.70
2005-11-1485.0084.1184.3683.1378.01
2005-11-1585.9084.0385.5384.8781.79
2005-11-1686.5885.3986.5488.3685.45
2005-11-1786.9885.7686.8995.2589.49
2005-11-1888.0087.1787.7796.7493.45
2005-11-2187.8787.0187.2991.0994.36

Chart

 

Other Indicators

Previous indicator: Standard Error Over Period

Next indicator: Vector Subtraction

Random indicator: Market Facilitation Index