DocsStrategy APIindicators

STOCH (Stochastic Oscillator)

The Stochastic Oscillator compares a closing price to its price range over a given period. It produces two lines, %K and %D, oscillating between 0 and 100.

Declaration

function init(ctx) { ctx.indicator('stoch', 'STOCH', { kPeriod: 14, dPeriod: 3 }); }

Options

OptionTypeDefaultDescription
kPeriodnumber14%K lookback period
dPeriodnumber3%D smoothing period (SMA of %K)

Output

Returns an object with two series, accessed via underscore naming:

PropertyDescription
k%K line (fast stochastic)
d%D line (smoothed %K)

Accessing Values

function onBar(ctx, i) { const k = ctx.ind.stoch_k[i]; const d = ctx.ind.stoch_d[i]; }

Use Cases

Overbought / Oversold

function onBar(ctx, i) { const k = ctx.ind.stoch_k[i]; const d = ctx.ind.stoch_d[i]; // Buy when %K crosses above %D in oversold territory if (k < 20 && q.crossOver(ctx.ind.stoch_k, ctx.ind.stoch_d, i)) { ctx.order.market('ASSET', 1, { signal: 'buy', reason: 'stoch_oversold_cross' }); } // Sell when %K crosses below %D in overbought territory if (k > 80 && q.crossUnder(ctx.ind.stoch_k, ctx.ind.stoch_d, i)) { ctx.order.close('ASSET', { signal: 'sell', reason: 'stoch_overbought_cross' }); } }

Value Ranges

RangeInterpretation
0 - 20Oversold
20 - 80Neutral
80 - 100Overbought

Calculation

$$%K = \frac{Close - Lowest Low}{Highest High - Lowest Low} \times 100$$

$%D = SMA(%K, dPeriod)$

Related

indicatorstochasticmomentumoscillatorqsl