Indicator · Pine Script v5 · Own pane

Trailing Drawdown

by palitojendthen

The Trailing Drawdown indicator calculates and visualizes the percentage drop of a price series from its peak over a user-specified lookback period.

View on GitHub

GitHub stars20on the repository
Pine versionv5runs as is
Lines of code45
Settings4inputs
LicenceNone statedfile: MPL-2.0
Last update1 year ago12 Jul 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

45 lines of Pine Script v5 from indicator/trailing_drawdown.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

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

//@version=5
indicator(title = 'Trailing Drawdown', shorttitle = 'drawdown', overlay = false, scale = scale.right)


// input
src = input(defval = close, title = 'Source', group = 'Options', tooltip = 'Determines the source of input data, default to close')
viz_style = input.string(defval = 'Static', title = ' Type', group = 'Options', options = ['Static', 'Dynamic'], tooltip = 'Choose drawdown display type between static or dynamic')
show_level = input.bool(defval = true, title = 'Level', group = 'Lookback Period', tooltip = 'Determines whether to display current level, default to true', inline = 'f')
length = input.int(defval = 252, title = 'Length', minval = 100, maxval = 1000, group = 'Lookback Period', tooltip = 'Determines lookback period for compute trailing drawdown, default in a year', inline = 'f')


// drawdown
drawdown(_src, _length)=>
    var peaks = array.new_float(_length)
    var max_peaks = 0.0
    for i = 1 to _length-1
        if _src > _src[i]
            array.set(peaks, i, _src)
        max_peaks := array.max(peaks)
    dd = (src/max_peaks)-1
    [max_peaks, dd]

[max_peaks, dd] = drawdown(src, length)


// visualize
p1 = plot(dd, color = viz_style == 'Static' ? color.new(color.red, 80) : na, style = plot.style_area)
p2 = plot(dd, color = viz_style == 'Static' ? color.new(color.orange, 50) : na, style = plot.style_line, linewidth = 2)
p3 = plot(viz_style == 'Dynamic' ? max_peaks : 0, color = viz_style == 'Dynamic' ? color.navy : na)
p4 = plot(viz_style == 'Dynamic' ? src : 0, color = viz_style == 'Dynamic' ? color.new(color.orange, 50) : na)
fill(p3, p4, color = viz_style == 'Dynamic' ? color.new(color.red, 80) : na)

// additional shorter timeframe
[pp_month, dd_month] = drawdown(src, (length/12))
p5 = plot(viz_style == 'Dynamic' ? pp_month : 0, color = viz_style == 'Dynamic' ? color.navy : na)
fill(p5, p4, color = viz_style == 'Dynamic' ? color.new(color.orange, 95) : na)

// add current level line
float _level = viz_style == 'Static' ? dd : src
var line _line = line.new(na, na, na, na, xloc = xloc.bar_time, extend = extend.both, color = show_level ? color.olive : na, style = line.style_dotted)
line.set_xy1(_line, time, _level)
line.set_xy2(_line, time+1, _level)

Overview

Trailing Drawdown tracks maximum price levels within a rolling window and measures the current deviation from those peaks. Traders can switch between static area plots and dynamic peak filled views, with options to adjust the lookback length and display a dotted current level line. The script plots in a separate pane below the main chart.

Details

Published by

palitojendthen on GitHub

Type

Indicator, Pine Script v5

Draws

Separate pane below the chart

Category

Volatility & bands, Support & resistance

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.

Topics

indicatortechnical-analysispinescripttrading-algorithms

Settings

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

SettingTypeDefaultVariable
Sourcesourceclosesrc
TypestringStaticviz_style
Levelbooltrueshow_level
Lengthint252length
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

CCI DC

by be-thomas
65 stars

This indicator plots the Commodity Channel Index in a separate pane and surrounds it with a Donchian channel whose lookback length changes with volatility. It measures volatility as a ratio of short and long ATR values, then ranks that ratio and shortens or le

Indicator v5 Oscillators & momentumVolatility & bands
Updated 4 days ago · GPL-3.0 GitHub repository of CCI DC

QFA Pro Elite Refined

by JasonTeixeira
9 stars

This indicator plots a smoothed fear index in a separate pane. It measures how far the current low sits below the highest close over a lookback, scales that reading by an ATR percentile, and smooths it with a zero-lag EMA. A volatility regime raises or lowers

Indicator v6 Oscillators & momentumVolatility & bands
Updated 12 days ago · Custom licence GitHub repository of QFA Pro Elite Refined
7 stars

The Per Bak Self-Organized Criticality indicator calculates a composite fragility score by blending weights for tail risk from the SKEW index, volatility regimes from VIX terms, credit stress from HYG and LQD, and positioning from short volume data. It plots t

Indicator v5 Volatility & bandsSupport & resistance
Updated 3 months ago · None stated GitHub repository of Per Bak Self-Organized Criticality

ICT Liquidity Void Fill

by ArunKBhaskar
575 stars

This overlay indicator marks ICT liquidity void fills on the price chart and can show long and short signals. It places a Fibonacci level inside a displacement candle, defaulting to the midpoint, and draws lines in a chosen style. Optional filters limit the fi

Indicator v6 SMC / ICT / price actionSupport & resistance
Updated 1 year ago · MPL-2.0 GitHub repository of ICT Liquidity Void Fill

[RS]Donchian Fans

by ricardosantos79
261 stars

This indicator plots Donchian Channel boundary lines along with dynamic fan projections directly on the price chart. It calculates the highest high, lowest low, and median price over a customizable lookback period. Whenever price establishes a new channel high

Indicator v4 Support & resistanceTrend
Updated 4 years ago · None stated GitHub repository of [RS]Donchian Fans

Snag - VWAP

by dearvn
257 stars

This indicator plots a custom Anchored Volume-Weighted Average Price (VWAP) directly on the price chart along with optional standard deviation bands. Users can anchor the VWAP to standard time periods like sessions, weeks, or months, or to corporate events suc

Indicator v5 Moving averagesVolatility & bands Repaints
Updated 6 weeks ago · None stated GitHub repository of Snag - VWAP

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