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

Parameters:

  • data (Array)

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

Returns:

  • (Array<AdiValue>)

    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

.indicator_nameString

Returns the name of the technical indicator

Returns:

  • (String)

    A string of 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

Returns:

  • (String)

    A string of 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

Parameters:

  • options (Hash)

    The options for the technical indicator

Returns:

  • (Integer)

    Returns the minimum number of observations needed to calculate the technical indicator based on the options provided



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

Returns:

  • (Array)

    An array of keys as symbols for valid 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

Parameters:

  • options (Hash)

    The options for the technical indicator to be validated

Returns:

  • (Boolean)

    Returns true if options are valid or raises a ValidationError if they’re not



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