Skip to main content
BTC / USDTCRYPTO107,400+2.19%ETH / USDTCRYPTO3,840+2.13%SOL / USDTCRYPTO182.40−1.99%BNB / USDTCRYPTO652.30+0.66%XRP / USDTCRYPTO2.2150+1.61%DOGE / USDTCRYPTO0.3850−1.79%TON / USDTCRYPTO5.240+2.34%AVAX / USDTCRYPTO42.60−2.07%LINK / USDTCRYPTO22.40+2.28%ADA / USDTCRYPTO1.0520−1.68%TRX / USDTCRYPTO0.3300+0.92%DOT / USDTCRYPTO8.420+2.93%BTC / USDTCRYPTO107,400+2.19%ETH / USDTCRYPTO3,840+2.13%SOL / USDTCRYPTO182.40−1.99%BNB / USDTCRYPTO652.30+0.66%XRP / USDTCRYPTO2.2150+1.61%DOGE / USDTCRYPTO0.3850−1.79%TON / USDTCRYPTO5.240+2.34%AVAX / USDTCRYPTO42.60−2.07%LINK / USDTCRYPTO22.40+2.28%ADA / USDTCRYPTO1.0520−1.68%TRX / USDTCRYPTO0.3300+0.92%DOT / USDTCRYPTO8.420+2.93%
Prezzi

Stochastic Oscillator

%K / %D momentum oscillator — MT4, MT5, TradeStation and TradingView code.

The Stochastic locates the close within the recent high–low range on a 0–100 scale (%K), smoothed into %D. Above 80 is overbought and below 20 oversold; %K/%D crossovers inside those zones are the common entry cues. Copy the version for your platform.

MQL4 · Stochastic.mq4
#property indicator_separate_window
#property indicator_buffers 2

extern int K_Period = 14;
extern int D_Period = 3;
double KBuffer[], DBuffer[];

int OnInit() {
  SetIndexBuffer(0, KBuffer);
  SetIndexBuffer(1, DBuffer);
  IndicatorShortName("Stoch(" + K_Period + "," + D_Period + ")");
  return 0;
}

int OnCalculate(const int rates_total, const int prev_calculated, const double &close[]) {
  for (int i = prev_calculated; i < rates_total; i++) {
    KBuffer[i] = iStochastic(NULL, 0, K_Period, D_Period, 3, MODE_SMA, 0, MODE_MAIN, i);
    DBuffer[i] = iStochastic(NULL, 0, K_Period, D_Period, 3, MODE_SMA, 0, MODE_SIGNAL, i);
  }
  return rates_total;
}
MQL5 · Stochastic.mq5
#property indicator_separate_window
#property indicator_buffers 2

input int K_Period = 14;
input int D_Period = 3;
double KBuffer[], DBuffer[];

int OnInit() {
  SetIndexBuffer(0, KBuffer);
  SetIndexBuffer(1, DBuffer);
  return INIT_SUCCEEDED;
}

int OnCalculate(const int rates_total, const int prev_calculated,
  const datetime &time[], const double &open[], const double &high[],
  const double &low[], const double &close[], const long &tick_volume[],
  const long &volume[], const int &spread[]) {
  for (int i = prev_calculated; i < rates_total; i++) {
    KBuffer[i] = iStochastic(NULL, 0, K_Period, D_Period, 3, MODE_SMA, 0, MODE_MAIN, i);
    DBuffer[i] = iStochastic(NULL, 0, K_Period, D_Period, 3, MODE_SMA, 0, MODE_SIGNAL, i);
  }
  return rates_total;
}
EasyLanguage
inputs: KLength(14), DLength(3);
variables: KVal(0), DVal(0);
KVal = Stochastic(Close, KLength);
DVal = Average(KVal, DLength);
Plot1(KVal, "K");
Plot2(DVal, "D");
Pine Script
//@version=5
indicator("Stochastic", overlay=false)
kLength = input.int(14, "K Length")
dSmooth = input.int(3, "D Smoothing")
k = ta.stoch(close, high, low, kLength)
d = ta.sma(k, dSmooth)
plot(k, title="K", color=color.blue, linewidth=2)
plot(d, title="D", color=color.orange)
hline(80)
hline(20)
hline(50)

To use it inside dtcharts, paste any version into the Script Converter and it becomes a native indicator on your chart.