Home » Technical Analysis Functions » Understanding ta.stoch() Function in Pine Script

Understanding ta.stoch() Function in Pine Script

Photo of author
Published on

In this tutorial, we’ll dive into the ta.stoch() function in Pine Script, a crucial tool for traders and analysts looking to gauge market momentum by comparing a closing price to its price range over a given period. By the end, you’ll have a solid understanding of how to implement and interpret this function in your trading scripts.

What is ta.stoch()?

The ta.stoch() function computes the Stochastic Oscillator, a momentum indicator that shows the position of the current close relative to the high-low range over a specified period. It’s calculated with the formula:

100 * (close - lowest(low, period)) / (highest(high, period) - lowest(low, period))

This formula outputs a value that oscillates between 0 and 100, providing insights into overbought or oversold conditions in the market.


The function’s syntax is:

ta.stoch(source, peak, valley, period) → series float
  • source (series int/float): The data series, typically closing prices.
  • peak (series int/float): The series of high prices.
  • valley (series int/float): The series of low prices.
  • period (series int): The lookback period, determining the range of bars to consider.


  • source: This is the main series you’re analyzing, usually the closing prices of an asset.
  • peak and valley: These represent the high and low price series, respectively, across the asset’s trading period.
  • period: Defines the number of bars back to calculate the highest high and the lowest low.

Practical Example

Let’s create a simple script to apply the ta.stoch() function on a chart, visualizing the stochastic oscillator with a length of 14 periods.

indicator("Simple Stochastic Oscillator", overlay=false)
length = 14
stochValue = ta.stoch(close, high, low, length)

plot(stochValue, title="Stochastic Oscillator", color=color.blue)
hline(80, "Overbought", color=color.red)
hline(20, "Oversold", color=color.green)

Code Walkthrough

  1. //@version=5: This line specifies the version of Pine Script being used, in this case, version 5. Pine Script versions may have differences in syntax and functions, so it’s important to declare the correct version at the beginning of your script.
  2. indicator("Simple Stochastic Oscillator", overlay=false): This line declares a new indicator with the name “Simple Stochastic Oscillator”. The overlay=false parameter means that this indicator will be displayed in a separate window below the main chart, rather than overlaying the price chart.
  3. length = 14: This line sets a variable named length to 14. This variable represents the period or the number of bars used to calculate the stochastic oscillator. A period of 14 is commonly used in stochastic calculations.
  4. stochValue = ta.stoch(close, high, low, length): This line calculates the stochastic oscillator value using the built-in ta.stoch() function. It passes the close, high, and low price series, along with the previously defined length variable, as arguments. The result of this calculation is stored in the stochValue variable.
  5. plot(stochValue, title="Stochastic Oscillator", color=color.blue): This line plots the stochastic oscillator values on the chart. It sets the title of the plot to “Stochastic Oscillator” and uses blue color for the line.
  6. hline(80, "Overbought", color=color.red): This line draws a horizontal line at the value of 80 on the oscillator’s scale, labeling it as “Overbought”. The line is colored red. In stochastic oscillator analysis, values above 80 are typically considered to indicate overbought conditions.
  7. hline(20, "Oversold", color=color.green): Similarly, this line draws a horizontal line at the value of 20, labeling it as “Oversold” and coloring it green. Values below 20 are generally interpreted as indicating oversold conditions.

Key Takeaways

  • The ta.stoch() function is essential for identifying market momentum by comparing the closing price with its price range over a specific period.
  • The function’s flexibility allows for customization in the source data and period length, adapting to various trading strategies.
  • Recognizing the significance of the oscillator’s output values, specifically around the overbought (>80) and oversold (<20) levels, can be crucial for making informed trading decisions.

By integrating the ta.stoch() function into your Pine Script strategies, you can enhance your market analysis and potentially uncover valuable trading opportunities based on momentum shifts.

Leave a Comment