Module: Quant::Mixins::ExponentialMovingAverage

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

Instance Method Summary collapse

Instance Method Details

#exponential_moving_average(source, period:, previous: :ema) ⇒ Float Also known as: ema

Computes the Exponential Moving Average (EMA) of the given period.

The EMA computation is optimized to compute using just the last two indicator data points and is expected to be called in each indicator’s ‘#compute` method for each iteration on the series.

Examples:

def compute
  p0.ema = exponential_moving_average(:close_price, period: 3)
end

def compute
  p0.ema = exponential_moving_average(:close_price, previous: :ema, period: 3)
end

Raises:

  • (ArgumentError)

    if the source is not a Symbol.



25
26
27
28
29
30
31
# File 'lib/quant/mixins/exponential_moving_average.rb', line 25

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

  alpha = bars_to_alpha(period)
  (p0.send(source) * alpha) + (p1.send(previous) * (1.0 - alpha))
end