Class: TechnicalAnalysis::MovingAverage::KaMA
- Inherits:
-
Object
- Object
- TechnicalAnalysis::MovingAverage::KaMA
- Defined in:
- lib/technical_analysis/moving-average/ka-ma.rb
Overview
Calculates the exponential moving average (EMA) for the data over the given period https://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 kama, puts value to the buffer and returns the result.
-
#current ⇒ Object
returns the kama of the last computed item.
-
#initialize(period: 10, fast: 2, slow: 30, data: []) ⇒ KaMA
constructor
A new instance of KaMA.
-
#kaema ⇒ Object
returns the kama-buffer.
Constructor Details
#initialize(period: 10, fast: 2, slow: 30, data: []) ⇒ KaMA
Returns a new instance of KaMA.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/technical_analysis/moving-average/ka-ma.rb', line 23 def initialize period: 10, fast: 2, slow: 30, data: [] raise "Period must be greater then one" if period <= 1 @smoothConst_fast = 2 / (fast +1).to_f @smoothConst_slow = 2 / (slow +1).to_f @period = period @queue = [] @buffer = [] if !data.empty? data.map{|d| add_item d } end end |
Instance Method Details
#add_item(value) ⇒ Object
adds item, calculates the kama, puts value to the buffer and returns the result
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/technical_analysis/moving-average/ka-ma.rb', line 40 def add_item value begin @queue << value.to_f rescue NoMethodError => e puts "add item only supports single values. Using the :close method" @queue << value.send(:close) || value end if @queue.size < @period @buffer = [ @queue.sum / @queue.size ] # fill buffer with average else @queue.shift if @queue.size > @period @buffer << calculate end current # return the last buffer value end |
#current ⇒ Object
returns the kama of the last computed item
62 63 64 |
# File 'lib/technical_analysis/moving-average/ka-ma.rb', line 62 def current @buffer.last end |
#kaema ⇒ Object
returns the kama-buffer
57 58 59 |
# File 'lib/technical_analysis/moving-average/ka-ma.rb', line 57 def kaema @buffer end |