Documentation
¶
Overview ¶
Package volatility contains the volatility indicator functions.
This package belongs to the Indicator project. Indicator is a Golang module that supplies a variety of technical indicators, strategies, and a backtesting framework for analysis.
License ¶
Copyright (c) 2021-2026 Onur Cinar. The source code is provided under GNU AGPLv3 License. https://github.com/cinar/indicator
Disclaimer ¶
The information provided on this project is strictly for informational purposes and is not to be construed as advice or solicitation to buy or sell any security.
Index ¶
Examples ¶
Constants ¶
const ( // DefaultChandelierExitPeriod is the default period for the Chandelier Exit. DefaultChandelierExitPeriod = 22 // DefaultChandelierExitMultiplier is the default multiplier for the Chandelier Exit. DefaultChandelierExitMultiplier = 3 )
const ( // DefaultSuperTrendPeriod is the default period value. DefaultSuperTrendPeriod = 14 // DefaultSuperTrendMultiplier is the default multiplier value. DefaultSuperTrendMultiplier = 2.5 )
const (
// DefaultAccelerationBandsPeriod is the default period for the Acceleration Bands.
DefaultAccelerationBandsPeriod = 20
)
const (
// DefaultAtrPeriod is the default period for the Average True Range (ATR).
DefaultAtrPeriod = 14
)
const (
// DefaultBollingerBandsPeriod is the default period for the Bollinger Bands.
DefaultBollingerBandsPeriod = 20
)
const (
// DefaultChopPeriod is the default period for the Choppiness Index (CHOP).
DefaultChopPeriod = 14
)
const (
// DefaultDonchianChannelPeriod is the default period for the Donchian Channel.
DefaultDonchianChannelPeriod = 20
)
const (
// DefaultKeltnerChannelPeriod is the default period for the Keltner Channel.
DefaultKeltnerChannelPeriod = 20
)
const (
// DefaultMovingStdPeriod is the default time period for Moving Standard Deviation.
DefaultMovingStdPeriod = 1
)
const (
// DefaultPoPeriod is the default period for the Projection Oscillator (PO).
DefaultPoPeriod = 14
)
const (
// DefaultUlcerIndexPeriod is the default period for the Ulcer Index.
DefaultUlcerIndexPeriod = 14
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AccelerationBands ¶
AccelerationBands represents the configuration parameters for calculating the Acceleration Bands.
Upper Band = SMA(High * (1 + 4 * (High - Low) / (High + Low))) Middle Band = SMA(Closing) Lower Band = SMA(Low * (1 - 4 * (High - Low) / (High + Low)))
Example:
accelerationBands := NewAccelerationBands[float64]() accelerationBands.Compute(values)
func NewAccelerationBands ¶
func NewAccelerationBands[T helper.Number]() *AccelerationBands[T]
NewAccelerationBands function initializes a new Acceleration Bands instance with the default parameters.
func (*AccelerationBands[T]) Compute ¶
func (a *AccelerationBands[T]) Compute(high, low, closing <-chan T) (<-chan T, <-chan T, <-chan T)
Compute function takes a channel of numbers and computes the Acceleration Bands over the specified period.
func (*AccelerationBands[T]) IdlePeriod ¶
func (a *AccelerationBands[T]) IdlePeriod() int
IdlePeriod is the initial period that Acceleration Bands won't yield any results.
type Atr ¶
Atr represents the configuration parameters for calculating the Average True Range (ATR). It is a technical analysis indicator that measures market volatility by decomposing the entire range of stock prices for that period.
TR = Max((High - Low), (High - Previous Closing), (Previous Closing - Low)) ATR = MA TR
By default, SMA is used as the MA.
Example:
atr := volatility.NewAtr() atr.Compute(highs, lows, closings)
func NewAtrWithMa ¶
NewAtrWithMa function initializes a new ATR instance with the given moving average instance.
func NewAtrWithPeriod ¶
NewAtrWithPeriod function initializes a new ATR instance with the given period.
func (*Atr[T]) Compute ¶
func (a *Atr[T]) Compute(highs, lows, closings <-chan T) <-chan T
Compute function takes a channel of numbers and computes the ATR over the specified period.
func (*Atr[T]) IdlePeriod ¶
IdlePeriod is the initial period that Acceleration Bands won't yield any results.
type BollingerBandWidth ¶
type BollingerBandWidth[T helper.Number] struct { // Bollinger bands. BollingerBands *BollingerBands[T] }
BollingerBandWidth represents the configuration parameters for calculating the Bollinger Band Width. It measures the percentage difference between the upper band and the lower band. It decreases as Bollinger Bands narrows and increases as Bollinger Bands widens.
During a period of rising price volatity the bandwidth widens, and during a period of low market volatity bandwidth contracts.
Band Width = (Upper Band - Lower Band) / Middle BollingerBandWidth
Example:
bbw := NewBollingerBandWidth[float64]() bbw.Compute(c)
func NewBollingerBandWidth ¶
func NewBollingerBandWidth[T helper.Number]() *BollingerBandWidth[T]
NewBollingerBandWidth function initializes a new Bollinger Band Width instance with the default parameters.
func (*BollingerBandWidth[T]) Compute ¶
func (b *BollingerBandWidth[T]) Compute(c <-chan T) <-chan T
Compute function takes a channel of numbers and computes the Bollinger Band Width.
func (*BollingerBandWidth[T]) IdlePeriod ¶
func (b *BollingerBandWidth[T]) IdlePeriod() int
IdlePeriod is the initial period that Bollinger Band Width won't yield any results.
type BollingerBands ¶
BollingerBands represents the configuration parameters for calculating the Bollinger Bands. It is a technical analysis tool used to gauge a market's volatility and identify overbought and oversold conditions. Returns the upper band, the middle band, and the lower band.
Middle Band = 20-Period SMA. Upper Band = 20-Period SMA + 2 (20-Period Std) Lower Band = 20-Period SMA - 2 (20-Period Std)
Example:
bollingerBands := NewBollingerBands[float64]() bollingerBands.Compute(values)
func NewBollingerBands ¶
func NewBollingerBands[T helper.Number]() *BollingerBands[T]
NewBollingerBands function initializes a new Bollinger Bands instance with the default parameters.
func NewBollingerBandsWithPeriod ¶ added in v2.1.13
func NewBollingerBandsWithPeriod[T helper.Number](period int) *BollingerBands[T]
NewBollingerBandsWithPeriod function initializes a new Bollinger Bands instance with the given period.
func (*BollingerBands[T]) Compute ¶
func (b *BollingerBands[T]) Compute(c <-chan T) (<-chan T, <-chan T, <-chan T)
Compute function takes a channel of numbers and computes the Bollinger Bands over the specified period.
func (*BollingerBands[T]) IdlePeriod ¶
func (b *BollingerBands[T]) IdlePeriod() int
IdlePeriod is the initial period that Bollinger Bands won't yield any results.
type ChandelierExit ¶
type ChandelierExit[T helper.Number] struct { // Period is time period. Period int // Multiplier is for sensitivity. Multiplier T }
ChandelierExit represents the configuration parameters for calculating the Chandelier Exit. It sets a trailing stop-loss based on the Average True Value (ATR).
Chandelier Exit Long = 22-Period SMA High - ATR(22) * 3 Chandelier Exit Short = 22-Period SMA Low + ATR(22) * 3
Example:
ce := volatility.NewChandelierExit[float64]() ceLong, ceShort := ce.Compute(highs, lows, closings)
func NewChandelierExit ¶
func NewChandelierExit[T helper.Number]() *ChandelierExit[T]
NewChandelierExit function initializes a new Chandelier Exit instance with the default parameters.
func (*ChandelierExit[T]) Compute ¶
func (c *ChandelierExit[T]) Compute(highs, lows, closings <-chan T) (<-chan T, <-chan T)
Compute function takes a channel of numbers and computes the Chandelier Exit over the specified period.
func (*ChandelierExit[T]) IdlePeriod ¶
func (c *ChandelierExit[T]) IdlePeriod() int
IdlePeriod is the initial period that Chandelier Exit won't yield any results.
type Chop ¶ added in v2.1.29
Chop represents the configuration parameters for calculating the Choppiness Index (CHOP). It is a technical analysis indicator that measures the market's trendiness or choppiness.
CHOP = 100 * LOG10( SUM(ATR(1), n) / (MAX(High, n) - MIN(Low, n)) ) / LOG10(n)
func NewChop ¶ added in v2.1.29
NewChop function initializes a new CHOP instance with the default parameters.
func NewChopWithPeriod ¶ added in v2.1.29
NewChopWithPeriod function initializes a new CHOP instance with the given period.
func (*Chop[T]) Compute ¶ added in v2.1.29
func (c *Chop[T]) Compute(highs, lows, closings <-chan T) <-chan T
Compute function takes channels of highs, lows, and closings, and computes the CHOP over the specified period.
func (*Chop[T]) IdlePeriod ¶ added in v2.1.29
IdlePeriod is the initial period that CHOP won't yield any results.
type DonchianChannel ¶
type DonchianChannel[T helper.Number] struct { // Max is the Moving Max instance. Max *trend.MovingMax[T] // Min is the Moving Min instance. Min *trend.MovingMin[T] }
DonchianChannel represents the configuration parameters for calculating the Donchian Channel (DC). It calculates three lines generated by moving average calculations that comprise an indicator formed by upper and lower bands around a midrange or median band. The upper band marks the highest price of an asset while the lower band marks the lowest price of an asset, and the area between the upper and lower bands represents the Donchian Channel.
Upper Channel = Mmax(period, closings) Lower Channel = Mmin(period, closings) Middle Channel = (Upper Channel + Lower Channel) / 2
Example:
dc := volatility.NewDonchianChannel[float64]() result := dc.Compute(values)
func NewDonchianChannel ¶
func NewDonchianChannel[T helper.Number]() *DonchianChannel[T]
NewDonchianChannel function initializes a new Donchian Channel instance with the default parameters.
func NewDonchianChannelWithPeriod ¶
func NewDonchianChannelWithPeriod[T helper.Number](period int) *DonchianChannel[T]
NewDonchianChannelWithPeriod function initializes a new Donchian Channel instance with the given period.
func (*DonchianChannel[T]) Compute ¶
func (d *DonchianChannel[T]) Compute(c <-chan T) (<-chan T, <-chan T, <-chan T)
Compute function takes a channel of numbers and computes the Donchian Channel over the specified period.
func (*DonchianChannel[T]) IdlePeriod ¶
func (d *DonchianChannel[T]) IdlePeriod() int
IdlePeriod is the initial period that Donchian Channel won't yield any results.
type KeltnerChannel ¶
type KeltnerChannel[T helper.Number] struct { // Atr is the ATR instance. Atr *Atr[T] // Ema is the EMA instance. Ema *trend.Ema[T] }
KeltnerChannel represents the configuration parameters for calculating the Keltner Channel (KC). It provides volatility-based bands that are placed on either side of an asset's price and can aid in determining the direction of a trend.
Middle Line = EMA(period, closings) Upper Band = EMA(period, closings) + 2 * ATR(period, highs, lows, closings) Lower Band = EMA(period, closings) - 2 * ATR(period, highs, lows, closings)
Example:
dc := volatility.NewKeltnerChannel[float64]() result := dc.Compute(highs, lows, closings)
func NewKeltnerChannel ¶
func NewKeltnerChannel[T helper.Number]() *KeltnerChannel[T]
NewKeltnerChannel function initializes a new Keltner Channel instance with the default parameters.
func NewKeltnerChannelWithPeriod ¶
func NewKeltnerChannelWithPeriod[T helper.Number](period int) *KeltnerChannel[T]
NewKeltnerChannelWithPeriod function initializes a new Keltner Channel instance with the given period.
func (*KeltnerChannel[T]) Compute ¶
func (k *KeltnerChannel[T]) Compute(highs, lows, closings <-chan T) (<-chan T, <-chan T, <-chan T)
Compute function takes a channel of numbers and computes the Keltner Channel over the specified period.
func (*KeltnerChannel[T]) IdlePeriod ¶
func (k *KeltnerChannel[T]) IdlePeriod() int
IdlePeriod is the initial period that Keltner Channel won't yield any results.
type MovingStd ¶
MovingStd represents the configuration parameters for calculating the Moving Standard Deviation over the specified period.
Std = Sqrt(1/Period * Sum(Pow(value - sma), 2))
func NewMovingStd ¶
NewMovingStd function initializes a new Moving Standard Deviation instance with the default parameters.
func NewMovingStdWithPeriod ¶
NewMovingStdWithPeriod function initializes a new Moving Standard Deviation instance with the given period.
func (*MovingStd[T]) Compute ¶
func (m *MovingStd[T]) Compute(c <-chan T) <-chan T
Compute function takes a channel of numbers and computes the Moving Standard Deviation over the specified period.
func (*MovingStd[T]) IdlePeriod ¶
IdlePeriod is the initial period that Moving Standard Deviation won't yield any results.
type PercentB ¶ added in v2.1.13
type PercentB[T helper.Number] struct { // BollingerBands is the underlying Bollinger Bands indicator used for calculations. BollingerBands *BollingerBands[T] }
PercentB represents the parameters for calculating the %B indicator.
%B = (Close - Lower Band) / (Upper Band - Lower Band)
Example ¶
package main
import (
"fmt"
"github.com/cinar/indicator/v2/helper"
"github.com/cinar/indicator/v2/volatility"
)
func main() {
// Closing prices
closes := helper.SliceToChan([]float64{
318.600006, 315.839996, 316.149994, 310.570007, 307.779999,
305.820007, 305.98999, 306.390015, 311.450012, 312.329987,
309.290009, 301.910004, 300, 300.029999, 302,
307.820007, 302.690002, 306.48999, 305.549988, 303.429993,
})
// Initialize the %B indicator
percentB := volatility.NewPercentB[float64]()
// Compute %B
result := percentB.Compute(closes)
// Round digits
result = helper.RoundDigits(result, 2)
fmt.Println(helper.ChanToSlice(result))
}
Output: [0.3]
func NewPercentB ¶ added in v2.1.13
NewPercentB function initializes a new %B instance with the default parameters.
func NewPercentBWithPeriod ¶ added in v2.1.13
NewPercentBWithPeriod function initializes a new %B instance with the given period.
func (*PercentB[T]) Compute ¶ added in v2.1.13
func (p *PercentB[T]) Compute(closings <-chan T) <-chan T
Compute function takes a channel of numbers and computes the %B over the specified period.
func (*PercentB[T]) IdlePeriod ¶ added in v2.1.13
IdlePeriod is the initial period that %B yield any results.
type Po ¶
Po represents the configuration parameters for calculating the Projection Oscillator (PO). It uses the linear regression slope, along with highs and lows. Period defines the moving window to calculates the PO.
PL = Min(period, (high + MLS(period, x, high))) PH = Max(period, (low + MLS(period, x, low))) PO = 100 * (Closing - PL) / (PH - PL)
Example:
po := volatility.NewPo() ps := po.Compute(highs, lows, closings)
func NewPoWithPeriod ¶
NewPoWithPeriod function initializes a new PO instance with the given period.
func (*Po[T]) Compute ¶
func (p *Po[T]) Compute(highs, lows, closings <-chan T) <-chan T
Compute function takes a channel of numbers and computes the PO over the specified period.
func (*Po[T]) IdlePeriod ¶
IdlePeriod is the initial period that PO won't yield any results.
type SuperTrend ¶
SuperTrend represents the configuration parameters for calculating the Super Trend.
BasicUpperBands = (High + Low) / 2 + Multiplier * ATR
BasicLowerBands = (High + Low) / 2 - Multiplier * ATR
FinalUpperBands = If (BasicUpperBand < PreviousFinalUpperBand)
Or (PreviousClose > PreviousFinalUpperBand)
Then BasicUpperBand Else PreviousFinalUpperBand
FinalLowerBands = If (BasicLowerBand > PreviousFinalLowerBand)
Or (PreviousClose < PreviousFinalLowerBand)
Then BasicLowerBand Else PreviousFinalLowerBand
SuperTrend = If upTrend
Then
If (Close <= FinalUpperBand) Then FinalUpperBand Else FinalLowerBand
Else
If (Close >= FinalLowerBand) Then FinalLowerBand Else FinalUpperBand
UpTrend = If (SuperTrend == FinalUpperBand) Then True Else False
Example:
func NewSuperTrend ¶
func NewSuperTrend[T helper.Number]() *SuperTrend[T]
NewSuperTrend function initializes a new Super Trend instance with the default parameters.
func NewSuperTrendWithMa ¶
func NewSuperTrendWithMa[T helper.Number](ma trend.Ma[T], multiplier T) *SuperTrend[T]
NewSuperTrendWithMa function initializes a new Super Trend instance with the given moving average instance and multiplier.
func NewSuperTrendWithPeriod ¶
func NewSuperTrendWithPeriod[T helper.Number](period int, multiplier T) *SuperTrend[T]
NewSuperTrendWithPeriod initializes a new Super Trend instance with the given period and multiplier.
func (*SuperTrend[T]) Compute ¶
func (s *SuperTrend[T]) Compute(highs, lows, closings <-chan T) <-chan T
Compute function calculates the Super Trend, using separate channels for highs, lows, and closings.
func (*SuperTrend[T]) IdlePeriod ¶
func (s *SuperTrend[T]) IdlePeriod() int
IdlePeriod is the initial period that Super Trend won't yield any results.
type UlcerIndex ¶
UlcerIndex represents the configuration parameters for calculating the Ulcer Index (UI). It measures downside risk. The index increases in value as the price moves farther away from a recent high and falls as the price rises to new highs.
High Closings = Max(period, Closings) Percentage Drawdown = 100 * ((Closings - High Closings) / High Closings) Squared Average = Sma(period, Percent Drawdown * Percent Drawdown) Ulcer Index = Sqrt(Squared Average)
Example:
ui := volatility.NewUlcerIndex[float64]() ui.Compute(closings)
func NewUlcerIndex ¶
func NewUlcerIndex[T helper.Number]() *UlcerIndex[T]
NewUlcerIndex function initializes a new Ulcer Index instance with the default parameters.
func (*UlcerIndex[T]) Compute ¶
func (u *UlcerIndex[T]) Compute(closings <-chan T) <-chan T
Compute function takes a channel of numbers and computes the Ulcer Index over the specified period.
func (*UlcerIndex[T]) IdlePeriod ¶
func (u *UlcerIndex[T]) IdlePeriod() int
IdlePeriod is the initial period that Ulcer Index won't yield any results.