Module: ChandeMomentumOscillator

Included in:
Array
Defined in:
lib/ruby-technical-analysis/indicators/chande_momentum_oscillator.rb

Overview

Chaikin Money Flow indicator Returns a current singular value

Instance Method Summary collapse

Instance Method Details

#chande_momentum_oscillator(period) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ruby-technical-analysis/indicators/chande_momentum_oscillator.rb', line 6

def chande_momentum_oscillator(period)
  if size < period + 1
    raise ArgumentError,
          "Array size is less than the minimum size of the period + 1 for the Chande Momentum Oscillator."
  end

  closes = last(period + 1)

  up_change_sum = 0
  down_change_sum = 0

  (1..period).each do |i|
    if closes[i] >= closes[i - 1]
      up_change_sum += (closes[i] - closes[i - 1])
    else
      down_change_sum += (closes[i - 1] - closes[i])
    end
  end

  up_sum_minus_down_sum = up_change_sum - down_change_sum
  up_sum_plus_down_sum = up_change_sum + down_change_sum

  ((up_sum_minus_down_sum.to_f / up_sum_plus_down_sum) * 100).round(4)
end