Module: PriceChannel

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

Overview

Price Channel indicator Returns an array containing the current upper and lower values of the series

Instance Method Summary collapse

Instance Method Details

#price_channel(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
# File 'lib/ruby-technical-analysis/indicators/price_channel.rb', line 6

def price_channel(period)
  highs = []
  lows = []

  each do |i|
    highs << i[0]
    lows << i[1]
  end

  if highs.size < period + 1
    raise ArgumentError,
          "The highs array size is less than the period + 1 size required."
  end

  if lows.size < period + 1
    raise ArgumentError,
          "The lows array size is less than the period + 1 size required."
  end

  highs = highs.last(period + 1)
  lows = lows.last(period + 1)

  upper_pc = (highs[0..period - 1]).max
  lower_pc = (lows[0..period - 1]).min

  [upper_pc, lower_pc]
end