tango

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2024 License: MIT Imports: 3 Imported by: 0

README

Tango - (Trading Analysis & Go)

Go Reference Build Status Go Report Card

Features

  • Simple API
  • Built-in parameters validation
  • Includes thorough documentation
  • A wide variety of Oscillators and Overlays.

Installation

go get github.com/jellydator/tango

Usage

All of the tools must be created using New* function. It performs parameters validation and returns an object that is capable of working with data slices.

The main calculations are done using Calc method. The return types varies based on the tool.

A simple use case could look like this:

func main() {
  sma, err := tango.NewSMA(3)
  if err != nil {
    // handle the error.
  }

  dataPoints := []decimal.Decimal{
    decimal.NewFromInt(2),
    decimal.NewFromInt(3),
    decimal.NewFromInt(4),
  }

  // the value is 3
  value, err := sma.Calc(dataPoints)
  if err != nil {
    // handle the error.
  }
}

For the calculation to be successful, the Calc method should receive only the information that it requires. In some scenarios, it might not be known how many data points is needed, for this, a Count method may be used.

func CalculateMA(ma tango.MA, values []decimal.Decimal) (decimal.Decimal, error) {
  requiredValues := ma.Count()

  if len(values) < requiredValues {
    return decimal.Zero, errors.New("invalid count of values")
  }

  return ma.Calc(values[:requiredValues])
}

Oscillators

Overlays

Documentation

Overview

Package tango provides types and functions to calculate values of various market indicators.

Package tango provides types and functions to calculate values of various market indicators.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidIndicator is returned when indicator is invalid.
	ErrInvalidIndicator = errors.New("invalid indicator")

	// ErrInvalidLength is returned when incorrect length is provided.
	ErrInvalidLength = errors.New("invalid length")

	// ErrInvalidDataSize is returned when incorrect data size is provided.
	ErrInvalidDataSize = errors.New("invalid data size")

	// ErrInvalidTrend is returned when trend doesn't match any of the
	// available trends.
	ErrInvalidTrend = errors.New("invalid trend")

	// ErrInvalidBand is returned when band doesn't match any of the
	// available bands.
	ErrInvalidBand = errors.New("invalid band")

	// ErrInvalidMA is returned when ma doesn't match any of the
	// availabble ma types.
	ErrInvalidMA = errors.New("invalid moving average")
)

Functions

func Average

func Average(dd []decimal.Decimal) decimal.Decimal

Average is a helper function that calculates average decimal number of given slice.

func MeanDeviation

func MeanDeviation(dd []decimal.Decimal) decimal.Decimal

MeanDeviation calculates mean deviation of given slice.

func SquareRoot

func SquareRoot(d decimal.Decimal) decimal.Decimal

SquareRoot is a helper function that calculated the square root of decimal number.

func StandardDeviation

func StandardDeviation(dd []decimal.Decimal) decimal.Decimal

StandardDeviation calculates standard deviation of given slice.

Types

type Aroon

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

Aroon holds all the necessary information needed to calculate Aroon. The zero value is not usable.

func NewAroon

func NewAroon(length int) (Aroon, error)

NewAroon validates provided configuration options and creates new Aroon indicator instance.

func (Aroon) Calc

func (aroon Aroon) Calc(dd []decimal.Decimal) (
	uptrend decimal.Decimal,
	downtrend decimal.Decimal,
	err error,
)

Calc calculates both Aroon trends from the provided data points slice. Calculation is based on formula provided by investopedia. https://www.investopedia.com/terms/a/aroon.asp. All credits are due to Tushar Chande who developed Aroon indicator.

func (Aroon) CalcTrend

func (aroon Aroon) CalcTrend(dd []decimal.Decimal, trend Trend) (decimal.Decimal, error)

CalcTrend calculates specified Aroon trend from the provided data points slice. Calculation is based on formula provided by investopedia. https://www.investopedia.com/terms/a/aroon.asp. All credits are due to Tushar Chande who developed Aroon indicator.

func (Aroon) Count

func (aroon Aroon) Count() int

Count determines the total amount of data points needed for Aroon calculation.

type BB

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

BB holds all the necessary information needed to calculate Bollinger Bands. The zero value is not usable.

func NewBB

func NewBB(mat MAType, stdDev decimal.Decimal, length int) (BB, error)

NewBB validates provided configuration options and creates new BB indicator.

func (BB) Calc

func (bb BB) Calc(dd []decimal.Decimal) (
	upper decimal.Decimal,
	lower decimal.Decimal,
	width decimal.Decimal,
	err error,
)

Calc calculates all BB values from provided data points slice. Calculation is based on formula provided by investopedia. https://www.investopedia.com/terms/b/bollingerbands.asp. All credits are due to John Bollinger who developed BB indicator.

func (BB) CalcBand

func (bb BB) CalcBand(dd []decimal.Decimal, band Band) (decimal.Decimal, error)

CalcBand calculates specified BB value from provided data points slice. Calculation is based on formula provided by investopedia. https://www.investopedia.com/terms/b/bollingerbands.asp. All credits are due to John Bollinger who developed BB indicator.

func (BB) Count

func (bb BB) Count() int

Count determines the total amount of data points needed for BB calculation.

type Band

type Band int

Band specifies which band should be used.

const (
	BandUpper Band = iota + 1
	BandLower
	BandWidth
)

Available Bollinger Band indicator types.

func (Band) MarshalText

func (b Band) MarshalText() ([]byte, error)

MarshalText turns band into appropriate string representation in JSON.

func (*Band) UnmarshalText

func (b *Band) UnmarshalText(d []byte) error

UnmarshalText turns JSON string to appropriate band value.

func (Band) Validate

func (b Band) Validate() error

Validate checks whether band is one of supported band types.

type CCI

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

CCI holds all the necessary information needed to calculate commodity channel index. The zero value is not usable.

func NewCCI

func NewCCI(mat MAType, length int) (CCI, error)

NewCCI validates provided configuration options and creates new CCI indicator. If provided factor is zero, default value is going to be used (0.015f).

func (CCI) Calc

func (cci CCI) Calc(dd []decimal.Decimal) (decimal.Decimal, error)

Calc calculates CCI from the provided data points slice. Calculation is based on formula provided by investopedia. https://www.investopedia.com/terms/c/commoditychannelindex.asp. All credits are due to Donald Lambert who developed CCI indicator.

func (CCI) Count

func (cci CCI) Count() int

Count determines the total amount of data points needed for CCI calculation.

type DEMA

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

DEMA holds all the necessary information needed to calculate double exponential moving average. The zero value is not usable.

func NewDEMA

func NewDEMA(length int) (DEMA, error)

NewDEMA validates provided configuration options and creates new DEMA indicator.

func (DEMA) Calc

func (dema DEMA) Calc(dd []decimal.Decimal) (decimal.Decimal, error)

Calc calculates DEMA from the provided data points slice. Calculation is based on formula provided by investopedia. https://www.investopedia.com/terms/d/double-exponential-moving-average.asp. All credits are due to Patrick Mulloy who developed DEMA indicator.

func (DEMA) Count

func (dema DEMA) Count() int

Count determines the total amount of data points needed for DEMA calculation.

type EMA

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

EMA holds all the necessary information needed to calculate exponential moving average. The zero value is not usable.

func NewEMA

func NewEMA(length int) (EMA, error)

NewEMA validates provided configuration options and creates new EMA indicator.

func (EMA) Calc

func (ema EMA) Calc(dd []decimal.Decimal) (decimal.Decimal, error)

Calc calculates EMA from the provided data points slice. Calculation is based on formula provided by investopedia. https://www.investopedia.com/terms/e/ema.asp.

func (EMA) CalcNext

func (ema EMA) CalcNext(lres, dec decimal.Decimal) (decimal.Decimal, error)

CalcNext calculates sequential EMA by using previous EMA.

func (EMA) Count

func (ema EMA) Count() int

Count determines the total amount of data points needed for EMA calculation.

type HMA

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

HMA holds all the necessary information needed to calculate hull moving average. The zero value is not usable.

func NewHMA

func NewHMA(length int) (HMA, error)

NewHMA validates provided configuration options and creates new HMA indicator.

func (HMA) Calc

func (h HMA) Calc(dd []decimal.Decimal) (decimal.Decimal, error)

Calc calculates HMA from the provided data points slice. Calculation is based on formula provided by fidelity. https://www.fidelity.com/learning-center/trading-investing/technical-analysis/technical-indicator-guide/hull-moving-average. All credits are due to Alan Hull who developed HMA indicator.

func (HMA) Count

func (h HMA) Count() int

Count determines the total amount of data points needed for HMA calculation.

type MA

type MA interface {
	// Calc should return calculation results based on provided data
	// points slice.
	Calc([]decimal.Decimal) (decimal.Decimal, error)

	// Count should determine the total amount data points required for
	// the calculation.
	Count() int
}

MA is an interface that all moving averages implement.

func NewMA

func NewMA(mat MAType, length int) (MA, error)

NewMA constructs new moving average based on the provided type.

type MAType

type MAType int

MAType is a custom type that validates it to be only of existing moving average types.

const (
	MATypeDoubleExponential MAType = iota + 1
	MATypeExponential
	MATypeHull
	MATypeSimple
	MATypeWeighted
)

Available moving average indicator types.

func (MAType) MarshalText

func (mat MAType) MarshalText() ([]byte, error)

MarshalText turns MAType into appropriate string representation in JSON.

func (*MAType) UnmarshalText

func (mat *MAType) UnmarshalText(d []byte) error

UnmarshalText turns JSON string to appropriate moving average type value.

type ROC

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

ROC holds all the necessary information needed to calculate rate of change. The zero value is not usable.

func NewROC

func NewROC(length int) (ROC, error)

NewROC validates provided configuration options and creates new ROC indicator.

func (ROC) Calc

func (roc ROC) Calc(dd []decimal.Decimal) (decimal.Decimal, error)

Calc calculates ROC from the provided data points slice. Calculation is based on formula provided by investopedia. https://www.investopedia.com/terms/p/pricerateofchange.asp.

func (ROC) Count

func (roc ROC) Count() int

Count determines the total amount of data points needed for ROC calculation.

type RSI

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

RSI holds all the necessary information needed to calculate relative strength index. The zero value is not usable.

func NewRSI

func NewRSI(length int) (RSI, error)

NewRSI validates provided configuration options and creates new RSI indicator.

func (RSI) Calc

func (rsi RSI) Calc(dd []decimal.Decimal) (decimal.Decimal, error)

Calc calculates RSI from the provided data points slice. Calculation is based on formula provided by investopedia. https://www.investopedia.com/terms/r/rsi.asp. All credits are due to J. Welles Wilder Jr. who developed RSI indicator.

func (RSI) Count

func (rsi RSI) Count() int

Count determines the total amount of data points needed for RSI calculation.

type SMA

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

SMA holds all the necessary information needed to calculate simple moving average. The zero value is not usable.

func NewSMA

func NewSMA(length int) (SMA, error)

NewSMA validates provided configuration options and creates new SMA indicator.

func (SMA) Calc

func (sma SMA) Calc(dd []decimal.Decimal) (decimal.Decimal, error)

Calc calculates SMA from the provided data points slice. Calculation is based on formula provided by investopedia. https://www.investopedia.com/terms/s/sma.asp.

func (SMA) Count

func (sma SMA) Count() int

Count determines the total amount of data points needed for SMA calculation.

type Stoch

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

Stoch holds all the necessary information needed to calculate stochastic oscillator. The zero value is not usable.

func NewStoch

func NewStoch(length int) (Stoch, error)

NewStoch validates provided configuration options and creates new Stoch indicator.

func (Stoch) Calc

func (stoch Stoch) Calc(dd []decimal.Decimal) (decimal.Decimal, error)

Calc calculates Stoch from the provided data points slice. Calculation is based on formula provided by investopedia. https://www.investopedia.com/terms/s/stochasticoscillator.asp.

func (Stoch) Count

func (stoch Stoch) Count() int

Count determines the total amount of data points needed for Stoch calculation.

type StochRSI

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

StochRSI holds all the necessary information needed to calculate stoch relative strength index. The zero value is not usable.

func NewStochRSI

func NewStochRSI(length int) (StochRSI, error)

NewStochRSI validates provided configuration options and creates new StochRSI indicator.

func (StochRSI) Calc

func (s StochRSI) Calc(dd []decimal.Decimal) (decimal.Decimal, error)

Calc calculates StochRSI from the provided data slice. Calculation is based on formula provided by investopedia. https://www.investopedia.com/terms/s/stochrsi.asp.

func (StochRSI) Count

func (s StochRSI) Count() int

Count determines the total amount of data needed for StochRSI calculation.

type Trend

type Trend int

Trend specifies which trend should be used.

const (
	// TrendUp specifies increasing value trend.
	TrendUp Trend = iota + 1

	// TrendDown specifies decreasing value value.
	TrendDown
)

func (Trend) MarshalText

func (t Trend) MarshalText() ([]byte, error)

MarshalText turns trend into appropriate string representation.

func (*Trend) UnmarshalText

func (t *Trend) UnmarshalText(d []byte) error

UnmarshalText turns string to appropriate trend value.

func (Trend) Validate

func (t Trend) Validate() error

Validate checks whether the trend is one of supported trend types or not.

type WMA

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

WMA holds all the necessary information needed to calculate weighted moving average. The zero value is not usable.

func NewWMA

func NewWMA(length int) (WMA, error)

NewWMA validates provided configuration options and creates new WMA indicator.

func (WMA) Calc

func (wma WMA) Calc(dd []decimal.Decimal) (decimal.Decimal, error)

Calc calculates WMA from the provided data points slice. Calculation is based on formula provided by investopedia. https://www.investopedia.com/articles/technical/060401.asp.

func (WMA) Count

func (wma WMA) Count() int

Count determines the total amount of data points needed for WMA calculation.

Jump to

Keyboard shortcuts

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