Indicator · Pine Script v6 · Own pane

Schaff Trend Cycle (STC)

by mihakralj

This indicator calculates the Schaff Trend Cycle oscillator using custom exponential moving averages with deterministic warm-up logic.

View on GitHub

GitHub stars47on the repository
Pine versionv6current
Lines of code73
Settings5inputs
LicenceMIT
Last update8 months ago28 Feb 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

73 lines of Pine Script v6 from indicators/oscillators/stc.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

stc.pine
// The MIT License (MIT)
// © mihakralj
//@version=6
indicator("Schaff Trend Cycle (STC)", "STC", overlay=false)

ema(series float source,simple int period=0,simple float alpha=0)=>
    if alpha<=0 and period<=0
        runtime.error("Alpha or period must be provided")
    float a=alpha>0?alpha:2.0/math.max(period,1)
    var float raw_ema=na
    var float ema=na
    var float e=1.0 
    var bool warmup=true
    if not na(source)
        if na(raw_ema)
            raw_ema:=0
            ema:=source
        else
            raw_ema:=a*(source-raw_ema)+raw_ema
            if warmup
                e*=(1-a)
                float c=1.0/(1.0-e)
                ema:=c*raw_ema
                if e<=1e-10
                    warmup:=false
            else
                ema:=raw_ema
    ema

//@function Calculates the Schaff Trend Cycle (STC) indicator
//@param source Input price series
//@param cycleLength Main cycle length parameter for lookback periods
//@param fastLength Period for fast EMA calculation
//@param slowLength Period for slow EMA calculation
//@param smoothingType Type of smoothing (0:none, 1:ema, 2:sigmoid, 3:digital)
//@returns Smoothed STC value
stc(series float source, simple int cycleLength, simple int fastLength, simple int slowLength, simple int smoothingType = 2) =>
    float fast_ema = ema(source, fastLength)
    float slow_ema = ema(source, slowLength)
    float macdLine = fast_ema - slow_ema

    h1 = ta.highest(macdLine, cycleLength)
    l1 = ta.lowest(macdLine, cycleLength)
    float stoch1_raw = (h1 - l1) > 0 ? 100 * (macdLine - l1) / (h1 - l1) : 0
    float stoch1 = ema(stoch1_raw, 3)
    h2 = ta.highest(stoch1, cycleLength)
    l2 = ta.lowest(stoch1, cycleLength)
    float stoch2 = (h2 - l2) > 0 ? 100 * (stoch1 - l2) / (h2 - l2) : 0


    float stcValue = stoch2
    if smoothingType == 1
        stcValue := ema(stoch2, 3)
    else if smoothingType == 2
        stcValue := 100 / (1 + math.exp(-0.1 * (stcValue - 50)))
    else if smoothingType == 3
        stcValue := stcValue > 75 ? 100 : stcValue < 25 ? 0 : stcValue[1]
    stcValue

// ---------- Main loop ----------

// Inputs
i_source = input.source(close, title="Source")
i_cycleLength = input.int(12, title="Cycle Length", minval=2)
i_fastLength = input.int(26, title="Fast Length", minval=2)
i_slowLength = input.int(50, title="Slow Length", minval=2)
i_smoothingType = input.int(2, title="Smoothing", minval=0, maxval=3, tooltip="0: none, 1:ema, 2:sigmoid, 3:digital")

// Calculation
stcValue = stc(i_source, i_cycleLength, i_fastLength, i_slowLength, i_smoothingType)

// Plot
plot(stcValue, "STC", color=color.yellow, linewidth=2)

Overview

Schaff Trend Cycle (STC) computes a MACD line from user-defined fast and slow lengths, passes it through a double stochastic calculation over a specified cycle length, and applies optional post-smoothing (none, EMA, sigmoid, or digital thresholding). The final line plots in a separate pane to help identify cyclical momentum and trend transitions.

Details

Published by

mihakralj on GitHub

Type

Indicator, Pine Script v6

Draws

Separate pane below the chart

Category

Oscillators & momentum, Trend

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

pinescriptpinescript-indicatorstechnical-analysistradingview-indicators

Settings

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

SettingTypeDefaultVariable
Sourcesourceclosei_source
Cycle Lengthint12i_cycleLength
Fast Lengthint26i_fastLength
Slow Lengthint50i_slowLength
Smoothingint2i_smoothingType
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

Schaff Trend Cycle

by everget
948 stars

This indicator plots the Schaff Trend Cycle in a separate pane. It subtracts a slow exponential moving average from a fast one, applies a stochastic cycle twice, and smooths each result to form a 0–100 oscillator. Line color follows the slope, or the upper and

Indicator v4 Oscillators & momentumTrend
Updated 10 days ago · GPL-3.0 GitHub repository of Schaff Trend Cycle

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

[RS]Standardized Trend Map

by ricardosantos79
261 stars

Standardized Trend Map calculates series momentum relative to active price range across 40 distinct lookback periods ranging from 2 to 80 bars on an exponential moving average source. Each standardized calculation rounds to an integer values of -1, 0, or 1 to

Indicator v4 TrendOscillators & momentum
Updated 4 years ago · None stated GitHub repository of [RS]Standardized Trend Map

ADX Histogram

by harryguiacorn
219 stars

This indicator calculates and displays the Average Directional Index (ADX) as a column histogram in a separate pane. It evaluates market trend strength by calculating directional movement metrics based on specified smoothing and DI length inputs. When the ADX

Indicator v5 TrendOscillators & momentum
Updated 1 year ago · None stated GitHub repository of ADX Histogram
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

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