Module: Quant::Mixins::SimpleMovingAverage

Included in:
MovingAverages
Defined in:
lib/quant/mixins/simple_moving_average.rb

Instance Method Summary collapse

Instance Method Details

#simple_moving_average(source, period:) ⇒ Float Also known as: sma

Computes the Simple Moving Average (SMA) of the given period.

Parameters:

  • the source of the data points to be used in the calculation.

  • the number of elements to compute the SMA over.

Returns:

  • the simple moving average of the period.

Raises:



13
14
15
16
17
# File 'lib/quant/mixins/simple_moving_average.rb', line 13

def simple_moving_average(source, period:)
  raise ArgumentError, "source must be a Symbol" unless source.is_a?(Symbol)

  values.last(period).map { |value| value.send(source) }.mean
end