import "github.com/cinar/indicator/v2/volatility"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.
Copyright (c) 2021-2026 Onur Cinar.
The source code is provided under GNU AGPLv3 License.
https://github.com/cinar/indicator
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.
- Constants
- type AccelerationBands
- func NewAccelerationBands[T helper.Number]() *AccelerationBands[T]
- func (a *AccelerationBands[T]) Compute(high, low, closing <-chan T) (<-chan T, <-chan T, <-chan T)
- func (a *AccelerationBands[T]) ComputeWithContext(ctx context.Context, high, low, closing <-chan T) (<-chan T, <-chan T, <-chan T)
- func (a *AccelerationBands[T]) IdlePeriod() int
- type AnnualizedHistoricalVolatility
- func NewAnnualizedHistoricalVolatility[T helper.Number]() *AnnualizedHistoricalVolatility[T]
- func NewAnnualizedHistoricalVolatilityWithPeriod[T helper.Number](period int) *AnnualizedHistoricalVolatility[T]
- func (a *AnnualizedHistoricalVolatility[T]) Compute(prices <-chan T) <-chan T
- func (a *AnnualizedHistoricalVolatility[T]) ComputeWithContext(ctx context.Context, prices <-chan T) <-chan T
- func (a *AnnualizedHistoricalVolatility[T]) IdlePeriod() int
- func (a *AnnualizedHistoricalVolatility[T]) String() string
- type Atr
- func NewAtr[T helper.Number]() *Atr[T]
- func NewAtrWithMa[T helper.Number](ma trend.Ma[T]) *Atr[T]
- func NewAtrWithPeriod[T helper.Number](period int) *Atr[T]
- func (a *Atr[T]) Compute(highs, lows, closings <-chan T) <-chan T
- func (a *Atr[T]) ComputeWithContext(ctx context.Context, highs, lows, closings <-chan T) <-chan T
- func (a *Atr[T]) IdlePeriod() int
- type BollingerBandWidth
- type BollingerBands
- func NewBollingerBands[T helper.Number]() *BollingerBands[T]
- func NewBollingerBandsWithPeriod[T helper.Number](period int) *BollingerBands[T]
- func (b *BollingerBands[T]) Compute(c <-chan T) (<-chan T, <-chan T, <-chan T)
- func (b *BollingerBands[T]) ComputeWithContext(ctx context.Context, c <-chan T) (<-chan T, <-chan T, <-chan T)
- func (b *BollingerBands[T]) IdlePeriod() int
- type ChandelierExit
- func NewChandelierExit[T helper.Number]() *ChandelierExit[T]
- func (c *ChandelierExit[T]) Compute(highs, lows, closings <-chan T) (<-chan T, <-chan T)
- func (c *ChandelierExit[T]) ComputeWithContext(ctx context.Context, highs, lows, closings <-chan T) (<-chan T, <-chan T)
- func (c *ChandelierExit[T]) IdlePeriod() int
- type Chop
- func NewChop[T helper.Number]() *Chop[T]
- func NewChopWithPeriod[T helper.Number](period int) *Chop[T]
- func (c *Chop[T]) Compute(highs, lows, closings <-chan T) <-chan T
- func (c *Chop[T]) ComputeWithContext(ctx context.Context, highs, lows, closings <-chan T) <-chan T
- func (c *Chop[T]) IdlePeriod() int
- func (c *Chop[T]) String() string
- type DonchianChannel
- func NewDonchianChannel[T helper.Number]() *DonchianChannel[T]
- func NewDonchianChannelWithPeriod[T helper.Number](period int) *DonchianChannel[T]
- func (d *DonchianChannel[T]) Compute(c <-chan T) (<-chan T, <-chan T, <-chan T)
- func (d *DonchianChannel[T]) ComputeWithContext(ctx context.Context, c <-chan T) (<-chan T, <-chan T, <-chan T)
- func (d *DonchianChannel[T]) IdlePeriod() int
- type HistoricalVolatility
- func NewHistoricalVolatility[T helper.Number]() *HistoricalVolatility[T]
- func NewHistoricalVolatilityWithPeriod[T helper.Number](period int) *HistoricalVolatility[T]
- func (h *HistoricalVolatility[T]) Compute(prices <-chan T) <-chan T
- func (h *HistoricalVolatility[T]) ComputeWithContext(ctx context.Context, prices <-chan T) <-chan T
- func (h *HistoricalVolatility[T]) IdlePeriod() int
- func (h *HistoricalVolatility[T]) String() string
- type KeltnerChannel
- func NewKeltnerChannel[T helper.Number]() *KeltnerChannel[T]
- func NewKeltnerChannelWithPeriod[T helper.Number](period int) *KeltnerChannel[T]
- func (k *KeltnerChannel[T]) Compute(highs, lows, closings <-chan T) (<-chan T, <-chan T, <-chan T)
- func (k *KeltnerChannel[T]) ComputeWithContext(ctx context.Context, highs, lows, closings <-chan T) (<-chan T, <-chan T, <-chan T)
- func (k *KeltnerChannel[T]) IdlePeriod() int
- type MovingStd
- type PercentB
- func NewPercentB[T helper.Number]() *PercentB[T]
- func NewPercentBWithPeriod[T helper.Number](period int) *PercentB[T]
- func (p *PercentB[T]) Compute(closings <-chan T) <-chan T
- func (p *PercentB[T]) ComputeWithContext(ctx context.Context, closings <-chan T) <-chan T
- func (p *PercentB[T]) IdlePeriod() int
- func (p *PercentB[T]) String() string
- type Po
- type SuperTrend
- func NewSuperTrend[T helper.Number]() *SuperTrend[T]
- func NewSuperTrendWithMa[T helper.Number](ma trend.Ma[T], multiplier T) *SuperTrend[T]
- func NewSuperTrendWithPeriod[T helper.Number](period int, multiplier T) *SuperTrend[T]
- func (s *SuperTrend[T]) Compute(highs, lows, closings <-chan T) <-chan T
- func (s *SuperTrend[T]) ComputeWithContext(ctx context.Context, highs, lows, closings <-chan T) <-chan T
- func (s *SuperTrend[T]) IdlePeriod() int
- type TrueRange
- func NewTrueRange[T helper.Number]() *TrueRange[T]
- func (tr *TrueRange[T]) Compute(highs, lows, closings <-chan T) <-chan T
- func (tr *TrueRange[T]) ComputeWithContext(ctx context.Context, highs, lows, closings <-chan T) <-chan T
- func (tr *TrueRange[T]) IdlePeriod() int
- func (tr *TrueRange[T]) String() string
- type UlcerIndex
- type ZScore
- func NewZScore[T helper.Number]() *ZScore[T]
- func NewZScoreWithPeriod[T helper.Number](period int) *ZScore[T]
- func (z *ZScore[T]) Compute(c <-chan T) <-chan T
- func (z *ZScore[T]) ComputeWithContext(ctx context.Context, c <-chan T) <-chan T
- func (z *ZScore[T]) IdlePeriod() int
- func (z *ZScore[T]) String() string
const (
// DefaultAnnualizedHistoricalVolatilityPeriod is the default period for
// Annualized Historical Volatility (AHV).
DefaultAnnualizedHistoricalVolatilityPeriod = 21
// DefaultTradingDaysPerYear is the standard number of trading days in a year.
DefaultTradingDaysPerYear = 252
)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 (
// DefaultHistoricalVolatilityPeriod is the default period for Historical Volatility (HV).
DefaultHistoricalVolatilityPeriod = 21
)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
)const (
// DefaultZScorePeriod is the default period for Z-Score.
DefaultZScorePeriod = 20
)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)
type AccelerationBands[T helper.Number] struct {
// Time period.
Period int
}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 wraps ComputeWithContext for backwards compatibility.
Deprecated: Use ComputeWithContext instead.
func (*AccelerationBands[T]) ComputeWithContext
func (a *AccelerationBands[T]) ComputeWithContext(ctx context.Context, high, low, closing <-chan T) (<-chan T, <-chan T, <-chan T)ComputeWithContext function takes a channel of numbers and computes the Acceleration Bands over the specified period.
func (*AccelerationBands[T]) IdlePeriod
func (a *AccelerationBands[T]) IdlePeriod() intIdlePeriod is the initial period that Acceleration Bands won't yield any results.
AnnualizedHistoricalVolatility represents the configuration parameters for calculating Annualized Historical Volatility (AHV). It annualizes the Historical Volatility by multiplying it by the square root of the number of trading days per year.
AHV = HV × √TradingDaysPerYear
type AnnualizedHistoricalVolatility[T helper.Number] struct {
// Hv is the underlying Historical Volatility indicator.
Hv *HistoricalVolatility[T]
// TradingDaysPerYear is the number of trading days in a year (default: 252).
TradingDaysPerYear int
}func NewAnnualizedHistoricalVolatility[T helper.Number]() *AnnualizedHistoricalVolatility[T]NewAnnualizedHistoricalVolatility function initializes a new Annualized Historical Volatility instance with the default parameters.
func NewAnnualizedHistoricalVolatilityWithPeriod[T helper.Number](period int) *AnnualizedHistoricalVolatility[T]NewAnnualizedHistoricalVolatilityWithPeriod function initializes a new Annualized Historical Volatility instance with the given period.
func (*AnnualizedHistoricalVolatility[T]) Compute
func (a *AnnualizedHistoricalVolatility[T]) Compute(prices <-chan T) <-chan TCompute wraps ComputeWithContext for backwards compatibility.
Deprecated: Use ComputeWithContext instead.
func (*AnnualizedHistoricalVolatility[T]) ComputeWithContext
func (a *AnnualizedHistoricalVolatility[T]) ComputeWithContext(ctx context.Context, prices <-chan T) <-chan TComputeWithContext function takes a channel of prices and computes the Annualized Historical Volatility over the specified period.
func (*AnnualizedHistoricalVolatility[T]) IdlePeriod
func (a *AnnualizedHistoricalVolatility[T]) IdlePeriod() intIdlePeriod is the initial period that Annualized Historical Volatility won't yield any results.
func (*AnnualizedHistoricalVolatility[T]) String
func (a *AnnualizedHistoricalVolatility[T]) String() stringString function returns a string representation of the Annualized Historical Volatility.
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)
type Atr[T helper.Number] struct {
// Ma is the moving average for the ATR.
Ma trend.Ma[T]
}func NewAtr
func NewAtr[T helper.Number]() *Atr[T]NewAtr function initializes a new ATR instance with the default parameters.
func NewAtrWithMa
func NewAtrWithMa[T helper.Number](ma trend.Ma[T]) *Atr[T]NewAtrWithMa function initializes a new ATR instance with the given moving average instance.
func NewAtrWithPeriod
func NewAtrWithPeriod[T helper.Number](period int) *Atr[T]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 TCompute wraps ComputeWithContext for backwards compatibility.
Deprecated: Use ComputeWithContext instead.
func (*Atr[T]) ComputeWithContext
func (a *Atr[T]) ComputeWithContext(ctx context.Context, highs, lows, closings <-chan T) <-chan TComputeWithContext function takes a channel of numbers and computes the ATR over the specified period, supporting context cancellation.
func (*Atr[T]) IdlePeriod
func (a *Atr[T]) IdlePeriod() intIdlePeriod is the initial period that Acceleration Bands won't yield any results.
type BollingerBandWidth
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)
type BollingerBandWidth[T helper.Number] struct {
// Bollinger bands.
BollingerBands *BollingerBands[T]
}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 TCompute wraps ComputeWithContext for backwards compatibility.
Deprecated: Use ComputeWithContext instead.
func (*BollingerBandWidth[T]) ComputeWithContext
func (b *BollingerBandWidth[T]) ComputeWithContext(ctx context.Context, c <-chan T) <-chan TComputeWithContext function takes a channel of numbers and computes the Bollinger Band Width.
func (*BollingerBandWidth[T]) IdlePeriod
func (b *BollingerBandWidth[T]) IdlePeriod() intIdlePeriod 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)
type BollingerBands[T helper.Number] struct {
// Time period.
Period int
}func NewBollingerBands
func NewBollingerBands[T helper.Number]() *BollingerBands[T]NewBollingerBands function initializes a new Bollinger Bands instance with the default parameters.
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 wraps ComputeWithContext for backwards compatibility.
Deprecated: Use ComputeWithContext instead.
func (*BollingerBands[T]) ComputeWithContext
func (b *BollingerBands[T]) ComputeWithContext(ctx context.Context, c <-chan T) (<-chan T, <-chan T, <-chan T)ComputeWithContext function takes a channel of numbers and computes the Bollinger Bands over the specified period.
func (*BollingerBands[T]) IdlePeriod
func (b *BollingerBands[T]) IdlePeriod() intIdlePeriod is the initial period that Bollinger Bands won't yield any results.
type ChandelierExit
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)
type ChandelierExit[T helper.Number] struct {
// Period is time period.
Period int
// Multiplier is for sensitivity.
Multiplier T
}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 wraps ComputeWithContext for backwards compatibility.
Deprecated: Use ComputeWithContext instead.
func (*ChandelierExit[T]) ComputeWithContext
func (c *ChandelierExit[T]) ComputeWithContext(ctx context.Context, highs, lows, closings <-chan T) (<-chan T, <-chan T)ComputeWithContext function takes a channel of numbers and computes the Chandelier Exit over the specified period.
func (*ChandelierExit[T]) IdlePeriod
func (c *ChandelierExit[T]) IdlePeriod() intIdlePeriod is the initial period that Chandelier Exit won't yield any results.
type Chop
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)
type Chop[T helper.Number] struct {
// Period is the period for the CHOP.
Period int
}func NewChop
func NewChop[T helper.Number]() *Chop[T]NewChop function initializes a new CHOP instance with the default parameters.
func NewChopWithPeriod
func NewChopWithPeriod[T helper.Number](period int) *Chop[T]NewChopWithPeriod function initializes a new CHOP instance with the given period.
func (*Chop[T]) Compute
func (c *Chop[T]) Compute(highs, lows, closings <-chan T) <-chan TCompute wraps ComputeWithContext for backwards compatibility.
Deprecated: Use ComputeWithContext instead.
func (*Chop[T]) ComputeWithContext
func (c *Chop[T]) ComputeWithContext(ctx context.Context, highs, lows, closings <-chan T) <-chan TComputeWithContext function takes channels of highs, lows, and closings, and computes the CHOP over the specified period.
func (*Chop[T]) IdlePeriod
func (c *Chop[T]) IdlePeriod() intIdlePeriod is the initial period that CHOP won't yield any results.
func (*Chop[T]) String
func (c *Chop[T]) String() stringString function returns a string representation of the CHOP.
type DonchianChannel
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)
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]
}func NewDonchianChannel
func NewDonchianChannel[T helper.Number]() *DonchianChannel[T]NewDonchianChannel function initializes a new Donchian Channel instance with the default parameters.
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 wraps ComputeWithContext for backwards compatibility.
Deprecated: Use ComputeWithContext instead.
func (*DonchianChannel[T]) ComputeWithContext
func (d *DonchianChannel[T]) ComputeWithContext(ctx context.Context, c <-chan T) (<-chan T, <-chan T, <-chan T)ComputeWithContext function takes a channel of numbers and computes the Donchian Channel over the specified period.
func (*DonchianChannel[T]) IdlePeriod
func (d *DonchianChannel[T]) IdlePeriod() intIdlePeriod is the initial period that Donchian Channel won't yield any results.
type HistoricalVolatility
HistoricalVolatility represents the configuration parameters for calculating Historical Volatility (HV).
HV = StdDev(R_t, n)
where R_t = (P_t / P_(t-1)) - 1
Refactored to utilize composition of helper.ChangeRatio and MovingStd.
type HistoricalVolatility[T helper.Number] struct {
// Time period.
Period int
}func NewHistoricalVolatility[T helper.Number]() *HistoricalVolatility[T]NewHistoricalVolatility function initializes a new Historical Volatility instance with the default parameters.
func NewHistoricalVolatilityWithPeriod[T helper.Number](period int) *HistoricalVolatility[T]NewHistoricalVolatilityWithPeriod function initializes a new Historical Volatility instance with the given period.
func (*HistoricalVolatility[T]) Compute
func (h *HistoricalVolatility[T]) Compute(prices <-chan T) <-chan TCompute wraps ComputeWithContext for backwards compatibility.
Deprecated: Use ComputeWithContext instead.
func (*HistoricalVolatility[T]) ComputeWithContext
func (h *HistoricalVolatility[T]) ComputeWithContext(ctx context.Context, prices <-chan T) <-chan TComputeWithContext function takes a channel of prices and computes the Historical Volatility over the specified period.
func (*HistoricalVolatility[T]) IdlePeriod
func (h *HistoricalVolatility[T]) IdlePeriod() intIdlePeriod is the initial period that Historical Volatility won't yield any results.
func (*HistoricalVolatility[T]) String
func (h *HistoricalVolatility[T]) String() stringString function returns a string representation of the Historical Volatility.
type KeltnerChannel
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)
type KeltnerChannel[T helper.Number] struct {
// Atr is the ATR instance.
Atr *Atr[T]
// Ema is the EMA instance.
Ema *trend.Ema[T]
}func NewKeltnerChannel
func NewKeltnerChannel[T helper.Number]() *KeltnerChannel[T]NewKeltnerChannel function initializes a new Keltner Channel instance with the default parameters.
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 wraps ComputeWithContext for backwards compatibility.
Deprecated: Use ComputeWithContext instead.
func (*KeltnerChannel[T]) ComputeWithContext
func (k *KeltnerChannel[T]) ComputeWithContext(ctx context.Context, highs, lows, closings <-chan T) (<-chan T, <-chan T, <-chan T)ComputeWithContext function takes a channel of numbers and computes the Keltner Channel over the specified period.
func (*KeltnerChannel[T]) IdlePeriod
func (k *KeltnerChannel[T]) IdlePeriod() intIdlePeriod 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))
type MovingStd[T helper.Number] struct {
// Time period.
Period int
}func NewMovingStd
func NewMovingStd[T helper.Number]() *MovingStd[T]NewMovingStd function initializes a new Moving Standard Deviation instance with the default parameters.
func NewMovingStdWithPeriod[T helper.Number](period int) *MovingStd[T]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 TCompute wraps ComputeWithContext for backwards compatibility.
Deprecated: Use ComputeWithContext instead.
func (*MovingStd[T]) ComputeWithContext
func (m *MovingStd[T]) ComputeWithContext(ctx context.Context, c <-chan T) <-chan TComputeWithContext function takes a channel of numbers and computes the Moving Standard Deviation over the specified period, supporting context cancellation.
func (*MovingStd[T]) IdlePeriod
func (m *MovingStd[T]) IdlePeriod() intIdlePeriod is the initial period that Moving Standard Deviation won't yield any results.
type PercentB
PercentB represents the parameters for calculating the %B indicator.
%B = (Close - Lower Band) / (Upper Band - Lower Band)
type PercentB[T helper.Number] struct {
// BollingerBands is the underlying Bollinger Bands indicator used for calculations.
BollingerBands *BollingerBands[T]
}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))
}[0.3]
func NewPercentB
func NewPercentB[T helper.Number]() *PercentB[T]NewPercentB function initializes a new %B instance with the default parameters.
func NewPercentBWithPeriod[T helper.Number](period int) *PercentB[T]NewPercentBWithPeriod function initializes a new %B instance with the given period.
func (*PercentB[T]) Compute
func (p *PercentB[T]) Compute(closings <-chan T) <-chan TCompute wraps ComputeWithContext for backwards compatibility.
Deprecated: Use ComputeWithContext instead.
func (*PercentB[T]) ComputeWithContext
func (p *PercentB[T]) ComputeWithContext(ctx context.Context, closings <-chan T) <-chan TComputeWithContext function takes a channel of numbers and computes the %B over the specified period.
func (*PercentB[T]) IdlePeriod
func (p *PercentB[T]) IdlePeriod() intIdlePeriod is the initial period that %B yield any results.
func (*PercentB[T]) String
func (p *PercentB[T]) String() stringString is the string representation of the %B.
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)
type Po[T helper.Number] struct {
// contains filtered or unexported fields
}func NewPo
func NewPo[T helper.Number]() *Po[T]NewPo function initializes a new PO instance with the default parameters.
func NewPoWithPeriod
func NewPoWithPeriod[T helper.Number](period int) *Po[T]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 TCompute wraps ComputeWithContext for backwards compatibility.
Deprecated: Use ComputeWithContext instead.
func (*Po[T]) ComputeWithContext
func (p *Po[T]) ComputeWithContext(ctx context.Context, highs, lows, closings <-chan T) <-chan TComputeWithContext function takes a channel of numbers and computes the PO over the specified period.
func (*Po[T]) IdlePeriod
func (p *Po[T]) IdlePeriod() intIdlePeriod 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:
type SuperTrend[T helper.Number] struct {
Atr *Atr[T]
Multiplier T
}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[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 TCompute wraps ComputeWithContext for backwards compatibility.
Deprecated: Use ComputeWithContext instead.
func (*SuperTrend[T]) ComputeWithContext
func (s *SuperTrend[T]) ComputeWithContext(ctx context.Context, highs, lows, closings <-chan T) <-chan TComputeWithContext function calculates the Super Trend, using separate channels for highs, lows, and closings.
func (*SuperTrend[T]) IdlePeriod
func (s *SuperTrend[T]) IdlePeriod() intIdlePeriod is the initial period that Super Trend won't yield any results.
type TrueRange
TrueRange represents the parameters for calculating the True Range (TR). It evaluates the greatest distance covered by price in a single period, accounting for gaps.
TR = Max((High - Low), (High - Previous Closing), (Previous Closing - Low))
Example:
tr := volatility.NewTrueRange[float64]()
result := tr.Compute(highs, lows, closings)
type TrueRange[T helper.Number] struct{}func NewTrueRange
func NewTrueRange[T helper.Number]() *TrueRange[T]NewTrueRange function initializes a new TrueRange instance.
func (*TrueRange[T]) Compute
func (tr *TrueRange[T]) Compute(highs, lows, closings <-chan T) <-chan TCompute wraps ComputeWithContext for backwards compatibility.
Deprecated: Use ComputeWithContext instead.
func (*TrueRange[T]) ComputeWithContext
func (tr *TrueRange[T]) ComputeWithContext(ctx context.Context, highs, lows, closings <-chan T) <-chan TComputeWithContext function takes channels of highs, lows, and closings and computes the True Range.
func (*TrueRange[T]) IdlePeriod
func (tr *TrueRange[T]) IdlePeriod() intIdlePeriod is the initial period that TrueRange won't yield any results.
func (*TrueRange[T]) String
func (tr *TrueRange[T]) String() stringString is the string representation of the TrueRange.
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)
type UlcerIndex[T helper.Number] struct {
// Time period.
Period int
}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 TCompute wraps ComputeWithContext for backwards compatibility.
Deprecated: Use ComputeWithContext instead.
func (*UlcerIndex[T]) ComputeWithContext
func (u *UlcerIndex[T]) ComputeWithContext(ctx context.Context, closings <-chan T) <-chan TComputeWithContext function takes a channel of numbers and computes the Ulcer Index over the specified period.
func (*UlcerIndex[T]) IdlePeriod
func (u *UlcerIndex[T]) IdlePeriod() intIdlePeriod is the initial period that Ulcer Index won't yield any results.
type ZScore
ZScore represents the configuration parameters for Z-Score. It measures how many standard deviations price is away from its SMA.
Z-Score = (Price - SMA) / StdDev
Example:
z := NewZScore[float64]()
z.Compute(c)
type ZScore[T helper.Number] struct {
// Period is the time period.
Period int
}func NewZScore
func NewZScore[T helper.Number]() *ZScore[T]NewZScore function initializes a new Z-Score instance with default parameters.
func NewZScoreWithPeriod
func NewZScoreWithPeriod[T helper.Number](period int) *ZScore[T]NewZScoreWithPeriod function initializes a new Z-Score instance with the given period.
func (*ZScore[T]) Compute
func (z *ZScore[T]) Compute(c <-chan T) <-chan TCompute wraps ComputeWithContext for backwards compatibility.
Deprecated: Use ComputeWithContext instead.
func (*ZScore[T]) ComputeWithContext
func (z *ZScore[T]) ComputeWithContext(ctx context.Context, c <-chan T) <-chan TComputeWithContext function takes a channel of numbers and computes the Z-Score over the specified period.
func (*ZScore[T]) IdlePeriod
func (z *ZScore[T]) IdlePeriod() intIdlePeriod is the initial period that Z-Score won't yield any results.
func (*ZScore[T]) String
func (z *ZScore[T]) String() stringString is the string representation of Z-Score.
Generated by gomarkdoc