trend

package
v2.1.12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 24, 2024 License: AGPL-3.0 Imports: 3 Imported by: 2

README

trend

import "github.com/cinar/indicator/v2/trend"

Package trend contains the trend 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 (
    // DefaultApoFastPeriod is the default APO fast period of 14.
    DefaultApoFastPeriod = 14

    // DefaultApoFastSmoothing is the default APO fast smoothing.
    DefaultApoFastSmoothing = DefaultEmaSmoothing

    // DefaultApoSlowPeriod is the default APO slow period of 30.
    DefaultApoSlowPeriod = 30

    // DefaultApoSlowSmoothing is the default APO slow smoothing.
    DefaultApoSlowSmoothing = DefaultEmaSmoothing
)

const (
    // DefaultEmaPeriod is the default EMA period of 20.
    DefaultEmaPeriod = 20

    // DefaultEmaSmoothing is the default EMA smooting of 2.
    DefaultEmaSmoothing = 2
)

const (
    // DefaultEnvelopePercentage is the default envelope percentage of 20%.
    DefaultEnvelopePercentage = 20

    // DefaultEnvelopePeriod is the default envelope period of 20.
    DefaultEnvelopePeriod = 20
)

const (
    // DefaultKamaErPeriod is the default Efficiency Ratio (ER) period of 10.
    DefaultKamaErPeriod = 10

    // DefaultKamaFastScPeriod is the default Fast Smoothing Constant (SC) period of 2.
    DefaultKamaFastScPeriod = 2

    // DefaultKamaSlowScPeriod is the default Slow Smoothing Constant (SC) period of 30.
    DefaultKamaSlowScPeriod = 30
)

const (
    // DefaultKdjMinMaxPeriod is the default period for moving min
    // of low, and moving max of high.
    DefaultKdjMinMaxPeriod = 9

    // DefaultKdjSma1Period is the default period for SMA of RSV.
    DefaultKdjSma1Period = 3

    // DefaultKdjSma2Period is the default period for SMA of K.
    DefaultKdjSma2Period = 3
)

const (
    // DefaultMacdPeriod1 is the period for the first EMA.
    DefaultMacdPeriod1 = 12

    // DefaultMacdPeriod2 is the period for the second EMA.
    DefaultMacdPeriod2 = 26

    // DefaultMacdPeriod3 is the period for the third EMA.
    DefaultMacdPeriod3 = 9
)

const (
    // DefaultMassIndexPeriod1 is the period for the first EMA.
    DefaultMassIndexPeriod1 = 9

    // DefaultMassIndexPeriod2 is the period for the second EMA.
    DefaultMassIndexPeriod2 = 9

    // DefaultMassIndexPeriod3 is the period for the third MovingSum.
    DefaultMassIndexPeriod3 = 25
)

const (
    // DefaultTsiFirstSmoothingPeriod is the default first smoothing period of 25.
    DefaultTsiFirstSmoothingPeriod = 25

    // DefaultTsiSecondSmoothingPeriod is the default second smoothing period of 13.
    DefaultTsiSecondSmoothingPeriod = 13
)

const (
    // DefaultAroonPeriod is the default Aroon period of 25.
    DefaultAroonPeriod = 25
)

const (
    // DefaultCciPeriod is the default time period for CCI.
    DefaultCciPeriod = 20
)

const (
    // DefaultRmaPeriod is the default RMA period.
    DefaultRmaPeriod = 20
)

const (
    // DefaultSmaPeriod is the default SMA period.
    DefaultSmaPeriod = 50
)

const (
    // DefaultSmmaPeriod is the default SMMA period of 7.
    DefaultSmmaPeriod = 7
)

const (
    // DefaultTrimaPeriod is the default period for TRIMA.
    DefaultTrimaPeriod = 15
)

const (
    // DefaultTrixPeriod is the default time period for TRIX.
    DefaultTrixPeriod = 15
)

const (
    // DefaultVwmaPeriod is the default period for the VWMA.
    DefaultVwmaPeriod = 20
)

type Apo

Apo represents the configuration parameters for calculating the Absolute Price Oscillator (APO). An APO value crossing above zero suggests a bullish trend, while crossing below zero indicates a bearish trend. Positive APO values signify an upward trend, while negative values signify a downward trend.

Fast = Ema(values, fastPeriod)
Slow = Ema(values, slowPeriod)
APO = Fast - Slow

Example:

apo := trend.NewApo[float64]()
apo.FastPeriod = 12
apo.SlowPeriod = 26

result := apo.Compute(c)
type Apo[T helper.Number] struct {
    // Fast period.
    FastPeriod int

    // Fast smoothing.
    FastSmoothing T

    // Slow period.
    SlowPeriod int

    // Slow smoothing.
    SlowSmoothing T
}

func NewApo
func NewApo[T helper.Number]() *Apo[T]

NewApo function initializes a new APO instance with the default parameters.

func (*Apo[T]) Compute
func (apo *Apo[T]) Compute(c <-chan T) <-chan T

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

type Aroon

Aroon represent the configuration for calculating the Aroon indicator. It is a technical analysis tool that gauges trend direction and strength in asset prices. It comprises two lines: Aroon Up and Aroon Down. Aroon Up measures uptrend strength, while Aroon Down measures downtrend strength. When Aroon Up exceeds Aroon Down, it suggests a bullish trend; when Aroon Down surpasses Aroon Up, it indicates a bearish trend.

Aroon Up = ((25 - Period Since Last 25 Period High) / 25) * 100
Aroon Down = ((25 - Period Since Last 25 Period Low) / 25) * 100

Example:

aroon := trend.NewAroon[float64]()
aroon.Period = 25

result := aroon.Compute(c)
type Aroon[T helper.Number] struct {
    // Period is the period to use.
    Period int
}

func NewAroon
func NewAroon[T helper.Number]() *Aroon[T]

NewAroon function initializes a new Aroon instance with the default parameters.

func (*Aroon[T]) Compute
func (a *Aroon[T]) Compute(high, low <-chan T) (<-chan T, <-chan T)

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

type Bop

Bop gauges the strength of buying and selling forces using the Balance of Power (BoP) indicator. A positive BoP value suggests an upward trend, while a negative value indicates a downward trend. A BoP value of zero implies equilibrium between the two forces.

Formula: BOP = (Closing - Opening) / (High - Low)
type Bop[T helper.Number] struct{}

func NewBop
func NewBop[T helper.Number]() *Bop[T]

NewBop function initializes a new BOP instance with the default parameters.

func (*Bop[T]) Compute
func (*Bop[T]) Compute(opening, high, low, closing <-chan T) <-chan T

Compute processes a channel of open, high, low, and close values, computing the BOP for each entry.

type Cci

Cci represents the configuration parameters for calculating the Community Channel Index (CCI). CCI is a momentum-based oscillator used to help determine when an investment vehicle is reaching a condition of being overbought or oversold.

Moving Average = Sma(Period, Typical Price)
Mean Deviation = Sma(Period, Abs(Typical Price - Moving Average))
CCI = (Typical Price - Moving Average) / (0.015 * Mean Deviation)

Example:

cmi := trend.NewCmi()
cmi.Period = 20
values = cmi.Compute(highs, lows, closings)
type Cci[T helper.Number] struct {
    // Time period.
    Period int
}

func NewCci
func NewCci[T helper.Number]() *Cci[T]

NewCci function initializes a new CCI instance with the default parameters.

func NewCciWithPeriod
func NewCciWithPeriod[T helper.Number](period int) *Cci[T]

NewCciWithPeriod function initializes a new CCI instance with the given period.

func (*Cci[T]) Compute
func (c *Cci[T]) Compute(highs, lows, closings <-chan T) <-chan T

Compute function takes a channel of numbers and computes the CCI and the signal line.

func (*Cci[T]) IdlePeriod
func (c *Cci[T]) IdlePeriod() int

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

type Dema

Dema represents the parameters for calculating the Double Exponential Moving Average (DEMA). A bullish cross occurs when DEMA with 5 days period moves above DEMA with 35 days period. A bearish cross occurs when DEMA with 35 days period moves above DEMA With 5 days period.

DEMA = (2 * EMA1(values)) - EMA2(EMA1(values))

Example:

dema := trend.NewDema[float64]()
dema.Ema1.Period = 10
dema.Ema2.Period = 16

result := dema.Compute(input)
type Dema[T helper.Number] struct {
    // Ema1 represents the configuration parameters for
    // calculating the first EMA.
    Ema1 *Ema[T]

    // Ema2 represents the configuration parameters for
    // calculating the second EMA.
    Ema2 *Ema[T]
}

func NewDema
func NewDema[T helper.Number]() *Dema[T]

NewDema function initializes a new DEMA instance with the default parameters.

func (*Dema[T]) Compute
func (d *Dema[T]) Compute(c <-chan T) <-chan T

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

func (*Dema[T]) IdlePeriod
func (d *Dema[T]) IdlePeriod() int

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

type Ema

Ema represents the parameters for calculating the Exponential Moving Average.

Example:

ema := trend.NewEma[float64]()
ema.Period = 10

result := ema.Compute(c)
type Ema[T helper.Number] struct {
    // Time period.
    Period int

    // Smoothing constant.
    Smoothing T
}

func NewEma
func NewEma[T helper.Number]() *Ema[T]

NewEma function initializes a new EMA instance with the default parameters.

func NewEmaWithPeriod
func NewEmaWithPeriod[T helper.Number](period int) *Ema[T]

NewEmaWithPeriod function initializes a new EMA instance with the given period.

func (*Ema[T]) Compute
func (e *Ema[T]) Compute(c <-chan T) <-chan T

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

func (*Ema[T]) IdlePeriod
func (e *Ema[T]) IdlePeriod() int

IdlePeriod is the initial period that EMA yield any results.

func (*Ema[T]) String
func (e *Ema[T]) String() string

String is the string representation of the EMA.

type Envelope

Envelope represents the parameters neededd to calcualte the Envelope.

type Envelope[T helper.Number] struct {
    // Ma is the moving average used.
    Ma  Ma[T]

    // Percentage is the envelope percentage.
    Percentage T
}

func NewEnvelope
func NewEnvelope[T helper.Number](ma Ma[T], percentage T) *Envelope[T]

NewEnvelope function initializes a new Envelope instance with the default parameters.

func NewEnvelopeWithEma
func NewEnvelopeWithEma[T helper.Number]() *Envelope[T]

NewEnvelopeWithEma function initializes a new Envelope instance using EMA.

func NewEnvelopeWithSma
func NewEnvelopeWithSma[T helper.Number]() *Envelope[T]

NewEnvelopeWithSma function initalizes a new Envelope instance using SMA.

func (*Envelope[T]) Compute
func (e *Envelope[T]) Compute(closings <-chan T) (<-chan T, <-chan T, <-chan T)

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

func (*Envelope[T]) IdlePeriod
func (e *Envelope[T]) IdlePeriod() int

IdlePeriod is the initial period that Envelope yield any results.

func (*Envelope[T]) String
func (e *Envelope[T]) String() string

String is the string representation of the Envelope.

type Hma

Hma represents the configuration parameters for calculating the Hull Moving Average (HMA). Developed by Alan Hull in 2005, HMA attempts to minimize the lag of a traditional moving average.

WMA1 = WMA(period/2 , values)
WMA2 = WMA(period, values)
WMA3 = WMA(sqrt(period), (2 * WMA1) - WMA2)
HMA = WMA3
type Hma[T helper.Number] struct {
    // contains filtered or unexported fields
}

func NewHmaWithPeriod
func NewHmaWithPeriod[T helper.Number](period int) *Hma[T]

NewHmaWithPeriod function initializes a new HMA instance with the given parameters.

func (*Hma[T]) Compute
func (h *Hma[T]) Compute(values <-chan T) <-chan T

Compute function takes a channel of numbers and computes the HMA and the signal line.

func (*Hma[T]) IdlePeriod
func (h *Hma[T]) IdlePeriod() int

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

func (*Hma[T]) String
func (h *Hma[T]) String() string

String is the string representation of the HMA.

type Kama

Kama represents the parameters for calculating the Kaufman's Adaptive Moving Average (KAMA). It is a type of moving average that adapts to market noise or volatility. It tracks prices closely during periods of small price swings and low noise.

Direction = Abs(Close - Previous Close Period Ago)
Volatility = MovingSum(Period, Abs(Close - Previous Close))
Efficiency Ratio (ER) = Direction / Volatility
Smoothing Constant (SC) = (ER * (2/(Fast + 1) - 2/(Slow + 1)) + (2/(Slow + 1)))^2
KAMA = Previous KAMA + SC * (Price - Previous KAMA)

Example:

kama := trend.NewKama[float64]()
result := kama.Compute(c)
type Kama[T helper.Number] struct {
    // ErPeriod is the Efficiency Ratio time period.
    ErPeriod int

    // FastScPeriod is the Fast Smoothing Constant time period.
    FastScPeriod int

    // SlowScPeriod is the Slow Smoothing Constant time period.
    SlowScPeriod int
}

func NewKama
func NewKama[T helper.Number]() *Kama[T]

NewKama function initializes a new KAMA instance with the default parameters.

func NewKamaWith
func NewKamaWith[T helper.Number](erPeriod, fastScPeriod, slowScPeriod int) *Kama[T]

NewKamaWith function initializes a new KAMA instance with the given parameters.

func (*Kama[T]) Compute
func (k *Kama[T]) Compute(closings <-chan T) <-chan T

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

func (*Kama[T]) IdlePeriod
func (k *Kama[T]) IdlePeriod() int

IdlePeriod is the initial period that KAMA yield any results.

func (*Kama[T]) String
func (k *Kama[T]) String() string

String is the string representation of the KAMA.

type Kdj

Kdj represents the configuration parameters for calculating the KDJ, also known as the Random Index. KDJ is calculated similar to the Stochastic Oscillator with the difference of having the J line. It is used to analyze the trend and entry points.

The K and D lines show if the asset is overbought when they crosses above 80%, and oversold when they crosses below 20%. The J line represents the divergence.

RSV = ((Closing - Min(Low, rPeriod))
/ (Max(High, rPeriod) - Min(Low, rPeriod))) * 100

K = Sma(RSV, kPeriod)
D = Sma(K, dPeriod)
J = (3 * K) - (2 * D)

Example:

kdj := NewKdj[float64]()
values := kdj.Compute(highs, lows, closings)
type Kdj[T helper.Number] struct {
    // MovingMax is the highest high.
    MovingMax *MovingMax[T]

    // MovingMin is the lowest low.
    MovingMin *MovingMin[T]

    // Sma1 is the SMA of RSV.
    Sma1 *Sma[T]

    // Sma2 is the SMA of K.
    Sma2 *Sma[T]
}

func NewKdj
func NewKdj[T helper.Number]() *Kdj[T]

NewKdj function initializes a new Kdj instance with the default parameters

func (*Kdj[T]) Compute
func (kdj *Kdj[T]) Compute(high, low, closing <-chan T) (<-chan T, <-chan T, <-chan T)

Compute function takes a channel of numbers and computes the KDJ over the specified period. Returns K, D, J.

func (*Kdj[T]) IdlePeriod
func (kdj *Kdj[T]) IdlePeriod() int

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

type Ma

Ma represents the interface for the Moving Average (MA) indicators.

type Ma[T helper.Number] interface {
    // Compute function takes a channel of numbers and computes the MA.
    Compute(<-chan T) <-chan T

    // IdlePeriod is the initial period that MA won't yield any results.
    IdlePeriod() int

    // String is the string representation of the MA instance.
    String() string
}

type Macd

Macd represents the configuration parameters for calculating the Moving Average Convergence Divergence (MACD).

MACD = 12-Period EMA - 26-Period EMA.
Signal = 9-Period EMA of MACD.

Example:

type Macd[T helper.Number] struct {
    Ema1 *Ema[T]
    Ema2 *Ema[T]
    Ema3 *Ema[T]
}

func NewMacd
func NewMacd[T helper.Number]() *Macd[T]

NewMacd function initializes a new MACD instance with the default parameters.

func NewMacdWithPeriod
func NewMacdWithPeriod[T helper.Number](period1, period2, period3 int) *Macd[T]

NewMacdWithPeriod function initializes a new MACD instance with the given parameters.

func (*Macd[T]) Compute
func (m *Macd[T]) Compute(c <-chan T) (<-chan T, <-chan T)

Compute function takes a channel of numbers and computes the MACD and the signal line.

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

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

type MassIndex

MassIndex represents the configuration parameters for calculating the Mass Index. It uses the high-low range to identify trend reversals based on range expansions.

Single EMA = EMA(9, Highs - Lows)
Double EMA = EMA(9, Single EMA)
Ratio = Single EMA / Double Ema1
Mass Index = SUM(Ratio, 25)

Example:

type MassIndex[T helper.Number] struct {
    Ema1      *Ema[T]
    Ema2      *Ema[T]
    MovingSum *MovingSum[T]
}

func NewMassIndex
func NewMassIndex[T helper.Number]() *MassIndex[T]

NewMassIndex function initializes a new APO instance with the default parameters.

func (*MassIndex[T]) Compute
func (m *MassIndex[T]) Compute(highs, lows <-chan T) <-chan T

Compute function takes a channel of numbers and computes the Mass Index.

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

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

type Mlr

Mlr represents the configuration parameters for calculating the Moving Linear Regression.

y = mx + b

Example:

mlr := trend.NewMlrWithPeriod[float64](14)
rs := mlr.Compute(x , y)
type Mlr[T helper.Number] struct {
    // Mls is the Moving Least Square instance.
    Mls *Mls[T]
}

func NewMlrWithPeriod
func NewMlrWithPeriod[T helper.Number](period int) *Mlr[T]

NewMlrWithPeriod function initializes a new MLR instance with the given period.

func (*Mlr[T]) Compute
func (m *Mlr[T]) Compute(x, y <-chan T) <-chan T

Compute function takes a channel of numbers and computes the MLR r.

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

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

type Mls

Mls represents the configuration parameters for calculating the Moving Least Square (MLS). It is a regression analysis to determine the line of best fit for the given set of data.

y = mx + b
b = y-intercept
y = slope

m = (period * sumXY - sumX * sumY) / (period * sumX2 - sumX * sumX)
b = (sumY - m * sumX) / period

Example:

mls := trend.NewMlsWithPeriod[float64](14)
ms, bs := mls.Compute(x , y)
type Mls[T helper.Number] struct {
    // Sum is the moving sum instance.
    Sum *MovingSum[T]
}

func NewMlsWithPeriod
func NewMlsWithPeriod[T helper.Number](period int) *Mls[T]

NewMlsWithPeriod function initializes a new MLS instance with the given period.

func (*Mls[T]) Compute
func (m *Mls[T]) Compute(x, y <-chan T) (<-chan T, <-chan T)

Compute function takes a channel of numbers and computes the MLS m and b.

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

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

type MovingMax

MovingMax represents the configuration parameters for calculating the Moving Max over the specified period.

Example:

type MovingMax[T helper.Number] struct {
    // Time period.
    Period int
}

func NewMovingMax
func NewMovingMax[T helper.Number]() *MovingMax[T]

NewMovingMax function initializes a new Moving Max instance with the default parameters.

func NewMovingMaxWithPeriod
func NewMovingMaxWithPeriod[T helper.Number](period int) *MovingMax[T]

NewMovingMaxWithPeriod function initializes a new Moving Max instance with the given period.

func (*MovingMax[T]) Compute
func (m *MovingMax[T]) Compute(c <-chan T) <-chan T

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

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

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

type MovingMin

MovingMin represents the configuration parameters for calculating the Moving Min over the specified period.

Example:

type MovingMin[T helper.Number] struct {
    // Time period.
    Period int
}

func NewMovingMin
func NewMovingMin[T helper.Number]() *MovingMin[T]

NewMovingMin function initializes a new Moving Min instance with the default parameters.

func NewMovingMinWithPeriod
func NewMovingMinWithPeriod[T helper.Number](period int) *MovingMin[T]

NewMovingMinWithPeriod function initializes a new Moving Min instance with the given period.

func (*MovingMin[T]) Compute
func (m *MovingMin[T]) Compute(c <-chan T) <-chan T

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

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

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

type MovingSum

MovingSum represents the configuration parameters for calculating the Moving Sum over the specified period.

Example:

sum := trend.NewMovingSum[float64]()
sum.Period = 20
type MovingSum[T helper.Number] struct {
    // Time period.
    Period int
}

func NewMovingSum
func NewMovingSum[T helper.Number]() *MovingSum[T]

NewMovingSum function initializes a new Moving Sum instance with the default parameters.

func NewMovingSumWithPeriod
func NewMovingSumWithPeriod[T helper.Number](period int) *MovingSum[T]

NewMovingSumWithPeriod function initializes a new Moving Sum instance with the given period.

func (*MovingSum[T]) Compute
func (m *MovingSum[T]) Compute(c <-chan T) <-chan T

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

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

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

type Rma

Rma represents the parameters for calculating Rolling Moving Average (RMA).

R[0] to R[p-1] is SMA(values)
R[p] and after is R[i] = ((R[i-1]*(p-1)) + v[i]) / p

Example:

rma := trend.NewRma[float64]()
rma.Period = 10

result := rma.Compute(c)
type Rma[T helper.Number] struct {
    // Time period.
    Period int
}

func NewRma
func NewRma[T helper.Number]() *Rma[T]

NewRma function initializes a new RMA instance with the default parameters.

func NewRmaWithPeriod
func NewRmaWithPeriod[T helper.Number](period int) *Rma[T]

NewRmaWithPeriod function initializes a new RMA instance with the given period.

func (*Rma[T]) Compute
func (r *Rma[T]) Compute(c <-chan T) <-chan T

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

func (*Rma[T]) IdlePeriod
func (r *Rma[T]) IdlePeriod() int

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

type Sma

Sma represents the parameters for calculating the Simple Moving Average.

Example:

sma := trend.NewSma[float64]()
sma.Period = 10

result := sma.Compute(c)
type Sma[T helper.Number] struct {
    // Period is the time period for the SMA.
    Period int
}

func NewSma
func NewSma[T helper.Number]() *Sma[T]

NewSma function initializes a new SMA instance with the default parameters.

func NewSmaWithPeriod
func NewSmaWithPeriod[T helper.Number](period int) *Sma[T]

NewSmaWithPeriod function initializes a new SMA instance with the default parameters.

func (*Sma[T]) Compute
func (s *Sma[T]) Compute(c <-chan T) <-chan T

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

func (*Sma[T]) IdlePeriod
func (s *Sma[T]) IdlePeriod() int

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

func (*Sma[T]) String
func (s *Sma[T]) String() string

String is the string representation of the SMA.

type Smma

Smma represents the parameters for calculating the Smoothed Moving Average (SMMA).

SMMA[0] = SMA(N)
SMMA[i] = ((SMMA[i-1] * (N - 1)) + Close[i]) / N

Example:

smma := trend.NewSmma[float64]()
smma.Period = 10

result := smma.Compute(c)
type Smma[T helper.Number] struct {
    // Time period.
    Period int
}

func NewSmma
func NewSmma[T helper.Number]() *Smma[T]

NewSmma function initializes a new SMMA instance with the default parameters.

func NewSmmaWithPeriod
func NewSmmaWithPeriod[T helper.Number](period int) *Smma[T]

NewSmmaWithPeriod function initializes a new SMMA instance with the given period.

func (*Smma[T]) Compute
func (s *Smma[T]) Compute(c <-chan T) <-chan T

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

func (*Smma[T]) IdlePeriod
func (s *Smma[T]) IdlePeriod() int

IdlePeriod is the initial period that SMMA yield any results.

func (*Smma[T]) String
func (s *Smma[T]) String() string

String is the string representation of the SMMA.

type Tema

Tema represents the configuration parameters for calculating the Triple Exponential Moving Average (TEMA).

TEMA = (3 * EMA1) - (3 * EMA2) + EMA3
EMA1 = EMA(values)
EMA2 = EMA(EMA1)
EMA3 = EMA(EMA2)
type Tema[T helper.Number] struct {
    Ema1 *Ema[T]
    Ema2 *Ema[T]
    Ema3 *Ema[T]
}

func NewTema
func NewTema[T helper.Number]() *Tema[T]

NewTema function initializes a new TEMA instance with the default parameters.

func (*Tema[T]) Compute
func (t *Tema[T]) Compute(c <-chan T) <-chan T

Compute function takes a channel of numbers and computes the TEMA and the signal line.

func (*Tema[T]) IdlePeriod
func (t *Tema[T]) IdlePeriod() int

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

type Trima

Trima represents the configuration parameters for calculating the Triangular Moving Average (TRIMA).

If period is even:

TRIMA = SMA(period / 2, SMA((period / 2) + 1, values))

If period is odd:

TRIMA = SMA((period + 1) / 2, SMA((period + 1) / 2, values))
type Trima[T helper.Number] struct {
    // Time period.
    Period int
}

func NewTrima
func NewTrima[T helper.Number]() *Trima[T]

NewTrima function initializes a new TRIMA instance with the default parameters.

func (*Trima[T]) Compute
func (t *Trima[T]) Compute(c <-chan T) <-chan T

Compute function takes a channel of numbers and computes the TRIMA and the signal line.

func (*Trima[T]) IdlePeriod
func (t *Trima[T]) IdlePeriod() int

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

type Trix

Trix represents the configuration parameters for calculating the Triple Exponential Average (TRIX). TRIX indicator is an oscillator used to identify oversold and overbought markets, and it can also be used as a momentum indicator. Like many oscillators, TRIX oscillates around a zero line.

EMA1 = EMA(period, values)
EMA2 = EMA(period, EMA1)
EMA3 = EMA(period, EMA2)
TRIX = (EMA3 - Previous EMA3) / Previous EMA3

Example:

trix := trend.NewTrix[float64]()
result := trix.Compute(values)
type Trix[T helper.Number] struct {
    // Time period.
    Period int
}

func NewTrix
func NewTrix[T helper.Number]() *Trix[T]

NewTrix function initializes a new TRIX instance with the default parameters.

func (*Trix[T]) Compute
func (t *Trix[T]) Compute(c <-chan T) <-chan T

Compute function takes a channel of numbers and computes the TRIX and the signal line.

func (*Trix[T]) IdlePeriod
func (t *Trix[T]) IdlePeriod() int

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

type Tsi

Tsi represents the parameters needed to calculate the True Strength Index (TSI). It is a technical momentum oscillator used in financial analysis. The TSI helps identify trends and potential trend reversals.

PCDS = Ema(13, Ema(25, (Current - Prior)))
APCDS = Ema(13, Ema(25, Abs(Current - Prior)))
TSI = (PCDS / APCDS) * 100

Example:

tsi := trend.NewTsi[float64]()
result := tsi.Compute(closings)
type Tsi[T helper.Number] struct {
    // FirstSmoothing is the first smoothing moving average.
    FirstSmoothing Ma[T]

    // SecondSmoothing is the second smoothing moving average.
    SecondSmoothing Ma[T]
}

func NewTsi
func NewTsi[T helper.Number]() *Tsi[T]

NewTsi function initializes a new TSI instance with the default parameters.

func NewTsiWith
func NewTsiWith[T helper.Number](firstSmoothingPeriod, secondSmoothingPeriod int) *Tsi[T]

NewTsiWith function initializes a new TSI instance with the given parameters.

func (*Tsi[T]) Compute
func (t *Tsi[T]) Compute(closings <-chan T) <-chan T

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

func (*Tsi[T]) IdlePeriod
func (t *Tsi[T]) IdlePeriod() int

IdlePeriod is the initial period that TSI yield any results.

func (*Tsi[T]) String
func (t *Tsi[T]) String() string

String is the string representation of the TSI.

type TypicalPrice

TypicalPrice represents the configuration parameters for calculating the Typical Price. It is another approximation of average price for each period and can be used as a filter for moving average systems.

Typical Price = (High + Low + Closing) / 3
type TypicalPrice[T helper.Number] struct{}

func NewTypicalPrice
func NewTypicalPrice[T helper.Number]() *TypicalPrice[T]

NewTypicalPrice function initializes a new Typical Price instance with the default parameters.

func (*TypicalPrice[T]) Compute
func (*TypicalPrice[T]) Compute(high, low, closing <-chan T) <-chan T

Compute function takes a channel of numbers and computes the Typical Price and the signal line.

type Vwma

Vwma represents the configuration parameters for calculating the Volume Weighted Moving Average (VWMA) It averages the price data with an emphasis on volume, meaning areas with higher volume will have a greater weight.

VWMA = Sum(Price * Volume) / Sum(Volume)
type Vwma[T helper.Number] struct {
    // Time period.
    Period int
}

func NewVwma
func NewVwma[T helper.Number]() *Vwma[T]

NewVwma function initializes a new VWMA instance with the default parameters.

func (*Vwma[T]) Compute
func (v *Vwma[T]) Compute(closing, volume <-chan T) <-chan T

Compute function takes a channel of numbers and computes the VWMA and the signal line.

func (*Vwma[T]) IdlePeriod
func (v *Vwma[T]) IdlePeriod() int

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

type WeightedClose

WeightedClose represents the parameters for calculating the Weighted Close indicator.

Weighted Close = (High + Low + (Close * 2)) / 4

Example:

weightedClose := trend.NewWeightedClose[float64]()
result := weightedClose.Compute(highs, lows, closes)
type WeightedClose[T helper.Number] struct {
}

func NewWeightedClose
func NewWeightedClose[T helper.Number]() *WeightedClose[T]

NewWeightedClose function initializes a new Weighted Close instance with the default parameters.

func (*WeightedClose[T]) Compute
func (w *WeightedClose[T]) Compute(highs, lows, closes <-chan T) <-chan T

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

func (*WeightedClose[T]) IdlePeriod
func (*WeightedClose[T]) IdlePeriod() int

IdlePeriod is the initial period that Weighted Close yield any results.

func (*WeightedClose[T]) String
func (*WeightedClose[T]) String() string

String is the string representation of the Weighted Close.

type Wma

Wma represents the configuration parameters for calculating the Weighted Moving Average (WMA). It calculates a moving average by putting more weight on recent data and less on past data.

WMA = ((Value1 * 1/N) + (Value2 * 2/N) + ...) / 2
type Wma[T helper.Number] struct {
    // Time period.
    Period int
}

func NewWmaWith
func NewWmaWith[T helper.Number](period int) *Wma[T]

NewWmaWith function initializes a new WMA instance with the given parameters.

func (*Wma[T]) Compute
func (w *Wma[T]) Compute(values <-chan T) <-chan T

Compute function takes a channel of numbers and computes the WMA and the signal line.

func (*Wma[T]) IdlePeriod
func (w *Wma[T]) IdlePeriod() int

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

func (*Wma[T]) String
func (w *Wma[T]) String() string

String is the string representation of the WMA.

Generated by gomarkdoc

Documentation

Overview

Package trend contains the trend 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

View Source
const (
	// DefaultApoFastPeriod is the default APO fast period of 14.
	DefaultApoFastPeriod = 14

	// DefaultApoFastSmoothing is the default APO fast smoothing.
	DefaultApoFastSmoothing = DefaultEmaSmoothing

	// DefaultApoSlowPeriod is the default APO slow period of 30.
	DefaultApoSlowPeriod = 30

	// DefaultApoSlowSmoothing is the default APO slow smoothing.
	DefaultApoSlowSmoothing = DefaultEmaSmoothing
)
View Source
const (
	// DefaultEmaPeriod is the default EMA period of 20.
	DefaultEmaPeriod = 20

	// DefaultEmaSmoothing is the default EMA smooting of 2.
	DefaultEmaSmoothing = 2
)
View Source
const (
	// DefaultEnvelopePercentage is the default envelope percentage of 20%.
	DefaultEnvelopePercentage = 20

	// DefaultEnvelopePeriod is the default envelope period of 20.
	DefaultEnvelopePeriod = 20
)
View Source
const (
	// DefaultKamaErPeriod is the default Efficiency Ratio (ER) period of 10.
	DefaultKamaErPeriod = 10

	// DefaultKamaFastScPeriod is the default Fast Smoothing Constant (SC) period of 2.
	DefaultKamaFastScPeriod = 2

	// DefaultKamaSlowScPeriod is the default Slow Smoothing Constant (SC) period of 30.
	DefaultKamaSlowScPeriod = 30
)
View Source
const (
	// DefaultKdjMinMaxPeriod is the default period for moving min
	// of low, and moving max of high.
	DefaultKdjMinMaxPeriod = 9

	// DefaultKdjSma1Period is the default period for SMA of RSV.
	DefaultKdjSma1Period = 3

	// DefaultKdjSma2Period is the default period for SMA of K.
	DefaultKdjSma2Period = 3
)
View Source
const (
	// DefaultMacdPeriod1 is the period for the first EMA.
	DefaultMacdPeriod1 = 12

	// DefaultMacdPeriod2 is the period for the second EMA.
	DefaultMacdPeriod2 = 26

	// DefaultMacdPeriod3 is the period for the third EMA.
	DefaultMacdPeriod3 = 9
)
View Source
const (
	// DefaultMassIndexPeriod1 is the period for the first EMA.
	DefaultMassIndexPeriod1 = 9

	// DefaultMassIndexPeriod2 is the period for the second EMA.
	DefaultMassIndexPeriod2 = 9

	// DefaultMassIndexPeriod3 is the period for the third MovingSum.
	DefaultMassIndexPeriod3 = 25
)
View Source
const (
	// DefaultTsiFirstSmoothingPeriod is the default first smoothing period of 25.
	DefaultTsiFirstSmoothingPeriod = 25

	// DefaultTsiSecondSmoothingPeriod is the default second smoothing period of 13.
	DefaultTsiSecondSmoothingPeriod = 13
)
View Source
const (
	// DefaultAroonPeriod is the default Aroon period of 25.
	DefaultAroonPeriod = 25
)
View Source
const (
	// DefaultCciPeriod is the default time period for CCI.
	DefaultCciPeriod = 20
)
View Source
const (
	// DefaultRmaPeriod is the default RMA period.
	DefaultRmaPeriod = 20
)
View Source
const (
	// DefaultSmaPeriod is the default SMA period.
	DefaultSmaPeriod = 50
)
View Source
const (
	// DefaultSmmaPeriod is the default SMMA period of 7.
	DefaultSmmaPeriod = 7
)
View Source
const (
	// DefaultTrimaPeriod is the default period for TRIMA.
	DefaultTrimaPeriod = 15
)
View Source
const (
	// DefaultTrixPeriod is the default time period for TRIX.
	DefaultTrixPeriod = 15
)
View Source
const (
	// DefaultVwmaPeriod is the default period for the VWMA.
	DefaultVwmaPeriod = 20
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Apo

type Apo[T helper.Number] struct {
	// Fast period.
	FastPeriod int

	// Fast smoothing.
	FastSmoothing T

	// Slow period.
	SlowPeriod int

	// Slow smoothing.
	SlowSmoothing T
}

Apo represents the configuration parameters for calculating the Absolute Price Oscillator (APO). An APO value crossing above zero suggests a bullish trend, while crossing below zero indicates a bearish trend. Positive APO values signify an upward trend, while negative values signify a downward trend.

Fast = Ema(values, fastPeriod)
Slow = Ema(values, slowPeriod)
APO = Fast - Slow

Example:

apo := trend.NewApo[float64]()
apo.FastPeriod = 12
apo.SlowPeriod = 26

result := apo.Compute(c)

func NewApo

func NewApo[T helper.Number]() *Apo[T]

NewApo function initializes a new APO instance with the default parameters.

func (*Apo[T]) Compute

func (apo *Apo[T]) Compute(c <-chan T) <-chan T

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

type Aroon

type Aroon[T helper.Number] struct {
	// Period is the period to use.
	Period int
}

Aroon represent the configuration for calculating the Aroon indicator. It is a technical analysis tool that gauges trend direction and strength in asset prices. It comprises two lines: Aroon Up and Aroon Down. Aroon Up measures uptrend strength, while Aroon Down measures downtrend strength. When Aroon Up exceeds Aroon Down, it suggests a bullish trend; when Aroon Down surpasses Aroon Up, it indicates a bearish trend.

Aroon Up = ((25 - Period Since Last 25 Period High) / 25) * 100
Aroon Down = ((25 - Period Since Last 25 Period Low) / 25) * 100

Example:

aroon := trend.NewAroon[float64]()
aroon.Period = 25

result := aroon.Compute(c)

func NewAroon

func NewAroon[T helper.Number]() *Aroon[T]

NewAroon function initializes a new Aroon instance with the default parameters.

func (*Aroon[T]) Compute

func (a *Aroon[T]) Compute(high, low <-chan T) (<-chan T, <-chan T)

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

type Bop

type Bop[T helper.Number] struct{}

Bop gauges the strength of buying and selling forces using the Balance of Power (BoP) indicator. A positive BoP value suggests an upward trend, while a negative value indicates a downward trend. A BoP value of zero implies equilibrium between the two forces.

Formula: BOP = (Closing - Opening) / (High - Low)

func NewBop

func NewBop[T helper.Number]() *Bop[T]

NewBop function initializes a new BOP instance with the default parameters.

func (*Bop[T]) Compute

func (*Bop[T]) Compute(opening, high, low, closing <-chan T) <-chan T

Compute processes a channel of open, high, low, and close values, computing the BOP for each entry.

type Cci

type Cci[T helper.Number] struct {
	// Time period.
	Period int
}

Cci represents the configuration parameters for calculating the Community Channel Index (CCI). CCI is a momentum-based oscillator used to help determine when an investment vehicle is reaching a condition of being overbought or oversold.

Moving Average = Sma(Period, Typical Price)
Mean Deviation = Sma(Period, Abs(Typical Price - Moving Average))
CCI = (Typical Price - Moving Average) / (0.015 * Mean Deviation)

Example:

cmi := trend.NewCmi()
cmi.Period = 20
values = cmi.Compute(highs, lows, closings)

func NewCci

func NewCci[T helper.Number]() *Cci[T]

NewCci function initializes a new CCI instance with the default parameters.

func NewCciWithPeriod

func NewCciWithPeriod[T helper.Number](period int) *Cci[T]

NewCciWithPeriod function initializes a new CCI instance with the given period.

func (*Cci[T]) Compute

func (c *Cci[T]) Compute(highs, lows, closings <-chan T) <-chan T

Compute function takes a channel of numbers and computes the CCI and the signal line.

func (*Cci[T]) IdlePeriod

func (c *Cci[T]) IdlePeriod() int

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

type Dema

type Dema[T helper.Number] struct {
	// Ema1 represents the configuration parameters for
	// calculating the first EMA.
	Ema1 *Ema[T]

	// Ema2 represents the configuration parameters for
	// calculating the second EMA.
	Ema2 *Ema[T]
}

Dema represents the parameters for calculating the Double Exponential Moving Average (DEMA). A bullish cross occurs when DEMA with 5 days period moves above DEMA with 35 days period. A bearish cross occurs when DEMA with 35 days period moves above DEMA With 5 days period.

DEMA = (2 * EMA1(values)) - EMA2(EMA1(values))

Example:

dema := trend.NewDema[float64]()
dema.Ema1.Period = 10
dema.Ema2.Period = 16

result := dema.Compute(input)

func NewDema

func NewDema[T helper.Number]() *Dema[T]

NewDema function initializes a new DEMA instance with the default parameters.

func (*Dema[T]) Compute

func (d *Dema[T]) Compute(c <-chan T) <-chan T

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

func (*Dema[T]) IdlePeriod

func (d *Dema[T]) IdlePeriod() int

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

type Ema

type Ema[T helper.Number] struct {
	// Time period.
	Period int

	// Smoothing constant.
	Smoothing T
}

Ema represents the parameters for calculating the Exponential Moving Average.

Example:

ema := trend.NewEma[float64]()
ema.Period = 10

result := ema.Compute(c)

func NewEma

func NewEma[T helper.Number]() *Ema[T]

NewEma function initializes a new EMA instance with the default parameters.

func NewEmaWithPeriod

func NewEmaWithPeriod[T helper.Number](period int) *Ema[T]

NewEmaWithPeriod function initializes a new EMA instance with the given period.

func (*Ema[T]) Compute

func (e *Ema[T]) Compute(c <-chan T) <-chan T

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

func (*Ema[T]) IdlePeriod

func (e *Ema[T]) IdlePeriod() int

IdlePeriod is the initial period that EMA yield any results.

func (*Ema[T]) String

func (e *Ema[T]) String() string

String is the string representation of the EMA.

type Envelope added in v2.1.7

type Envelope[T helper.Number] struct {
	// Ma is the moving average used.
	Ma Ma[T]

	// Percentage is the envelope percentage.
	Percentage T
}

Envelope represents the parameters neededd to calcualte the Envelope.

func NewEnvelope added in v2.1.7

func NewEnvelope[T helper.Number](ma Ma[T], percentage T) *Envelope[T]

NewEnvelope function initializes a new Envelope instance with the default parameters.

func NewEnvelopeWithEma added in v2.1.7

func NewEnvelopeWithEma[T helper.Number]() *Envelope[T]

NewEnvelopeWithEma function initializes a new Envelope instance using EMA.

func NewEnvelopeWithSma added in v2.1.7

func NewEnvelopeWithSma[T helper.Number]() *Envelope[T]

NewEnvelopeWithSma function initalizes a new Envelope instance using SMA.

func (*Envelope[T]) Compute added in v2.1.7

func (e *Envelope[T]) Compute(closings <-chan T) (<-chan T, <-chan T, <-chan T)

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

func (*Envelope[T]) IdlePeriod added in v2.1.7

func (e *Envelope[T]) IdlePeriod() int

IdlePeriod is the initial period that Envelope yield any results.

func (*Envelope[T]) String added in v2.1.7

func (e *Envelope[T]) String() string

String is the string representation of the Envelope.

type Hma

type Hma[T helper.Number] struct {
	// contains filtered or unexported fields
}

Hma represents the configuration parameters for calculating the Hull Moving Average (HMA). Developed by Alan Hull in 2005, HMA attempts to minimize the lag of a traditional moving average.

WMA1 = WMA(period/2 , values)
WMA2 = WMA(period, values)
WMA3 = WMA(sqrt(period), (2 * WMA1) - WMA2)
HMA = WMA3

func NewHmaWithPeriod

func NewHmaWithPeriod[T helper.Number](period int) *Hma[T]

NewHmaWithPeriod function initializes a new HMA instance with the given parameters.

func (*Hma[T]) Compute

func (h *Hma[T]) Compute(values <-chan T) <-chan T

Compute function takes a channel of numbers and computes the HMA and the signal line.

func (*Hma[T]) IdlePeriod

func (h *Hma[T]) IdlePeriod() int

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

func (*Hma[T]) String

func (h *Hma[T]) String() string

String is the string representation of the HMA.

type Kama added in v2.0.2

type Kama[T helper.Number] struct {
	// ErPeriod is the Efficiency Ratio time period.
	ErPeriod int

	// FastScPeriod is the Fast Smoothing Constant time period.
	FastScPeriod int

	// SlowScPeriod is the Slow Smoothing Constant time period.
	SlowScPeriod int
}

Kama represents the parameters for calculating the Kaufman's Adaptive Moving Average (KAMA). It is a type of moving average that adapts to market noise or volatility. It tracks prices closely during periods of small price swings and low noise.

Direction = Abs(Close - Previous Close Period Ago)
Volatility = MovingSum(Period, Abs(Close - Previous Close))
Efficiency Ratio (ER) = Direction / Volatility
Smoothing Constant (SC) = (ER * (2/(Fast + 1) - 2/(Slow + 1)) + (2/(Slow + 1)))^2
KAMA = Previous KAMA + SC * (Price - Previous KAMA)

Example:

kama := trend.NewKama[float64]()
result := kama.Compute(c)

func NewKama added in v2.0.2

func NewKama[T helper.Number]() *Kama[T]

NewKama function initializes a new KAMA instance with the default parameters.

func NewKamaWith added in v2.0.3

func NewKamaWith[T helper.Number](erPeriod, fastScPeriod, slowScPeriod int) *Kama[T]

NewKamaWith function initializes a new KAMA instance with the given parameters.

func (*Kama[T]) Compute added in v2.0.2

func (k *Kama[T]) Compute(closings <-chan T) <-chan T

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

func (*Kama[T]) IdlePeriod added in v2.0.2

func (k *Kama[T]) IdlePeriod() int

IdlePeriod is the initial period that KAMA yield any results.

func (*Kama[T]) String added in v2.0.2

func (k *Kama[T]) String() string

String is the string representation of the KAMA.

type Kdj

type Kdj[T helper.Number] struct {
	// MovingMax is the highest high.
	MovingMax *MovingMax[T]

	// MovingMin is the lowest low.
	MovingMin *MovingMin[T]

	// Sma1 is the SMA of RSV.
	Sma1 *Sma[T]

	// Sma2 is the SMA of K.
	Sma2 *Sma[T]
}

Kdj represents the configuration parameters for calculating the KDJ, also known as the Random Index. KDJ is calculated similar to the Stochastic Oscillator with the difference of having the J line. It is used to analyze the trend and entry points.

The K and D lines show if the asset is overbought when they crosses above 80%, and oversold when they crosses below 20%. The J line represents the divergence.

RSV = ((Closing - Min(Low, rPeriod))
/ (Max(High, rPeriod) - Min(Low, rPeriod))) * 100

K = Sma(RSV, kPeriod)
D = Sma(K, dPeriod)
J = (3 * K) - (2 * D)

Example:

kdj := NewKdj[float64]()
values := kdj.Compute(highs, lows, closings)

func NewKdj

func NewKdj[T helper.Number]() *Kdj[T]

NewKdj function initializes a new Kdj instance with the default parameters

func (*Kdj[T]) Compute

func (kdj *Kdj[T]) Compute(high, low, closing <-chan T) (<-chan T, <-chan T, <-chan T)

Compute function takes a channel of numbers and computes the KDJ over the specified period. Returns K, D, J.

func (*Kdj[T]) IdlePeriod

func (kdj *Kdj[T]) IdlePeriod() int

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

type Ma

type Ma[T helper.Number] interface {
	// Compute function takes a channel of numbers and computes the MA.
	Compute(<-chan T) <-chan T

	// IdlePeriod is the initial period that MA won't yield any results.
	IdlePeriod() int

	// String is the string representation of the MA instance.
	String() string
}

Ma represents the interface for the Moving Average (MA) indicators.

type Macd

type Macd[T helper.Number] struct {
	Ema1 *Ema[T]
	Ema2 *Ema[T]
	Ema3 *Ema[T]
}

Macd represents the configuration parameters for calculating the Moving Average Convergence Divergence (MACD).

MACD = 12-Period EMA - 26-Period EMA.
Signal = 9-Period EMA of MACD.

Example:

func NewMacd

func NewMacd[T helper.Number]() *Macd[T]

NewMacd function initializes a new MACD instance with the default parameters.

func NewMacdWithPeriod

func NewMacdWithPeriod[T helper.Number](period1, period2, period3 int) *Macd[T]

NewMacdWithPeriod function initializes a new MACD instance with the given parameters.

func (*Macd[T]) Compute

func (m *Macd[T]) Compute(c <-chan T) (<-chan T, <-chan T)

Compute function takes a channel of numbers and computes the MACD and the signal line.

func (*Macd[T]) IdlePeriod

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

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

type MassIndex

type MassIndex[T helper.Number] struct {
	Ema1      *Ema[T]
	Ema2      *Ema[T]
	MovingSum *MovingSum[T]
}

MassIndex represents the configuration parameters for calculating the Mass Index. It uses the high-low range to identify trend reversals based on range expansions.

Single EMA = EMA(9, Highs - Lows)
Double EMA = EMA(9, Single EMA)
Ratio = Single EMA / Double Ema1
Mass Index = SUM(Ratio, 25)

Example:

func NewMassIndex

func NewMassIndex[T helper.Number]() *MassIndex[T]

NewMassIndex function initializes a new APO instance with the default parameters.

func (*MassIndex[T]) Compute

func (m *MassIndex[T]) Compute(highs, lows <-chan T) <-chan T

Compute function takes a channel of numbers and computes the Mass Index.

func (*MassIndex[T]) IdlePeriod

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

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

type Mlr

type Mlr[T helper.Number] struct {
	// Mls is the Moving Least Square instance.
	Mls *Mls[T]
}

Mlr represents the configuration parameters for calculating the Moving Linear Regression.

y = mx + b

Example:

mlr := trend.NewMlrWithPeriod[float64](14)
rs := mlr.Compute(x , y)

func NewMlrWithPeriod

func NewMlrWithPeriod[T helper.Number](period int) *Mlr[T]

NewMlrWithPeriod function initializes a new MLR instance with the given period.

func (*Mlr[T]) Compute

func (m *Mlr[T]) Compute(x, y <-chan T) <-chan T

Compute function takes a channel of numbers and computes the MLR r.

func (*Mlr[T]) IdlePeriod

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

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

type Mls

type Mls[T helper.Number] struct {
	// Sum is the moving sum instance.
	Sum *MovingSum[T]
}

Mls represents the configuration parameters for calculating the Moving Least Square (MLS). It is a regression analysis to determine the line of best fit for the given set of data.

y = mx + b
b = y-intercept
y = slope

m = (period * sumXY - sumX * sumY) / (period * sumX2 - sumX * sumX)
b = (sumY - m * sumX) / period

Example:

mls := trend.NewMlsWithPeriod[float64](14)
ms, bs := mls.Compute(x , y)

func NewMlsWithPeriod

func NewMlsWithPeriod[T helper.Number](period int) *Mls[T]

NewMlsWithPeriod function initializes a new MLS instance with the given period.

func (*Mls[T]) Compute

func (m *Mls[T]) Compute(x, y <-chan T) (<-chan T, <-chan T)

Compute function takes a channel of numbers and computes the MLS m and b.

func (*Mls[T]) IdlePeriod

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

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

type MovingMax

type MovingMax[T helper.Number] struct {
	// Time period.
	Period int
}

MovingMax represents the configuration parameters for calculating the Moving Max over the specified period.

Example:

func NewMovingMax

func NewMovingMax[T helper.Number]() *MovingMax[T]

NewMovingMax function initializes a new Moving Max instance with the default parameters.

func NewMovingMaxWithPeriod

func NewMovingMaxWithPeriod[T helper.Number](period int) *MovingMax[T]

NewMovingMaxWithPeriod function initializes a new Moving Max instance with the given period.

func (*MovingMax[T]) Compute

func (m *MovingMax[T]) Compute(c <-chan T) <-chan T

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

func (*MovingMax[T]) IdlePeriod

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

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

type MovingMin

type MovingMin[T helper.Number] struct {
	// Time period.
	Period int
}

MovingMin represents the configuration parameters for calculating the Moving Min over the specified period.

Example:

func NewMovingMin

func NewMovingMin[T helper.Number]() *MovingMin[T]

NewMovingMin function initializes a new Moving Min instance with the default parameters.

func NewMovingMinWithPeriod

func NewMovingMinWithPeriod[T helper.Number](period int) *MovingMin[T]

NewMovingMinWithPeriod function initializes a new Moving Min instance with the given period.

func (*MovingMin[T]) Compute

func (m *MovingMin[T]) Compute(c <-chan T) <-chan T

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

func (*MovingMin[T]) IdlePeriod

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

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

type MovingSum

type MovingSum[T helper.Number] struct {
	// Time period.
	Period int
}

MovingSum represents the configuration parameters for calculating the Moving Sum over the specified period.

Example:

sum := trend.NewMovingSum[float64]()
sum.Period = 20

func NewMovingSum

func NewMovingSum[T helper.Number]() *MovingSum[T]

NewMovingSum function initializes a new Moving Sum instance with the default parameters.

func NewMovingSumWithPeriod

func NewMovingSumWithPeriod[T helper.Number](period int) *MovingSum[T]

NewMovingSumWithPeriod function initializes a new Moving Sum instance with the given period.

func (*MovingSum[T]) Compute

func (m *MovingSum[T]) Compute(c <-chan T) <-chan T

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

func (*MovingSum[T]) IdlePeriod

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

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

type Rma

type Rma[T helper.Number] struct {
	// Time period.
	Period int
}

Rma represents the parameters for calculating Rolling Moving Average (RMA).

R[0] to R[p-1] is SMA(values)
R[p] and after is R[i] = ((R[i-1]*(p-1)) + v[i]) / p

Example:

rma := trend.NewRma[float64]()
rma.Period = 10

result := rma.Compute(c)

func NewRma

func NewRma[T helper.Number]() *Rma[T]

NewRma function initializes a new RMA instance with the default parameters.

func NewRmaWithPeriod

func NewRmaWithPeriod[T helper.Number](period int) *Rma[T]

NewRmaWithPeriod function initializes a new RMA instance with the given period.

func (*Rma[T]) Compute

func (r *Rma[T]) Compute(c <-chan T) <-chan T

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

func (*Rma[T]) IdlePeriod

func (r *Rma[T]) IdlePeriod() int

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

type Sma

type Sma[T helper.Number] struct {
	// Period is the time period for the SMA.
	Period int
}

Sma represents the parameters for calculating the Simple Moving Average.

Example:

sma := trend.NewSma[float64]()
sma.Period = 10

result := sma.Compute(c)

func NewSma

func NewSma[T helper.Number]() *Sma[T]

NewSma function initializes a new SMA instance with the default parameters.

func NewSmaWithPeriod

func NewSmaWithPeriod[T helper.Number](period int) *Sma[T]

NewSmaWithPeriod function initializes a new SMA instance with the default parameters.

func (*Sma[T]) Compute

func (s *Sma[T]) Compute(c <-chan T) <-chan T

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

func (*Sma[T]) IdlePeriod

func (s *Sma[T]) IdlePeriod() int

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

func (*Sma[T]) String

func (s *Sma[T]) String() string

String is the string representation of the SMA.

type Smma added in v2.1.10

type Smma[T helper.Number] struct {
	// Time period.
	Period int
}

Smma represents the parameters for calculating the Smoothed Moving Average (SMMA).

SMMA[0] = SMA(N)
SMMA[i] = ((SMMA[i-1] * (N - 1)) + Close[i]) / N

Example:

smma := trend.NewSmma[float64]()
smma.Period = 10

result := smma.Compute(c)

func NewSmma added in v2.1.10

func NewSmma[T helper.Number]() *Smma[T]

NewSmma function initializes a new SMMA instance with the default parameters.

func NewSmmaWithPeriod added in v2.1.10

func NewSmmaWithPeriod[T helper.Number](period int) *Smma[T]

NewSmmaWithPeriod function initializes a new SMMA instance with the given period.

func (*Smma[T]) Compute added in v2.1.10

func (s *Smma[T]) Compute(c <-chan T) <-chan T

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

func (*Smma[T]) IdlePeriod added in v2.1.10

func (s *Smma[T]) IdlePeriod() int

IdlePeriod is the initial period that SMMA yield any results.

func (*Smma[T]) String added in v2.1.10

func (s *Smma[T]) String() string

String is the string representation of the SMMA.

type Tema

type Tema[T helper.Number] struct {
	Ema1 *Ema[T]
	Ema2 *Ema[T]
	Ema3 *Ema[T]
}

Tema represents the configuration parameters for calculating the Triple Exponential Moving Average (TEMA).

TEMA = (3 * EMA1) - (3 * EMA2) + EMA3
EMA1 = EMA(values)
EMA2 = EMA(EMA1)
EMA3 = EMA(EMA2)

func NewTema

func NewTema[T helper.Number]() *Tema[T]

NewTema function initializes a new TEMA instance with the default parameters.

func (*Tema[T]) Compute

func (t *Tema[T]) Compute(c <-chan T) <-chan T

Compute function takes a channel of numbers and computes the TEMA and the signal line.

func (*Tema[T]) IdlePeriod

func (t *Tema[T]) IdlePeriod() int

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

type Trima

type Trima[T helper.Number] struct {
	// Time period.
	Period int
}

Trima represents the configuration parameters for calculating the Triangular Moving Average (TRIMA).

If period is even:

TRIMA = SMA(period / 2, SMA((period / 2) + 1, values))

If period is odd:

TRIMA = SMA((period + 1) / 2, SMA((period + 1) / 2, values))

func NewTrima

func NewTrima[T helper.Number]() *Trima[T]

NewTrima function initializes a new TRIMA instance with the default parameters.

func (*Trima[T]) Compute

func (t *Trima[T]) Compute(c <-chan T) <-chan T

Compute function takes a channel of numbers and computes the TRIMA and the signal line.

func (*Trima[T]) IdlePeriod

func (t *Trima[T]) IdlePeriod() int

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

type Trix

type Trix[T helper.Number] struct {
	// Time period.
	Period int
}

Trix represents the configuration parameters for calculating the Triple Exponential Average (TRIX). TRIX indicator is an oscillator used to identify oversold and overbought markets, and it can also be used as a momentum indicator. Like many oscillators, TRIX oscillates around a zero line.

EMA1 = EMA(period, values)
EMA2 = EMA(period, EMA1)
EMA3 = EMA(period, EMA2)
TRIX = (EMA3 - Previous EMA3) / Previous EMA3

Example:

trix := trend.NewTrix[float64]()
result := trix.Compute(values)

func NewTrix

func NewTrix[T helper.Number]() *Trix[T]

NewTrix function initializes a new TRIX instance with the default parameters.

func (*Trix[T]) Compute

func (t *Trix[T]) Compute(c <-chan T) <-chan T

Compute function takes a channel of numbers and computes the TRIX and the signal line.

func (*Trix[T]) IdlePeriod

func (t *Trix[T]) IdlePeriod() int

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

type Tsi added in v2.0.3

type Tsi[T helper.Number] struct {
	// FirstSmoothing is the first smoothing moving average.
	FirstSmoothing Ma[T]

	// SecondSmoothing is the second smoothing moving average.
	SecondSmoothing Ma[T]
}

Tsi represents the parameters needed to calculate the True Strength Index (TSI). It is a technical momentum oscillator used in financial analysis. The TSI helps identify trends and potential trend reversals.

PCDS = Ema(13, Ema(25, (Current - Prior)))
APCDS = Ema(13, Ema(25, Abs(Current - Prior)))
TSI = (PCDS / APCDS) * 100

Example:

tsi := trend.NewTsi[float64]()
result := tsi.Compute(closings)

func NewTsi added in v2.0.3

func NewTsi[T helper.Number]() *Tsi[T]

NewTsi function initializes a new TSI instance with the default parameters.

func NewTsiWith added in v2.0.3

func NewTsiWith[T helper.Number](firstSmoothingPeriod, secondSmoothingPeriod int) *Tsi[T]

NewTsiWith function initializes a new TSI instance with the given parameters.

func (*Tsi[T]) Compute added in v2.0.3

func (t *Tsi[T]) Compute(closings <-chan T) <-chan T

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

func (*Tsi[T]) IdlePeriod added in v2.0.3

func (t *Tsi[T]) IdlePeriod() int

IdlePeriod is the initial period that TSI yield any results.

func (*Tsi[T]) String added in v2.0.3

func (t *Tsi[T]) String() string

String is the string representation of the TSI.

type TypicalPrice

type TypicalPrice[T helper.Number] struct{}

TypicalPrice represents the configuration parameters for calculating the Typical Price. It is another approximation of average price for each period and can be used as a filter for moving average systems.

Typical Price = (High + Low + Closing) / 3

func NewTypicalPrice

func NewTypicalPrice[T helper.Number]() *TypicalPrice[T]

NewTypicalPrice function initializes a new Typical Price instance with the default parameters.

func (*TypicalPrice[T]) Compute

func (*TypicalPrice[T]) Compute(high, low, closing <-chan T) <-chan T

Compute function takes a channel of numbers and computes the Typical Price and the signal line.

type Vwma

type Vwma[T helper.Number] struct {
	// Time period.
	Period int
}

Vwma represents the configuration parameters for calculating the Volume Weighted Moving Average (VWMA) It averages the price data with an emphasis on volume, meaning areas with higher volume will have a greater weight.

VWMA = Sum(Price * Volume) / Sum(Volume)

func NewVwma

func NewVwma[T helper.Number]() *Vwma[T]

NewVwma function initializes a new VWMA instance with the default parameters.

func (*Vwma[T]) Compute

func (v *Vwma[T]) Compute(closing, volume <-chan T) <-chan T

Compute function takes a channel of numbers and computes the VWMA and the signal line.

func (*Vwma[T]) IdlePeriod

func (v *Vwma[T]) IdlePeriod() int

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

type WeightedClose added in v2.1.12

type WeightedClose[T helper.Number] struct {
}

WeightedClose represents the parameters for calculating the Weighted Close indicator.

Weighted Close = (High + Low + (Close * 2)) / 4

Example:

weightedClose := trend.NewWeightedClose[float64]()
result := weightedClose.Compute(highs, lows, closes)

func NewWeightedClose added in v2.1.12

func NewWeightedClose[T helper.Number]() *WeightedClose[T]

NewWeightedClose function initializes a new Weighted Close instance with the default parameters.

func (*WeightedClose[T]) Compute added in v2.1.12

func (*WeightedClose[T]) Compute(highs, lows, closes <-chan T) <-chan T

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

func (*WeightedClose[T]) IdlePeriod added in v2.1.12

func (*WeightedClose[T]) IdlePeriod() int

IdlePeriod is the initial period that Weighted Close yield any results.

func (*WeightedClose[T]) String added in v2.1.12

func (*WeightedClose[T]) String() string

String is the string representation of the Weighted Close.

type Wma

type Wma[T helper.Number] struct {
	// Time period.
	Period int
}

Wma represents the configuration parameters for calculating the Weighted Moving Average (WMA). It calculates a moving average by putting more weight on recent data and less on past data.

WMA = ((Value1 * 1/N) + (Value2 * 2/N) + ...) / 2

func NewWmaWith

func NewWmaWith[T helper.Number](period int) *Wma[T]

NewWmaWith function initializes a new WMA instance with the given parameters.

func (*Wma[T]) Compute

func (w *Wma[T]) Compute(values <-chan T) <-chan T

Compute function takes a channel of numbers and computes the WMA and the signal line.

func (*Wma[T]) IdlePeriod

func (w *Wma[T]) IdlePeriod() int

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

func (*Wma[T]) String

func (w *Wma[T]) String() string

String is the string representation of the WMA.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL