Class: TechnicalAnalysis::MovingAverage::Wma

Inherits:
Base
  • Object
show all
Defined in:
lib/technical_analysis/moving-average/weighted-ma.rb

Instance Method Summary collapse

Methods inherited from Base

#current, #ma

Constructor Details

#initialize(period: 15, data: [], strict: true) ⇒ Wma

Returns a new instance of Wma.



7
8
9
10
11
# File 'lib/technical_analysis/moving-average/weighted-ma.rb', line 7

def initialize period: 15, data: [], strict: true
  super
  @denominator =  (1..@period).sum
  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



14
15
16
17
18
19
20
21
22
# File 'lib/technical_analysis/moving-average/weighted-ma.rb', line 14

def add_item  value

  @queue << value.to_f
  @queue.shift if @queue.size > @period
  weights = (1..@queue.size).to_a
  nominator = weights.zip(@queue).map { |w, x| w * x }.sum
  @buffer << nominator / @denominator 
 current  #  return the last buffer value
end