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
| Option | Type | Default | Description |
|---|---|---|---|
kPeriod | number | 14 | %K lookback period |
dPeriod | number | 3 | %D smoothing period (SMA of %K) |
Output
Returns an object with two series, accessed via underscore naming:
| Property | Description |
|---|---|
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
| Range | Interpretation |
|---|---|
| 0 - 20 | Oversold |
| 20 - 80 | Neutral |
| 80 - 100 | Overbought |
Calculation
$$%K = \frac{Close - Lowest Low}{Highest High - Lowest Low} \times 100$$
$%D = SMA(%K, dPeriod)$
Related
- Stochastic RSI - Stochastic applied to RSI
- RSI - Momentum oscillator
- Williams %R - Similar range-based oscillator
indicatorstochasticmomentumoscillatorqsl