Indicator · Pine Script v6 · Own pane

Stochastic MACD Oscillator (STMACD)

by mihakralj

This indicator plots a Stochastic MACD in a separate pane.

View on GitHub

GitHub stars99on the repository
Pine versionv6current
Lines of code60
Settings4inputs
LicenceApache-2.0
Last update4 weeks ago27 Sep 2026

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

60 lines of Pine Script v6 from lib/oscillators/stmacd/stmacd.pine, published under Apache-2.0. Keep the author's credit and licence lines when you reuse it.

stmacd.pine
// Licensed under the Apache License, Version 2.0
// © mihakralj
//@version=6
indicator("Stochastic MACD Oscillator (STMACD)", "STMACD", overlay = false)

//@function Stochastic MACD — normalises each EMA against the rolling high-low range,
//          converting absolute MACD into a percentage-scale momentum reading.
//          Pipeline: HH/LL(periods) → FastEMA/SlowEMA → StochNorm → Signal EMA.
//          Output unbounded (typically −100…+100), OB/OS at ±10.
//@param h High price series
//@param l Low price series
//@param c Close price series
//@param periods Rolling H-L range lookback (>= 2)
//@param fastLength Fast EMA period (>= 2)
//@param slowLength Slow EMA period (>= 2)
//@param signalLength Signal EMA period (>= 2)
//@returns [stmacd, signal] — dual output
//@reference Apirine, V. (2019). "The Stochastic MACD Oscillator."
//           Technical Analysis of Stocks & Commodities, Nov 2019.
//@optimized O(1) amortized per bar via ta.highest/ta.lowest
stmacd(series float h, series float l, series float c,
       simple int periods, simple int fastLength,
       simple int slowLength, simple int signalLength) =>
    if periods < 2 or fastLength < 2 or slowLength < 2 or signalLength < 2
        runtime.error("All periods must be at least 2")

    // --- Sliding-window extremes ---
    float hh = ta.highest(h, periods)
    float ll = ta.lowest(l, periods)

    // --- Dual EMA of Close ---
    float fastEma = ta.ema(c, fastLength)
    float slowEma = ta.ema(c, slowLength)

    // --- Stochastic normalisation ---
    float rng = hh - ll
    float fastStoch = rng > 0 ? (fastEma - ll) / rng : 0.0
    float slowStoch = rng > 0 ? (slowEma - ll) / rng : 0.0
    float stmacd_val = (fastStoch - slowStoch) * 100.0

    // --- Signal line ---
    float signal_val = ta.ema(stmacd_val, signalLength)

    [stmacd_val, signal_val]

// ── Inputs ──
int p_periods      = input.int(45, "Periods",       minval = 2)
int p_fastLength   = input.int(12, "Fast Length",    minval = 2)
int p_slowLength   = input.int(26, "Slow Length",    minval = 2)
int p_signalLength = input.int(9,  "Signal Length",  minval = 2)

// ── Calculation ──
[stmacd_out, signal_out] = stmacd(high, low, close, p_periods, p_fastLength, p_slowLength, p_signalLength)

// ── Plot ──
plot(stmacd_out, "STMACD", color.blue, 2)
plot(signal_out, "Signal", color.orange, 1)
hline(10,  "Overbought", color.new(color.gray, 50), linestyle = hline.style_dotted)
hline(-10, "Oversold",   color.new(color.gray, 50), linestyle = hline.style_dotted)
hline(0,   "Zero",       color.gray, linestyle = hline.style_dashed)

Overview

Stochastic MACD Oscillator (STMACD) measures the rolling high and low over a lookback, applies fast and slow exponential moving averages to the close, and normalizes each average against that range. The difference, scaled by 100, is the oscillator, and an exponential moving average of that value is the signal line. Periods, fast length, slow length, and signal length change the calculation. Dotted lines mark plus and minus 10, with a dashed zero line. It does not generate trades.

Details

Published by

mihakralj on GitHub

Type

Indicator, Pine Script v6

Draws

Separate pane below the chart

Category

Oscillators & momentum, Moving averages

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.

Topics

algorithmic-tradingcsharpdotnetpinescriptpythonquantitative-financesimdta-libtechnical-analysistechnical-indicators

Settings

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

SettingTypeDefaultVariable
Periodsint45p_periods
Fast Lengthint12p_fastLength
Slow Lengthint26p_slowLength
Signal Lengthint9p_signalLength
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

948 stars

The Derivative Oscillator calculates a double-exponentially smoothed Relative Strength Index (RSI) and subtracts its Simple Moving Average (SMA) to form the main oscillator line. A secondary signal line is plotted using an SMA of the main oscillator values. Th

Indicator v4 Oscillators & momentumMoving averages
Updated 10 days ago · GPL-3.0 GitHub repository of Derivative Oscillator

Beep Boop

by magic8bot
409 stars

This indicator calculates a modified MACD oscillator and signal line alongside a trend EMA filter, plotting the output as a stylized column histogram in a separate pane. Instead of plotting true MACD distance, it quantifies positive and negative histogram mome

Indicator v4 Oscillators & momentumTrend
Updated 3 years ago · MIT GitHub repository of Beep Boop

MACD

by Alorse
330 stars

This indicator calculates and plots the standard Moving Average Convergence Divergence oscillator in a separate pane. It computes the difference between a fast and slow moving average, derives a signal line through smoothing, and plots a histogram representing

Indicator v4 Oscillators & momentumMoving averages
Updated 2 months ago · None stated GitHub repository of MACD

MACD-V

by harryguiacorn
219 stars

MACD-V is a volatility-normalized momentum indicator based on research by Alex Spiroglou. It calculates the standard MACD histogram and normalizes it by dividing by the Average True Range (ATR). Plotted in a separate pane as a colored histogram, it includes us

Indicator v5 Oscillators & momentumVolatility & bands
Updated 1 year ago · None stated GitHub repository of MACD-V
218 stars

Multi-Timeframe Analysis Suite calculates trend direction and momentum conditions across five user-selected timeframes using moving averages, RSI, and Stochastic oscillators. It evaluates moving average slope and oscillator levels to compute trend alignment, m

Indicator v6 Screeners & dashboardsTrend
Updated 3 months ago · None stated GitHub repository of Multi-Timeframe Analysis Suite

KamaFama

by Mastaaa1987
141 stars

KamaFama is an indicator that translates a Freqtrade trading strategy into Pine Script for visualization. It combines Kaufman Adaptive Moving Average (KAMA), MAMA/FAMA calculations derived via Hilbert Transform, Williams %R, and correlation trend filters to ev

Indicator v6 Moving averagesOscillators & momentum
Updated 6 weeks ago · MIT GitHub repository of KamaFama

More indicators

Trade Your Charts With a Regulated Broker

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

Compare brokers