Indicator · Pine Script v5 · On the chart

Trend Strength & Volatility v2

by Zettt

This indicator calculates standardized trend strength and historical volatility to estimate position sizes.

View on GitHub

GitHub stars43on the repository
Pine versionv5runs as is
Lines of code57
Settings6inputs
LicenceNone statedfile: MPL-2.0
Last update1 year ago23 Sep 2025

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

57 lines of Pine Script v5 from Trend Strength & Volatility v2.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

Trend Strength & Volatility v2.pine
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Zettt

//@version=5
indicator("Trend Strength & Volatility v2", overlay=true)

// EMA lengths
ema1Length = input.int(defval = 21, title = "EMA 1", tooltip = "EMA1/2 and EMA2/3 differences will be calculated and standarized, putting them in relation to price.") 
ema2Length = input.int(defval = 55, title = "EMA 2")
ema3Length = input.int(defval = 100, title = "EMA 3")
src = input.source(open, "Source")

// Standardized Trend Strength
// ###########################
// We use MACD as trend strength indicator because it calculates the difference of 2 EMAs
// The measurement is standardized to price
[macdShort, signalShort, histShort] = ta.macd(close[1], ema1Length, ema2Length, 9) // MACD for shorter EMAs
[macdLong, signalLong, histLong] = ta.macd(close[1], ema2Length, ema3Length, 9) // MACD for longer EMAs
differenceToPriceShort = macdShort / close
differenceToPriceLong = macdLong / close

// Historical Volatility
// #####################
lookbackLength = input.int(title='Lookback Length', defval=100, minval=1)

dailyReturns = close / close[1] - 1
historicalVolatility = ta.stdev(dailyReturns, lookbackLength) * math.sqrt(lookbackLength)

// Position Size
tradingCapital = input.int(defval = 1, title = "Trading Capital", minval = 1)
positionSizeShort = tradingCapital * (differenceToPriceShort / historicalVolatility)
positionSizeLong = tradingCapital * (differenceToPriceLong / historicalVolatility)

// Create a table with two columns and three rows
t = table.new(position.bottom_left, 6, 2, bgcolor=color.new(color.white, 10))

// Update table cells
// Table Header
table.cell(t, 0, 0, text="Short") // Trend strength short
table.cell(t, 1, 0, text="Long") // Trend strength short
table.cell(t, 2, 0, text="HV") // Trend strength short
table.cell(t, 3, 0, text="Size S") // ATR distance in percent
table.cell(t, 4, 0, text="Size L") // ATR distance in percent

// Column 0
table.cell(t, 0, 1, text=str.tostring(differenceToPriceShort, "#.##"))

// Column 1
table.cell(t, 1, 1, text=str.tostring(differenceToPriceLong, "#.##"))
// Column 2
table.cell(t, 2, 1, text=str.tostring(historicalVolatility, "#.##"))
// Column 3
// table.cell(t, 3, 1, text=str.tostring(risk, "#.##"))
// Column 4
table.cell(t, 3, 1, text=str.tostring(positionSizeShort, "#.##"))
// Column 5
table.cell(t, 4, 1, text=str.tostring(positionSizeLong, "#.##"))

Overview

Trend Strength & Volatility v2 computes the differences between three user-defined EMAs standardized by price using MACD logic, while historical volatility is derived from the standard deviation of returns over a selectable lookback. It then scales position size by dividing trend strength by volatility against a capital setting. All values are displayed in a summary table on the chart without generating direct trade signals.

Details

Published by

Zettt on GitHub

Type

Indicator, Pine Script v5

Draws

On the price chart

Category

Trend, Volatility & bands, Screeners & dashboards

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
EMA 1int21ema1Length
EMA 2int55ema2Length
EMA 3int100ema3Length
Sourcesourceopensrc
Lookback Lengthint100lookbackLength
Trading Capitalint1tradingCapital
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

92 stars

This indicator classifies the market into gamma regime types (negative gamma trend, positive gamma mean-reversion, or transition) by calculating a composite score based on daily ATR ratios, session VWAP crossover frequencies, and opening range displacement. It

Indicator v5 Volatility & bandsTrend
Updated 8 weeks ago · Custom licence GitHub repository of Moon Dev GEX Regime & Trap
46 stars

This indicator uses a Hidden Markov Model (HMM) framework combined with an adaptive dual moving average engine and Directional Movement Index (DMI) features to identify market regimes (bullish, bearish, or ranging/chop). It calculates regime probabilities on e

Indicator v5 AI / machine learningTrend
Updated 7 weeks ago · None stated GitHub repository of HMM Enhanced: Regime Probability

This overlay indicator plots a SuperTrend-style band on the price chart using ATR length, a selectable source, and a base factor. Optional settings enable an adaptive factor driven by rate of change, a volatility filter, K-means clustering, and an LSTM layer w

Indicator v6 TrendVolatility & bands
Updated 4 months ago · MIT GitHub repository of ML SuperTrend (Ultimate) - Auto-Optimized AI with LSTM

Adaptive Fair Value Cloud calculates a volatility-adaptive fair value channel around a selectable centerline, such as KAMA, EMA, linear regression, or VWMA. It blends ATR and mean deviation with volatility percentiles and trend sensors, including ADX, Efficien

Indicator v6 Volatility & bandsTrend
Updated yesterday · MIT GitHub repository of Adaptive Fair Value Cloud [WavesUnchained]

This indicator scans a pasted list of symbols and shows matching long and short setups in a table in a separate pane. It uses a Supertrend crossover, then applies an ATR or fixed-point volatility range so quiet, sideways conditions can be skipped. Optional fil

Indicator v5 TrendVolatility & bands
Updated 1 year ago · MPL-2.0 GitHub repository of Trend Following Setup - Sideways Market Skipper Scanner

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