Historical volatility
All indicators

LibraryVolatility

Indicator

Historical volatility

How much the closes have moved from bar to bar, as an annualised volatility in percent, the unit options traders quote.

VolatilityOwn pane2 inputsHV

How it is calculated

Each bar's log return is the natural log of the close over the previous close. HV is the population standard deviation of the last length log returns, times the square root of annual (bars per year), times 100. A close of 0 or below, or a ratio too extreme for a float, has no return: the window starts over and the line comes back after length valid returns. Close to close volatility.

Reference: Close to close volatility (annualised population deviation of log returns)

How to read it

An HV of 20 means the closes moved as if the yearly standard deviation were about 20 percent. Compare it with its own history or with implied volatility. annual must match the timeframe, 252 for daily stock bars or 365 for daily crypto bars; a wrong value scales the whole line.

In a strategy

Scale position size inversely to volatility, or trade only while ctx.ind("hv", length=20, annual=252) is below a level such as 40, so a strategy carries a similar risk in calm and wild markets.

Inputs

InputTypeDefaultRange or choices
LengthlengthWhole number10min 2, max 5000
Bars per yearannualWhole number252min 1, max 100000

What it draws

LineDrawn as
HVLine

Used in

No strategy template reads it yet.

OrionQuant