Skip to content

Indicators

Stochastic Oscillator: %K/%D Formula, Fast vs Slow and Crossings

The stochastic oscillator, developed by George Lane in the 1950s, shows where the close sits within the recent high-low range on a 0–100 scale. Raw %K = 100 × (close − lowest low) ÷ (highest high − lowest low) over n bars. %D is a moving average of %K, commonly a 3-period simple average. A slow stochastic smooths %K first, so slow %K equals fast %D.

Illustrative stochastic oscillator cover with a %K line and a smoother %D line between two reference levels.
%K locates the close in its range. %D is a moving average of %K.

Stochastic oscillator at a glance

Scroll horizontally to read every column.

Feature Meaning
Measures The close's location within the highest high and lowest low of the lookback.
Scale 0 when the close equals the lowest low; 100 when it equals the highest high.
Lines %K, the main line, and %D, a moving average of %K used as a signal line.
Versions Fast (raw %K), slow (%K smoothed once) and full (every period adjustable).
Commonly cited levels 80 and 20 are traditional overbought and oversold lines, not instructions.
Platforms TradingView's Stochastic; Pine Script v6's ta.stoch; MetaTrader 5's Stochastic Oscillator.

StockCharts credits George C. Lane with developing the indicator in the late 1950s, and TradingView's page also attributes it to Lane.[1][2] The indicator rules library compares the inputs each indicator uses. RSI uses smoothed close-to-close changes; the stochastic uses the close's position within a high-low range.

What is the stochastic formula for %K and %D?

Choose a lookback n for %K. Lowest low is the lowest low of the last n bars, including the current bar. Highest high is the highest high over the same bars.

Raw %K = 100 × (close − lowest low) ÷ (highest high − lowest low)

%K = SMA(raw %K, smoothing); %D = SMA(%K, d)

TradingView documents this form, with a %K smoothing input where a value of 1 disables the extra smoothing.[2] StockCharts defines the three versions by how often raw %K is smoothed:[1]

  • Fast: %K is raw %K; %D is a 3-period SMA of raw %K.
  • Slow: %K is raw %K smoothed with a 3-period SMA, which equals fast %D; slow %D smooths that again.
  • Full: the lookback, the %K smoothing and the %D period are all chosen by the user.

MetaTrader 5 calls the %K smoothing “Slowing”. A value of 1 is considered fast and 3 is considered slow. MT5 also lets the user choose the %D averaging method: exponential, simple, smoothed or weighted.[6] A %D line averaged another way is a different line. The MT5 help page gives the raw %K formula but does not show how Slowing is applied, so compare MT5 and TradingView values before assuming they match.

When the highest high equals the lowest low, the denominator is zero and raw %K is undefined. The Pine reference for ta.stoch does not state a result for that case.[3] Decide what a flat range means before a rule relies on it.

Worked example: fast and slow stochastic values

Illustrative prices and periods, not a recommendation. Assume three consecutive bars whose lookback windows all have lowest low 40 and highest high 50, so the range is 10.

  1. Bar A closes at 48: raw %K = 100 × (48 − 40) ÷ 10 = 80.
  2. Bar B closes at 45: raw %K = 100 × (45 − 40) ÷ 10 = 50.
  3. Bar C closes at 42: raw %K = 100 × (42 − 40) ÷ 10 = 20.
  4. Fast stochastic at bar C: fast %K = 20; fast %D = (80 + 50 + 20) ÷ 3 = 50.
  5. Slow stochastic at bar C: slow %K = the same 3-bar average, 50. Slow %D would average three slow %K values, so it needs two earlier slow values.
Illustrative stochastic diagram: with a 40 to 50 range, closes of 48, 45 and 42 give raw %K values of 80, 50 and 20.
Illustrative prices. Raw %K places the close within the lookback's high-low range; it does not predict the next bar.

At bar C, fast %K is below fast %D, while slow %K is 50. The fast and slow readings describe the same bars differently. A rule must name which version and which periods it uses.

What do stochastic 80 and 20 mean?

StockCharts describes 80 and 20 as the traditional overbought and oversold thresholds. TradingView lists the same levels as defaults and says they may not suit every instrument.[1][2] A reading above 80 only means the close sits in the top fifth of the lookback's range.

StockCharts adds that overbought readings are not necessarily bearish: a market can stay above 80 through a strong uptrend. Oversold readings are likewise not necessarily bullish.[1] There is no universal stochastic level, and neither label specifies a trade.

How can a stochastic crossing be written as a rule?

A level state, a level crossing and a %K/%D crossing are three different conditions. Write each with its comparison and its evaluation time.

  • State: on the closed bar, %K is below level L.
  • Level crossing (event): previous %K ≤ L and current %K > L.
  • Signal-line crossing (event): previous %K ≤ previous %D and current %K > current %D.

Both events match the Pine definition of ta.crossover: greater on the current bar, and less than or equal on the previous bar.[5] MetaTrader 5's help lists level-exit and %K/%D crossings among popular interpretations.[6] Popularity is not evidence that either rule has an edge.

Previous → current %K Current %K above 20? Cross above 20?
18 → 20 No No: equality fails the current comparison.
20 → 26 Yes Yes: equality passes the previous comparison.
26 → 31 Yes No: %K was already above 20.

The level 20 here defines the example, not a recommended trigger. Divergence needs extra definitions for swings, pairing and confirmation time. The pivot confirmation guide explains why a swing is known only after later bars.

How do you use ta.stoch in Pine Script?

As of 25 September 2026, Pine Script v6 documents ta.stoch(source, high, low, length). Its formula is 100 × (close − lowest low) ÷ (highest high − lowest low) over the length.[3] The function returns raw %K. Smooth it with ta.sma(source, length) to build %K and %D.[4]

Illustrative sketch; not compiled or tested. The input defaults are placeholders, not recommended settings.

//@version=6
indicator("Stochastic %K/%D crossing")
periodK = input.int(14, "%K length", minval = 1)
smoothK = input.int(3, "%K smoothing", minval = 1)
periodD = input.int(3, "%D length", minval = 1)
k = ta.sma(ta.stoch(close, high, low, periodK), smoothK)
d = ta.sma(k, periodD)
kCross = ta.crossover(k, d)
event = barstate.isconfirmed and kCross
plot(k, "%K")
plot(d, "%D")
alertcondition(event, "%K above %D", "%K crossed above %D on a closed bar")

Setting smoothK to 1 reproduces the fast version. Setting it to 3 gives slow %K. alertcondition() makes the event available; you still create the TradingView alert and choose its frequency. The alert keeps the script and inputs it was created with.[7] %K can move while a realtime bar develops, so an unfinished crossing may disappear by the close.[8] The crossing sits on its own line because Pine v6 evaluates and lazily; a skipped ta.crossover call would miss bars of history.[13]

How does a stochastic event reach MetaTrader 5?

A stochastic crossing needs a separate action specification: direction, broker symbol, volume, exits and repeat handling. Test it on a demo account first, and choose your own symbol, size and rules. PineConnector's setup test checks message processing separately from the broker trade.[9]

  1. Condition: the chosen crossing is true on the closed bar.
  2. Alert: the TradingView alert triggers; check its log.
  3. Delivery: the webhook carries the message.
  4. Processing: match it in PineConnector Portal → Bridge.
  5. EA request: confirm whether an order request was submitted.
  6. Broker outcome: check acceptance, the deal or deals and the position.

MetaQuotes treats orders, deals and positions as separate records.[10] A stochastic exit evaluated on the chart is a strategy-side rule, not a broker-side stop. TradingView and MetaTrader can also use different feeds and quotes, as PineConnector's price-difference FAQ explains.[11] MT5's stochastic can therefore differ from the TradingView value that triggered the alert.

Which stochastic mistakes change the rule?

  • Mixing fast and slow values. Fast %D and slow %K are the same line; fast %K is not.
  • Assuming %D is always an SMA. MT5 lets the user choose the %D method.[6]
  • Reading 80 as a sell instruction. The reading can stay high through a strong advance.[1]
  • Ignoring a flat range. Equal highest high and lowest low leave raw %K undefined.
  • Tuning periods and levels after seeing results. Every adjusted choice is fitted to the same history. TradingView describes testing outside the sample used for optimisation as a widely used way to reduce overfitting.[12]

A precise stochastic condition establishes what the rule means. It does not establish that the rule is profitable.

Frequently asked questions

What is the stochastic oscillator formula?

Raw %K equals 100 × (close − lowest low) ÷ (highest high − lowest low), using the lookback's lowest low and highest high. %K may be smoothed with a simple moving average, and %D is a moving average of %K. A reading of 0 means the close equals the range low; 100 means it equals the range high.

What is the difference between %K and %D?

%K is the stochastic's main line: the close's position within the lookback range, possibly smoothed. %D is a moving average of %K, commonly over three periods, and is used as a signal line. A %K/%D crossing compares the two lines on consecutive bars. Equality on the previous bar belongs to the upward-crossing case.

What is the difference between fast and slow stochastic?

The fast stochastic uses raw %K and a 3-period SMA of it as %D. The slow stochastic smooths raw %K with a 3-period SMA first, so slow %K equals fast %D, then averages again for slow %D. The full stochastic lets the user set the lookback, the %K smoothing and the %D period.

How does ta.stoch work in Pine Script?

In Pine Script v6, ta.stoch(source, high, low, length) returns raw %K: 100 × (close − lowest low) ÷ (highest high − lowest low) over the length. Smooth it with ta.sma for %K, then smooth %K again for %D. Calculating ta.stoch creates no alert and no order by itself.

Reviewed 25 September 2026. Facts were checked against the linked sources on that date. Nothing in this article was tested on a trading account and no code was compiled.

Related reading

Sources

  1. StockCharts ChartSchool – Stochastic Oscillator (Fast, Slow, and Full), accessed 25 September 2026.
  2. TradingView – Stochastic (STOCH), accessed 25 September 2026.
  3. TradingView – Pine Script v6 reference: ta.stoch, accessed 25 September 2026.
  4. TradingView – Pine Script v6 reference: ta.sma, accessed 25 September 2026.
  5. TradingView – Pine Script v6 reference: ta.crossover, accessed 25 September 2026.
  6. MetaQuotes – Stochastic Oscillator, accessed 25 September 2026.
  7. TradingView – Alerts, accessed 25 September 2026.
  8. TradingView – Repainting, accessed 25 September 2026.
  9. PineConnector – Test your setup, accessed 25 September 2026.
  10. MetaQuotes – Basic principles: orders, deals and positions, accessed 25 September 2026.
  11. PineConnector – Frequently asked questions: price differences, accessed 25 September 2026.
  12. TradingView – Strategies: overfitting, accessed 25 September 2026.
  13. TradingView – Migrating to Pine Script v6: lazy evaluation of conditions, accessed 25 September 2026.

PineConnector executes the instructions you send it. It does not select trades, manage money, or hold funds. Trading carries risk, and past performance of any strategy does not indicate future results.


Leave a comment

Back To PiCo Blog

Ready when your strategy is

You bring the strategy.We bring the infrastructure.

Connect TradingView to MetaTrader, choose where MT5 runs and put the full PineConnector workflow through its paces from your first month.

Strategy and trading decisions remain yours. The MT5 environment can be ours.

PineConnector Edge

Run the full PineConnector workflow.

$59/mo at launch

Core plan · 1 connection · 1 hosted MT5 environment

Try Core for $7

7 days of Core for $7, then $59/month at the launch price unless you cancel.