ta

package
v0.0.26 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2022 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package ta offers technical analysis functions for price series data.

Index

Constants

View Source
const (
	// DefaultALMAOffset is the default offset for the ALMA indicator.
	DefaultALMAOffset = 0.85

	// DefaultALMASigma is the default sigma for the ALMA indicator.
	DefaultALMASigma = 6
)
View Source
const DefaultValueAreaPercentage = 0.68

DefaultValueAreaPercentage is the percentage of the total volume used to calculate the value area.

Variables

This section is empty.

Functions

func Close

func Close(price market.Kline) float64

Close returns the close price.

func CrossDown

func CrossDown(series []float64, x float64) bool

CrossDown returns true if the latest series values cross below the given value. The series must be in chronological order, with the earliest value at index 0. The series must have at least 2 values

func CrossUp

func CrossUp(series []float64, x float64) bool

CrossUp returns true if the latest series values cross above the given value. The series must be in chronological order, with the earliest value at index 0. The series must have at least 2 values

func HL2

func HL2(price market.Kline) float64

HL2 returns the average of the high and low prices.

func HLC3

func HLC3(price market.Kline) float64

HLC3 returns the average of the high, low and close prices.

func Lookback

func Lookback[T any](series []T, n int) T

Lookback returns a value from series at n index ago. Series must be in chronological order, with the earliest value at slice index 0. n = 0 returns the latest value. n = 1 returns the value before the latest etc.

func Median

func Median(v []float64) float64

Median returns the median of the given values.

func OHLC4

func OHLC4(price market.Kline) float64

OHLC4 returns the average of the open, high, low and close prices.

func Peak

func Peak(series []float64, delta float64) bool

Peak returns true if the latest values in the series have formed a peak. Series must be in chronological order, with the earliest value at slice index 0. Series must have at least 3 values. Arg delta is the threshold change in the series values required to detect a peak.

func Slope

func Slope(t1, t2 float64) int

Slope indicates the direction between two points. t1 is the first point, t2 is the second point. Returns 1 for up, -1 for down, 0 for flat.

func Valley

func Valley(series []float64, delta float64) bool

Valley returns true if the latest values in the series have formed a valley. Series must be in chronological order, with the earliest value at slice index 0. Series must have at least 3 values. Arg delta is the threshold change in the series values required to detect a valley.

func Window

func Window[T any](series []T, n int) []T

Window returns a copied slice of series starting at n index ago. Semantics of n argument are the same as Lookback function.

func WindowAppend

func WindowAppend[T any](series []T, n int, v T) []T

WindowAppend appends a value to the end of the series and slices it to the window starting at n index ago. Semantics of n argument are the same as Window and Lookback functions.

Types

type ALMA

type ALMA struct {
	Length int
	Offset float64
	Sigma  float64
	// contains filtered or unexported fields
}

ALMA is a modern low lag moving average. Ported from https://www.tradingview.com/pine-script-reference/#fun_alma

func NewALMA

func NewALMA(length int) *ALMA

NewALMA creates a new ALMA indicator with default parameters.

func NewALMAWithSigma

func NewALMAWithSigma(length int, offset, sigma float64) *ALMA

NewALMAWithSigma creates a new ALMA indicator with the given offset and sigma.

func (*ALMA) History

func (ind *ALMA) History() []float64

History returns the historical values of the indicator.

func (*ALMA) Update

func (ind *ALMA) Update(v ...float64) error

Update updates the indicator with the next value(s).

func (*ALMA) Valid

func (ind *ALMA) Valid() bool

Valid returns true if the indicator is valid. An indicator is invalid if it hasn't received enough values yet.

func (*ALMA) Value

func (ind *ALMA) Value() float64

Value returns the current value of the indicator.

type Indicator

type Indicator[T any] interface {

	// Update the indicator with new inputs (typically a price series).
	Update(v ...T) error

	// Value returns the latest value of the indicator.
	Value() float64

	// History returns all the historical indicator values (including the latest value).
	History() []float64

	// Valid returns true if the indicator is valid.
	Valid() bool
}

Indicator is the interface for all technical analysis functions.

type MMI

type MMI struct {
	// Length is the number of values to use for the calculation.
	Length int

	// Smoother is the indicator used to smooth the MMI.
	Smoother Indicator[float64]
	// contains filtered or unexported fields
}

MMI (Market Meaness Index) is a statistical measure between 0 - 100 that indicates if the series exhibits serial correlation (trendiness). Reference: https://financial-hacker.com/the-market-meanness-index/.

func NewMMI

func NewMMI(length int) *MMI

NewMMI returns a new MMI indicator with a default ALMA smoother. The smoothing length is the same as the given MMI length.

func NewMMIWithSmoother

func NewMMIWithSmoother(length int, smoother Indicator[float64]) *MMI

NewMMIWithSmoother returns a new MMI indicator with the given smoother.

func (*MMI) History

func (ind *MMI) History() []float64

History returns the historical data of the indicator.

func (*MMI) Update

func (ind *MMI) Update(v ...float64) error

Update updates the indicator with the next value(s).

func (*MMI) Valid

func (ind *MMI) Valid() bool

Valid returns true if the indicator has enough data to be calculated.

func (*MMI) Value

func (ind *MMI) Value() float64

Value returns the current value of the indicator.

type MockIndicator

type MockIndicator struct {
	mock.Mock
}

MockIndicator is a mock implementation of the Indicator interface.

func (*MockIndicator) History

func (ind *MockIndicator) History() []float64

History returns the history of the indicator.

func (*MockIndicator) Update

func (ind *MockIndicator) Update(v ...float64) error

Update updates the indicator with the next value(s).

func (*MockIndicator) Valid

func (ind *MockIndicator) Valid() bool

Valid returns true if the indicator has enough data to be calculated.

func (*MockIndicator) Value

func (ind *MockIndicator) Value() float64

Value returns the current value of the indicator.

type Osc

type Osc struct {
	// Fast is the fast moving average indicator.
	Fast Indicator[float64]

	// Slow is the slow moving average indicator.
	Slow Indicator[float64]
	// contains filtered or unexported fields
}

Osc is a composite of a fast and slow moving average indicator. Osc value = fast value minus slow value. Osc is not normalized and has an unbounded range.

func NewOsc

func NewOsc(fast, slow Indicator[float64]) *Osc

NewOsc returns a new oscillator with the given fast and slow moving averages.

func (*Osc) History

func (ind *Osc) History() []float64

History returns the history of the indicator.

func (*Osc) Update

func (ind *Osc) Update(v ...float64) error

Update updates the indicator with the next value(s).

func (*Osc) Valid

func (ind *Osc) Valid() bool

Valid returns true if the indicator has enough data to be calculated.

func (*Osc) Value

func (ind *Osc) Value() float64

Value returns the current value of the indicator.

type PriceSelector

type PriceSelector func(price market.Kline) float64

PriceSelector is a selector that returns a price value for the given kline.

type SD

type SD struct {
	// Length is the number of values to use in the calculation.
	Length int

	// Factor is the factor to multiply the standard deviation by.
	Factor float64
	// contains filtered or unexported fields
}

SD is a sample standard deviation indicator.

func NewSD

func NewSD(length int) *SD

NewSD returns a new SD indicator with default factor of 1.

func NewSDWithFactor

func NewSDWithFactor(length int, factor float64) *SD

NewSDWithFactor returns a new SD indicator with the given factor.

func (*SD) History

func (ind *SD) History() []float64

History returns the history of the indicator.

func (*SD) Update

func (ind *SD) Update(v ...float64) error

Update updates the indicator with the next value(s).

func (*SD) Valid

func (ind *SD) Valid() bool

Valid returns true if the indicator has enough data to be calculated.

func (*SD) Value

func (ind *SD) Value() float64

Value returns the current value of the indicator.

type StubIndicator

type StubIndicator struct {
	// Values is the history of the indicator.
	Values []float64

	// IsValid is the validity of the indicator.
	IsValid bool
}

StubIndicator is a test double for an indicator.

func (*StubIndicator) History

func (ind *StubIndicator) History() []float64

History returns the history of the indicator.

func (*StubIndicator) Update

func (ind *StubIndicator) Update(v ...float64) error

Update is not implemented.

func (*StubIndicator) Valid

func (ind *StubIndicator) Valid() bool

Valid returns IsValid.

func (*StubIndicator) Value

func (ind *StubIndicator) Value() float64

Value returns the latest value in Values

type VWAP

type VWAP struct {
	// contains filtered or unexported fields
}

VWAP is a volume weighted average price.

func NewVWAP

func NewVWAP() *VWAP

NewVWAP creates a new VWAP indicator with default parameters.

func (*VWAP) History

func (ind *VWAP) History() []float64

History returns the historical values of the indicator.

func (*VWAP) Update

func (ind *VWAP) Update(prices ...market.Kline) error

Update updates the indicator with the next value(s).

func (*VWAP) Valid

func (ind *VWAP) Valid() bool

Valid returns true if the indicator is valid. An indicator is invalid if it hasn't received enough values yet.

func (*VWAP) Value

func (ind *VWAP) Value() float64

Value returns the current value of the indicator.

type VolumeLevel

type VolumeLevel struct {

	// Price is the market price, typically the high/low average of the kline.
	Price float64

	// Volume is the total buy and sell volume at the price.
	Volume float64
}

VolumeLevel is a price and volume pair used to build a volume profile.

type VolumeProfile

type VolumeProfile struct {

	// Bins is the histogram bins.
	Bins []float64

	// Hist is the histogram values.
	Hist []float64

	// POC is the point of control.
	POC float64

	// VAH is the value area high.
	VAH float64

	// VAL is the value area low.
	VAL float64

	// High is the highest price in the profile.
	High float64

	// Low is the lowest price in the profile.
	Low float64
}

VolumeProfile is a histogram of market price and volume. Intent is to show the price points with most volume during a period. The profile gives key features such as:

Point of control (POC)

Value area high (VAH)

Value area low (VAL)

Session High

Session Low

func NewVolumeProfile

func NewVolumeProfile(nBins int, levels []VolumeLevel) *VolumeProfile

NewVolumeProfile creates a new profile for the price and volume series given by levels. nBins is the number of bins to use for the profile histogram.

func NewVolumeProfileFixedBinWidth added in v0.0.20

func NewVolumeProfileFixedBinWidth(binWidth float64, levels []VolumeLevel) *VolumeProfile

NewVolumeProfileFixedBinWidth creates a new volume profile with a variable number of bins dictated by binWidth. Returns nil if binWidth is not positive.

Jump to

Keyboard shortcuts

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