Module: RateOfChange

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

Overview

RateOfChange indicator Returns a single value

Instance Method Summary collapse

Instance Method Details

#rate_of_change(period) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/ruby-technical-analysis/indicators/rate_of_change.rb', line 6

def rate_of_change(period)
  if size < (period + 1)
    raise ArgumentError,
          "Closes array passed to RateOfChange cannot be less than the period argument + 1."
  end

  (((self[-1] - last(period + 1)[0]).to_f / last(period + 1)[0]) * 100).round(2)
end