Module: Quant::Mixins::MovingAverages
- Defined in:
- lib/quant/mixins/moving_averages.rb
Instance Method Summary collapse
-
#exponential_moving_average(source, previous: :ema, period:) ⇒ Float
(also: #ema)
Computes the Exponential Moving Average (EMA) of the given period.
-
#extended_weighted_moving_average(source) ⇒ Float
(also: #ewma)
Computes the Weighted Moving Average (WMA) of the series, using the seven most recent data points.
-
#simple_moving_average(source, period:) ⇒ Float
(also: #sma)
Computes the Simple Moving Average (SMA) of the given period.
-
#weighted_moving_average(source) ⇒ Float
(also: #wma)
Computes the Weighted Moving Average (WMA) of the series, using the four most recent data points.
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.
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.
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.
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.
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 |