Indicator · Pine Script v6 · On the chart

Root Mean Deviation Trail

by vedkanani123· code credited to LyroRS

An overlay indicator that measures how far price sits from a central EMA, smooths the signed deviation with a selectable moving average, and normalizes it with a root mean square deviation to scale a trailing band.

View on GitHub

GitHub stars1on the repository
Pine versionv6current
Lines of code252
Settings17inputs
LicenceNone statedfile: MPL-2.0
Last update2 months ago23 Aug 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

252 lines of Pine Script v6 from indicator/olg_7_6_26/LyroRS/5.txt, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

5.txt
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © LyroRS

//@version=6
indicator("Root Mean Deviation Trail | Lyro RS", "Root Mean Deviation Trail | Lyro RS", overlay=true)


import LyroRS/LMAs/1 as DynamicMAs

// ─────────────────────────────────────────────────────────────────────────────
// Input Groups
// ─────────────────────────────────────────────────────────────────────────────
engine_g  = "𝗗𝗘𝗩𝗜𝗔𝗧𝗜𝗢𝗡 𝗘𝗡𝗚𝗜𝗡𝗘"
band_g    = "𝗕𝗔𝗡𝗗 𝗦𝗘𝗧𝗧𝗜𝗡𝗚𝗦"
display_g = "𝗗𝗜𝗦𝗣𝗟𝗔𝗬"
colors_g  = "𝗖𝗢𝗟𝗢𝗥𝗦"
table_g   = "𝗧𝗔𝗕𝗟𝗘"

// ─────────────────────────────────────────────────────────────────────────────
// Deviation Engine Inputs
// ─────────────────────────────────────────────────────────────────────────────
source    = input.source(close, "Source",            group=engine_g, tooltip="Price source for all calculations.")
maLen     = input.int(21,       "MA Length",         group=engine_g, tooltip="EMA used as the central mean anchor.")
smoothType = input.string("EMA", "Smoothing MA", options=["SMA", "EMA", "WMA", "VWMA", "DEMA", "TEMA", "RMA", "HMA", "LSMA", "SMMA", "ALMA", "ZLSMA", "FRAMA", "KAMA", "JMA", "T3"], group=engine_g, tooltip="Moving average type applied to smooth the signed deviation signal.")
smoothLen  = input.int(6,       "Smoothing Length",  group=engine_g, tooltip="Lookback period for the deviation smoothing MA.")
rmsdLen   = input.int(20,       "RMSD Length",       group=engine_g, tooltip="Lookback for Root Mean Square Deviation — the volatility normalizer.")

// ─────────────────────────────────────────────────────────────────────────────
// Band Settings
// ─────────────────────────────────────────────────────────────────────────────
multTight = input.float(0.7, "Multiplier Tight (High Deviation)", step=0.1, group=band_g, tooltip="RMSD multiplier when deviation is strong — trail hugs price.")
multWide  = input.float(2.6, "Multiplier Wide  (Low Deviation)",  step=0.1, group=band_g, tooltip="RMSD multiplier when deviation is weak — trail drifts wide.")

// ─────────────────────────────────────────────────────────────────────────────
// Display Inputs
// ─────────────────────────────────────────────────────────────────────────────
colorMode = input.string("Gradient", "Color Mode",
 options=["Gradient", "Trend"],
 group=display_g, display=display.none, inline="d1",
 tooltip="Gradient: color intensity scales with deviation strength. Trend: flat UpC/DnC color.")
showFill  = input.bool(true, "Band Fill",  group=display_g, display=display.none, inline="d2")
showOuter = input.bool(true, "Outer Band", group=display_g, display=display.none, inline="d2")

// ─────────────────────────────────────────────────────────────────────────────
// Color Palette — LyroRS Style
// ─────────────────────────────────────────────────────────────────────────────
ColMode = input.string("Mystic", "Color Palette",
 options=["Classic", "Mystic", "Accented", "Royal"],
 group=colors_g, display=display.none, inline="pal",
 tooltip="Choose a predefined color scheme.")

cpyn   = input.bool(false,     "Use Custom Palette", tooltip="Enable manual selection of custom colors for trend signals.", group=colors_g, display=display.none)
cp_UpC = input.color(#00ff00, "Custom Up",   inline="Custom Palette", tooltip="Set a custom color for bullish signals.",  group=colors_g, display=display.none)
cp_DnC = input.color(#ff0000, "Custom Down", inline="Custom Palette", tooltip="Set a custom color for bearish signals.", group=colors_g, display=display.none)

// Palette resolver
color UpC = na
color DnC = na

switch ColMode
    "Classic"  =>
        UpC := #00E676
        DnC := #880E4F
    "Mystic"   =>
        UpC := #30FDCF
        DnC := #E117B7
    "Accented" =>
        UpC := #9618F7
        DnC := #FF0078
    "Royal"    =>
        UpC := #FFC107
        DnC := #673AB7

if cpyn
    UpC := cp_UpC
    DnC := cp_DnC

// ─────────────────────────────────────────────────────────────────────────────
// Table Inputs
// ─────────────────────────────────────────────────────────────────────────────
enable_table = input.bool(true,          "",           group=table_g, display=display.none, inline="T")
pos_table    = input.string("Top Right", "RMDT Table", group=table_g, display=display.none, inline="T",
 options=["Top Left","Middle Left","Bottom Left","Top Right","Middle Right","Bottom Right","Top Center","Middle Center","Bottom Center"])
tsize_input  = input.string("Normal",    "",           group=table_g, display=display.none, inline="T",
 options=["Huge","Large","Normal","Small","Tiny"])

// Table helpers
pot(pos) =>
    switch pos
        "Top Left"      => position.top_left
        "Middle Left"   => position.middle_left
        "Bottom Left"   => position.bottom_left
        "Top Right"     => position.top_right
        "Middle Right"  => position.middle_right
        "Bottom Right"  => position.bottom_right
        "Top Center"    => position.top_center
        "Middle Center" => position.middle_center
        "Bottom Center" => position.bottom_center

tsz(s) =>
    switch s
        "Huge"   => size.huge
        "Large"  => size.large
        "Normal" => size.normal
        "Small"  => size.small
        "Tiny"   => size.tiny

// ─────────────────────────────────────────────────────────────────────────────
// RMSD Function (LyroRS)
// ─────────────────────────────────────────────────────────────────────────────
rmsd(src, benchmark, length) =>
    sum_sq = 0.0
    for i = 0 to length - 1
        d = src[i] - benchmark[i]
        sum_sq += d * d
    math.sqrt(sum_sq / length)

// ─────────────────────────────────────────────────────────────────────────────
// Core Calculations
// ─────────────────────────────────────────────────────────────────────────────
ma      = DynamicMAs.EMA(source, maLen)
rmsdVal = rmsd(source, ma, rmsdLen)

devRaw  = rmsdVal > 0 ? (source - ma) / rmsdVal : 0.0
float devSig = na
switch smoothType
    "SMA"   => devSig := DynamicMAs.SMA(devRaw, smoothLen)
    "EMA"   => devSig := DynamicMAs.EMA(devRaw, smoothLen)
    "WMA"   => devSig := DynamicMAs.WMA(devRaw, smoothLen)
    "VWMA"  => devSig := DynamicMAs.VWMA(devRaw, volume, smoothLen)
    "DEMA"  => devSig := DynamicMAs.DEMA(devRaw, smoothLen)
    "TEMA"  => devSig := DynamicMAs.TEMA(devRaw, smoothLen)
    "RMA"   => devSig := DynamicMAs.RMA(devRaw, smoothLen)
    "HMA"   => devSig := DynamicMAs.HMA(devRaw, smoothLen)
    "LSMA"  => devSig := DynamicMAs.LSMA(devRaw, smoothLen, 0)
    "SMMA"  => devSig := DynamicMAs.SMMA(devRaw, smoothLen)
    "ALMA"  => devSig := DynamicMAs.ALMA(devRaw, smoothLen, 0, 20)
    "ZLSMA" => devSig := DynamicMAs.ZLSMA(devRaw, smoothLen)
    "FRAMA" => devSig := DynamicMAs.FRAMA(devRaw, smoothLen)
    "KAMA"  => devSig := DynamicMAs.KAMA(devRaw, smoothLen)
    "JMA"   => devSig := DynamicMAs.JMA(devRaw, smoothLen, 0)
    "T3"    => devSig := DynamicMAs.T3(devRaw, smoothLen, 0.5)
sigAbs  = math.abs(devSig)
sigPeak = ta.highest(sigAbs, 80)
sigNorm = sigPeak > 0 ? math.min(sigAbs / sigPeak, 1.0) : 0.0

int  trend = devSig > 0 ? 1 : -1
bool cross = trend != trend[1]

// ─────────────────────────────────────────────────────────────────────────────
// Adaptive Trail
// ─────────────────────────────────────────────────────────────────────────────
mult      = multWide - sigNorm * (multWide - multTight)
bandwidth = rmsdVal * mult

float trail = trend == 1 ? ma - bandwidth : ma + bandwidth
trail       := DynamicMAs.EMA(trail, 3)

float outer = trend == 1 ? ma + bandwidth * 0.5 : ma - bandwidth * 0.5
outer       := DynamicMAs.EMA(outer, 3)

// ─────────────────────────────────────────────────────────────────────────────
// Color Mode — Gradient vs Trend
// ─────────────────────────────────────────────────────────────────────────────
col = colorMode == "Gradient"
     ? (trend == 1
         ? color.from_gradient(sigNorm, 0.05, 0.7, color.new(UpC, 50), UpC)
         : color.from_gradient(sigNorm, 0.05, 0.7, color.new(DnC, 50), DnC))
     : (trend == 1 ? UpC : DnC)

// ─────────────────────────────────────────────────────────────────────────────
// Glow (always on)
// ─────────────────────────────────────────────────────────────────────────────
glow = rmsdVal * 0.08 * (0.4 + sigNorm * 1.2)

g3p   = plot(ma + glow * 3, display=display.none, editable=false)
g2p   = plot(ma + glow * 2, display=display.none, editable=false)
g1p   = plot(ma + glow,     display=display.none, editable=false)
pMA   = plot(ma, "MA", col, 2)
g1b   = plot(ma - glow,     display=display.none, editable=false)
g2b   = plot(ma - glow * 2, display=display.none, editable=false)
g3b   = plot(ma - glow * 3, display=display.none, editable=false)

fill(g3p, g2p, color.new(col, 94))
fill(g2p, g1p, color.new(col, 88))
fill(g1p, pMA,  color.new(col, 78))
fill(pMA,  g1b, color.new(col, 78))
fill(g1b, g2b, color.new(col, 88))
fill(g2b, g3b, color.new(col, 94))

// ─────────────────────────────────────────────────────────────────────────────
// Trail & Outer Plot
// ─────────────────────────────────────────────────────────────────────────────
float trailBrk = cross ? float(na) : trail

pt  = plot(trailBrk, "Trail", col, 2, plot.style_linebr)
phl = plot(hl2, display=display.none, editable=false)
fill(pt, phl, hl2, trailBrk, color(na), color.new(col, showFill ? 70 : 100), editable=false)

plot(showOuter ? DynamicMAs.EMA(outer, 3) : na, "Outer", color.new(col, 78), 1)

// ─────────────────────────────────────────────────────────────────────────────
// Candles & Signals
// ─────────────────────────────────────────────────────────────────────────────
plotcandle(open, high, low, close, "Candles", col, col, bordercolor=col)

plotshape(cross and trend == 1,  title="Long Signal",  location=location.belowbar,
           style=shape.labelup,   text="𝓛𝓸𝓷𝓰",  textcolor=#000000,
           size=size.small, color=UpC, force_overlay=true)

plotshape(cross and trend == -1, title="Short Signal", location=location.abovebar,
           style=shape.labeldown, text="𝓢𝓱𝓸𝓻𝓽", textcolor=#000000,
           size=size.small, color=DnC, force_overlay=true)

barcolor(col)

// ─────────────────────────────────────────────────────────────────────────────
// Status Table
// ─────────────────────────────────────────────────────────────────────────────
var table stat_tbl = table.new(pot(pos_table), 100, 100,
 frame_width=1, frame_color=color.gray, border_color=color.gray, border_width=1)

trendLbl    = trend == 1 ? "▲  Bullish" : "▼  Bearish"
trendCol    = trend == 1 ? UpC : DnC
strengthLbl = sigNorm > 0.75 ? "Strong" : sigNorm > 0.4 ? "Moderate" : "Weak"
crossLbl    = cross ? "⚡ New Signal" : "—"

if enable_table
    table.cell(stat_tbl, 0, 0, "RMDT | 𝓛𝔂𝓻𝓸 𝓡𝓢", text_color=color.white, bgcolor=#000000, text_size=tsz(tsize_input))
    table.cell(stat_tbl, 1, 0, "",                   text_color=color.white, bgcolor=#000000, text_size=tsz(tsize_input))
    table.merge_cells(stat_tbl, 0, 0, 1, 0)

    table.cell(stat_tbl, 0, 1, "Trend",     text_color=color.gray, bgcolor=#000000, text_size=tsz(tsize_input))
    table.cell(stat_tbl, 1, 1, trendLbl,    text_color=trendCol,   bgcolor=#000000, text_size=tsz(tsize_input))

    table.cell(stat_tbl, 0, 2, "Strength",  text_color=color.gray, bgcolor=#000000, text_size=tsz(tsize_input))
    table.cell(stat_tbl, 1, 2, strengthLbl, text_color=trendCol,   bgcolor=#000000, text_size=tsz(tsize_input))

    table.cell(stat_tbl, 0, 3, "Signal",    text_color=color.gray, bgcolor=#000000, text_size=tsz(tsize_input))
    table.cell(stat_tbl, 1, 3, crossLbl,    text_color=cross ? trendCol : color.gray, bgcolor=#000000, text_size=tsz(tsize_input))

// ─────────────────────────────────────────────────────────────────────────────
// Alerts
// ─────────────────────────────────────────────────────────────────────────────
alertcondition(cross and trend == 1  and barstate.isconfirmed, "RMDT Long",  "Root Mean Deviation Trend — Bullish {{ticker}}")
alertcondition(cross and trend == -1 and barstate.isconfirmed, "RMDT Short", "Root Mean Deviation Trend — Bearish {{ticker}}")

// ─────────────────────────────────────────────────────────────────────────────
// ⚠️ Disclaimer: This indicator is for educational purposes only.
// It does not constitute financial advice. Always combine with
// price action, proper risk management, and additional analysis.
// ─────────────────────────────────────────────────────────────────────────────

Overview

The multiplier shrinks when deviation is strong and grows when it is weak, so the trail follows price closely in trends and loosens in weak moves. Band fill, an outer band, color modes and an information table are optional.

Details

Published by

vedkanani123 on GitHub

Code credit

LyroRS, as written in the script's header

Type

Indicator, Pine Script v6

Draws

On the price chart

Category

Trend, Volatility & bands, 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.

Settings

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

SettingTypeDefaultVariable
Sourcesourceclosesource
MA Lengthint21maLen
Smoothing MAstringEMAsmoothType
Smoothing Lengthint6smoothLen
RMSD Lengthint20rmsdLen
Multiplier Tight (High Deviation)float0.7multTight
Multiplier Wide (Low Deviation)float2.6multWide
Color ModestringGradientcolorMode
Band FillbooltrueshowFill
Outer BandbooltrueshowOuter
Color PalettestringMysticColMode
Use Custom Paletteboolfalsecpyn
Show all 17 settings
SettingTypeDefaultVariable
Custom Upcolor#00ff00cp_UpC
Custom Downcolor#ff0000cp_DnC
enable_tablebooltrueenable_table
RMDT TablestringTop Rightpos_table
tsize_inputstringNormaltsize_input
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

318 stars

This indicator plots four customizable moving averages directly on the main price chart using default lengths of 3, 8, 21, and 50 periods. It fills the space between adjacent averages in green or maroon depending on trend alignment. Additionally, it computes B

Indicator v5 Moving averagesTrend
Updated 2 months ago · MIT GitHub repository of Multiple Moving Averages

NVDA Trend Ribbon

by longsurf-ai
280 stars

This indicator plots three exponential moving averages and ATR volatility envelopes on the price chart. It compares the fast and slow EMAs to shade a trend ribbon and marks confirmed upward and downward crossovers with triangle labels. Settings change the thre

Indicator v6 TrendMoving averages
Updated 2 days ago · MIT GitHub repository of NVDA Trend Ribbon

Bollinger Bands Force

by harryguiacorn
219 stars

This indicator modifies standard Bollinger Bands to measure directional trend force based on line alignment. It calculates a simple moving average basis along with upper and lower volatility bands. When all three lines move upward or downward simultaneously, i

Indicator v5 Volatility & bandsTrend
Updated 1 year ago · None stated GitHub repository of Bollinger Bands Force

Adaptive MA Slope

by capissimo
114 stars

This indicator calculates Kaufman's Adaptive Moving Average (KAMA) on a chosen price source and determines trend direction based on the slope across a configurable lookback window. Signals can be filtered by short-term volatility expansion using ATR comparison

Indicator v4 Moving averagesTrend
Updated 1 year ago · None stated GitHub repository of Adaptive MA Slope

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