Class: TechnicalAnalysis::Ao

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

Overview

Awesome Oscillator

Class Method Summary collapse

Methods inherited from Indicator

find, roster

Class Method Details

.calculate(data, short_period: 5, long_period: 34) ⇒ Array<AoValue>

Calculates the awesome oscillator for the data over the given period www.tradingview.com/wiki/Awesome_Oscillator_(AO)

Parameters:

  • data (Array)

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

  • short_period (Integer) (defaults to: 5)

    The given period to calculate the short period SMA

  • long_period (Integer) (defaults to: 34)

    The given period to calculate the long period SMA

Returns:

  • (Array<AoValue>)

    An array of AoValue instances



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
78
79
80
81
# File 'lib/technical_analysis/indicators/ao.rb', line 53

def self.calculate(data, short_period: 5, long_period: 34)
  short_period = short_period.to_i
  long_period = long_period.to_i
  Validation.validate_numeric_data(data, :high, :low)
  Validation.validate_length(data, min_data_size(long_period: long_period))
  Validation.validate_date_time_key(data)

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

  midpoint_values = []
  output = []

  data.each do |v|
    midpoint = (v[:high] + v[:low]) / 2
    midpoint_values << midpoint

    if midpoint_values.size == long_period
      short_period_sma = ArrayHelper.average(midpoint_values.last(short_period))
      long_period_sma = ArrayHelper.average(midpoint_values)
      value = short_period_sma - long_period_sma

      output << AoValue.new(date_time: v[:date_time], ao: value)

      midpoint_values.shift
    end
  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/ao.rb', line 15

def self.indicator_name
  "Awesome Oscillator"
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/ao.rb', line 8

def self.indicator_symbol
  "ao"
end

.min_data_size(long_period: 34, **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



41
42
43
# File 'lib/technical_analysis/indicators/ao.rb', line 41

def self.min_data_size(long_period: 34, **params)
  long_period.to_i
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/ao.rb', line 22

def self.valid_options
  %i(short_period long_period)
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
# File 'lib/technical_analysis/indicators/ao.rb', line 31

def self.validate_options(options)
  Validation.validate_options(options, valid_options)
end