Bentrix Docs
HomePricingDashboard

KDJ Stochastic KDJ

Oscillatorsoscillator3 linesin every package

A stochastic's %K and %D with a third line, J, three times %K less twice %D, which runs ahead of both and beyond 0 and 100.

It measures where the close sits in its recent range, and how fast that is changing.

01The study, live

Live figureScroll to zoom, drag the bars to pan.
2080
bar —O — H — L — C —main line —signal —third line —first value at —
Show where:

Computed by Bentrix’s own code over the same synthetic bars on every page — an illustration of how the study behaves, not a market.

What this figure shows

The figure is computed over 300 sample bars. KDJ has no value on the first 12 — the bars it needs before it can answer — and its first value stands on bar 13. Its line runs between 6.41 and 93.87 on these bars, and the last value is 49.68. Counting the conditions the study declares on these bars: crosses above 20 8 times; crosses below 20 8 times; crosses above 80 8 times; crosses below 80 9 times; +DI crosses above −DI 29 times; +DI crosses below −DI 29 times. Press a condition under the figure to see each place marked. As states rather than moments: is above 20 on 218 bars; is below 20 on 70 bars; is above 80 on 69 bars; is below 80 on 219 bars; +DI is above −DI on 147 bars; +DI is below −DI on 141 bars.

02Its parts, named

The bars it readseach bar's open, high, low and closeWarm-upno value on the first 12 bars: thestudy needs them before it cananswerThe third linethe study's third valueThe second linedrawn with the main line: its signalor its partnerLevel 80a fixed reference the study is readagainstThe main linethe study's own value on each barLevel 20a fixed reference the study is readagainst
Computed by Bentrix’s own code over the sample bars. It is drawn on once, as you reach the figure.

03What you see

KDJ is three lines in a pane of its own, with levels at 20 and 80. K and D are a stochastic's two lines and stay between 0 and 100. J is drawn from them — three of K less two of D — so it runs ahead of K whenever K and D are apart, and it leaves the 0–100 scale at both ends. The study is popular in Asian markets.

04Where it comes from

The stochastic underneath is credited to George C. Lane, who taught and wrote about it from the 1950s on: in a rise, closes tend to sit near the top of the recent range; in a fall, near the bottom. KDJ is the form of it used on Chinese and other Asian charting platforms, where K and D are smoothed by a running average that takes a third of each new value, and the J line is added.

05The formula

The raw value is the close's place in the range of the last N bars (9 by default):

RSVt=100·Ct−min(LN)max(HN)−min(LN)

K smooths it and D smooths K, each over 3 bars by default with Wilder's average (a third of the new value, two thirds of the old):

Kt=23Kt−1+13RSVt,Dt=23Dt−1+13Kt,Jt=3Kt−2Dt
Ctthe close
max(HN), min(LN)the highest high and the lowest low of the last N bars
RSVtthe raw value: the close's place in that range, 0 to 100

The two smoothing periods and the kind of each average are inputs.

06A worked example

K and D are a stochastic's two lines. Take K = 80.0 and D = 70.0.

J = 3 × 80.0 − 2 × 70.0 = 100.0

J is K pushed away from D by twice the gap between them (80 + 2 × 10). When K is above D, J is above K; when K falls below D, J drops below both. With K = 85.0 and D = 70.0, J = 115.0 — beyond the 0–100 scale that confines the other two.

With K = 20.0 and D = 30.0: J = 3 × 20.0 − 2 × 30.0 = 0.0.

On real bars: take eight with highs 21, 22, 24, 24, 25, 26, 26, 27, lows 18, 19, 20, 21, 21, 23, 23, 23 and closes 20, 21, 23, 22, 24, 25, 24, 26, a range of 3 bars and both smoothings at 3. On the last bar K reads 72.243 and D 73.326, so J = 3 × 72.243 − 2 × 73.326 = 70.08: K is a little below D, and J is below both.

07Built in Assay

The same study written in Assay, Bentrix’s script language, and drawn by Bentrix itself as the script’s graph: each block is one step of the formula. On the ladder the steps stand in the script’s order, one a row; on the board each wire carries a value from the block that gives it to the block that reads it, left to right. The script computes exactly what the study computes — Bentrix checks the two against each other, candle by candle.

THE SCRIPT’S GRAPH
//@version=6
indicator("KDJ Stochastic")
fastKPeriod = input.int(9, "Fast-K Period")
slowKPeriod = input.int(3, "Slow-K Period")
slowKMa = input.float(13.0, "Slow-K MA")
slowDPeriod = input.int(3, "Slow-D Period")
slowDMa = input.float(13.0, "Slow-D MA")
[k, d, j] = talib.KDJ(fastKPeriod, slowKPeriod, slowKMa, slowDPeriod, slowDMa)
// A stochastic's %K and %D, and J: three of %K less two of %D.
plot(k, "K")
plot(d, "D")
plot(j, "J")
hline(20)
hline(80)

Paste it into the Assay IDE to put it on a chart, or to change it. Looking at the graph is free in every package; editing it as blocks is Plus.

08Readings — what traders look for

K and D above 80, or below 20 are read as the close sitting near the top, or the bottom, of the recent range.

K crossing D is read as the close's position within the range changing direction; J crosses both on the same bar.

J beyond 100 or below 0 is read as K running far ahead of D — the stochastic moving fast.

J turning back inside the scale is read as that speed easing.

How J beyond 100 or below 0 fails. J leaves the scale whenever K runs ahead of D by enough — that is, whenever the stochastic is moving fast. It happens in the first bars of a strong move as often as in the last.

How the triple crossing fails. All three lines meet at one point by construction (when K = D, J equals both), so a three-line cross is a K–D cross with an extra line drawn through it.

09In different markets and timeframes

KDJ is a stochastic with a third line, J, that exaggerates the gap between K and D and runs beyond 0 and 100. It is quick, and J's excursions come at the start of fast moves as well as their end.

10With other studies

Read beside the Stochastic Oscillator of the same settings: K and D are its lines.

11What it does not tell you

!

KDJ measures the close's place in a range, not value: K and D pin near one extreme in a steady trend. J adds no measurement of its own — it is K and D combined — so a J far outside the scale says how far apart those two are, not how far price has gone.

12Inputs and defaults

KDJ (9, 3)settings card
1Fast-K Period9
2Slow-K Period3
3Slow-K MA13
4Slow-D Period3
5Slow-D MA13
6TimeframeChart
7Wait for the candle to closeOn
  1. 1
    Fast-K Period — 9Time period for building the Fast-K line
  2. 2
    Slow-K Period — 3Smoothing for making the Slow-K line. Usually set to 3
  3. 3
    Slow-K MA — 13Type of Moving Average for Slow-K
  4. 4
    Slow-D Period — 3Smoothing for making the Slow-D line
  5. 5
    Slow-D MA — 13Type of Moving Average for Slow-D
  6. 6
    Timeframe — ChartThe chart's own by default; any higher one, read on the last closed candle. A finer one is never offered.
  7. 7
    Wait for the candle to close — OnThe value of a higher timeframe is taken only from a candle that has finished.

What each input changes

  • Fast-K PeriodThe window of highs and lows the close is placed within (9).
  • Slow-K PeriodThe smoothing of raw %K (3).
  • Slow-K MAThe kind of that smoothing's average.
  • Slow-D PeriodThe smoothing of K that makes D (3).
  • Slow-D MAThe kind of D's average.
  • In use9, 3, 3.

13Limits and common misreadings

Short settings make it very fast and noisy; on most markets it changes direction many times a day on M5.

14Conditions an alert or a strategy can watch

crosses above 20 eventcrosses below 20 eventis above 20 stateis below 20 statecrosses above 80 eventcrosses below 80 eventis above 80 stateis below 80 state+DI crosses above −DI event+DI crosses below −DI event+DI is above −DI state+DI is below −DI state

Event: the bar it happens on an event happens on one barState: every bar while it holds a state holds on every bar while it lasts

These are the conditions the study itself declares — the same rules the alert editor and the strategy engine read, and the ones the figure's chips show.

15Practise it in the laboratory

Put KDJ under a chart and check on a few bars that J = 3K − 2D; then note that the three lines always cross at a single point.

Open the laboratory

17In Bentrix

  • From the menu: a chart's Indicators button › Oscillators › KDJ, or type KDJ in its search.
  • Its settings: the settings card opens when it is added; its legend's name opens it again.

18From an agent or a program

Two tools reach it. study_values computes it over a workbench’s candles up to the clock — no chart needed — and study_attach puts it on a chart, where it is drawn and saved with the chart ({"chart": "<chart uri>", "kind": "kdj"}).

The study, as a chart files it

This is a new KDJ’s entry — its kind, its calculation, the price it reads and its settings. Change a setting here to read it with other inputs.

{
  "applied": "close",
  "convention": "talib",
  "kind": "kdj",
  "settings": {
    "shape": "ta",
    "values": {
      "optInFastK_Period": 9.0,
      "optInSlowD_MAType": 13.0,
      "optInSlowD_Period": 3.0,
      "optInSlowK_MAType": 13.0,
      "optInSlowK_Period": 3.0
    }
  }
}

An agent

An agent calls the MCP tool with it:

{
  "name": "study_values",
  "arguments": {
    "bench": "bentrix://lab/\u2026/bench/\u2026",
    "symbol": "EURUSD",
    "timeframe": "H1",
    "limit": 5,
    "study": {
      "applied": "close",
      "convention": "talib",
      "kind": "kdj",
      "settings": {
        "shape": "ta",
        "values": {
          "optInFastK_Period": 9.0,
          "optInSlowD_MAType": 13.0,
          "optInSlowD_Period": 3.0,
          "optInSlowK_MAType": 13.0,
          "optInSlowK_Period": 3.0
        }
      }
    }
  }
}

A program

A program does the same in its language — bench and market as in the built-in indicators guide:

Python

values = client.study.values(bench, market, "H1", {"applied": "close", "convention": "talib", "kind": "kdj", "settings": {"shape": "ta", "values": {"optInFastK_Period": 9.0, "optInSlowD_MAType": 13.0, "optInSlowD_Period": 3.0, "optInSlowK_MAType": 13.0, "optInSlowK_Period": 3.0}}}, limit=5)
print("times:", values["times"])
for line in values["lines"]:
    print("line:", line["line"])
    print("  newest values:", line["values"])

JSJavaScript

const values = await client.study.values({ bench, symbol: market, timeframe: "H1", study: { "applied": "close", "convention": "talib", "kind": "kdj", "settings": { "shape": "ta", "values": { "optInFastK_Period": 9.0, "optInSlowD_MAType": 13.0, "optInSlowD_Period": 3.0, "optInSlowK_MAType": 13.0, "optInSlowK_Period": 3.0 } } }, limit: 5 });
console.log("times:", values.times);
for (const line of values.lines) {
  console.log("line:", line.line);
  console.log("  newest values:", line.values);
}

C#C#

var values = await client.Study.Values(bench, market, "H1", new Dictionary<string, object?> { ["applied"] = "close", ["convention"] = "talib", ["kind"] = "kdj", ["settings"] = new Dictionary<string, object?> { ["shape"] = "ta", ["values"] = new Dictionary<string, object?> { ["optInFastK_Period"] = 9.0, ["optInSlowD_MAType"] = 13.0, ["optInSlowD_Period"] = 3.0, ["optInSlowK_MAType"] = 13.0, ["optInSlowK_Period"] = 3.0 } } }, limit: 5);
Console.WriteLine($"times: {values!["times"]!}");
foreach (var line in values!["lines"]!.AsArray())
{
    Console.WriteLine($"line: {line!["line"]!}");
    Console.WriteLine($"  newest values: {line!["values"]!}");
}

GOGo

values := must(client.Study.Values(ctx, mcp.StudyValuesArgs{Bench: bench, Symbol: market, Timeframe: "H1", Study: map[string]any{"applied": "close", "convention": "talib", "kind": "kdj", "settings": map[string]any{"shape": "ta", "values": map[string]any{"optInFastK_Period": 9.0, "optInSlowD_MAType": 13.0, "optInSlowD_Period": 3.0, "optInSlowK_MAType": 13.0, "optInSlowK_Period": 3.0}}}, Limit: mcp.Ptr[int64](5)}))
fmt.Println("times:", at(values, "times"))
for _, line := range at(values, "lines").([]any) {
	fmt.Println("line:", at(line, "line"))
	fmt.Println("  newest values:", at(line, "values"))
}

Java

Object values = client.study.values(new Bentrix.StudyApi.ValuesArgs(bench, market, "H1", obj("applied", "close", "convention", "talib", "kind", "kdj", "settings", obj("shape", "ta", "values", obj("optInFastK_Period", 9.0, "optInSlowD_MAType", 13.0, "optInSlowD_Period", 3.0, "optInSlowK_MAType", 13.0, "optInSlowK_Period", 3.0)))).limit(5L));
System.out.println("times:" + " " + at(values, "times"));
for (Object line : (List<Object>) at(values, "lines")) {
    System.out.println("line:" + " " + at(line, "line"));
    System.out.println("  newest values:" + " " + at(line, "values"));
}

C++C++

auto values = client.study.values_(StudyApi::ValuesArgs(bench, market, "H1", Json::Object{{"applied", "close"}, {"convention", "talib"}, {"kind", "kdj"}, {"settings", Json::Object{{"shape", "ta"}, {"values", Json::Object{{"optInFastK_Period", 9.0}, {"optInSlowD_MAType", 13.0}, {"optInSlowD_Period", 3.0}, {"optInSlowK_MAType", 13.0}, {"optInSlowK_Period", 3.0}}}}}}).limit(5));
std::cout << "times:" << " " << values["times"].dump() << std::endl;
for (const Json &line : values["lines"].arr()) {
    std::cout << "line:" << " " << line["line"].dump() << std::endl;
    std::cout << "  newest values:" << " " << line["values"].dump() << std::endl;
}

Julia

values = client.study.values(bench, market, "H1", Dict{String,Any}("applied" => "close", "convention" => "talib", "kind" => "kdj", "settings" => Dict{String,Any}("shape" => "ta", "values" => Dict{String,Any}("optInFastK_Period" => 9.0, "optInSlowD_MAType" => 13.0, "optInSlowD_Period" => 3.0, "optInSlowK_MAType" => 13.0, "optInSlowK_Period" => 3.0))); limit = 5)
println("times:", " ", values["times"])
for line in values["lines"]
    println("line:", " ", line["line"])
    println("  newest values:", " ", line["values"])
end

RR

values <- client$study$values(bench, market, "H1", list(applied = "close", convention = "talib", kind = "kdj", settings = list(shape = "ta", values = list(optInFastK_Period = 9.0, optInSlowD_MAType = 13.0, optInSlowD_Period = 3.0, optInSlowK_MAType = 13.0, optInSlowK_Period = 3.0))), limit = 5)
cat("times:", format(values$times), "\n")
for (line in values$lines) {
  cat("line:", format(line$line), "\n")
  cat("  newest values:", format(line$values), "\n")
}

RRust

let values = client.study().values(mcp::StudyValuesArgs::new(bench.clone(), market.clone(), "H1", serde_json::json!({"applied": "close", "convention": "talib", "kind": "kdj", "settings": {"shape": "ta", "values": {"optInFastK_Period": 9.0, "optInSlowD_MAType": 13.0, "optInSlowD_Period": 3.0, "optInSlowK_MAType": 13.0, "optInSlowK_Period": 3.0}}})).limit(5))?;
println!("times: {}", values["times"]);
for line in values["lines"].as_array().into_iter().flatten() {
    println!("line: {}", line["line"]);
    println!("  newest values: {}", line["values"]);
}

Reading the answer

  • times — the candles’ open times, in milliseconds since 1970 (UTC), oldest first.
  • lines — one entry per line, in this order, each with its line name and its values; values[i] belongs to the candle at times[i].
  • The last value is the candle at the clock’s, which may still be forming: it changes until that candle closes, as on the chart. Nothing after the clock is ever computed.
  • null is a candle with no value yet — the warm-up, about 1 × the period at the start of the history.
  • Its fixed levels, drawn on the chart: [20.0, 80.0].
lineWhat it is
Mainthe indicator's own value
PlusDithe directional movement up (+DI)
MinusDithe directional movement down (−DI)

In a strategy

Filed under a key of your choice in a strategy document’s studies — here kdj: {"kdj": {"applied": "close", "convention": "talib", "kind": "kdj", "settings": {"shape": "ta", "values": {"optInFastK_Period": 9.0, "optInSlowD_MAType": 13.0, "optInSlowD_Period": 3.0, "optInSlowK_MAType": 13.0, "optInSlowK_Period": 3.0}}}} — its rules can name:

WriteWhat it is
kdjits main line, as a value in a compare
kdj.plus_dithis line, as a value in a compare
kdj.minus_dithis line, as a value in a compare
{"cond": "kdj:level.cross_up:20"}a condition it declares itself
{"cond": "kdj:level.cross_down:20"}a condition it declares itself
{"cond": "kdj:level.above:20"}a condition it declares itself
{"cond": "kdj:level.below:20"}a condition it declares itself
{"cond": "kdj:level.cross_up:80"}a condition it declares itself
{"cond": "kdj:level.cross_down:80"}a condition it declares itself
{"cond": "kdj:level.above:80"}a condition it declares itself
{"cond": "kdj:level.below:80"}a condition it declares itself
{"cond": "kdj:di.cross_up"}a condition it declares itself
{"cond": "kdj:di.cross_down"}a condition it declares itself
{"cond": "kdj:di.above"}a condition it declares itself
{"cond": "kdj:di.below"}a condition it declares itself

19Sources

  1. Murphy, John J. Technical Analysis of the Financial Markets. New York Institute of Finance, 1999, ch. 10.
  2. Colby, Robert W. The Encyclopedia of Technical Market Indicators, 2nd ed. McGraw-Hill, 2003, "Stochastics".

Bentrix 0.1.382Every number, name and default on these pages is read from the application itself.What is comingWhat changed