OPEN-SOURCE SCRIPT
Supertrend Oscillator Pro | TR

🎯 Overview
Supertrend Oscillator Pro transforms the classic Supertrend into a versatile, dual‑mode oscillator. It measures price distance from the Supertrend line either as a signed percentage (-100% to +100%) or as a raw ATR ratio. The indicator adds a powerful smoothing engine (11 moving average types), dynamic momentum‑based transparency, two independent signal systems (level crosses and reversal diamonds), plus comprehensive visual enhancements – gradient candles, trend fills, a persistent trend table, and nine color themes. It turns the simple Supertrend into a complete trend‑momentum trading system.
⚙️ Core Calculations
1. Base Supertrend (Classic Pine Logic)
Source: hl2 (high+low)/2
ATR Period: user defined (default 10)
Factor: user defined (default 3.0)
Outputs: stLine (Supertrend line), stDir (trend direction: -1 = uptrend, +1 = downtrend), atrValue
2. Raw Oscillator (rawOsc)
3. Oscillator Mode (User Selectable)
Mode A – Percentage (0‑100)
oscValue = (rawOsc / bandWidth) * 100
Then clamped to [-100, +100].
Mode B – Ratio (ATR)
oscValue = rawOsc / bandWidth (unbounded ratio, typically between -3 and +3)
Then normalised over last 70 bars to a [-3, +3] range:
norm = (smoothOsc - minRatio) / rangeRatio → oscPlot := norm * 6 - 3.
4. Smoothing (Moving Average Engine)
The raw oscillator is passed through one of 11 moving averages (user choice):
EMA, SMA, RMA, WMA, VWMA, HMA, DEMA, TEMA, TRIMA, FRAMA, SWMA, T3 (with adjustable T3 factor).
Default: HMA(25).
5. Normalisation for Gradient Coloring (normOsc)
Over the last 70 bars, oscPlot is re‑scaled to a [0,1] range → used for smooth candle color interpolation.
6. Momentum & Dynamic Fill Transparency
Momentum = absolute 5‑bar change of oscPlot
Max momentum over 50 bars → normalised normMomentum
Fill transparency = 35 - (normMomentum × 25), clamped 0‑100 (user can disable dynamic transparency).
→ Higher momentum = less transparent (more vivid fill).
🎨 Visual Features
9 Color Themes
Same palette as the classic version:
Classic, Modern, Heat, Robust, Accented, Monochrome, Moderate, Aqua, Cosmic.
Each defines upColor (bullish) and dnColor (bearish).
Gradient Candles (Overlay on Price)
Candle color interpolates between dnColor and upColor based on normOsc.
Plotted via plotcandle directly on the price chart.
Dynamic Fills (Oscillator Pane)
Positive area (osc > 0) – gradient from solid upColor (at upper level) to transparent (at zero), with momentum‑based transparency.
Negative area (osc < 0) – gradient from transparent (zero) to solid dnColor (at lower level).
Two separate fill pairs handle Percentage vs Ratio modes (levels differ).
Supertrend Overlay (Optional)
When Plot Supertrend Line is true, the classic Supertrend line is drawn on the price pane:
Uptrend (stDir < 0) → green (upColor)
Downtrend (stDir > 0) → red (dnColor)
Signal Markers
Triangles (level crosses) – user toggles showTriangles
LONG (bull cross) → green triangle below bar
SHORT (bear cross) → red triangle above bar
Diamonds (reversal signals) – user toggles showReversals
Reversal Up (price reverses from downtrend to uptrend) → diamond above/below bar with semi‑transparent upColor
Reversal Down → diamond with dnColor
Background Tint (Zone Background)
When bgZone enabled, the chart background gets a very light bullish or bearish tint matching the current trend (trendST).
Trend Table (Middle‑Right Corner)
Shows ⬆️ BULLISH or ⬇️ BEARISH in large colored text. Updates on trend changes.
Value Label (Last Bar)
Floating label showing the current oscillator value (e.g., 42.35% or 1.24). Positioned near the oscillator line with a semi‑transparent background matching the trend color.
Upper / Lower Level Lines
Percentage mode → user‑defined overbought (default 70) and oversold (default -70)
Ratio mode → automatically scaled to [-3, +3] equivalents (e.g., 70% → 2.1, -70% → -2.1)
Plotted as gray circle‑style lines.
📈 Signal System
1. Level Cross Signals (Triangles)
LONG (Bull_Cross) – ta.crossover(oscPlot, lowerLevel) OR ta.crossover(oscPlot, upperLevel)
(Entering oversold or overbought zone from below)
SHORT (Bear_Cross) – ta.crossunder(oscPlot, lowerLevel) OR ta.crossunder(oscPlot, upperLevel)
Debouncing logic:
lastSignal stores the direction (1 = long, -1 = short). A new signal is only generated if it differs from the last one.
2. Reversal Signals (Diamonds)
Reversal Up (Bullish reversal) – ta.crossover(oscPlot, oscPlot[1]) AND oscPlot < -thresholdForSignal
(Oscillator turns up from deep negative territory)
Reversal Down (Bearish reversal) – ta.crossunder(oscPlot, oscPlot[1]) AND oscPlot > thresholdForSignal
Cooldown mechanism:
Reversals are only plotted if the last reversal bar was more than 10 bars ago (prevents clustered signals).
3. Trend Persistence (trendST)
Set to 1 (bullish) on any LONG signal (level cross)
Set to -1 (bearish) on any SHORT signal
Used for background tint, table text, and fill reference colors.
4. Interpretation Guide (Percentage Mode)
+100 → price is exactly one full ATR‑bandwidth above Supertrend (very strong uptrend)
-100 → price is exactly one full ATR‑bandwidth below Supertrend (very strong downtrend)
Crossing zero → price crosses the Supertrend line (classic trend change)
Oversold / Overbought zones (e.g., ±70) → potential exhaustion / reversal areas.
5. Divergence Possibility (Not Coded, But Usable)
The raw oscPlot value (especially in Ratio mode) can be used for divergences:
Price makes higher high, oscillator makes lower high → bearish divergence
Price makes lower low, oscillator makes higher low → bullish divergence.
🚨 Alert System
Four alert conditions available (user‑toggleable):
LONG (Cross) – level cross triggers a bullish entry
SHORT (Cross) – level cross triggers a bearish entry
LONG (Reversal) – reversal diamond (bullish)
SHORT (Reversal) – reversal diamond (bearish)
Alerts include the ticker symbol in the message.
Supertrend Oscillator Pro transforms the classic Supertrend into a versatile, dual‑mode oscillator. It measures price distance from the Supertrend line either as a signed percentage (-100% to +100%) or as a raw ATR ratio. The indicator adds a powerful smoothing engine (11 moving average types), dynamic momentum‑based transparency, two independent signal systems (level crosses and reversal diamonds), plus comprehensive visual enhancements – gradient candles, trend fills, a persistent trend table, and nine color themes. It turns the simple Supertrend into a complete trend‑momentum trading system.
⚙️ Core Calculations
1. Base Supertrend (Classic Pine Logic)
Source: hl2 (high+low)/2
ATR Period: user defined (default 10)
Factor: user defined (default 3.0)
Outputs: stLine (Supertrend line), stDir (trend direction: -1 = uptrend, +1 = downtrend), atrValue
2. Raw Oscillator (rawOsc)
3. Oscillator Mode (User Selectable)
Mode A – Percentage (0‑100)
oscValue = (rawOsc / bandWidth) * 100
Then clamped to [-100, +100].
Mode B – Ratio (ATR)
oscValue = rawOsc / bandWidth (unbounded ratio, typically between -3 and +3)
Then normalised over last 70 bars to a [-3, +3] range:
norm = (smoothOsc - minRatio) / rangeRatio → oscPlot := norm * 6 - 3.
4. Smoothing (Moving Average Engine)
The raw oscillator is passed through one of 11 moving averages (user choice):
EMA, SMA, RMA, WMA, VWMA, HMA, DEMA, TEMA, TRIMA, FRAMA, SWMA, T3 (with adjustable T3 factor).
Default: HMA(25).
5. Normalisation for Gradient Coloring (normOsc)
Over the last 70 bars, oscPlot is re‑scaled to a [0,1] range → used for smooth candle color interpolation.
6. Momentum & Dynamic Fill Transparency
Momentum = absolute 5‑bar change of oscPlot
Max momentum over 50 bars → normalised normMomentum
Fill transparency = 35 - (normMomentum × 25), clamped 0‑100 (user can disable dynamic transparency).
→ Higher momentum = less transparent (more vivid fill).
🎨 Visual Features
9 Color Themes
Same palette as the classic version:
Classic, Modern, Heat, Robust, Accented, Monochrome, Moderate, Aqua, Cosmic.
Each defines upColor (bullish) and dnColor (bearish).
Gradient Candles (Overlay on Price)
Candle color interpolates between dnColor and upColor based on normOsc.
Plotted via plotcandle directly on the price chart.
Dynamic Fills (Oscillator Pane)
Positive area (osc > 0) – gradient from solid upColor (at upper level) to transparent (at zero), with momentum‑based transparency.
Negative area (osc < 0) – gradient from transparent (zero) to solid dnColor (at lower level).
Two separate fill pairs handle Percentage vs Ratio modes (levels differ).
Supertrend Overlay (Optional)
When Plot Supertrend Line is true, the classic Supertrend line is drawn on the price pane:
Uptrend (stDir < 0) → green (upColor)
Downtrend (stDir > 0) → red (dnColor)
Signal Markers
Triangles (level crosses) – user toggles showTriangles
LONG (bull cross) → green triangle below bar
SHORT (bear cross) → red triangle above bar
Diamonds (reversal signals) – user toggles showReversals
Reversal Up (price reverses from downtrend to uptrend) → diamond above/below bar with semi‑transparent upColor
Reversal Down → diamond with dnColor
Background Tint (Zone Background)
When bgZone enabled, the chart background gets a very light bullish or bearish tint matching the current trend (trendST).
Trend Table (Middle‑Right Corner)
Shows ⬆️ BULLISH or ⬇️ BEARISH in large colored text. Updates on trend changes.
Value Label (Last Bar)
Floating label showing the current oscillator value (e.g., 42.35% or 1.24). Positioned near the oscillator line with a semi‑transparent background matching the trend color.
Upper / Lower Level Lines
Percentage mode → user‑defined overbought (default 70) and oversold (default -70)
Ratio mode → automatically scaled to [-3, +3] equivalents (e.g., 70% → 2.1, -70% → -2.1)
Plotted as gray circle‑style lines.
📈 Signal System
1. Level Cross Signals (Triangles)
LONG (Bull_Cross) – ta.crossover(oscPlot, lowerLevel) OR ta.crossover(oscPlot, upperLevel)
(Entering oversold or overbought zone from below)
SHORT (Bear_Cross) – ta.crossunder(oscPlot, lowerLevel) OR ta.crossunder(oscPlot, upperLevel)
Debouncing logic:
lastSignal stores the direction (1 = long, -1 = short). A new signal is only generated if it differs from the last one.
2. Reversal Signals (Diamonds)
Reversal Up (Bullish reversal) – ta.crossover(oscPlot, oscPlot[1]) AND oscPlot < -thresholdForSignal
(Oscillator turns up from deep negative territory)
Reversal Down (Bearish reversal) – ta.crossunder(oscPlot, oscPlot[1]) AND oscPlot > thresholdForSignal
Cooldown mechanism:
Reversals are only plotted if the last reversal bar was more than 10 bars ago (prevents clustered signals).
3. Trend Persistence (trendST)
Set to 1 (bullish) on any LONG signal (level cross)
Set to -1 (bearish) on any SHORT signal
Used for background tint, table text, and fill reference colors.
4. Interpretation Guide (Percentage Mode)
+100 → price is exactly one full ATR‑bandwidth above Supertrend (very strong uptrend)
-100 → price is exactly one full ATR‑bandwidth below Supertrend (very strong downtrend)
Crossing zero → price crosses the Supertrend line (classic trend change)
Oversold / Overbought zones (e.g., ±70) → potential exhaustion / reversal areas.
5. Divergence Possibility (Not Coded, But Usable)
The raw oscPlot value (especially in Ratio mode) can be used for divergences:
Price makes higher high, oscillator makes lower high → bearish divergence
Price makes lower low, oscillator makes higher low → bullish divergence.
🚨 Alert System
Four alert conditions available (user‑toggleable):
LONG (Cross) – level cross triggers a bullish entry
SHORT (Cross) – level cross triggers a bearish entry
LONG (Reversal) – reversal diamond (bullish)
SHORT (Reversal) – reversal diamond (bearish)
Alerts include the ticker symbol in the message.
สคริปต์โอเพนซอร์ซ
ด้วยเจตนารมณ์หลักของ TradingView ผู้สร้างสคริปต์นี้ได้ทำให้เป็นโอเพนซอร์ส เพื่อให้เทรดเดอร์สามารถตรวจสอบและยืนยันฟังก์ชันการทำงานของมันได้ ขอชื่นชมผู้เขียน! แม้ว่าคุณจะใช้งานได้ฟรี แต่โปรดจำไว้ว่าการเผยแพร่โค้ดซ้ำจะต้องเป็นไปตาม กฎระเบียบการใช้งาน ของเรา
คำจำกัดสิทธิ์ความรับผิดชอบ
ข้อมูลและบทความไม่ได้มีวัตถุประสงค์เพื่อก่อให้เกิดกิจกรรมทางการเงิน, การลงทุน, การซื้อขาย, ข้อเสนอแนะ หรือคำแนะนำประเภทอื่น ๆ ที่ให้หรือรับรองโดย TradingView อ่านเพิ่มเติมใน ข้อกำหนดการใช้งาน
สคริปต์โอเพนซอร์ซ
ด้วยเจตนารมณ์หลักของ TradingView ผู้สร้างสคริปต์นี้ได้ทำให้เป็นโอเพนซอร์ส เพื่อให้เทรดเดอร์สามารถตรวจสอบและยืนยันฟังก์ชันการทำงานของมันได้ ขอชื่นชมผู้เขียน! แม้ว่าคุณจะใช้งานได้ฟรี แต่โปรดจำไว้ว่าการเผยแพร่โค้ดซ้ำจะต้องเป็นไปตาม กฎระเบียบการใช้งาน ของเรา
คำจำกัดสิทธิ์ความรับผิดชอบ
ข้อมูลและบทความไม่ได้มีวัตถุประสงค์เพื่อก่อให้เกิดกิจกรรมทางการเงิน, การลงทุน, การซื้อขาย, ข้อเสนอแนะ หรือคำแนะนำประเภทอื่น ๆ ที่ให้หรือรับรองโดย TradingView อ่านเพิ่มเติมใน ข้อกำหนดการใช้งาน