OPEN-SOURCE SCRIPT
Cập nhật Supertrend Oscillator | TR

🎯 Overview
Supertrend Oscillator transforms the classic Supertrend (which plots a directional line on price) into a bounded oscillator that ranges from -100% to +100%. It measures how far price is from the Supertrend line relative to the ATR‑based bandwidth, giving a clear, normalised view of trend strength. The indicator keeps all the familiar Supertrend logic but adds gradient candle coloring, dynamic fills, nine color themes, entry signals, and a persistent trend table – turning a trend‑following line into a full visual momentum system.
⚙️ Core Calculations
1. Standard Supertrend
Source: hl2 (high+low / 2)
ATR Period: default 10
Factor: default 3
The Supertrend line and direction (pineDir) are computed using the classic Pine Script logic:
pineDir = -1 → uptrend (price above Supertrend)
pineDir = +1 → downtrend (price below Supertrend)
2. Oscillator (%)
Uptrend (pineDir = -1): positive values (0 to +100)
Downtrend (pineDir = +1): negative values (0 to -100)
The result is clamped to [-100, +100].
3. Normalised Percent (0..1)
Over the last 70 bars, percent is re‑scaled to a 0–1 range. Used for gradient candle coloring (smooth transition between bearish and bullish theme colors).
4. Momentum & Fill Transparency
Momentum = absolute 5‑bar change of percent
Normalised over a 50‑bar max → normMomentum
Fill transparency = 35 - (normMomentum × 25) clamped 0–100
→ Higher momentum = less transparent (more visible fill).
🎨 Visual Features
9 Color Themes
Same palette as the previous Williams %R indicator:
Classic, Modern, Heat, Robust, Accented, Monochrome, Moderate, Aqua, Cosmic.
Each theme defines UpC (bullish color) and DnC (bearish color).
Gradient Candles
Candle color interpolates between DnC and UpC based on the normalised percent.
Overlay on the price chart (plotcandle).
Dynamic Fills (between oscillator and zero line)
Overbought fill (percent > 0) – gradient from solid UpC (at +100) to transparent (at 0)
Oversold fill (percent < 0) – gradient from transparent (at 0) to solid DnC (at -100)
Trend‑direction fill – only fills the side of the current trend (bullish or bearish), with momentum‑based transparency. Higher momentum = less transparent.
Supertrend Overlay (optional)
Plot Chart toggle (false by default). When enabled, plots the classic Supertrend lines directly on the price pane:
Uptrend line (green) when pineDir = -1
Downtrend line (red) when pineDir = +1
Signal Markers
LONG (Bull_Cross): percent crosses above zero → green triangle below bar.
SHORT (Bear_Cross): percent crosses below zero → red triangle above bar.
Background Tint
Very light bullish / bearish tint matching the active trend.
Trend Table (middle‑right corner)
Shows “BULLISH” (⬆️) or “BEARISH” (⬇️) in large colored text.
Floating Value Label
On the last bar, shows the current percent value (e.g., 42.35%) near the oscillator line, with a semi‑transparent background matching the trend color.
📈 Signal System
Entry Signals
LONG (Bull_Cross): percent crosses above 0
→ Occurs when price moves from below Supertrend to above it, pushing the oscillator from negative into positive territory.
SHORT (Bear_Cross): percent crosses below 0
Trend Persistence
Trend_Supertrend stores the last confirmed direction:
1 = bullish (percent > 0)
-1 = bearish (percent < 0)
The trend state only changes on a zero‑line cross – prevents flickering near the threshold.
Overbought / Oversold Interpretation
+100 → price is exactly one ATR‑bandwidth above the Supertrend (strong uptrend).
-100 → price is exactly one ATR‑bandwidth below the Supertrend (strong downtrend).
Crossing zero indicates the price has just crossed the Supertrend line – a classic reversal signal.
Divergence Possibility
Although not explicitly coded, the oscillator’s raw percent value can be used to spot divergences:
Price makes a higher high but percent makes a lower high → bearish divergence.
Price makes a lower low but percent makes a higher low → bullish divergence.
Supertrend Oscillator transforms the classic Supertrend (which plots a directional line on price) into a bounded oscillator that ranges from -100% to +100%. It measures how far price is from the Supertrend line relative to the ATR‑based bandwidth, giving a clear, normalised view of trend strength. The indicator keeps all the familiar Supertrend logic but adds gradient candle coloring, dynamic fills, nine color themes, entry signals, and a persistent trend table – turning a trend‑following line into a full visual momentum system.
⚙️ Core Calculations
1. Standard Supertrend
Source: hl2 (high+low / 2)
ATR Period: default 10
Factor: default 3
The Supertrend line and direction (pineDir) are computed using the classic Pine Script logic:
pineDir = -1 → uptrend (price above Supertrend)
pineDir = +1 → downtrend (price below Supertrend)
2. Oscillator (%)
Uptrend (pineDir = -1): positive values (0 to +100)
Downtrend (pineDir = +1): negative values (0 to -100)
The result is clamped to [-100, +100].
3. Normalised Percent (0..1)
Over the last 70 bars, percent is re‑scaled to a 0–1 range. Used for gradient candle coloring (smooth transition between bearish and bullish theme colors).
4. Momentum & Fill Transparency
Momentum = absolute 5‑bar change of percent
Normalised over a 50‑bar max → normMomentum
Fill transparency = 35 - (normMomentum × 25) clamped 0–100
→ Higher momentum = less transparent (more visible fill).
🎨 Visual Features
9 Color Themes
Same palette as the previous Williams %R indicator:
Classic, Modern, Heat, Robust, Accented, Monochrome, Moderate, Aqua, Cosmic.
Each theme defines UpC (bullish color) and DnC (bearish color).
Gradient Candles
Candle color interpolates between DnC and UpC based on the normalised percent.
Overlay on the price chart (plotcandle).
Dynamic Fills (between oscillator and zero line)
Overbought fill (percent > 0) – gradient from solid UpC (at +100) to transparent (at 0)
Oversold fill (percent < 0) – gradient from transparent (at 0) to solid DnC (at -100)
Trend‑direction fill – only fills the side of the current trend (bullish or bearish), with momentum‑based transparency. Higher momentum = less transparent.
Supertrend Overlay (optional)
Plot Chart toggle (false by default). When enabled, plots the classic Supertrend lines directly on the price pane:
Uptrend line (green) when pineDir = -1
Downtrend line (red) when pineDir = +1
Signal Markers
LONG (Bull_Cross): percent crosses above zero → green triangle below bar.
SHORT (Bear_Cross): percent crosses below zero → red triangle above bar.
Background Tint
Very light bullish / bearish tint matching the active trend.
Trend Table (middle‑right corner)
Shows “BULLISH” (⬆️) or “BEARISH” (⬇️) in large colored text.
Floating Value Label
On the last bar, shows the current percent value (e.g., 42.35%) near the oscillator line, with a semi‑transparent background matching the trend color.
📈 Signal System
Entry Signals
LONG (Bull_Cross): percent crosses above 0
→ Occurs when price moves from below Supertrend to above it, pushing the oscillator from negative into positive territory.
SHORT (Bear_Cross): percent crosses below 0
Trend Persistence
Trend_Supertrend stores the last confirmed direction:
1 = bullish (percent > 0)
-1 = bearish (percent < 0)
The trend state only changes on a zero‑line cross – prevents flickering near the threshold.
Overbought / Oversold Interpretation
+100 → price is exactly one ATR‑bandwidth above the Supertrend (strong uptrend).
-100 → price is exactly one ATR‑bandwidth below the Supertrend (strong downtrend).
Crossing zero indicates the price has just crossed the Supertrend line – a classic reversal signal.
Divergence Possibility
Although not explicitly coded, the oscillator’s raw percent value can be used to spot divergences:
Price makes a higher high but percent makes a lower high → bearish divergence.
Price makes a lower low but percent makes a higher low → bullish divergence.
Phát hành các Ghi chú
DebugMã nguồn mở
Theo đúng tinh thần TradingView, tác giả của tập lệnh này đã công bố nó dưới dạng mã nguồn mở, để các nhà giao dịch có thể xem xét và xác minh chức năng. Chúc mừng tác giả! Mặc dù bạn có thể sử dụng miễn phí, hãy nhớ rằng việc công bố lại mã phải tuân theo Nội quy.
Thông báo miễn trừ trách nhiệm
Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.
Mã nguồn mở
Theo đúng tinh thần TradingView, tác giả của tập lệnh này đã công bố nó dưới dạng mã nguồn mở, để các nhà giao dịch có thể xem xét và xác minh chức năng. Chúc mừng tác giả! Mặc dù bạn có thể sử dụng miễn phí, hãy nhớ rằng việc công bố lại mã phải tuân theo Nội quy.
Thông báo miễn trừ trách nhiệm
Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.