Indicator · Pine Script v5 · Own pane

Risk

by tseitz· code credited to Skywalking2874

An indicator in a separate pane that estimates a normalised risk metric between 0 and 1.

View on GitHub

GitHub stars1on the repository
Pine versionv5runs as is
Lines of code101
Settings1inputs
LicenceNone statedfile: MPL-2.0
Last update4 years ago2 Dec 2021

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

101 lines of Pine Script v5 from packages/trading-bot-server/docs/strategies/RiskMetric.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

RiskMetric.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Skywalking2874

//@version=5
indicator("Risk", overlay = false, max_bars_back=5000)

find_ath(_src) => 
    // Returns a series of the ATH value
    var ath = 0.0
    if _src > ath
        ath := _src
    ath

find_atl(_src) =>
    // Returns a series of the ATL value
    var atl = 2.5
    if _src < atl
        atl := _src
    atl

threesixtyfive = ta.sma(close, 365)

average = (math.log(close) - math.log(threesixtyfive)) * math.pow(bar_index, 0.475)

highest_value = find_ath(average)
lowest_value = find_atl(average)

average_normalized = (average - lowest_value) / (highest_value - lowest_value)

colored_bars = input.bool(true, "Display the risk metric as colored bars", "The symbols bars and the risk metric will be displayed on a color gradient when this is set to true")

fill0 = color.new(color.green, 61)
fill1 = color.new(color.green, 69)
fill2 = color.new(color.green, 76)
fill3 = color.new(color.green, 83)
fill4 = color.new(color.green, 90)
fill5 = color.new(color.red, 90)
fill6 = color.new(color.red, 83)
fill7 = color.new(color.red, 76)
fill8 = color.new(color.red, 69)
fill9 = color.new(color.red, 61)

transparant = color.rgb(0, 0, 0, 100)
bar_color = color.rgb(0, 128, 255)

gradient0 = color.from_gradient(average_normalized, 0, 0.1, #0000ff, #000bff)
gradient1 = color.from_gradient(average_normalized, 0.1, 0.2, #000bff, #0090ff)
gradient2 = color.from_gradient(average_normalized, 0.2, 0.3, #0090ff, #00fbff)
gradient3 = color.from_gradient(average_normalized, 0.3, 0.4, #00fbff, #00ff7e)
gradient4 = color.from_gradient(average_normalized, 0.4, 0.5, #00ff7e, #00ff37)
gradient5 = color.from_gradient(average_normalized, 0.5, 0.6, #00ff37, #94ff00)
gradient6 = color.from_gradient(average_normalized, 0.6, 0.7, #94ff00, #ffff00)
gradient7 = color.from_gradient(average_normalized, 0.7, 0.8, #ffff00, #ffb200)
gradient8 = color.from_gradient(average_normalized, 0.8, 0.9, #ffb200, #ff8900)
gradient9 = color.from_gradient(average_normalized, 0.9, 1.0, #ff8900, #ff0017)

if colored_bars
    fill0 := color.new(#005555, 61)
    fill1 := color.new(#005555, 69)
    fill2 := color.new(#005555, 76)
    fill3 := color.new(#005555, 83)
    fill4 := color.new(#005555, 90)
    fill5 := color.new(#8b0000, 90)
    fill6 := color.new(#8b0000, 83)
    fill7 := color.new(#8b0000, 76)
    fill8 := color.new(#8b0000, 69)
    fill9 := color.new(#8b0000, 61)
    bar_color := average_normalized < 0.1 ? gradient0 : 
      average_normalized < 0.2 ? gradient1 : 
      average_normalized < 0.3 ? gradient2 : 
      average_normalized < 0.4 ? gradient3 : 
      average_normalized < 0.5 ? gradient4 : 
      average_normalized < 0.6 ? gradient5 :
      average_normalized < 0.7 ? gradient6 : 
      average_normalized < 0.8 ? gradient7 :
      average_normalized < 0.9 ? gradient8 : gradient9

buy0 = hline(0.0, color=transparant, linestyle=hline.style_solid)
buy1 = hline(0.1, color=transparant, linestyle=hline.style_solid)
buy2 = hline(0.2, color=transparant, linestyle=hline.style_solid)
buy3 = hline(0.3, color=transparant, linestyle=hline.style_solid)
buy4 = hline(0.4, color=transparant, linestyle=hline.style_solid)
buy5 = hline(0.5, color=transparant, linestyle=hline.style_solid)

sell0 = hline(0.6, color=transparant, linestyle=hline.style_solid)
sell1 = hline(0.7, color=transparant, linestyle=hline.style_solid)
sell2 = hline(0.8, color=transparant, linestyle=hline.style_solid)
sell3 = hline(0.9, color=transparant, linestyle=hline.style_solid)
sell4 = hline(1.0, color=transparant, linestyle=hline.style_solid)

fill(buy0, buy1, fill0, "DCA 5x")
fill(buy1, buy2, fill1, "DCA 4x")
fill(buy2, buy3, fill2, "DCA 3x")
fill(buy3, buy4, fill3, "DCA 2x")
fill(buy4, buy5, fill4, "DCA 1x")
fill(sell0, sell1, fill5, "DCA 1y")
fill(sell1, sell2, fill6, "DCA 2y")
fill(sell2, sell3, fill7, "DCA 3y")
fill(sell3, sell4, fill8, "DCA 4y")

plot(average_normalized, color=bar_color, title = "Risk")

Overview

Risk takes the log distance between price and its 365 period simple moving average, scales it by the bar index raised to a power, then normalises it against the running highest and lowest values. Bars and fills are tinted along a blue to red gradient, with horizontal bands every 0.1. One option switches the colored bars on. It needs long price history to behave sensibly.

Details

Published by

tseitz on GitHub

Code credit

Skywalking2874, as written in the script's header

Type

Indicator, Pine Script v5

Draws

Separate pane below the chart

Category

Volatility & bands

Markets

Crypto

Timeframes

H4 and above

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
Display the risk metric as colored barsbooltruecolored_bars
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