Module: IntradayMomentumIndex

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

Overview

Intraday Momentum Index indicator Returns a singular current value

Instance Method Summary collapse

Instance Method Details

#intraday_momentum_index(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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ruby-technical-analysis/indicators/intraday_momentum_index.rb', line 6

def intraday_momentum_index(period)
  opens = []
  closes = []

  each do |o, c|
    opens << o
    closes << c
  end

  if opens.size < period
    raise ArgumentError,
          "Opens array passed to Intraday Momentum Index cannot be less than the period argument."
  end

  if closes.size < period
    raise ArgumentError,
          "Closes array passed to Intraday Momentum Index cannot be less than the period argument."
  end

  closes = closes.last(period)
  opens = opens.last(period)

  gsum = 0.0
  lsum = 0.0

  (0..(period - 1)).each do |i|
    cmo = (closes[i] - opens[i]).abs
    if closes[i] > opens[i]
      gsum += cmo
    else
      lsum += cmo
    end
  end

  gsum_plus_lsum = gsum + lsum

  ((gsum.to_f / gsum_plus_lsum) * 100).round(4)
end