Module: WilliamsPercentR

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

Overview

Williams Percent R indicator Returns a single value

Instance Method Summary collapse

Instance Method Details

#williams_percent_r(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
44
45
46
47
# File 'lib/ruby-technical-analysis/indicators/williams_percent_r.rb', line 6

def williams_percent_r(period)
  highs = []
  lows = []
  closes = []

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

  if highs.size < period
    raise ArgumentError,
          "High array passed to Williams Percent R cannot be less than the period argument."
  end

  if lows.size < period
    raise ArgumentError,
          "Low array passed to Williams Percent R cannot be less than the period argument."
  end

  if closes.size < period
    raise ArgumentError,
          "Close array passed to Williams Percent R cannot be less than the period argument."
  end

  highest_highs = []
  lowest_lows = []
  hh_min_close = []
  hh_min_ll = []
  pct_r = []

  (0..highs.length - period).each do |i|
    highest_highs << highs[i..period - 1 + i].max
    lowest_lows << lows[i..period - 1 + i].min
    hh_min_close << (highest_highs[-1] - closes[period - 1 + i]).round(2)
    hh_min_ll << (highest_highs[-1] - lowest_lows[-1]).round(2)
    pct_r << ((hh_min_close[-1].to_f / hh_min_ll[-1]) * -100).round(2)
  end

  pct_r[-1]
end