Indicator · Pine Script v6 · Own pane

KDJ Indicator

by YooooungLee

This indicator calculates the standard KDJ oscillator lines using the raw stochastic value (RSV) smoothed with exponential moving averages.

View on GitHub

GitHub stars4on the repository
Pine versionv6current
Lines of code40
Settings3inputs
LicenceApache-2.0
Last update10 months ago29 Dec 2025

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

40 lines of Pine Script v6 from Quant-code/indicator/KDJ.pine, published under Apache-2.0. Keep the author's credit and licence lines when you reuse it.

KDJ.pine
//@version=6
indicator("KDJ指标", overlay=false)  // 定义指标名称为“KDJ Indicator”,不覆盖在价格图表上

// Input parameters
length = input.int(9, title="KDJ Length")  // 输入参数:KDJ的周期长度
smoothK = input.int(3, title="K Smoothing")  // 输入参数:K值的平滑周期
smoothD = input.int(3, title="D Smoothing")  // 输入参数:D值的平滑周期

// Calculate RSV
highestHigh = ta.highest(high, length)  // 计算指定周期内的最高价
lowestLow = ta.lowest(low, length)  // 计算指定周期内的最低价
rsv = (close - lowestLow) / (highestHigh - lowestLow) * 100  // 计算RSV(未成熟随机值)

// Calculate K, D, and J
k = ta.ema(rsv, smoothK)  // 计算K值,使用RSV的指数移动平均
d = ta.ema(k, smoothD)  // 计算D值,使用K值的指数移动平均
j = 3 * d - 2 * k  // 计算J值,J值为k值的三倍减去d值的两倍

// Plot K, D, and J
plot(k, color=color.blue, title="K")  // 绘制K值,颜色为蓝色
plot(d, color=color.orange, title="D")  // 绘制D值,颜色为橙色
plot(j, color=color.white, title="J")  // 绘制J值,颜色为白色

// Add overbought and oversold levels
hline(80, "Overbought", color=color.red, linestyle=hline.style_dotted)  // 添加超买水平线,值为80,颜色为红色,虚线样式
hline(20, "Oversold", color=color.green, linestyle=hline.style_dotted)  // 添加超卖水平线,值为20,颜色为绿色,虚线样式

// Calculate cross signals
crossBuy = ta.crossover(k - d, 0)  // 判断K-D从小于0到大于0的交叉(买入信号)
crossSell = ta.crossunder(k - d, 0)  // 判断K-D从大于0到小于0的交叉(卖出信号)

// Plot buy and sell signals on the price chart
plotshape(crossBuy, style=shape.labelup, location=location.belowbar, color=color.red, text="多", title="提示做多信号")  // 绘制买入信号
plotshape(crossSell, style=shape.labeldown, location=location.abovebar, color=color.green, text="空", title="提示做空信号")  // 绘制卖出信号

// Detect bearish divergence (顶背离)
bearishDivergence = ta.highest(close, length) > ta.highest(close[1], length) and ta.highest(j, length) <= ta.highest(j[1], length)

// Plot bearish divergence signal
plotshape(bearishDivergence, style=shape.triangledown, location=location.abovebar, color=color.green, text="↓", title="顶背离信号")

Overview

KDJ Indicator plots the K, D, and J lines alongside horizontal overbought (80) and oversold (20) levels in a separate pane. The script marks bullish and bearish crossover signals between K and D, and detects bearish divergence between price highs and the J line over the lookback period. Users can adjust the calculation period and smoothing lengths for both K and D.

Details

Published by

YooooungLee on GitHub

Draws

Separate pane below the chart

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
KDJ Lengthint9length
K Smoothingint3smoothK
D Smoothingint3smoothD
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

This indicator calculates momentum setups based on the Relative Strength Index (RSI) and plots in a separate pane. It offers multiple scanning modes such as Continuous Break, Continuous Retracement, Flip Break, Flip Retracement, and both regular and hidden div

Indicator v6 Oscillators & momentumDivergence Repaints
Updated 1 year ago · MPL-2.0 GitHub repository of Momentum Setup - RSI Directional Momentum Indicator
330 stars

This indicator calculates the MACD oscillator from a configurable source with fast and slow lengths, then plots the histogram, MACD line, and signal line in a separate pane. It detects regular bullish divergences (price lower lows with oscillator higher lows),

Indicator v5 Oscillators & momentumDivergence
Updated 2 months ago · None stated GitHub repository of MACD + Divergences [Alorse]
228 stars

This indicator calculates the Relative Strength Index (RSI) across four user-selected timeframes and plots the results in a separate pane. It identifies regular bullish and bearish divergences between price pivot points and the primary timeframe RSI value. Add

Indicator v6 Oscillators & momentumDivergence
Updated 3 months ago · None stated GitHub repository of Multi-Timeframe RSI with Divergence

Bubbles Indicator

by capissimo
114 stars

The Bubbles Indicator measures volatility squeezes and expansion bursts using Bollinger Bands combined with a sinusoid oscillator and RSI divergence. It processes price data through scaled Bollinger Bands to highlight volatility changes while confirming trend

Indicator v4 Volatility & bandsOscillators & momentum Repaints
Updated 1 year ago · None stated GitHub repository of Bubbles Indicator

RSI [SystemAlpha]

by iamc1oud
56 stars

This indicator plots a standard Relative Strength Index (RSI) in a separate pane alongside customizable overbought and oversold threshold levels with shaded fills. It identifies pivot highs and lows on the oscillator to detect regular and hidden bullish and be

Indicator v4 Oscillators & momentumDivergence Repaints
Updated 2 months ago · None stated GitHub repository of RSI [SystemAlpha]
55 stars

This indicator calculates a 14-period RSI from close and plots it in a separate pane with dotted overbought and dashed oversold lines at 70 and 30. It marks bullish divergence when a five-bar pivot low in price is lower than the price twenty bars earlier while

Indicator v6 Oscillators & momentumDivergence
Updated 6 months ago · Apache-2.0 GitHub repository of RSI Divergence Detector

More Oscillators & momentum scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers