Class: Indicators::Ema

Inherits:
Object
  • Object
show all
Defined in:
lib/indicators/calculations/ema.rb

Overview

Exponential Moving Average

Class Method Summary collapse

Class Method Details

.calculate(data, parameters) ⇒ Object

Multiplier: (2 / (Time periods + 1) ) = (2 / (10 + 1) ) = 0.1818 (18.18%) EMA: - EMA(previous day) x multiplier + EMA(previous day).



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/indicators/calculations/ema.rb', line 8

def self.calculate data, parameters
  periods = parameters
  output = Array.new
  adj_closes = Indicators::Helper.validate_data(data, :adj_close, periods)

  k = 2.0/(periods+1)
  adj_closes.each_with_index do |adj_close, index|
    start = index+1-periods
    if start == 0
      output[index] = Indicators::Sma.calculate(adj_closes[start..index], periods).last
    elsif start > 0
      output[index] = ((adj_close - output[index-1]) * k + output[index-1])
    else
      output[index] = nil
    end
  end
  
  return output

end