module MovingAverage(T)

Overview

The MovingAverage mixin provides methods for calculating different moving averages of a collection.

Including types must respond to basic arithmetic operators: +, -, *, and /.

MovingAverage uses the following methods which must be defined:

Direct including types

Defined in:

moving_average/errors.cr
moving_average/exponential_moving_average.cr
moving_average/simple_moving_average.cr
moving_average/smoothed_moving_average.cr
moving_average/version.cr
moving_average.cr

Constant Summary

VERSION = "0.1.0"

Instance Method Summary

Instance Method Detail

def ema(period : Int) #

[View source]
def ema!(period : Int, value : T, previous_value = nil) #

[View source]
def exponential_moving_average(period : Int) #

Creates a new self containing the exponential moving average (EMA) of each element.

The EMA can be defined as the series of weighted values in which the weighting decreases exponentially. The first element of the EMA is the simple moving average, all subsequent elements can be calculated as follows:

EMAi = weighting_factor * (Pi - EMAi-1) + EMAi-1

Where

  • Pi is the value of element i.
  • weighting_factor is a constant between 0 and 1, determined by the period.
[1.0, 2.0, 3.0, 4.0, 5.0].exponential_moving_average(4) # => [2.5, 3.5]

Reference


[View source]
def exponential_moving_average!(period : Int, value : T, previous_ema = nil) #

Evaluates the exponential moving average of value and appends it to self.

Useful for updating an existing EMA series quickly. If previous_ema is nil, the last element in the collection will be used.

ema = [1, 2, 3, 4, 5].exponential_moving_average(4) # => [2, 3]
ema.exponential_moving_average!(4, 8)               # => [2, 3, 5]

[View source]
def simple_moving_average(period : Int) #

Creates a new self containing the simple moving average (SMA) of each element.

The SMA of a single element can be defined as the mean of previous n data:

SMAi = (Pi + Pi-1 + ... + Pi-(n-1)) / n

Where

  • Pi is the value of element i.
  • n corresponds to the period and is the number of datum to consider.
[1, 2, 3, 4, 5].simple_moving_average(4) # => [2, 3]

Reference


[View source]
def sma(period : Int) #

[View source]
def smma(period : Int) #

[View source]
def smma!(period : Int, value : T, previous_smma = nil) #

[View source]
def smoothed_moving_average(period : Int) #

Creates a new self containing the smoothed moving average (SMMA) of each element.

The SMMA is an exponential weighted average without a fixed period. Each previous datum has some affect to the current SMMA. The first element is the simple moving average, all subsequent elements can be calculated as follows:

SMMAi = (Pi - SMMAi-1) / n + SMMAi-1

Where

  • Pi is the value of element i.
  • n is the smoothing period.
[1, 2, 3, 4, 5].smoothed_moving_average(4) # => [2, 2]

Reference


[View source]
def smoothed_moving_average!(period : Int, value : T, previous_smma = nil) #

Evaluates the smoothed moving average of value and appends it to self.

Useful when updating an existing SMMA series quickly. If previous_smma is nil, the last element will be used.

smma = [1, 2, 3, 4, 5].smoothed_moving_average(4) # => [2, 2]
smma.smoothed_moving_average!(4, 8)               # => [2, 2, 3]

[View source]