Stochastic Oscillator

Definition

Mathematical Formula

Usage

Stochastic Oscillator is a momentum oscillator. This means it gives an indication about overbought & oversold trading securities. When a security is overbought, it means it’s price is too high and most probably it will reverse direction to begin price decrease. When a security is oversold, it’s usually indication that the security price is too low and it may be an indication that price is going to revert to up direction.

Stochastic Oscillator is calculated as a percentage from current price to historical prices. As it’s a percentage, Stochastic Oscillator value at any point is bound between 0 and 100. Tarditionally When Stochastic Oscillator is below 20, it’s considered oversold. When it’s above 80, it’s considered overbought.

Stochastic Oscillator Value is mathematically calculated as the relative value of the current security price minus historical lowest to the differnce between historical high and low. Usually the period studied is 14 period unit although this can be varied and other values can be used.

A Signal indicator (%D) is usually used with Stochastic Oscillator. The Signal value is simple the 3-period moving average of the stochastic Oscillator (%K).

Stochastic Oscillator is usually drawn in a separate chart below the main price chart with horizontal lines identifying the 20 and 80 percent values.

%K=(C−L14H14−L14)×100

where:

C = The most recent closing price

L14 = The lowest price traded of the 14 previoustrading sessions

H14 = The highest price traded during the same14-day period

%K = The current value of the stochastic indicator​

When the Stochastic Oscillator is in the above 80 Zone, it’s an indication that the security is overbought. However a security can still be traded for longer time periods and stay in the overbought zone. When the Stochastic however starts to leave the above 80 zone and break into the “middle” (20 - 80) Zone, it’s a good sell signal (or taking short position).

Conversely the stochastic oscillator below 20 is in the oversold zone. But, this is not enough indicator as a security can stay in oversold zone for some time. If the stochastic hover breaks the 20 zone and go into the “middle” (20 - 80) Zone, it’s a good buy signal (or taking long position).

Stochastic Oscillator in Pine Script V6

//@version=6

indicator("Stochastic Oscillator (Built-in)", shorttitle="Stoch", overlay=false)

// Input parameters for the Stochastic Oscillator

lenK = input.int(14, title="%K Length", minval=1)

lenD = input.int(3, title="%D Length", minval=1)

smoothK = input.int(3, title="%K Smoothing", minval=1) // Often used for Fast/Slow Stochastic

// Calculate Stochastic Oscillator using the built-in ta.stoch function

// ta.stoch(source, high, low, length) returns a tuple: [k, d]

[k, d] = ta.stoch(close, high, low, lenK)

// Note: The 'smoothK' input is typically used to smooth the %K before calculating %D.

// The built-in ta.stoch function's 'length' parameter already incorporates this smoothing

// for the %K line itself if you consider it the "Slow Stochastic %K".

// If you need further smoothing or a "Fast Stochastic" where 'smoothK' applies to the raw %K,

// you would implement it manually as in the previous version.

// For standard Slow Stochastic, ta.stoch(close, high, low, lenK) directly gives the %K and %D.

// If your 'smoothK' is meant to be the period for the %K line's internal smoothing,

// you might pass it as the last argument to ta.stoch, depending on how you interpret the parameters.

// For typical Slow Stochastic, the 'length' parameter of ta.stoch acts as the %K length.

// The %D length is then applied as a simple moving average on the resulting %K.

// Plotting the lines

plot(k, color=color.blue, title="%K Line")

plot(d, color=color.red, title="%D Line")

// Plotting Overbought/Oversold levels

hLevel = input.int(80, title="Overbought Level")

lLevel = input.int(20, title="Oversold Level")

hline(hLevel, "Overbought", color.gray, linestyle=hline.style_dashed)

hline(lLevel, "Oversold", color.gray, linestyle=hline.style_dashed)

References and Sources

https://www.investopedia.com/articles/technical/073001.asp

https://www.oanda.com/sg-en/trading/learn/indicators-oscillators/stochastic-oscillator/