Skip to content

Volatility API Reference

Volatility Measurement Functions

This module provides functions for measuring volatility in financial time series data.

atr(close, high, low, period=14)

Calculate Average True Range (ATR) over a specified period.

ATR measures market volatility by decomposing the entire range of an asset price.

Parameters:

Name Type Description Default
close ndarray

Close prices

required
high ndarray

High prices. If None, assumes close contains close prices and high=low=close

required
low ndarray

Low prices. If None, assumes close contains close prices and high=low=close

required
period int

Number of periods to calculate ATR

14

Returns:

Type Description
ndarray

ATR values

bollinger_bands(data, period=20, std_dev=2.0)

Calculate Bollinger Bands over a specified period.

Bollinger Bands consist of a middle band (SMA), an upper band (SMA + kstd), and a lower band (SMA - kstd).

Parameters:

Name Type Description Default
data ndarray

Input time series data

required
period int

Number of periods for the moving average

20
std_dev float

Number of standard deviations for the upper and lower bands

2.0

Returns:

Type Description
tuple of numpy.ndarray

Tuple containing (upper_band, middle_band, lower_band)

donchian_channels(data, high, low, period=20)

Calculate Donchian Channels over a specified period.

Donchian Channels consist of an upper band (highest high), a lower band (lowest low), and a middle band (average of upper and lower).

Parameters:

Name Type Description Default
data ndarray

Input time series data (typically close prices)

required
high ndarray

High prices. If None, uses data

required
low ndarray

Low prices. If None, uses data

required
period int

Number of periods for the channels

20

Returns:

Type Description
tuple of numpy.ndarray

Tuple containing (upper_channel, middle_channel, lower_channel)

historical_volatility(data, period=21, annualization_factor=252)

Calculate historical volatility over a specified period.

Historical volatility is the standard deviation of log returns, typically annualized.

Parameters:

Name Type Description Default
data ndarray

Input time series data

required
period int

Number of periods to calculate volatility

21
annualization_factor int

Factor to annualize volatility (252 for daily data, 52 for weekly, 12 for monthly)

252

Returns:

Type Description
ndarray

Historical volatility values as percentage

keltner_channels(close, high, low, period=20, atr_period=10, multiplier=2.0)

Calculate Keltner Channels over a specified period.

Keltner Channels consist of a middle band (EMA), an upper band (EMA + kATR), and a lower band (EMA - kATR).

Parameters:

Name Type Description Default
close ndarray

Close prices

required
high ndarray

High prices. If None, assumes close contains close prices and high=low=close

required
low ndarray

Low prices. If None, assumes close contains close prices and high=low=close

required
period int

Number of periods for the EMA

20
atr_period int

Number of periods for the ATR

10
multiplier float

Multiplier for the ATR

2.0

Returns:

Type Description
tuple of numpy.ndarray

Tuple containing (upper_channel, middle_channel, lower_channel)

volatility_ratio(data, period=21, smooth_period=5)

Calculate Volatility Ratio over a specified period.

Volatility Ratio compares recent volatility to historical volatility. Values above 1 indicate increasing volatility, values below 1 indicate decreasing volatility.

Parameters:

Name Type Description Default
data ndarray

Input time series data

required
period int

Number of periods for historical volatility

21
smooth_period int

Number of periods to smooth the ratio

5

Returns:

Type Description
ndarray

Volatility Ratio values