Class: TechnicalAnalysis::Adi

Inherits:
Indicator show all
Defined in:
lib/technical_analysis/indicators/adi.rb

Overview

Accumulation/Distribution Index

Class Method Summary collapse

Methods inherited from Indicator

find, roster

Class Method Details

.calculate(data) ⇒ Array<AdiValue>

Calculates the Accumulation/Distribution Index (ADI) for the given data en.wikipedia.org/wiki/Accumulation/distribution_index



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/technical_analysis/indicators/adi.rb', line 51

def self.calculate(data)
  Validation.validate_numeric_data(data, :high, :low, :close, :volume)
  Validation.validate_date_time_key(data)

  data = data.sort_by { |row| row[:date_time] }

  ad = 0
  clv = 0
  output = []
  prev_ad = 0

  data.each_with_index do |values, i|
    if values[:high] == values[:low]
      clv = 0
    else
      clv = ((values[:close] - values[:low]) - (values[:high] - values[:close])) / (values[:high] - values[:low])
    end

    ad = prev_ad + (clv * values[:volume])
    prev_ad = ad
    date_time = values[:date_time]

    output << AdiValue.new(date_time: date_time, adi: ad)
  end

  output.sort_by(&:date_time).reverse
end

.indicator_nameString

Returns the name of the technical indicator



15
16
17
# File 'lib/technical_analysis/indicators/adi.rb', line 15

def self.indicator_name
  "Accumulation/Distribution Index"
end

.indicator_symbolString

Returns the symbol of the technical indicator



8
9
10
# File 'lib/technical_analysis/indicators/adi.rb', line 8

def self.indicator_symbol
  "adi"
end

.min_data_size(**params) ⇒ Integer

Calculates the minimum number of observations needed to calculate the technical indicator



42
43
44
# File 'lib/technical_analysis/indicators/adi.rb', line 42

def self.min_data_size(**params)
  1
end

.valid_optionsArray

Returns an array of valid keys for options for this technical indicator



22
23
24
# File 'lib/technical_analysis/indicators/adi.rb', line 22

def self.valid_options
  []
end

.validate_options(options) ⇒ Boolean

Validates the provided options for this technical indicator



31
32
33
34
# File 'lib/technical_analysis/indicators/adi.rb', line 31

def self.validate_options(options)
  return true if options == {}
  raise Validation::ValidationError.new "This indicator doesn't accept any options."
end