Sidechair

ta.stoch()

Browse the reference

Returns the raw stochastic: where source sits between the lowest low and highest high of the last length bars, from 0 to 100.

Try it in the editor

ta.stoch computes 100 * (source - lowestLow) / (highestHigh - lowestLow), where the extremes come from the low and high series you pass over the last length bars. It is the unsmoothed %K.

A close at the top of the range gives 100, at the bottom 0. %K starts on bar number length, when the first full range exists. A window where high equals low everywhere has no range, and the result is na.

Syntax

Pine Script
ta.stoch(source: series int/float, high: series int/float, low: series int/float, length: series int) -> series float

Arguments

NameTypeDefaultDescription
sourceseries int/floatrequiredSeries positioned inside the range, usually close.
highseries int/floatrequiredSeries searched for the highest value, usually high.
lowseries int/floatrequiredSeries searched for the lowest value, usually low.
lengthseries intrequiredNumber of bars in the range. A whole number of 1 or more.

Returns

series float: a value from 0 to 100, or na while the range window is not full.

Remarks

  • The familiar %K and %D lines are smoothed versions: ta.sma(ta.stoch(close, high, low, 14), 3) and an average of that.

Example

Pine ScriptSmoothed stochasticOpen in editor
//@version=6
indicator("Stochastic")
raw = ta.stoch(close, high, low, 14)
k = ta.sma(raw, 3)
d = ta.sma(k, 3)
plot(k, "%K", color = color.blue)
plot(d, "%D", color = color.orange)
hline(80)
hline(20)

See also

Runs on Sidechair