Module: TechnicalAnalysis::MovingAverage
- Defined in:
- lib/calculation_helpers.rb,
lib/technical_analysis/moving_average.rb,
lib/technical_analysis/moving-average/base.rb,
lib/technical_analysis/moving-average/ka-ma.rb,
lib/technical_analysis/moving-average/exp-ma.rb,
lib/technical_analysis/moving-average/simple-ma.rb,
lib/technical_analysis/moving-average/weighted-ma.rb
Defined Under Namespace
Classes: Base, EMA, ExpMA, KAMA, KaMA, SMA, SimpleMA, WMA, Wma
Class Method Summary collapse
-
.ema(current_value, data, period, prev_ema) ⇒ Object
Calculates the exponential moving average (EMA) for the data over the given period en.wikipedia.org/wiki/Moving_average#Exponential_moving_average.
- .kama(current_value = nil, kama_source = [], period = 10, fast = 2, slow = 30, prev_kama = nil) ⇒ Object
-
.sma(current_value = nil, data = [], period = 15) ⇒ Object
calculates the simple moving average of the current value using the data as reference.
-
.wma(current_value = nil, wma_source = [], period = 0) ⇒ Object
Calculates the weighted moving average.
Class Method Details
.ema(current_value, data, period, prev_ema) ⇒ Object
Calculates the exponential moving average (EMA) for the data over the given period en.wikipedia.org/wiki/Moving_average#Exponential_moving_average
z = Symbols::Futures.mini_dax.eod( duration: ‘30 d’).each e= nil ema= z.map{|y| e= TechnicalAnalysis::MovingAverage.ema( y.close, z.map(&:close), 30, e ) }
or
EMA = Struct.new :time, :ema e = nil ema = z.map do |y|
EMA.new y.time,
e = TechnicalAnalysis::MovingAverage.ema y.close, z.map(&:close), 30, e
end
44 45 46 47 48 49 50 |
# File 'lib/calculation_helpers.rb', line 44 def self.ema current_value, data, period, prev_ema if prev_ema.nil? data.sum / data.size.to_f # Average else (current_value - prev_ema) * (2.0 / (period + 1.0)) + prev_ema end end |
.kama(current_value = nil, kama_source = [], period = 10, fast = 2, slow = 30, prev_kama = nil) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/technical_analysis/moving_average.rb', line 49 def self.kama current_value=nil, kama_source=[], period=10, fast=2, slow= 30, prev_kama= nil # kama_source: Array of values (length: period) # They are used to calculate trend and volatility # build the SmootingFactor (alpha) # fast = 2 / GDperiod(fast) +1 ; slow = 2 / GDperiod(slow) +1 # kama_source is updated. smoothConst_fast = 2 / (fast +1).to_f smoothConst_slow = 2 / (slow +1).to_f if current_value.present? kama_source.push current_value else current_value = kama_source.last end if prev_kama.nil? kama_source.sum / kama_source.size.to_f # return Average --> sma else kama_source = kama_source[ -period , period ] if kama_source.size > period # define the Effiency Ratio to be used by the "kaufmans Adaptive Moving Average" : kama # ER is calculated by dividing the absolute difference between the # current price and the price at the beginning of the period by the sum # of the absolute difference between each pair of closes during the # period. # | x(t) - x(t-n) | # er = ---------------------------- # sum | x(i) - x(i-1) | er= (kama_source.first - kama_source.last).abs / (1..kama_source.size-1).map{|x| (kama_source[x] - kama_source[x-1]).abs }.sum # rescue 1 alpha = (er * ( smoothConst_fast - smoothConst_slow ) + smoothConst_slow ) ** 2 prev_kama + alpha * (current_value - prev_kama) end end |
.sma(current_value = nil, data = [], period = 15) ⇒ Object
calculates the simple moving average of the current value using the data as reference
if current_value is
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/technical_analysis/moving_average.rb', line 37 def self.sma current_value=nil, data=[], period=15 sma_source = if current_value.present? data.push current_value # add it as last element else data end sma_source = sma_source[ -period , period ] if sma_source.size > period sma_source.sum / sma_source.size .to_f end |
.wma(current_value = nil, wma_source = [], period = 0) ⇒ Object
Calculates the weighted moving average
Parameter is the data-array.
Takes an optional Block. Specify a method name that returns the data item
Symbols::Futures.mini_dax.eod( duration: ‘30 d’).map do | z |
TechnicalAnalysis::MovingAverage.wma( z ){ :close }
end
61 62 63 64 65 66 67 68 |
# File 'lib/calculation_helpers.rb', line 61 def self.wma(data) intermediate_values = [] data.each_with_index do |datum, i| datum = datum.send yield if block_given? intermediate_values << datum * (i + 1) / (data.size * (data.size + 1) / 2).to_f end intermediate_values.sum end |