Skip to content

Latest commit

 

History

History
822 lines (597 loc) · 28 KB

README.md

File metadata and controls

822 lines (597 loc) · 28 KB

volatility

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.

License

Copyright (c) 2021-2024 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

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 (
    // DefaultDonchianChannelPeriod is the default period for the Donchian Channel.
    DefaultDonchianChannelPeriod = 20
)

const (
    // DefaultKeltnerChannelPeriod is the default period for the Keltner Channel.
    DefaultKeltnerChannelPeriod = 20
)

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
)

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[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)
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[T helper.Number](ma trend.Ma[T]) *Atr[T]

NewAtrWithMa function initializes a new ATR instance with the given moving average instance.

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 T

Compute function takes a channel of numbers and computes the ATR over the specified period.

func (*Atr[T]) IdlePeriod

func (a *Atr[T]) IdlePeriod() int

IdlePeriod is the initial period that Acceleration Bands won't yield any results.

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 band width widens, and during a period of low market volatity band width 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 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.

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[T helper.Number]() *BollingerBands[T]

NewBollingerBands function initializes a new Bollinger Bands instance with the default parameters.

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.

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[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.

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[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 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.

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[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 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.

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[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 T

Compute function takes a channel of numbers and computes the Moving Standard Deviation over the specified period.

func (*MovingStd[T]) IdlePeriod

func (m *MovingStd[T]) IdlePeriod() int

IdlePeriod is the initial period that Moving Standard Deviation won't 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)
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[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 T

Compute function takes a channel of numbers and computes the PO over the specified period.

func (*Po[T]) IdlePeriod

func (p *Po[T]) IdlePeriod() int

IdlePeriod is the initial period that PO won't yield any results.

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[T helper.Number]() *SuperTrend[T]

NewSuperTrend function initializes a new Super Trend instance with the default parameters.

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 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.

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[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.

Generated by gomarkdoc