Class: TechnicalAnalysis::MovingAverage::SimpleMA

Inherits:
Base
  • Object
show all
Defined in:
lib/technical_analysis/moving-average/simple-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

Methods inherited from Base

#current, #ma

Constructor Details

#initialize(period: 15, data: [], strict: false) ⇒ SimpleMA

Returns a new instance of SimpleMA.



23
24
25
26
27
28
# File 'lib/technical_analysis/moving-average/simple-ma.rb', line 23

def initialize period: 15, data: [], strict: false

  super

  data.map{|d| add_item d }
end

Instance Method Details

#add_item(value) ⇒ Object

adds item, calculates the sma, puts value to the buffer and returns the result



31
32
33
34
35
36
37
38
39
# File 'lib/technical_analysis/moving-average/simple-ma.rb', line 31

def add_item  value

  @queue << value.to_f
  @queue.shift if @buffer.size > @period

  @buffer <<  @queue.sum / @queue.size    # @queue.sum is always a float. 

 current  #  return the last buffer value
end