Module: CommodityChannelIndex

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

Overview

Commodity Channel Index indicator Returns a current singular value

Instance Method Summary collapse

Instance Method Details

#commodity_channel_index(period) ⇒ Object



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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ruby-technical-analysis/indicators/commodity_channel_index.rb', line 8

def commodity_channel_index(period)
  min_size = ((period * 2) - 1)

  highs = []
  lows = []
  closes = []

  each do |h, l, c|
    highs << h
    lows << l
    closes << c
  end

  if highs.size < period
    raise ArgumentError,
          "High array passed to Chaikin Money Flow cannot be less than the period argument."
  end

  if lows.size < period
    raise ArgumentError,
          "Low array passed to Chaikin Money Flow cannot be less than the period argument."
  end

  if closes.size < period
    raise ArgumentError,
          "Close array passed to Chaikin Money Flow cannot be less than the period argument."
  end

  highs = highs.last(min_size)
  lows = lows.last(min_size)
  closes = closes.last(min_size)

  typical_prices = []
  tp_sma = []
  period_sum = 0

  (0..(min_size - 1)).each do |i|
    typical_prices << (highs[i] + closes[i] + lows[i]) / 3
  end

  (0..(period - 1)).each do |i|
    tp_sma << typical_prices[i..(i + period - 1)].sma(period)
  end

  typical_prices.last(period).each do |tp|
    period_sum += (tp_sma[-1] - tp).abs
  end

  ps_next = (period_sum.to_f / period) * 0.015
  tp_sma_min_tp = typical_prices[-1] - tp_sma[-1]
  (tp_sma_min_tp.to_f / ps_next)
end