Trading metric
Ulcer Index Calculator
Paste a series of prices or account balances and this computes the Ulcer Index over your lookback period. It measures how deep and how long a series stays below its recent highs, so it penalizes a long grind lower more than a brief dip.
What the Ulcer Index measures
Peter Martin and Byron McCann introduced the Ulcer Index in 1987 as a measure of downside risk. Ordinary volatility counts upward and downward moves alike. The Ulcer Index looks only at how far the series sits below its recent high, and it squares those distances before averaging, so a deep decline weighs far more than several shallow ones.
The calculation has three steps. First, find the highest value over the lookback window. Second, express the current value as a percentage below that high. Third, average the squared percentages over the window and take the square root. A series that only rises scores zero.
How to read the result
The result is in percentage points, so a reading of 5 means the series has typically been several percent under its recent peak, weighted toward the worst stretches. It is most useful for comparing two series over the same window and period, not as an absolute threshold.
It differs from the max drawdown, which reports only the single deepest fall. The Ulcer Index reflects every bar spent below a high, so it captures duration as well as depth. It also depends on the period you choose, and a past reading says nothing about future results.
What are TradingView and Pine Script?
TradingView is one of the most widely used charting and market-analysis platforms, where traders and analysts study price movement across stocks, crypto, forex, and futures on interactive charts. Pine Script is TradingView's own lightweight programming language, created so anyone can build custom tools that run directly on those charts.
People use Pine Script to build four main kinds of tools. Indicators calculate and plot values on the chart, exactly like the calculation above, but recomputed automatically on every bar. Strategies add explicit entry and exit rules and can be backtested against historical data in TradingView's Strategy Tester to see how they would have behaved. Screeners scan many symbols at once for conditions you define. Alerts notify you the moment a condition you specified occurs, so you do not have to watch the screen.
The value is precision and automation. Instead of eyeballing a chart, you describe exactly what you want measured, visualized, or notified about, and TradingView runs it consistently across any market and timeframe. That is why traders, analysts, and developers write Pine Script: it turns a manual charting idea into a repeatable tool. These tools are for tracking, visualizing, and testing market ideas; they do not tell you what to trade, and that decision always remains yours.
Writing that code by hand means learning Pine Script's syntax, its type system, and the exact names of hundreds of built-in functions. It is a real programming language, and small mistakes stop a script from compiling in the Pine Editor.
Turn this into Pine Script
The Pine Script version follows the same three steps with built-ins: ta.highest for the rolling high, ta.sma to average the squared drawdowns, and math.sqrt for the final root. It plots as an oscillator in its own pane.
Like the calculator, it needs 2 * length - 1 bars before the first value appears, because the average runs over drawdowns that themselves need a full window.
//@version=6
indicator("Ulcer Index", overlay = false)
length = input.int(14, "Length", minval = 2)
src = input.source(close, "Source")
// Percent drawdown from the highest value of the last `length` bars
highest = ta.highest(src, length)
pctDrawdown = 100 * (src - highest) / highest
// Ulcer Index: square root of the average squared drawdown
ulcer = math.sqrt(ta.sma(pctDrawdown * pctDrawdown, length))
plot(ulcer, "Ulcer Index", color = color.new(color.purple, 0))PineScripter is an AI built specifically for Pine Script. You describe what you want in plain English and it writes TradingView-ready v6 code. Because it is specialized on the Pine Script language and its exact function signatures, it tends to produce code that compiles far more reliably than general-purpose models like ChatGPT, which often invent functions that do not exist in Pine Script.
Related free calculators
From the blog
PineScripter is an AI developer tool that helps you write Pine Script code. It is not a financial advisor and will never offer financial, investment, or trading advice. Everything on this page, including the calculator and the explanations, is provided purely for educational and informational purposes. Any decision about how to interpret an indicator or trade a market is entirely your own. See our full disclaimer for more.