sma

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2024 License: GPL-3.0 Imports: 2 Imported by: 1

README

SMA

Simple Moving Average

This is an example of simple moving average calculator using the built-in golang's list type.

This calculator uses an optimized calculation method which in its turn remembers the last calculated SMA value.

Notes
  1. In statistics, a moving average (rolling average or running average) is a calculation to analyze data points by creating a series of averages of different subsets of the full data set. It is also called a moving mean (MM) or rolling mean and is a type of finite impulse response filter. Variations include: simple, cumulative, or weighted forms.
    Source: https://en.wikipedia.org/wiki/Moving_average

  2. In financial applications a simple moving average (SMA) is the unweighted mean of the previous k data-points.
    Source: https://en.wikipedia.org/wiki/Moving_average#Simple_moving_average

  3. In mathematics and statistics, the arithmetic mean, or simply the mean or the average (when the context is clear), is the sum of a collection of numbers divided by the count of numbers in the collection. The collection is often a set of results of an experiment or an observational study, or frequently a set of results from a survey. The term "arithmetic mean" is preferred in some contexts in mathematics and statistics, because it helps distinguish it from other means, such as the geometric mean and the harmonic mean.
    Source: https://en.wikipedia.org/wiki/Arithmetic_mean

General Formula

Formula for Calculating Next SMA Value

This formula is used for calculating the next SMA value using the previous SMA value.

Documentation

Index

Constants

View Source
const (
	ErrWindowsSize   = "window size is wrong"
	ErrDataSetIsCold = "data set is cold"
	ErrNotEnoughData = "not enough data"
)

Error messages.

Variables

This section is empty.

Functions

This section is empty.

Types

type Calculator

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

Calculator is a simple moving average (SMA) calculator.

If a data set does not have enough values to calculate the SMA, the calculator is considered to be cold. If a data set contains enough values to calculate the SMA, the calculator is considered to be hot. A newly created calculator is cold.

func New

func New(windowSize int) (c *Calculator, err error)

New is the constructor of the Calculator.

func (*Calculator) AddItemAndGetSMA

func (c *Calculator) AddItemAndGetSMA(p ValueType) (sma ValueType, err error)

AddItemAndGetSMA inserts a new item into the data series and returns a new SMA value.

func (Calculator) IsCold

func (c Calculator) IsCold() bool

IsCold checks whether the data set is cold.

type ValueType

type ValueType = float64

ValueType is the type of elements used for calculation of Calculator.

Jump to

Keyboard shortcuts

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