βœ–

Home Forums Pine Script How can I create a Stochastic Oscillator in Pine Script?

How can I create a Stochastic Oscillator in Pine Script?

  • Author
    Posts
    • I’m trying to create a Stochastic Oscillator in Pine Script to understand how the %K and %D lines are calculated instead of simply using the built-in indicator.

      I want to plot both lines and add the standard 80 and 20 levels so the overbought and oversold zones are easy to identify.

      I’m also looking for a way to keep the settings configurable, such as the %K length, %D length, and smoothing.

      Can anyone help me with the correct way to implement this in Pine Script?

    • Yes sir, you can definitely create a Stochastic Oscillator in Pine Script.

      The Stochastic Oscillator mainly uses the current close compared with the highest high and lowest low over a selected period.

      -> %K measures the current price position within that high-low range.

      -> %D is a moving average of the %K line.

      You can create a basic version using:

      //@version=6
      indicator("Custom Stochastic", overlay=false)
      
      kLength = input.int(14, "%K Length")
      kSmooth = input.int(3, "%K Smoothing")
      dLength = input.int(3, "%D Length")
      
      rawK = ta.stoch(close, high, low, kLength)
      k = ta.sma(rawK, kSmooth)
      d = ta.sma(k, dLength)
      
      kPlot = plot(k, "%K")
      dPlot = plot(d, "%D", color = color.orange)
      
      overbought = hline(80, "Overbought")
      MiddLine = hline(50, "Midline")
      oversold = hline(20, "Oversold")
      
      fill(overbought, oversold, color=color.new(color.blue, 90))

      -> The %K line represents the current price position within the selected high-low range.

      -> The %D line is a smoothed version of %K.

      -> 80 is commonly used as the overbought level.

      -> 20 is commonly used as the oversold level.

      -> The input values allow to adjust the calculation from the indicator settings.

      => This gives you a configurable Stochastic Oscillator without depending on the built-in chart indicator.

      So that’s how you can create a basic Stochastic Oscillator in Pine Script.

Viewing 1 reply thread
×

Start a Discussion

Get help from the AlgoDelta community.

×

Welcome Back!

Enter your email to sign in or create an account. No passwords needed.

⚠️

Delete This?

Are you sure you want to delete this? This action is permanent and cannot be undone.

Scroll to Top