Method: TechnicalAnalysis::Adi.calculate

Defined in:
lib/technical_analysis/indicators/adi.rb

.calculate(data) ⇒ Array<AdiValue>

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

Parameters:

  • Array of hashes with keys (:date_time, :high, :low, :close, :volume)

Returns:

  • An array of AdiValue instances



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