Indicator · Pine Script v5 · Own pane

Barrier Volatility Estimator

by creatidy· code credited to Marlborough76

An indicator in a separate pane that estimates annualised volatility with a barrier method.

View on GitHub

GitHub stars1on the repository
Pine versionv5runs as is
Lines of code71
Settings2inputs
LicenceGPL-3.0
Last update3 years ago10 Aug 2023

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

71 lines of Pine Script v5 from Indicators/Barrier Volatility Estimator.pine, published under GPL-3.0. Keep the author's credit and licence lines when you reuse it.

Barrier Volatility Estimator.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Marlborough76

//@version=5
indicator("Barrier Volatility Estimator", overlay=false, precision=6, max_bars_back=1500)

get_barrier(observations, delta) =>
    n = timeframe.isminutes ? observations * 24 * 60 / timeframe.multiplier : 14
    float close_volatility = na
    float E = na
    float barrier = na
    int log_time = na
    float exit_time = 0
    float exit_happend = 0
    float _delta = delta //na(close_volatility[1]) or close_volatility[1] * 0.02 < delta ? delta : close_volatility[1] * 0.02
    
    // zeroing first element
    barrier := (na(barrier[1])) ? open : barrier[1]
    log_time := (na(log_time[1])) ? time : log_time[1]
    
    float weight = 0
    if ((high > barrier * (1 + _delta) ) or ((low < barrier * (1 - _delta))))
        weight := high > barrier * (1 + _delta) ? ((high - barrier) / barrier) / _delta : ((barrier - low) / barrier) / _delta
        barrier := high > barrier * (1 + _delta) ? high : low  //close
        exit_time := (time / 1000 - log_time) / weight
        log_time := time / 1000
        exit_happend := 1
    
    float d = math.sum(exit_happend, n)
    float exit_time_sum = math.sum(exit_time, n)
    
    E := d > 0 ? exit_time_sum / d : na
    
    if(timeframe.isintraday)
        if(timeframe.isminutes)
            E := E / (60 * timeframe.multiplier)
            close_volatility := _delta / math.sqrt(E) * math.sqrt(365 * 24 * 60 / timeframe.multiplier)
    else
        if (timeframe.isdaily)
            E := E / (60 * 60 * 24)
            close_volatility := _delta / math.sqrt(E) * math.sqrt(365)
        else
            E := na
            close_volatility := na


// Barrier method for volatility estimation
delta = input.float(0.02, minval=0.001, maxval = 0.999, step=0.001, title="_delta")
observations = input.int(14, minval=1, step=1, title="Number of observations (days)")

float close_volatility = 0
close_volatility := close_volatility + get_barrier(observations-1, 0.5 * delta)
close_volatility := close_volatility + get_barrier(observations, 0.6 * delta)
close_volatility := close_volatility + get_barrier(observations+1, 0.7 * delta)
close_volatility := close_volatility + get_barrier(observations, 0.8 * delta)
close_volatility := close_volatility + get_barrier(observations-1, 0.9 * delta)
close_volatility := close_volatility + get_barrier(observations, 1.0 * delta)
close_volatility := close_volatility + get_barrier(observations+1, 1.1 * delta)
close_volatility := close_volatility + get_barrier(observations, 1.2 * delta)
close_volatility := close_volatility + get_barrier(observations-1, 1.3 * delta)
close_volatility := close_volatility + get_barrier(observations, 1.4 * delta)
close_volatility := close_volatility + get_barrier(observations+1, 1.5 * delta)
close_volatility := close_volatility / 11

color_blank = color.new(color.white, 100)

ema_5 = ta.ema(close_volatility, 5)
ema_5_highest = ta.lowest(ema_5, 200)
ema_5_lowest =  ta.highest(ema_5, 200)
col_up = ema_5 == ema_5_highest ? color.green : ema_5 == ema_5_lowest ? color.red : color.gray
plot(close_volatility, color=col_up)

Overview

Barrier Volatility Estimator resets a price barrier whenever high or low moves beyond a set fractional distance, records how long each exit took, averages those times over a window, and converts the result to volatility. Eleven barrier sizes around the chosen delta are averaged to smooth the estimate. The plotted line is coloured by whether its short EMA sits at a 200-bar extreme. It only works on minute-based and daily charts and needs no trading settings.

Details

Published by

creatidy on GitHub

Code credit

Marlborough76, as written in the script's header

Type

Indicator, Pine Script v5

Draws

Separate pane below the chart

Category

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
_deltafloat0.02delta
Number of observations (days)int14observations
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

This indicator calculates the distribution of close-to-close percentage changes using rate of change on closing prices. It bins percentage changes into intervals of 0.25 percent increments from negative to positive values and plots a normalized cumulative hist

Indicator v4 Volatility & bands
Updated 11 days ago · GPL-3.0 GitHub repository of Close-to-Close % Change Distribution

This indicator plots a Supertrend-based trend-following setup in a separate pane and skips conditions it treats as a sideways market. A volatility range is built from an ATR value, with RMA, SMA, EMA, or WMA smoothing, or from a fixed point distance. Optional

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

This indicator plots candle range and open-to-close movement as percentages in a separate pane. Range is the high-low difference divided by the low, and change is the close-open difference divided by the open. Both series draw as columns. Teal columns show ran

Indicator v5 Volatility & bands
Updated 2 months ago · None stated GitHub repository of Percent & Volatility [Alorse]

Mojito v3 Strategy

by namuan
318 stars

The Mojito v3 Strategy indicator evaluates volatility term structure by calculating the ratio between front-month and second-month VIX futures contracts. It smooths this ratio using a configurable median filter and compares the result against user-defined uppe

Indicator v5 Volatility & bands
Updated 2 months ago · MIT GitHub repository of Mojito v3 Strategy

Function - Logistic Equation

by ricardosantos79
261 stars

Calculates dynamic modeling values by applying a mathematical logistic differential equation to price change over a specified lookback length. It measures price momentum relative to the recent high-low price range. The script plots positive and negative logist

Indicator v5 Oscillators & momentumVolatility & bands
Updated 4 years ago · None stated GitHub repository of Function - Logistic Equation

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

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