Indicator · Pine Script v6 · Own pane

TD Sequential (TD_SEQ)

by mihakralj

TD Sequential calculates Tom DeMark's exhaustion counting system to identify potential trend reversals.

View on GitHub

GitHub stars99on the repository
Pine versionv6current
Lines of code101
Settings1inputs
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

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

td_seq.pine
// Licensed under the Apache License, Version 2.0
// © mihakralj
//@version=6
indicator("TD Sequential (TD_SEQ)", "TD_SEQ", overlay=false)

//@function Calculates TD Sequential — Tom DeMark's exhaustion counting system
//@param comparePeriod Lookback for setup comparison (close vs close[comparePeriod])
//@returns [setupCount, countdownCount] signed setup (1-9) and countdown (1-13) values
//@description TD Sequential identifies trend exhaustion via two phases:
//  Phase 1 — Setup (1 to 9):
//    Buy Setup: consecutive bars where close < close[comparePeriod] (count negative)
//    Sell Setup: consecutive bars where close > close[comparePeriod] (count positive)
//    A completed setup reaches ±9. The count resets when the condition breaks.
//  Phase 2 — Countdown (1 to 13):
//    After a completed 9-count setup, countdown begins:
//    Buy Countdown: close < low[2] (counts negative, toward −13)
//    Sell Countdown: close > high[2] (counts positive, toward +13)
//    Countdown is non-consecutive — only qualifying bars increment.
//    Countdown resets if an opposite setup completes (±9 in other direction).
//    A completed ±13 signals potential exhaustion reversal.
//  Setup values: −9 to −1 (buy) or +1 to +9 (sell), 0 = no active setup bar
//  Countdown values: −13 to −1 (buy) or +1 to +13 (sell), 0 = no active countdown
//  The oscillator output combines both: setup during phase 1, countdown during phase 2.
//  All state is O(1) via var scalars — no buffers needed.
td_seq(simple int comparePeriod) =>
    if comparePeriod <= 0
        runtime.error("Compare period must be greater than 0")

    var int setupCount     = 0
    var int countdownCount = 0
    var int countdownDir   = 0
    var bool setupComplete = false

    float curClose  = nz(close)
    float prevClose = nz(close[comparePeriod])

    // --- Phase 1: Setup counting ---
    int newSetup = 0
    if curClose < prevClose
        newSetup := setupCount < 0 ? setupCount - 1 : -1
    else if curClose > prevClose
        newSetup := setupCount > 0 ? setupCount + 1 : 1
    else
        newSetup := 0

    if newSetup > 9
        newSetup := 9
    if newSetup < -9
        newSetup := -9

    // --- Check for completed setup (reached ±9) ---
    bool justCompleted = false
    if math.abs(newSetup) == 9 and not setupComplete
        setupComplete := true
        justCompleted := true
        countdownCount := 0
        countdownDir := newSetup > 0 ? 1 : -1

    if math.abs(newSetup) < math.abs(setupCount) or (newSetup > 0 and setupCount < 0) or (newSetup < 0 and setupCount > 0)
        setupComplete := false

    setupCount := newSetup

    // --- Phase 2: Countdown (non-consecutive) ---
    if countdownDir != 0
        float low2  = nz(low[2])
        float high2 = nz(high[2])

        if countdownDir == -1 and curClose < low2
            countdownCount -= 1
        else if countdownDir == 1 and curClose > high2
            countdownCount += 1

        if math.abs(countdownCount) >= 13
            countdownCount := countdownDir == 1 ? 13 : -13
            countdownDir := 0

        if (countdownDir == 1 and newSetup == -9) or (countdownDir == -1 and newSetup == 9)
            countdownCount := 0
            countdownDir := newSetup > 0 ? 1 : -1

    // --- Output: show countdown if active, else setup ---
    int result = countdownDir != 0 ? countdownCount : setupCount
    [setupCount, countdownCount]

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

// Inputs
i_comparePeriod = input.int(4, "Compare Period", minval=1)

// Calculation
[setup, countdown] = td_seq(i_comparePeriod)

// Plot
plot(setup, "Setup", color=color.yellow, linewidth=2, style=plot.style_histogram)
plot(countdown, "Countdown", color=color.aqua, linewidth=1)
hline(0, "Zero", color=color.gray, linestyle=hline.style_dotted)
hline(9, "Sell Setup Complete", color=color.red, linestyle=hline.style_dashed)
hline(-9, "Buy Setup Complete", color=color.green, linestyle=hline.style_dashed)
hline(13, "Sell Countdown", color=color.red, linestyle=hline.style_dotted)
hline(-13, "Buy Countdown", color=color.green, linestyle=hline.style_dotted)

Overview

TD Sequential (TD_SEQ) operates in two phases: Phase 1 Setup tracks consecutive price comparisons against historical closes up to a 9-count, while Phase 2 Countdown tracks non-consecutive comparisons against high or low prices up to a 13-count once a setup completes. The indicator plots these setup and countdown values in a separate pane to signal market exhaustion. The primary input allows configuring the comparison lookback period for setup calculations.

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

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
Compare Periodint4i_comparePeriod
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