Class: RubyTechnicalAnalysis::MovingAverages
- Defined in:
- lib/ruby_technical_analysis/indicators/moving_averages.rb
Overview
Moving Averages
Find more information at:
Simple Moving Average (SMA): www.fidelity.com/learning-center/trading-investing/technical-analysis/technical-indicator-guide/sma
Exponential Moving Average (EMA): www.fidelity.com/learning-center/trading-investing/technical-analysis/technical-indicator-guide/ema
Weighted Moving Average (WMA): www.fidelity.com/learning-center/trading-investing/technical-analysis/technical-indicator-guide/wma
Instance Attribute Summary collapse
-
#period ⇒ Object
readonly
Returns the value of attribute period.
Attributes inherited from Indicator
Instance Method Summary collapse
-
#ema ⇒ Float
Exponential Moving Average.
-
#initialize(series: [], period: 20) ⇒ MovingAverages
constructor
A new instance of MovingAverages.
-
#sma ⇒ Float
Simple Moving Average.
-
#valid? ⇒ Boolean
Whether or not the object is valid.
-
#wma ⇒ Float
Weighted Moving Average.
Methods inherited from Indicator
Constructor Details
#initialize(series: [], period: 20) ⇒ MovingAverages
Returns a new instance of MovingAverages.
16 17 18 19 20 |
# File 'lib/ruby_technical_analysis/indicators/moving_averages.rb', line 16 def initialize(series: [], period: 20) @period = period super(series: series) end |
Instance Attribute Details
#period ⇒ Object (readonly)
Returns the value of attribute period.
12 13 14 |
# File 'lib/ruby_technical_analysis/indicators/moving_averages.rb', line 12 def period @period end |
Instance Method Details
#ema ⇒ Float
Exponential Moving Average
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/ruby_technical_analysis/indicators/moving_averages.rb', line 30 def ema return series.last if period == 1 series.last(period).each_with_object([]) do |num, result| result << if result.empty? num else (num * _ema_percentages.first) + (result.last * _ema_percentages.last) end end.last end |
#sma ⇒ Float
Simple Moving Average
24 25 26 |
# File 'lib/ruby_technical_analysis/indicators/moving_averages.rb', line 24 def sma series.last(period).sum.to_f / period end |
#valid? ⇒ Boolean
Returns Whether or not the object is valid.
53 54 55 |
# File 'lib/ruby_technical_analysis/indicators/moving_averages.rb', line 53 def valid? period <= series.length end |
#wma ⇒ Float
Weighted Moving Average
44 45 46 47 48 49 50 |
# File 'lib/ruby_technical_analysis/indicators/moving_averages.rb', line 44 def wma true_periods = (1..period).sum sigma_periods = series.last(period).each_with_index.sum { |num, index| (index + 1) * num } sigma_periods.to_f / true_periods end |