Indicator · Pine Script v6 · Own pane

Deviation Momentum For Loop

by vedkanani123· code credited to LyroRS

A separate-pane momentum indicator that measures how far price sits from a selectable moving average and sums that deviation over a for-loop range of lookbacks, from a start to an end value.

View on GitHub

GitHub stars1on the repository
Pine versionv6current
Lines of code138
Settings11inputs
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

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

12.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("Deviation Momentum For Loop | Lyro RS", shorttitle = "Deviation Momentum For Loop | Lyro RS", overlay= false, timeframe="")

// Import Libraries
import LyroRS/LMAs/1 as DynamicMA


// Groups
ma_g= "𝗠𝗢𝗩𝗜𝗡𝗚 𝗔𝗩𝗘𝗥𝗔𝗚𝗘"
forloop_g = "𝗙𝗢𝗥 𝗟𝗢𝗢𝗣"
thresholds_g = "𝗧𝗛𝗥𝗘𝗦𝗛𝗢𝗟𝗗𝗦"
colors_g = '𝗖𝗢𝗟𝗢𝗥𝗦'


// Moving Average Inputs
source = input.source(close, "Source", tooltip="Select the price source (e.g., close, open, high, low) used for calculations.", group=ma_g)
MA_Type = input.string("SMA", "Select Moving Average", options=["SMA", "EMA", "WMA", "VWMA", "DEMA", "TEMA", "RMA", "HMA", "LSMA", "SMMA", "ALMA", "ZLSMA", "FRAMA", "KAMA", "JMA", "T3"], group=ma_g, tooltip="Choose a moving average type to be applied in calculations.")
MA_len = input.int(30, "MA Length", tooltip="Defines the period length for the selected moving average.", group=ma_g)
constant_factor = 0.015


// For Loop Inputs
a_ = input.int(100, "From", tooltip="Sets the starting index for the loop iteration.", group=forloop_g)
b_ = input.int(600, "To", tooltip="Sets the ending index for the loop iteration.", group=forloop_g)


// Threshold Inputs
Threshold_L = input.int(150, "Threshold Long", step=1, tooltip="Defines the threshold level above which an uptrend is detected.", group=thresholds_g)
Threshold_S = input.int(-150, "Threshold Short", step=1, tooltip="Defines the threshold level below which a downtrend is detected.", group=thresholds_g)


// Color Inputs
ColMode = input.string("Mystic", "Custom Color Palette", inline="drop", options=["Classic", "Mystic", "Accented", "Royal"], display=display.none, group=colors_g,  tooltip="Choose a predefined color scheme for indicator visualization.")

cpyn = input.bool(true, "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)



// Colors
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



// Moving Average Switch
float ma = na
switch MA_Type
    "SMA" => ma := DynamicMA.SMA(source, MA_len)
    "EMA" => ma := DynamicMA.EMA(source, MA_len)
    "WMA" => ma := DynamicMA.WMA(source, MA_len)
    "VWMA" => ma := DynamicMA.VWMA(source, volume, MA_len)
    "DEMA" => ma := DynamicMA.DEMA(source, MA_len)
    "TEMA" => ma := DynamicMA.TEMA(source, MA_len)
    "RMA" => ma := DynamicMA.RMA(source, MA_len)
    "HMA" => ma := DynamicMA.HMA(source, MA_len)
    "LSMA" => ma := DynamicMA.LSMA(source, MA_len, 0)
    "SMMA" => ma := DynamicMA.SMMA(source, MA_len)
    "ALMA" => ma := DynamicMA.ALMA(source, MA_len, 0, 20)
    "ZLSMA" => ma := DynamicMA.ZLSMA(source, MA_len)
    "FRAMA" => ma := DynamicMA.FRAMA(source, MA_len)
    "KAMA" => ma := DynamicMA.KAMA(source, MA_len)
    "JMA" => ma := DynamicMA.JMA(source, MA_len, 0.5)
    "T3" => ma := DynamicMA.T3(source, MA_len, 0.5)


// Deviation
volatility = ta.stdev(source, MA_len)
deviation = (source - ma) / (volatility * constant_factor)

// Subject for For Loop
subject = deviation


// For Loop
system(a, b) =>
    total = 0.0
    for i = a to b by 1
        total += (subject > subject[i] ? 1 : -1)
        total
    total

ma_forloop = system(a_, b_)


// Condition
L = ma_forloop > Threshold_L
S = ma_forloop < Threshold_S

ml = math.avg(Threshold_L, Threshold_S)

// Color Based on Trend
var color pc = na
if L and not S
    pc := UpC

if S
    pc := DnC

plot(ma_forloop, "For Loop", color= pc, display= display.all)
plot(Threshold_L, title= "Long Threshold", color= color.new(UpC, 35), display= display.pane + display.status_line)
plot(Threshold_S, title= "Short Threshold", color= color.new(DnC, 35), display= display.pane + display.status_line)

plot(ma_forloop, "For Loop Glow 1", color= color.new(pc, 75), display= display.pane, linewidth = 5, editable = false)
plot(ma_forloop, "For Loop Glow 2", color= color.new(pc, 85), display= display.pane, linewidth = 10, editable = false)


barcolor(pc, title= "Bar Color")
plotcandle(open, high, low, close, color= pc, wickcolor=pc, bordercolor = pc, force_overlay = true, title= "Plot Candles", display=display.pane)
bgcolor(ta.crossover(ma_forloop, ml) ? color.new(UpC, 75) : bool(ta.crossunder(ma_forloop, Threshold_L)) ? color.new(DnC, 75) : na, editable = false, display=display.all)


// Alerts
alertcondition(L, "Commodity Index For Loop - Long", "Commodity Index For Loop is Long {{exchange}}:{{ticker}}")
alertcondition(S, "Commodity Index For Loop - Short", "Commodity Index For Loop is Short {{exchange}}:{{ticker}}")

Overview

The result is compared with a long threshold and a short threshold to mark uptrend and downtrend states, and the line is colored by state. It supports sixteen moving average types and several color palettes.

Details

Published by

vedkanani123 on GitHub

Code credit

LyroRS, as written in the script's header

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.

Settings

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

SettingTypeDefaultVariable
Sourcesourceclosesource
Select Moving AveragestringSMAMA_Type
MA Lengthint30MA_len
Fromint100a_
Toint600b_
Threshold Longint150Threshold_L
Threshold Shortint-150Threshold_S
Custom Color PalettestringMysticColMode
Use Custom Palettebooltruecpyn
Custom Upcolor#00ff00cp_UpC
Custom Downcolor#ff0000cp_DnC
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 11 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