Module: Quant::Mixins::MovingAverages

Defined in:
lib/quant/mixins/moving_averages.rb

Instance Method Summary collapse

Instance Method Details

#exponential_moving_average(source, previous: :ema, period:) ⇒ 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

Parameters:

  • source (Symbol)

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

  • previous (Symbol) (defaults to: :ema)

    the previous EMA value.

  • period (Integer)

    the number of elements to compute the EMA over.

Returns:

  • (Float)

    the exponential moving average of the period.

Raises:

  • (ArgumentError)

    if the source is not a Symbol.



76
77
78
79
80
81
82
# File 'lib/quant/mixins/moving_averages.rb', line 76

def exponential_moving_average(source, previous: :ema, period:)
  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 = 2.0 / (period + 1)
  p0.send(source) * alpha + p1.send(previous) * (1.0 - alpha)
end

#extended_weighted_moving_average(source) ⇒ Float Also known as: ewma

Computes the Weighted Moving Average (WMA) of the series, using the seven most recent data points.

Examples:

p0.wma = weighted_average(:close_price)

Parameters:

  • source (Symbol)

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

Returns:

  • (Float)

    the weighted average of the series.

Raises:

  • (ArgumentError)

    if the source is not a Symbol.



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/quant/mixins/moving_averages.rb', line 32

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

  [7.0 * p0.send(source),
   6.0 * p1.send(source),
   5.0 * p2.send(source),
   4.0 * p3.send(source),
   3.0 * p(4).send(source),
   2.0 * p(5).send(source),
         p(6).send(source)].sum / 28.0
end

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

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

Parameters:

  • source (Symbol)

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

  • period (Integer)

    the number of elements to compute the SMA over.

Returns:

  • (Float)

    the simple moving average of the period.

Raises:

  • (ArgumentError)


50
51
52
53
54
# File 'lib/quant/mixins/moving_averages.rb', line 50

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

#weighted_moving_average(source) ⇒ Float Also known as: wma

Computes the Weighted Moving Average (WMA) of the series, using the four most recent data points.

Examples:

p0.wma = weighted_average(:close_price)

Parameters:

  • source (Symbol)

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

Returns:

  • (Float)

    the weighted average of the series.

Raises:

  • (ArgumentError)

    if the source is not a Symbol.



15
16
17
18
19
20
21
22
# File 'lib/quant/mixins/moving_averages.rb', line 15

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

  [4.0 * p0.send(source),
   3.0 * p1.send(source),
   2.0 * p2.send(source),
         p3.send(source)].sum / 10.0
end