Class: TechnicalAnalysis::MovingAverage::ExpMA
- Defined in:
- lib/technical_analysis/moving-average/exp-ma.rb
Overview
Calculates the exponential moving average (EMA) for the data over the given period en.wikipedia.org/wiki/Moving_average#Exponential_moving_average
Takes a block which replaces the smooth-constant
z = Symbols::Futures.mini_dax.eod( duration: '90 d')
TechnicalAnalysis::MovingAverage::ExpMA.new( data=z.map(&:close))
returns an array with the calculated moving-average data
ema = TechnicalAnalysis::MovingAverage::ExpMA.new( default_value = z.first[:close])
moving_average = z.map{|x| EMA.new x.time, ema.add_item(x.close)}
returns an array of EMA-Objects
Instance Method Summary collapse
-
#add_item(value) ⇒ Object
adds item, calculates the ema, puts value to the buffer and returns the result.
-
#initialize(period: 15, data: [], strict: true) ⇒ ExpMA
constructor
A new instance of ExpMA.
Methods inherited from Base
Constructor Details
#initialize(period: 15, data: [], strict: true) ⇒ ExpMA
Returns a new instance of ExpMA.
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/technical_analysis/moving-average/exp-ma.rb', line 23 def initialize period: 15, data: [], strict: true super @smooth_constant = if block_given? yield period else (2.0 / (period + 1.0)) end if !data.empty? data.map{|d| add_item d } end end |
Instance Method Details
#add_item(value) ⇒ Object
adds item, calculates the ema, puts value to the buffer and returns the result
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/technical_analysis/moving-average/exp-ma.rb', line 37 def add_item value @queue << value @queue.shift if @queue.size > @period if @buffer.empty? @buffer=[value.to_f] else prev_ema = @buffer.last current_value = value.to_f @buffer << (current_value - prev_ema) * @smooth_constant + prev_ema end current # return the last buffer value end |