movingaverage

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AvgOverTime

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

AvgOverTime maintains change rate in the last avgInterval.

AvgOverTime takes changes with their own intervals, stores recent changes that happened in the last avgInterval, then calculates the change rate by (sum of changes) / (sum of intervals).

func NewAvgOverTime

func NewAvgOverTime(interval time.Duration) *AvgOverTime

NewAvgOverTime returns an AvgOverTime with given interval.

func (*AvgOverTime) Add

func (aot *AvgOverTime) Add(delta float64, interval time.Duration)

Add adds recent change to AvgOverTime. It will pop item until the retain item's sum is greater than avgInterval. such as: que [1,1,1,6], avgInterval is 5. It will pop 6 if adding 2, the retaining item's sum is 5(2,1,1,1) >= avgInterval. It can't pop 6 if adding 1, the retaining item's sum is 4(1,1,1,1) < avgInterval.

func (*AvgOverTime) Clear

func (aot *AvgOverTime) Clear()

Clear clears the AvgOverTime.

func (*AvgOverTime) Clone

func (aot *AvgOverTime) Clone() *AvgOverTime

Clone returns a copy of AvgOverTime

func (*AvgOverTime) Get

func (aot *AvgOverTime) Get() float64

Get returns change rate in the last interval.

func (*AvgOverTime) GetInstantaneous

func (aot *AvgOverTime) GetInstantaneous() float64

GetInstantaneous returns the value just added.

func (*AvgOverTime) GetIntervalSum

func (aot *AvgOverTime) GetIntervalSum() time.Duration

GetIntervalSum returns the sum of interval

func (*AvgOverTime) IsFull

func (aot *AvgOverTime) IsFull() bool

IsFull returns whether AvgOverTime is full

func (*AvgOverTime) Set

func (aot *AvgOverTime) Set(avg float64)

Set sets AvgOverTime to the given average.

type EMA

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

EMA works as an exponential moving average filter. References: https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average.

func NewEMA

func NewEMA(decays ...float64) *EMA

NewEMA returns an EMA.

func (*EMA) Add

func (e *EMA) Add(num float64)

Add adds a data point.

func (*EMA) Get

func (e *EMA) Get() float64

Get returns the result of the data set.If return 0.0, it means the filter can not be wake up.

func (*EMA) GetInstantaneous

func (e *EMA) GetInstantaneous() float64

GetInstantaneous returns the value just added.

func (*EMA) Reset

func (e *EMA) Reset()

Reset cleans the data set.

func (*EMA) Set

func (e *EMA) Set(n float64)

Set = Reset + Add.

type HMA

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

HMA works as hull moving average There are at most `size` data points for calculating. References: https://www.fidelity.com/learning-center/trading-investing/technical-analysis/technical-indicator-guide/hull-moving-average

func NewHMA

func NewHMA(sizes ...float64) *HMA

NewHMA returns a WMA.

func (*HMA) Add

func (h *HMA) Add(n float64)

Add adds a data point.

func (*HMA) Get

func (h *HMA) Get() float64

Get returns the weight average of the data set.

func (*HMA) GetInstantaneous

func (h *HMA) GetInstantaneous() float64

GetInstantaneous returns the value just added.

func (*HMA) Reset

func (h *HMA) Reset()

Reset cleans the data set.

func (*HMA) Set

func (h *HMA) Set(n float64)

Set = Reset + Add.

type MaxFilter

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

MaxFilter works as a maximum filter with specified window size. There are at most `size` data points for calculating.

func NewMaxFilter

func NewMaxFilter(size int) *MaxFilter

NewMaxFilter returns a MaxFilter.

func (*MaxFilter) Add

func (r *MaxFilter) Add(n float64)

Add adds a data point.

func (*MaxFilter) Get

func (r *MaxFilter) Get() float64

Get returns the maximum of the data set.

func (*MaxFilter) GetInstantaneous

func (r *MaxFilter) GetInstantaneous() float64

GetInstantaneous returns the value just added.

func (*MaxFilter) Reset

func (r *MaxFilter) Reset()

Reset cleans the data set.

func (*MaxFilter) Set

func (r *MaxFilter) Set(n float64)

Set = Reset + Add.

type MedianFilter

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

MedianFilter works as a median filter with specified window size. There are at most `size` data points for calculating. References: https://en.wikipedia.org/wiki/Median_filter.

func NewMedianFilter

func NewMedianFilter(size int) *MedianFilter

NewMedianFilter returns a MedianFilter.

func (*MedianFilter) Add

func (r *MedianFilter) Add(n float64)

Add adds a data point.

func (*MedianFilter) Clone

func (r *MedianFilter) Clone() *MedianFilter

Clone returns a copy of MedianFilter

func (*MedianFilter) Get

func (r *MedianFilter) Get() float64

Get returns the median of the data set.

func (*MedianFilter) GetInstantaneous

func (r *MedianFilter) GetInstantaneous() float64

GetInstantaneous returns the value just added.

func (*MedianFilter) Reset

func (r *MedianFilter) Reset()

Reset cleans the data set.

func (*MedianFilter) Set

func (r *MedianFilter) Set(n float64)

Set = Reset + Add.

type MovingAvg

type MovingAvg interface {
	// Add adds a data point to the data set.
	Add(data float64)
	// Get returns the moving average.
	Get() float64
	// GetInstantaneous returns the value just added.
	GetInstantaneous() float64
	// Reset cleans the data set.
	Reset()
	// Set = Reset + Add
	Set(data float64)
}

MovingAvg provides moving average. Ref: https://en.wikipedia.org/wiki/Moving_average

type TimeMedian

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

TimeMedian is AvgOverTime + MedianFilter Size of MedianFilter should be larger than double size of AvgOverTime to denoisy. Delay is aotSize * mfSize * reportInterval/4 and the min filled period is aotSize * reportInterval, which is not related with mfSize

func NewTimeMedian

func NewTimeMedian(aotSize, mfSize int, reportInterval time.Duration) *TimeMedian

NewTimeMedian returns a TimeMedian with given size.

func (*TimeMedian) Add

func (t *TimeMedian) Add(delta float64, interval time.Duration)

Add adds recent change to TimeMedian.

func (*TimeMedian) Clone

func (t *TimeMedian) Clone() *TimeMedian

Clone returns a copy of TimeMedian

func (*TimeMedian) Get

func (t *TimeMedian) Get() float64

Get returns change rate in the median of the several intervals.

func (*TimeMedian) GetInstantaneous

func (t *TimeMedian) GetInstantaneous() float64

GetInstantaneous returns instantaneous speed

func (*TimeMedian) Set

func (t *TimeMedian) Set(avg float64)

Set sets the given average.

type WMA

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

WMA works as a weight with specified window size. There are at most `size` data points for calculating. References:https://en.wikipedia.org/wiki/Moving_average#Weighted_moving_average

func NewWMA

func NewWMA(sizes ...int) *WMA

NewWMA returns a WMA.

func (*WMA) Add

func (w *WMA) Add(n float64)

Add adds a data point.

func (*WMA) Get

func (w *WMA) Get() float64

Get returns the weight average of the data set.

func (*WMA) GetInstantaneous

func (w *WMA) GetInstantaneous() float64

GetInstantaneous returns the value just added.

func (*WMA) Reset

func (w *WMA) Reset()

Reset cleans the data set.

func (*WMA) Set

func (w *WMA) Set(n float64)

Set = Reset + Add.

type WeightAllocator

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

WeightAllocator is used to alloc weight for cache. When we need to use multiple items from the cache, we may need to assign different weights to these items. WeightAllocator will divide these items into some segments whose number named as segNum which should great than 0. And the items at first segment will be assigned more weight that is `segNum` times that of item at last segment. If you want assign same weights, just input segNum as 1. If length is 10 and segNum is 3, it will make the weight arrry as [3,3,3,3,2,2,2,1,1,1], and then uniform it : [3,3,3,3,2,2,2,1,1,1]/sum(arr)=arr/21, And the final weight is [0.143,0.143,0.143,0.143,0.095,0.095,0.095,0.047,0.047,0.047]; If length is 10 and segNum is 1, the weight is [0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1]; If length is 3 and segNum is 3, the weight is [0.5,0.33,0.17].

func NewWeightAllocator

func NewWeightAllocator(length, segNum int) *WeightAllocator

NewWeightAllocator returns a new WeightAllocator.

func (*WeightAllocator) Get

func (a *WeightAllocator) Get(i int) float64

Get returns weight at pos i

Jump to

Keyboard shortcuts

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