Indicator · Pine Script v4 · Own pane

SMA Dynamic

by capissimo

This indicator plots two weighted moving averages in a separate pane to track price trends with volatility-based adaptation.

View on GitHub

GitHub stars114on the repository
Pine versionv4older, still runs
Lines of code24
Settings5inputs
LicenceNone statedfile: MPL-2.0
Last update1 year ago7 Nov 2024

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

24 lines of Pine Script v4 from scripts/089-SMA-Dynamic.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

089-SMA-Dynamic.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © capissimo

//@version=4
study("SMA Dynamic", '', false)

// Inputs
price     = input(close, 'Data source')
minlen    = input(1,     'Minimum length',  minval=1)
maxlen    = input(2,     'Maximum length',  minval=1)
adapt_pct = input(100.,  'Adaptation (%)',  minval=0, maxval=100) / 100.0
p         = input(5,     'Lookback for MA', minval=1)

// Calculations
dlen = avg(minlen, maxlen)   // dynamic length
dlen := atr(10) > atr(40) ? max(minlen, dlen * (1 - adapt_pct)) : min(maxlen, dlen * (1 + adapt_pct))

d = int(dlen[0])

ma1 = wma(price, p) 
ma2 = wma(price, d) 

plot(ma1, color=color.new(color.blue,0), transp=0)
plot(ma2, color=color.new(color.orange,0), transp=0)

Overview

One moving average uses a fixed user-defined lookback period, while the second adjusts its length dynamically by comparing a 10-period ATR to a 40-period ATR. When short-term volatility exceeds long-term volatility, the dynamic length decreases toward the minimum length setting; otherwise, it expands toward the maximum length based on the adaptation percentage. There are no built-in trade signals or alerts.

Details

Published by

capissimo on GitHub

Type

Indicator, Pine Script v4

Draws

Separate pane below the chart

Category

Moving averages, Volatility & bands

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
Data sourcesourcecloseprice
Minimum lengthint1minlen
Maximum lengthint2maxlen
Adaptation (%)input100.adapt_pct
Lookback for MAint5p
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

Calculates and plots moving averages on selected VIX volatility terms (VIX9D, VIX, VIX3M, or VIX6M) in a separate pane. The script computes user-defined short and long moving averages using EMA or SMA. It shades the background red when the short-term moving av

Indicator v5 Volatility & bandsMoving averages
Updated 2 months ago · MIT GitHub repository of [TVT] VIX Short Term Danger

MACD-V

by harryguiacorn
219 stars

MACD-V is a volatility-normalized momentum indicator based on research by Alex Spiroglou. It calculates the standard MACD histogram and normalizes it by dividing by the Average True Range (ATR). Plotted in a separate pane as a colored histogram, it includes us

Indicator v5 Oscillators & momentumVolatility & bands
Updated 1 year ago · None stated GitHub repository of MACD-V

The AMFM indicator decomposes price movements into amplitude and frequency components using Ehlers' digital signal processing methods. It whitens the derivative as close minus open, then computes AM as the smoothed envelope of the absolute difference over 4 pe

Indicator v6 Volatility & bandsMoving averages
Updated 4 weeks ago · Apache-2.0 GitHub repository of AMFM - Ehlers AM Detector / FM Demodulator

PPO ATR Bands

by cryptorife
75 stars

PPO ATR Bands plots a percentage price oscillator in a separate pane. It compares a Hull average of typical price with a volume-weighted exponential average of Parabolic SAR, then scales three true-range bands around the slow average into matching oscillator l

Indicator v4 Oscillators & momentumMoving averages
Updated 3 years ago · GPL-3.0 GitHub repository of PPO ATR Bands

This indicator plots a relative strength index in a separate pane. It calculates RSI from the average of the previous ten opens rather than the current bar, so that source does not change during the live candle. A moving average of the RSI is plotted and can b

Indicator v5 Oscillators & momentumMoving averages
Updated 2 years ago · None stated GitHub repository of No Repaint - Relative Strength Index

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