Class: TechnicalAnalysis::StockCalculation

Inherits:
Object
  • Object
show all
Defined in:
lib/technical_analysis/helpers/stock_calculation.rb

Class Method Summary collapse

Class Method Details

.ema(current_value, data, period, prev_value) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/technical_analysis/helpers/stock_calculation.rb', line 16

def self.ema(current_value, data, period, prev_value)
  if prev_value.nil?
    ArrayHelper.average(data)
  else
    (current_value - prev_value) * (2.0 / (period + 1.0)) + prev_value
  end
end

.true_range(current_high, current_low, previous_close) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/technical_analysis/helpers/stock_calculation.rb', line 4

def self.true_range(current_high, current_low, previous_close)
  [
    (current_high - current_low),
    (current_high - previous_close).abs,
    (current_low - previous_close).abs
  ].max
end

.typical_price(price) ⇒ Object



12
13
14
# File 'lib/technical_analysis/helpers/stock_calculation.rb', line 12

def self.typical_price(price)
  (price[:high] + price[:low] + price[:close]) / 3.0
end

.wma(data) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/technical_analysis/helpers/stock_calculation.rb', line 24

def self.wma(data)
  intermediate_values = []
  data.each_with_index do |datum, i|
    intermediate_values << datum * (i + 1)/(data.size * (data.size + 1)/2).to_f
  end
  ArrayHelper.sum(intermediate_values)
end